Skip to content

Commit af0026a

Browse files
authored
Fix crash in jquery-ember-run rule (#1431)
1 parent 8f57301 commit af0026a

File tree

2 files changed

+36
-21
lines changed

2 files changed

+36
-21
lines changed

lib/rules/jquery-ember-run.js

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,30 @@ module.exports = {
5353
let importedEmberName;
5454
const importedRunloopFunctions = [];
5555

56+
// Check for imported call to: bind()
57+
function isBindCall(expression) {
58+
return (
59+
types.isCallExpression(expression) &&
60+
expression.callee.type === 'Identifier' &&
61+
importedRunloopFunctions.includes(expression.callee.name)
62+
);
63+
}
64+
65+
// Check for old-style: Ember.run.bind()
66+
function isEmberBindCall(expression) {
67+
return (
68+
types.isCallExpression(expression) &&
69+
expression.callee.type === 'MemberExpression' &&
70+
expression.callee.property.type === 'Identifier' &&
71+
EMBER_RUNLOOP_FUNCTIONS.includes(expression.callee.property.name) &&
72+
expression.callee.object.type === 'MemberExpression' &&
73+
expression.callee.object.property.type === 'Identifier' &&
74+
expression.callee.object.property.name === 'run' &&
75+
expression.callee.object.object.type === 'Identifier' &&
76+
expression.callee.object.object.name === importedEmberName
77+
);
78+
}
79+
5680
function checkJqueryCall(node) {
5781
if (
5882
// Check to see if this jquery call looks like: $(...).on(() => { ... }));
@@ -69,27 +93,12 @@ module.exports = {
6993
const fnExpressions = utils.findNodes(fnBody, 'ExpressionStatement');
7094
for (const fnExpression of fnExpressions) {
7195
const expression = fnExpression.expression;
72-
73-
// Check for imported call to: bind()
74-
const isBindCall =
75-
types.isCallExpression(expression) &&
76-
expression.callee.type === 'Identifier' &&
77-
importedRunloopFunctions.includes(expression.callee.name);
78-
79-
// Check for old-style: Ember.run.bind()
80-
const isEmberBindCall =
81-
types.isCallExpression(expression) &&
82-
expression.callee.type === 'MemberExpression' &&
83-
expression.callee.property.type === 'Identifier' &&
84-
EMBER_RUNLOOP_FUNCTIONS.includes(expression.callee.property.name) &&
85-
expression.callee.object.type === 'MemberExpression' &&
86-
expression.callee.object.property.type === 'Identifier' &&
87-
expression.callee.object.property.name === 'run' &&
88-
expression.callee.object.object.type === 'Identifier' &&
89-
expression.callee.object.object.name === importedEmberName;
90-
91-
if (!isBindCall && !isEmberBindCall) {
92-
report(expression.callee);
96+
if (!isBindCall(expression) && !isEmberBindCall(expression)) {
97+
if (types.isCallExpression(expression)) {
98+
report(expression.callee);
99+
} else {
100+
report(expression);
101+
}
93102
}
94103
}
95104
}

tests/lib/rules/jquery-ember-run.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ eslintTester.run('jquery-ember-run', rule, {
9393
output: null,
9494
errors: [{ message: ERROR_MESSAGE, type: 'Identifier' }],
9595
},
96+
{
97+
// With assignment
98+
code: 'import $ from "jquery"; $("#item").on("click", () => { this.value = 1; });',
99+
output: null,
100+
errors: [{ message: ERROR_MESSAGE, type: 'AssignmentExpression' }],
101+
},
96102
{
97103
// With not from Ember
98104
code: 'import $ from "jquery"; $("#item").on("click", () => { notEmber.run.bind(); });',

0 commit comments

Comments
 (0)