question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add a --fix option for dot-notation

See original GitHub issue

I’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:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
nzakascommented, Sep 3, 2016

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.

1reaction
vitorbalcommented, Aug 30, 2016

@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

Read more comments on GitHub >

github_iconTop Results From Across the Web

dot-notation - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
dot-notation | typescript-eslint
See eslint/dot-notation options. This rule adds the following options: interface Options extends BaseDotNotationOptions { allowPrivateClassPropertyAccess?
Read more >
How to replace square bracket notation (abc['prop']) to dot ...
The dot-notation rule for ESLint with the --fix option can automatically correct it. quote-props (which can also be auto-fixed) can also ...
Read more >
JavaScript for Beginners #44 Objects Dot Notation vs Bracket ...
Here, we will explore the difference between using bracket notation and dot notation with objects.0:00 Setting up our object1:05 Viewing the ...
Read more >
Tag: Dot Notation - somewhat abstract
The `Distinct()` method is not the only LINQ method that has no query syntax alternative, there are plenty of others like `Aggregate()`, `Except()`,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found