Skip to content

Commit

Permalink
fix: avoid some false positives with jQuery usage when recognizing ex…
Browse files Browse the repository at this point in the history
…tended objects
  • Loading branch information
bmish committed Apr 21, 2021
1 parent 344fe44 commit ec10ff4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/utils/ember.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,20 @@ function isMirageConfig(fileName) {
return path.normalize(fileName).endsWith(path.join('mirage', 'config.js'));
}

// jQuery has an `extend` function and we want to avoid mistaking it for an extended object.
// TODO: ideally, this would check the actual name that jQuery is imported under, but there's a lot of plumbing needed for that.
const COMMON_JQUERY_NAMES = new Set(['$', 'jQuery']);

function isExtendObject(node) {
// Check for:
// * foo.extend();
// * foo['extend']();
return (
node.callee.property &&
(node.callee.property.name === 'extend' || node.callee.property.value === 'extend')
node.type === 'CallExpression' &&
node.callee.type === 'MemberExpression' &&
((node.callee.property.type === 'Identifier' && node.callee.property.name === 'extend') ||
(node.callee.property.type === 'Literal' && node.callee.property.value === 'extend')) &&
!(node.callee.object.type === 'Identifier' && COMMON_JQUERY_NAMES.has(node.callee.object.name))
);
}

Expand Down
42 changes: 42 additions & 0 deletions tests/lib/utils/ember-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,48 @@ describe('isEmberService', () => {
});
});

describe('isExtendObject', () => {
it('should detect using extend function name', () => {
const node = parse('foo.extend()');
expect(emberUtils.isExtendObject(node)).toBeTruthy();
});

it('should detect using extend string name', () => {
const node = parse('foo["extend"]()');
expect(emberUtils.isExtendObject(node)).toBeTruthy();
});

it('should detect using nested object', () => {
const node = parse('foo.bar.extend()');
expect(emberUtils.isExtendObject(node)).toBeTruthy();
});

it('should not detect a potential jQuery usage with `$`', () => {
const node = parse('$.extend()');
expect(emberUtils.isExtendObject(node)).toBeFalsy();
});

it('should not detect a potential jQuery usage with `jQuery`', () => {
const node = parse('jQuery.extend()');
expect(emberUtils.isExtendObject(node)).toBeFalsy();
});

it('should not detect with non-extend name', () => {
const node = parse('foo.notExtend()');
expect(emberUtils.isExtendObject(node)).toBeFalsy();
});

it('should not detect with no object', () => {
const node = parse('extend()');
expect(emberUtils.isExtendObject(node)).toBeFalsy();
});

it('should not detect with wrong function', () => {
const node = parse('extend.foo()');
expect(emberUtils.isExtendObject(node)).toBeFalsy();
});
});

describe('isEmberArrayProxy', () => {
it('should detect using old module style', () => {
const context = new FauxContext('Ember.ArrayProxy.extend()');
Expand Down

0 comments on commit ec10ff4

Please sign in to comment.