Skip to content
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

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

evanfarina
Copy link

…and more object types. The rule now checks any object that's using extend, and validates the init, willDestroy, destroy and willDestroyElement hooks. In addition, I updated the rule to validate that arguments were correctly passed to the super call. Both of the below pass the rule:

this._super(...arguments);
this._super.apply(this, arguments)

@evanfarina evanfarina changed the title Updated no-broken-super-chain rule[ to account for more lifecycle hooks … Updated no-broken-super-chain rule to account for more lifecycle hooks … Nov 2, 2017
Copy link
Collaborator

@rwjblue rwjblue left a comment

Choose a reason for hiding this comment

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

Looking good! I left some notes inline with suggested cleanups to make, and I think we need a few more test cases to cover those things.

I'd suggest the following additional test cases:

  • valid
    • Foo.extend(SomeMixin)
    • Foo.extend(SomeMixin, { init() { this._super(...arguments) } })
    • Foo.extend({ [lol]: function() {})

}
}
extendedObjects.forEach(extendedObj => {
extendedObj.properties.forEach(property => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

We need to guard for extendedObj.properties being undefined here (or filter out non ObjectExpressions before .forEaching).

I believe the following test should be added (which will fail ATM but pass if we filtered non ObjectExpressions):

Foo.extend(SomeMixin, {
  init() {
    this._super(...arguments);
  }
});

I'd suggest either of the following:

extendedObjects = node.arguments.filter(arg => arg.type === 'ObjectExpression');

// or

extendedObjects.forEach(extendedObj => {
  if (extendedObj.type !== 'ObjectExpression') { return; }

  // ...snip...
});

Copy link

Choose a reason for hiding this comment

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

I would also suggest adding a test case for this.

Copy link
Author

Choose a reason for hiding this comment

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

Updated, and added test.

}
CallExpression(node) {
if (isExtend(node)) {
let superCount = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Seems like this variable should be initialized from inside the extendedObjects.forEach loop (then you wouldn't need to reset it to 0 at the end of the loop).


if (propertyFnBody && propertyFnBody.body) {
let expression;
property.value.body.body.forEach(fnBody => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should likely use propertyFnBody.body instead of property.value.body.body.forEach

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you rename fnBody to expressionStatement?

Copy link
Author

Choose a reason for hiding this comment

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

  1. Correct. 2. Gladly

property.value.body.body.forEach(fnBody => {
expression = fnBody.expression;
if (expression.type === 'CallExpression') {
const line = expression.callee;
Copy link
Collaborator

Choose a reason for hiding this comment

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

line isn't super descriptive, maybe call this callee?

});
}

superCount = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This shouldn't be needed, we should initialize superCount inside the loop...

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';
Copy link
Collaborator

Choose a reason for hiding this comment

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

When would args be > 2? I think we want args.length === 2...

Copy link

Choose a reason for hiding this comment

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

+1

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, not sure what I had in mind here

}

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 or the use of `this` prior to `this._super()`',
Copy link
Collaborator

Choose a reason for hiding this comment

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

or the use of this prior to this._super()

I don't think we do this at the moment, do we? I don't see the code that does it at least...

Copy link
Author

Choose a reason for hiding this comment

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

good catch, also updated file description

Controller: true,
View: true
};
const LIFECYCLE_HOOKS = ['init', 'willDestroy', 'willDestroyElement', 'destroy'];
Copy link

Choose a reason for hiding this comment

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

@rwjblue to be 100% safe, I think we should add all life cycle hooks here. Otherwise, we have no guarantee that mixins will work as expected.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ya, makes sense. Do we have a canonical list?

I guess we can start with the ones listed in https://guides.emberjs.com/v2.16.0/components/the-component-lifecycle/...

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';
Copy link

Choose a reason for hiding this comment

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

+1

}
}
extendedObjects.forEach(extendedObj => {
extendedObj.properties.forEach(property => {
Copy link

Choose a reason for hiding this comment

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

I would also suggest adding a test case for this.

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;

}
});`,
errors: [{
message: noThisBeforeSuper
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let’s remove this TODO commented test.

Copy link
Collaborator

Choose a reason for hiding this comment

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

When we add a rule about no this before super we will probably make it a new more focused rule anyways.

Copy link
Author

Choose a reason for hiding this comment

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

Removed TODO

Controller: true,
View: true
};
const LIFECYCLE_HOOKS = ['init', 'willDestroy', 'willDestroyElement', 'destroy'];
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ya, makes sense. Do we have a canonical list?

I guess we can start with the ones listed in https://guides.emberjs.com/v2.16.0/components/the-component-lifecycle/...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants