Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore direct instantiation of EmberArray in no-array-prototype-extensions rule #1749

16 changes: 16 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,17 @@ module.exports = {
return;
}

// ignore ember array initialization with A([]) or EmberArray([])
canrozanes marked this conversation as resolved.
Show resolved Hide resolved
if (
node.type === 'CallExpression' &&
nodeInitializedTo.callee &&
canrozanes marked this conversation as resolved.
Show resolved Hide resolved
importedEmberArrayName &&
nodeInitializedTo.callee.name &&
canrozanes marked this conversation as resolved.
Show resolved Hide resolved
importedEmberArrayName === nodeInitializedTo.callee.name
) {
return;
}

if (EXTENSION_METHODS.has(node.callee.property.name)) {
context.report({
node,
Expand Down
14 changes: 14 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,20 @@ 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()
`,
bmish marked this conversation as resolved.
Show resolved Hide resolved
`
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