Skip to content

Commit 4295eef

Browse files
authored
Ignore Ember Data store service calls in no-array-prototype-extensions rule (#1748)
fixes #1561
1 parent 0891d4e commit 4295eef

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

docs/rules/no-array-prototype-extensions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ To reduce false positives, the rule ignores some common known-non-array classes/
2727
- `localStorage.clear()` / `sessionStorage.clear()`
2828
- `Promise.any()` / `Promise.reject()`
2929
- Lodash / jQuery
30+
- Ember Data `this.store` service
3031
- etc
3132

3233
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.

lib/rules/no-array-prototype-extensions.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,22 @@ function applyFix(callExpressionNode, fixer, context, options = {}) {
598598
}
599599
}
600600

601+
/**
602+
* Check for a call on `this.store` which we can assume is the Ember Data store service.
603+
* 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
604+
*/
605+
function isThisStoreCall(node) {
606+
return (
607+
node.type === 'CallExpression' &&
608+
node.callee.type === 'MemberExpression' &&
609+
node.callee.object.type === 'MemberExpression' &&
610+
node.callee.object.object.type === 'ThisExpression' &&
611+
node.callee.object.property.type === 'Identifier' &&
612+
node.callee.object.property.name === 'store' &&
613+
node.callee.property.type === 'Identifier' // Any function call on the store service.
614+
);
615+
}
616+
601617
//----------------------------------------------------------------------------------------------
602618
// General rule - Don't use Ember's array prototype extensions like .any(), .pushObject() or .firstObject
603619
//----------------------------------------------------------------------------------------------
@@ -698,6 +714,15 @@ module.exports = {
698714
return;
699715
}
700716

717+
if (
718+
(nodeInitializedTo.type === 'AwaitExpression' &&
719+
isThisStoreCall(nodeInitializedTo.argument)) ||
720+
isThisStoreCall(nodeInitializedTo)
721+
) {
722+
// Found call on the Ember Data this.store class.
723+
return;
724+
}
725+
701726
if (
702727
node.callee.type === 'MemberExpression' &&
703728
node.callee.object.type === 'MemberExpression' &&

tests/lib/rules/no-array-prototype-extensions.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,25 @@ ruleTester.run('no-array-prototype-extensions', rule, {
226226
array.without(2)
227227
`,
228228

229+
// Ember Data call with await.
230+
`
231+
class MyClass {
232+
async _fetch(query) {
233+
const response = await this.store.query('foo-bar', query);
234+
return response.toArray();
235+
}
236+
}
237+
`,
238+
// Ember Data call without await.
239+
`
240+
class MyClass {
241+
_fetch(query) {
242+
const response = this.store.peekAll('foo-bar', query);
243+
return response.toArray();
244+
}
245+
}
246+
`,
247+
229248
// TODO: handle non-Identifier property names:
230249
'foo["clear"]();',
231250
],

0 commit comments

Comments
 (0)