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-tracked-properties-from-args #1702

Merged
merged 14 commits into from Dec 19, 2022

Conversation

joancc
Copy link
Contributor

@joancc joancc commented Dec 16, 2022

Fixes #1701

/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'suggestion',
Copy link
Member

Choose a reason for hiding this comment

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

If this issue will cause a bug then we can use problem instead of suggestion.

});

ruleTester.run('no-tracked-properties-from-args', rule, {
valid: [
Copy link
Member

Choose a reason for hiding this comment

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

Another valid test case: @tracked test = this.notArgs.args.test

schema: [],
},

ERROR_MESSAGE,
Copy link
Member

Choose a reason for hiding this comment

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

Comment on lines 55 to 56
@tracked
test = this.args.test;
Copy link
Member

Choose a reason for hiding this comment

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

Is this a whitespace-only difference from the previous test case? If the AST is the same (astexplorer.net), then no need for a separate test case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point!

type: 'suggestion',
docs: {
description: 'disallow creating @tracked properties from this.args',
category: 'Ember Decorator',
Copy link
Member

Choose a reason for hiding this comment

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

Let's just put this under Ember Octane with the other decorator-related rules.


## Examples

Examples of **incorrect** code for this rule:
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 show a correct example? (with a getter?)

@bmish bmish changed the title Add no-tracked-properties-from-args rule Add new rule no-tracked-properties-from-args Dec 16, 2022
@bmish
Copy link
Member

bmish commented Dec 16, 2022

When creating a PR, you need to use the syntax Fixes #123 in the description to ensure the PR will automatically close the issue.

return {
PropertyDefinition(node) {
const hasTrackedDecorator =
node.decorators?.length > 0 && node.decorators[0].expression.name === 'tracked';
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we check against the imported name of tracked instead of hardcoding it? It will be a little more robust in case someone does:

import { tracked as foobar } from '@glimmer/tracking';
...
@foobar foo = this.args.bar;


create(context) {
return {
PropertyDefinition(node) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we still need to support ClassProperty as well? @bmish

Copy link
Member

Choose a reason for hiding this comment

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

It's not a bad idea to support that too until we drop support for ESLint v7. Context: #1529

Although I'm not super worried about it if it's a hassle...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! Borrowing the implementation from no-private-routing-service.js, but is there a way to have the tests run against both of these versions?

Copy link
Member

Choose a reason for hiding this comment

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

CI runs tests against both ESLint v7 and ESLint v8 so as long as it passes, we should be good.

README.md Outdated
@@ -115,6 +115,12 @@ module.exports = {
| [no-empty-attrs](docs/rules/no-empty-attrs.md) | disallow usage of empty attributes in Ember Data models | | | |
| [use-ember-data-rfc-395-imports](docs/rules/use-ember-data-rfc-395-imports.md) | enforce usage of `@ember-data/` package imports instead `ember-data` | ✅ | 🔧 | |

### Ember Decorator
Copy link
Member

Choose a reason for hiding this comment

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

need to re-run the doc generator


## Rule Details

This rule disallows the creation of @tracked properties with values from `this.args`. The @tracked property will not be updated when the args change, which is almost never what you want.
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 paragraph or at least a sentence explaining how to fix the issue?

Comment on lines 19 to 24
Examples of **correct** code for this rule:
```js
get someValue() {
return this.args.someValue;
}
```
Copy link
Member

Choose a reason for hiding this comment

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

Does adding a getter for the arg actually address the user's original intent in using @tracked? Does it mean that @tracked was entirely unnecessary?

Copy link
Member

Choose a reason for hiding this comment

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

I would still like to understand this a bit better, especially as to whether a getter is really a complete fix for what the user is attempting to achieve? We don't have to show a complete fix necessarily, but I would like to make sure we describe at a high-level what kind of more-involved fixes might be necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For the simple use case of trying to rename a deeply nested arg, @Tracked would be unnecessary and it would just be used interchangeably with a getter. Trying to raise awareness about the extra pitfall here.
I added a note on having to mutate the args to the docs portion, let me know if that helps!

@bmish
Copy link
Member

bmish commented Dec 19, 2022

Do you think this should become a recommended rule in a future major version?

@joancc
Copy link
Contributor Author

joancc commented Dec 19, 2022

Do you think this should become a recommended rule in a future major version?

I would say so, having to debug two sources of truth is usually a hard to debug case.

Co-authored-by: Bryan Mishkin <698306+bmish@users.noreply.github.com>
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 working on this!

@bmish bmish merged commit a2e2586 into ember-cli:master Dec 19, 2022
@joancc
Copy link
Contributor Author

joancc commented Dec 19, 2022

Wow, @bmish @lin-ll thank you so much for the incredibly fast but very thorough feedback! First small contribution to the Ember open source world, appreciate the help getting this over the line 😄

@bmish
Copy link
Member

bmish commented Dec 19, 2022

Excited to have you as a contributor!

@lin-ll
Copy link
Collaborator

lin-ll commented Dec 20, 2022

Excited as well - thanks for contributing a great rule :D

rtablada pushed a commit to rtablada/eslint-plugin-ember that referenced this pull request Dec 30, 2022
Co-authored-by: Bryan Mishkin <698306+bmish@users.noreply.github.com>
Co-authored-by: Joan Cejudo <joan@joans-mbp.myfiosgateway.com>
Co-authored-by: Joan Cejudo <joan@Joans-MacBook-Pro.local>
Fixes ember-cli#1701
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.

Propose new rule: no-tracked-properties-from-args
3 participants