Add a --fix option for dot-notation
See original GitHub issueI’m using Node v6.4.0 and ESLint v3.4.0.
It would be useful to have an autofix option for the dot-notation
rule. The rule currently reports cases where bracket notation can be safely replaced by dot notation; the fixer would go ahead and make that replacement.
var foo = bar["baz"];
// gets fixed to:
var foo = bar.baz;
The fixer could also replace invalid dot-notations (e.g. keywords when the {allowKeywords: false}
option is set) with the bracket-notation counterpart.
/*eslint dot-notation: ["error", { "allowKeywords": false }]*/
var foo = bar.while;
// gets fixed to:
var foo = bar["while"];
If the brackets contain comments, I’m unsure of what the fixer should do. I suppose one option would be to extract the comment out:
var foo = bar[ /* AA */ "baz" /* BB */ ];
// gets fixed to:
var foo = bar /* AA */ .baz /* BB */ ;
…but that doesn’t look very good. I think comments within bracketed properties are fairly rare anyway, so this might be okay. (Alternatively, we could refrain from fixing cases where the brackets contain a comment, but in that case it seems more consistent to not report those cases as errors either.)
I would be willing to add this if the issue is accepted.
edit: Added a section about fixing from dot-notation to bracket-notation with keywords. edit: Added a section about what to do with comments.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:5 (5 by maintainers)
Top GitHub Comments
Seems reasonable. 👍
To echo what @vitorbal said, if we can’t be 100% correct with the fix, we shouldn’t attempt it. So in the case of comments, we should just skip that.
@pmcelhaney if i understood your question correctly, this might be related: https://github.com/eslint/eslint/issues/5958#issuecomment-243117518 TL;DR we won’t autofix when a comment interferes