Skip to content

Commit

Permalink
feat: add place-fields check to autocomplete (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpoehnelt authored Dec 20, 2021
1 parent 828bd15 commit b63718a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/rules/place-fields.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ new TSESLint.RuleTester({
`const service = new google.maps.places.PlacesService();
const buildRequest = () => {};
service.getDetails(buildRequest())`,
// Autocomplete
`const service = new google.maps.places.Autocomplete(null, {fields: ['place_id']});`,
],
invalid: [
// getDetails
{
code: `const service = new google.maps.places.PlacesService();
const request = {place_id: 'foo'};
Expand All @@ -56,5 +59,10 @@ new TSESLint.RuleTester({
service.getDetails({...{bar: 'foo'}})`,
errors: [{ messageId }],
},
// Autocomplete
{
code: `const service = new google.maps.places.Autocomplete(null, {});`,
errors: [{ messageId }],
},
],
});
21 changes: 21 additions & 0 deletions src/rules/place-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,27 @@ export default createRule({
defaultOptions: [],
create: (context) => {
return {
NewExpression: (node: TSESTree.NewExpression): void => {
try {
if (
fullNamespace(node.callee).match(
/google\.maps\.places\.Autocomplete/
)
) {
const references = context.getScope().references;
const options = node.arguments[1];

if (objectMaybeHasKey(references, options) === Ternary.FALSE) {
context.report({
messageId,
node: node.arguments[1],
});
}
}
} catch (e) {
console.warn(`rule googlemaps/${__filename} failed with: ${e}`);
}
},
MemberExpression: (node: TSESTree.MemberExpression): void => {
try {
if (
Expand Down

0 comments on commit b63718a

Please sign in to comment.