Skip to content

Commit

Permalink
Ignore direct instantiation of EmberArray in `no-array-prototype-ex…
Browse files Browse the repository at this point in the history
…tensions` rule (#1749)

Co-authored-by: Bryan Mishkin <698306+bmish@users.noreply.github.com>
fixes #1561
  • Loading branch information
canrozanes committed Jan 23, 2023
1 parent 8ce78ea commit 9ded580
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
9 changes: 9 additions & 0 deletions docs/rules/no-array-prototype-extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ export default class SampleComponent extends Component {
}
```

```js
/** Direct usage of `@ember/array` **/
/** Use A() is OK **/
import { A } from '@ember/array';

const arr = A(['a', 'a', 'b', 'b']);
arr.uniq();
```

## References

- [EmberArray](https://api.emberjs.com/ember/release/classes/EmberArray)
Expand Down
15 changes: 15 additions & 0 deletions lib/rules/no-array-prototype-extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ module.exports = {
let importedGetName;
let importedSetName;
let importedCompareName;
let importedEmberArrayName;

// Track some information about the current class we're inside.
const classStack = new Stack();
Expand All @@ -635,6 +636,10 @@ module.exports = {
importedCompareName =
importedCompareName || getImportIdentifier(node, '@ember/utils', 'compare');
}
if (node.source.value === '@ember/array') {
importedEmberArrayName =
importedEmberArrayName || getImportIdentifier(node, '@ember/array', 'A');
}
},
/**
* Cover cases when `EXTENSION_METHODS` is getting called.
Expand Down Expand Up @@ -714,6 +719,16 @@ module.exports = {
return;
}

// Direct usage of `@ember/array` is allowed.
if (
node.type === 'CallExpression' &&
importedEmberArrayName &&
nodeInitializedTo.callee.type === 'Identifier' &&
importedEmberArrayName === nodeInitializedTo.callee.name
) {
return;
}

if (EXTENSION_METHODS.has(node.callee.property.name)) {
context.report({
node,
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 @@ -203,6 +203,25 @@ ruleTester.run('no-array-prototype-extensions', rule, {
parser: require.resolve('@typescript-eslint/parser'),
},

// Methods called directly on EmberArray.
`
import { A } from '@ember/array'
const array = A([1, 2, 3])
array.toArray()
`,
`
import { A } from '@ember/array'
A([1, 2, 3]).toArray();
`,
`
import { A as SomeWeirdName } from '@ember/array'
const array = SomeWeirdName([1, 2, 3])
array.without(2)
`,

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

0 comments on commit 9ded580

Please sign in to comment.