-
-
Notifications
You must be signed in to change notification settings - Fork 381
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 consistent-destructuring
rule
#325
Add consistent-destructuring
rule
#325
Conversation
@MrHen I added all of your suggested test cases, though I'm still not sure how to tackle this one: let foo = {a: 1};
const {a} = foo;
foo = {a: 2};
console.log(foo.a); I found another example in this codebase: const spinner = new Ora('foo');
spinner.start();
const {id} = spinner;
spinner.start();
t.is(id, spinner.id); The t.is(id, id); Now it doesn't make sense anymore. |
There needs to be a check to see if a variable in that scope already exists. If there is one, either (a) use avoid-capture to pick a name not being used or (b) chose not to fix it at all. I lean toward (b) because this is probably an unusual situation. |
I'm not talking about the case where the variable is already defined and therefore can't be destructured. I'm concerned about the situtation where the destructured member is changed after being destructured (The last test case you suggested). If I apply my fix to this scenario, the semantics of the code will change. |
Oh, I see. I wasn't even originally worried about that but now I get the issue. A really common example of this would be getters: class Foo {
let _a = 'foo';
set a(value) {
this._a = value;
}
get a() {
return this._a;
}
}
const foo = new Foo();
const { a } = foo;
foo.a = 'bar';
console.log(a, foo.a); The smallest example would just be: const foo = {a: 1};
const {a} = foo;
foo.a = 2;
console.log(a, foo.a); I'm not sure how to deal with this, either. @sindresorhus, @futpib, either of you have any thoughts / insights? |
The |
@sindresorhus The |
I think for that case we can use the ESLint suggestions API instead of a fixer. Then the user can decide to manually apply the suggestion. |
I have now switched to the suggestions API as suggested and it seems to be working great. It catched a bunch of problems in all kinds of different rules, that's why the lint task is failing. |
@medusalix Don't try to fix lint, We can do review first. |
Did you merge the latest master? Failed integration test should has a link link to the source code. |
Integration tests need to be fixed, you can use this as an example, find the code add to tests. |
Destructurings of function calls inside member expression should be ignored now. Might need some additional tests. |
Can you add some? |
Friendly bump :) |
I've added tests for various kinds of expressions (as defined by the |
Co-Authored-By: Sindre Sorhus <[email protected]>
Resolves #248
IssueHunt Summary
Referenced issues
This pull request has been submitted to: