Skip to content

Commit

Permalink
Fix crash in jquery-ember-run rule (#1431)
Browse files Browse the repository at this point in the history
  • Loading branch information
ef4 authored Feb 14, 2022
1 parent 8f57301 commit af0026a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
51 changes: 30 additions & 21 deletions lib/rules/jquery-ember-run.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,30 @@ module.exports = {
let importedEmberName;
const importedRunloopFunctions = [];

// Check for imported call to: bind()
function isBindCall(expression) {
return (
types.isCallExpression(expression) &&
expression.callee.type === 'Identifier' &&
importedRunloopFunctions.includes(expression.callee.name)
);
}

// Check for old-style: Ember.run.bind()
function isEmberBindCall(expression) {
return (
types.isCallExpression(expression) &&
expression.callee.type === 'MemberExpression' &&
expression.callee.property.type === 'Identifier' &&
EMBER_RUNLOOP_FUNCTIONS.includes(expression.callee.property.name) &&
expression.callee.object.type === 'MemberExpression' &&
expression.callee.object.property.type === 'Identifier' &&
expression.callee.object.property.name === 'run' &&
expression.callee.object.object.type === 'Identifier' &&
expression.callee.object.object.name === importedEmberName
);
}

function checkJqueryCall(node) {
if (
// Check to see if this jquery call looks like: $(...).on(() => { ... }));
Expand All @@ -69,27 +93,12 @@ module.exports = {
const fnExpressions = utils.findNodes(fnBody, 'ExpressionStatement');
for (const fnExpression of fnExpressions) {
const expression = fnExpression.expression;

// Check for imported call to: bind()
const isBindCall =
types.isCallExpression(expression) &&
expression.callee.type === 'Identifier' &&
importedRunloopFunctions.includes(expression.callee.name);

// Check for old-style: Ember.run.bind()
const isEmberBindCall =
types.isCallExpression(expression) &&
expression.callee.type === 'MemberExpression' &&
expression.callee.property.type === 'Identifier' &&
EMBER_RUNLOOP_FUNCTIONS.includes(expression.callee.property.name) &&
expression.callee.object.type === 'MemberExpression' &&
expression.callee.object.property.type === 'Identifier' &&
expression.callee.object.property.name === 'run' &&
expression.callee.object.object.type === 'Identifier' &&
expression.callee.object.object.name === importedEmberName;

if (!isBindCall && !isEmberBindCall) {
report(expression.callee);
if (!isBindCall(expression) && !isEmberBindCall(expression)) {
if (types.isCallExpression(expression)) {
report(expression.callee);
} else {
report(expression);
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions tests/lib/rules/jquery-ember-run.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ eslintTester.run('jquery-ember-run', rule, {
output: null,
errors: [{ message: ERROR_MESSAGE, type: 'Identifier' }],
},
{
// With assignment
code: 'import $ from "jquery"; $("#item").on("click", () => { this.value = 1; });',
output: null,
errors: [{ message: ERROR_MESSAGE, type: 'AssignmentExpression' }],
},
{
// With not from Ember
code: 'import $ from "jquery"; $("#item").on("click", () => { notEmber.run.bind(); });',
Expand Down

0 comments on commit af0026a

Please sign in to comment.