Skip to content

Updated no-broken-super-chain rule to account for more lifecycle hooks … #94

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 93 additions & 68 deletions lib/rules/no-broken-super-chain.js
Original file line number Diff line number Diff line change
@@ -1,95 +1,120 @@
/**
* @fileOverview Prevent the absence of this._super() in init() calls or the use of this prior to this._super()
* @fileOverview Prevent the absence of `this._super(...arguments)` in calls to various lifecycle hooks
*/

const MESSAGES = {
noSuper: '\'this._super(...arguments)\' must be called in init()',
noThisBeforeSuper: 'Must call \'this._super(...arguments)\' before accessing `this`',
tooManySupers: 'Only call `this._super(...arguments)` once per init()'
noSuper: '`this._super(...arguments)` must be called in',
tooManySupers: 'Only call `this._super(...arguments)` once per lifecycle hook',
argsNotPassedToSuper: '...arguments need to be passed to this._super() call'
};

// TODO: Make this configurable
const EMBER_MODULES_WITH_SUPER_CHAIN = {
Component: true,
Mixin: true,
Route: true,
Controller: true,
View: true
};
const LIFECYCLE_HOOKS = [
'init',
'didReceiveAttrs',
'willRender',
'didInsertElement',
'didRender',
'didUpdateAttrs',
'willUpdate',
'didUpdate',
'willDestroy',
'willDestroyElement',
'willClearRender',
'destroy',
'didDestroyElement'
];

/**
* Determines if this is an init method in an extension of Ember[EMBER_MODULES_WITH_SUPER_CHAIN.*]
* @param {Node} node
*/
function isInit(node) {
if (node.type === 'FunctionExpression' && node.parent && node.parent.key && node.parent.key.name === 'init') {
function isExtend(node) {
return node && node.callee && node.callee.property && node.callee.property.name === 'extend';
}

function isSuperCall(lineWithinFn) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more case we need to handle is:

Foo.extend({
  init() {
    return this._super(...argument);
  }
});

This ^ is valid, the linting rule should not complain about it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch. Updated to: expression = expressionStatement.type === 'ReturnStatement' ? expressionStatement.argument : expressionStatement.expression;

if (lineWithinFn.type !== 'MemberExpression') {
return false;
}

let isSuperCall = false;

if (lineWithinFn.object.type === 'ThisExpression') {
isSuperCall = lineWithinFn.property.type === 'Identifier' && lineWithinFn.property.name === '_super';
} else if (lineWithinFn.object.type === 'MemberExpression') {
isSuperCall = lineWithinFn.object.property.name === '_super';
}

return isSuperCall;
}

function wereArgumentsPassedToSuper(expression) {
const callee = expression.callee;

if (node.parent.parent
&& node.parent.parent.parent
&& node.parent.parent.parent.callee
&& node.parent.parent.parent.callee.object
&& node.parent.parent.parent.callee.object.object
&& node.parent.parent.parent.callee.object.object.name === 'Ember') {
return (node.parent.parent.parent.callee.object.property
&& EMBER_MODULES_WITH_SUPER_CHAIN[node.parent.parent.parent.callee.object.property.name]);
}
if (!expression || callee.type !== 'MemberExpression') {
return;
}

return false;
if (callee.property.name === '_super') {
const firstArgumentToSuper = expression.arguments[0];
return firstArgumentToSuper && firstArgumentToSuper.type === 'SpreadElement' && firstArgumentToSuper.argument.name === 'arguments';
} else if (callee.property.name === 'apply') {
const args = expression.arguments;
return args.length === 2 && args[0].type === 'ThisExpression' && args[1].name === 'arguments';
}
}

module.exports = {
meta: {
docs: {
description: 'Prevent the absence of `this._super(...arguments)` in `init()` calls or the use of `this` prior to `this._super()`',
description: 'Prevent the absence of `this._super(...arguments)` in calls to various lifecycle hooks',
category: 'Best Practices',
recommended: true
},
messages: MESSAGES
},
create(context) {
let initOverride = null;

return {
onCodePathStart(codePath, node) {
if (isInit(node)) {
initOverride = {
superCalled: false,
superCalledFirst: false
};
}
},
onCodePathEnd(codePath, node) {
if (initOverride && isInit(node)) { // TODO: Maybe check against codepath.name
if (!initOverride.superCalled) {
context.report({
message: MESSAGES.noSuper,
node
});
}
CallExpression(node) {
if (isExtend(node)) {
const extendedObjects = node.arguments.filter(arg => arg.type === 'ObjectExpression');
extendedObjects.forEach(extendedObj => {
let superCount = 0;
extendedObj.properties.forEach(property => {
if (LIFECYCLE_HOOKS.includes(property.key.name)) {
const propertyFnBody = property.value.body;

initOverride = null;
}
return;
},
'CallExpression:exit'(node) {
if (initOverride) {
const property = node.callee.property;
if (property && property.type === 'Identifier' && property.name === '_super') {
if (initOverride.superCalled) {
context.report({
message: MESSAGES.tooManySupers,
node
});
} else {
initOverride.superCalled = true;
}
}
if (propertyFnBody && propertyFnBody.body) {
let expression;
propertyFnBody.body.forEach(expressionStatement => {
expression = expressionStatement.type === 'ReturnStatement' ? expressionStatement.argument : expressionStatement.expression;
if (expression.type === 'CallExpression') {
const callee = expression.callee;
if (isSuperCall(callee)) {
if (!wereArgumentsPassedToSuper(expression)) {
context.report({
node: expressionStatement,
message: context.meta.messages.argsNotPassedToSuper
});
}
superCount++;
}
}
});

if (superCount === 0) {
context.report({
node: property,
message: `${context.meta.messages.noSuper} ${property.key.name}`
});
} else if (superCount > 1) {
context.report({
node: property,
message: context.meta.messages.tooManySupers
});
}
}
}
});
});
}
},
'Program:exit'() {
initOverride = null;
}
};
}
};
};
Loading