Skip to content

Commit

Permalink
Ignore Ember Data store service calls in `no-array-prototype-extens…
Browse files Browse the repository at this point in the history
…ions` rule (#1748)

fixes #1561
  • Loading branch information
bmish authored Jan 28, 2023
1 parent 0891d4e commit 4295eef
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/rules/no-array-prototype-extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ To reduce false positives, the rule ignores some common known-non-array classes/
- `localStorage.clear()` / `sessionStorage.clear()`
- `Promise.any()` / `Promise.reject()`
- Lodash / jQuery
- Ember Data `this.store` service
- etc

If you run into additional false positives, please file a bug or submit a PR to add it to the rule's hardcoded ignore list.
Expand Down
25 changes: 25 additions & 0 deletions lib/rules/no-array-prototype-extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,22 @@ function applyFix(callExpressionNode, fixer, context, options = {}) {
}
}

/**
* Check for a call on `this.store` which we can assume is the Ember Data store service.
* We don't check for an initialization as the service could be implicitly injected: https://github.com/ember-cli/eslint-plugin-ember/blob/master/docs/rules/no-implicit-injections.md
*/
function isThisStoreCall(node) {
return (
node.type === 'CallExpression' &&
node.callee.type === 'MemberExpression' &&
node.callee.object.type === 'MemberExpression' &&
node.callee.object.object.type === 'ThisExpression' &&
node.callee.object.property.type === 'Identifier' &&
node.callee.object.property.name === 'store' &&
node.callee.property.type === 'Identifier' // Any function call on the store service.
);
}

//----------------------------------------------------------------------------------------------
// General rule - Don't use Ember's array prototype extensions like .any(), .pushObject() or .firstObject
//----------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -698,6 +714,15 @@ module.exports = {
return;
}

if (
(nodeInitializedTo.type === 'AwaitExpression' &&
isThisStoreCall(nodeInitializedTo.argument)) ||
isThisStoreCall(nodeInitializedTo)
) {
// Found call on the Ember Data this.store class.
return;
}

if (
node.callee.type === 'MemberExpression' &&
node.callee.object.type === 'MemberExpression' &&
Expand Down
19 changes: 19 additions & 0 deletions tests/lib/rules/no-array-prototype-extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,25 @@ ruleTester.run('no-array-prototype-extensions', rule, {
array.without(2)
`,

// Ember Data call with await.
`
class MyClass {
async _fetch(query) {
const response = await this.store.query('foo-bar', query);
return response.toArray();
}
}
`,
// Ember Data call without await.
`
class MyClass {
_fetch(query) {
const response = this.store.peekAll('foo-bar', query);
return response.toArray();
}
}
`,

// TODO: handle non-Identifier property names:
'foo["clear"]();',
],
Expand Down

0 comments on commit 4295eef

Please sign in to comment.