-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Bug: rules should not introduce new directives #16988
Comments
I think the mentioned rules should keep reporting these problems. For example, As for the fixes, suggestions are allowed to change the behavior so I think On the other hand, autofixes should never change the behavior of the code, so it seems we should update |
Thanks for the quick reply @mdjermanovic.
Disabling the autofixes while keeping the rules reporting on the problems sounds very reasonable to me.
Shall we at least change the suggestion text to remove the misleading part? For example: Current text
New text
|
Sounds good to me. |
Thanks @mdjermanovic. I've already started to dig into the code of the rules in question and I have a strategy in my mind to ensure that no more than one autofix per program or function body is disabled: whenever a sequence of problems reported by the rules So for instance in this example all rules should report (as it is now the case), but
and the intended autofix would be:
In the end, Clearly, rules have no knowledge about the settings of other rules, so they should always operate under the assumption that all other rules are active and fixing. |
I think that just checking whether the potentially problematic statement is directive-like (ExpressionStatement whose expression is a string literal) is quite good enough for these edge cases. For example, this wouldn't be autofixed because the extra semi is followed by a directive-like statement, although it wouldn't really become a directive when the extra semi is removed: /* eslint no-extra-semi: "error" */
foo();
; "use strict"; |
I like the simplicity of this approach, but I think that the definition of directive-like should be extended to account for fixes provided by the ExpressionStatement whose expression is a string Literal or a TemplateLiteral with no expressions. Following this definition, |
I prepared a fix for |
Environment
Node version: v18.15.0
npm version: v9.5.0
Local ESLint version: v8.36.0 (Currently used)
Global ESLint version: Not found
Operating System: darwin 22.3.0
What parser are you using?
Default (Espree)
What did you do?
ECMAScript 5 or later has a concept of directives such as
"use strict"
and implementation-specific directives like"use asm"
and a few more. Directives follow special syntax rules and can change the semantics of a program.Apparently, some built-it rules do not take the syntax of directives into account.
If these rules have an autofix, they may occasionally introduce a new directive where there was none or turn an existing directive into a different one.
Configuration
REPRO
What did you expect to happen?
Autofixes and suggestions should not add new directives unless intended.
What actually happened?
Considering the examples above, the rules
no-extra-parens
,no-extra-semi
andno-unused-labels
have autofixes that cause a"use strict"
literal to become part of a directive in some cases, resulting in a syntax error (but other undesirable outcomes are also possible).Similarly, the rule
no-useless-escape
suggests to change an unrecognized"use\ strict"
directive into"use strict"
, incorrectly stating that this would maintain the existing functionality.There could be other rules with similar problems as well, I will update this issue if I find more.
On a critical note, if a rule happens to accidentally insert an additional directive by removing or changing what's around, it's likely that the original code was affected by a substantial problem, like a typo, a copy-paste mishap or a misunderstanding of the language syntax. Although the autofix doesn't make sense, removing it wouldn't necessarily make things any better.
Should we suppress the reports that produce incorrect autofixes and suggestions? Anything else we should do here?
Participation
Additional comments
If the team decides that any changes are necessary, there should be probably separate pull requests for each rule. I would be available to do parts of the work.
The text was updated successfully, but these errors were encountered: