-
Notifications
You must be signed in to change notification settings - Fork 34
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
evanfarina
wants to merge
4
commits into
ember-best-practices:master
Choose a base branch
from
evanfarina:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a8b7a68
Refactored and updated the rule to account for more lifecycle hooks …
evanfarina abdad6d
Addressed feedback, added tests for new cases
evanfarina 56dcd1b
Addressed feedback, added tests for new cases
evanfarina 1a8aad3
Added more lifecycle hooks
evanfarina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
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; | ||
} | ||
}; | ||
} | ||
}; | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
This ^ is valid, the linting rule should not complain about it.
There was a problem hiding this comment.
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;