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

Add new rule no-decorators-before-export #1700

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

Conversation

runspired
Copy link

resolves #1699

@lin-ll lin-ll changed the title feat: lint no-decorators-before-export Add new rule no-decorators-before-export Dec 16, 2022
Copy link
Collaborator

@lin-ll lin-ll left a comment

Choose a reason for hiding this comment

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

Overall this looks really good. Thank you for both raising this issue and creating a PR for this rule so quickly.


💼 This rule is enabled in the ✅ `recommended` [config](https://github.com/ember-cli/eslint-plugin-ember#-configurations).

🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe worth adding a note that the fixer only works for named exports

export default MyComponent;
```

Unfortunately, in order to maximize compatibility with existing decorators, the second rendition of the spec - the rendition of the spec which Ember adopted (there have been four) - allowed this syntax to still be used if using the babel plugin AND also explicitly allowing it in the plugin config. Ember chose to be maximally capatible and enabled this option.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
Unfortunately, in order to maximize compatibility with existing decorators, the second rendition of the spec - the rendition of the spec which Ember adopted (there have been four) - allowed this syntax to still be used if using the babel plugin AND also explicitly allowing it in the plugin config. Ember chose to be maximally capatible and enabled this option.
Unfortunately, in order to maximize compatibility with existing decorators, the second rendition of the spec - the rendition of the spec which Ember adopted (there have been four) - allowed this syntax to still be used if using the babel plugin AND also explicitly allowing it in the plugin config. Ember chose to be maximally compatible and enabled this option.


@classic
export class MyKlass extends EmberObject {}
```
Copy link
Collaborator

Choose a reason for hiding this comment

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

If possible, do you mind adding a couple link references as well, like the proposal for decorators and issues that were caused by this?

description:
'disallow the use of a class decorator on an export declaration. Enforces use of the decorator on the class directly before exporting the result.',
category: 'Miscellaneous',
recommended: true,
Copy link
Member

@bmish bmish Dec 16, 2022

Choose a reason for hiding this comment

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

Note: recommended rules are only added in major versions so this should be removed. We can enable it in the next major version.

Copy link
Member

@bmish bmish left a comment

Choose a reason for hiding this comment

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

Thanks for starting this! One tip is to try running it on a real codebase to ensure there are no missed edge cases / crashes.

@@ -0,0 +1,73 @@
'use strict';

/** @type {import('eslint').Rule.RuleModule} */
Copy link
Member

Choose a reason for hiding this comment

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

This type annotation is supposed to go directly above module.exports = { ....

invalid: [
// basic
{
code: "import classic from 'ember-classic-decorators';\n@classic\nexport class Foo {}",
Copy link
Member

Choose a reason for hiding this comment

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

I'm finding these single-line strings with \n a hard to read. Normally, we use multi-line template strings.

Comment on lines +19 to +20
const sourceCode = context.getSourceCode();
const src = sourceCode.getText(node);
Copy link
Member

Choose a reason for hiding this comment

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

These variables can move inside the fixer since they aren't used outside.

return (
node.declaration &&
node.declaration.type === 'ClassDeclaration' &&
node.declaration.decorators?.length
Copy link
Member

Choose a reason for hiding this comment

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

Normally we should have a comparison length > 0. Or are you trying to check if the array exists at all? Then Array.isArray() is more clear.

Copy link
Author

Choose a reason for hiding this comment

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

it might not exist, or it may exist and be empty, in either case that is fine.

Copy link
Member

Choose a reason for hiding this comment

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

Okay, well !Array.isArray(node.declaration.decorators) || node.declaration.decorators.length > 0 would convey that intent more clearly to me. Feel free to leave as is if you prefer.


/** @type {import('eslint').Rule.RuleModule} */

function reportInvalidSyntax(context, node) {
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a comment explaining the general strategy/approach/what's going on?

}
}

function isInvalidSyntax(node) {
Copy link
Member

Choose a reason for hiding this comment

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

Seems like this could just be a generic classDeclarationHasDecorators(node) function. That way, it's more clear what it's doing, and could be reusable in other places.

Copy link
Author

Choose a reason for hiding this comment

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

It isn’t though, it’s checking against an export declaration specifically

Copy link
Member

@bmish bmish Dec 16, 2022

Choose a reason for hiding this comment

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

My bad, then exportDeclarationHasDecorators?. Feel free to leave it if you want, but I think my own confusion illustrates the point. I'm just thinking of ways to make it more clear about what it's doing and reusable.

@runspired
Copy link
Author

Thanks for starting this! One tip is to try running it on a real codebase to ensure there are no missed edge cases / crashes.

@bmish it is already running on one

@bmish
Copy link
Member

bmish commented Jan 6, 2023

@runspired would love to get this rule in still! Are you planning to work on it?

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

Successfully merging this pull request may close these issues.

New Rule: no-decorators-before-export
3 participants