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

feat: warn by default for unused disable directives #17879

Merged
merged 11 commits into from Dec 28, 2023

Conversation

bmish
Copy link
Sponsor Member

@bmish bmish commented Dec 19, 2023

Prerequisites checklist

What is the purpose of this pull request? (put an "X" next to an item)

[ ] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofix to a rule
[ ] Add a CLI option
[x] Add something to the core
[ ] Other, please explain:

What changes did you make? (Give an overview)

Fixes #15466. This is part two of that issue. Part one was in #17212.

The goal here is to warn by default for reporting unused disable directives, only when using the new flat config.

TODO:

  • Fix 35 tests in tests/lib/linter/linter.js
  • Update docs
  • Add new test

Is there anything you'd like reviewers to focus on?

  1. Is this the right place to change the default?
  2. Where does the default need to be documented?

@eslint-github-bot eslint-github-bot bot added the feature This change adds a new feature to ESLint label Dec 19, 2023
Copy link

netlify bot commented Dec 19, 2023

Deploy Preview for docs-eslint canceled.

Name Link
🔨 Latest commit 07352ef
🔍 Latest deploy log https://app.netlify.com/sites/docs-eslint/deploys/658caff9cb3f6500083855ff

@mdjermanovic
Copy link
Member

  1. Is this the right place to change the default?

I was thinking about setting this default in default-config.js. For example, in its first object. That way, it will appear in --print-config, which is good I believe.

     languageOptions: {
         sourceType: "module",
         ecmaVersion: "latest",
         parser: require("espree"),
         parserOptions: {}
     },
+    linterOptions: {
+        reportUnusedDisableDirectives: 1
+    }

* main: (25 commits)
  test: ensure that CLI tests run with FlatESLint (eslint#17884)
  fix!: Behavior of CLI when no arguments are passed (eslint#17644)
  docs: Update README
  Revert "feat!: Remove CodePath#currentSegments" (eslint#17890)
  feat!: Update shouldUseFlatConfig and CLI so flat config is default (eslint#17748)
  feat!: Remove CodePath#currentSegments (eslint#17756)
  chore: update dependency markdownlint-cli to ^0.38.0 (eslint#17865)
  feat!: deprecate no-new-symbol, recommend no-new-native-nonconstructor (eslint#17710)
  feat!: check for parsing errors in suggestion fixes (eslint#16639)
  feat!: assert suggestion messages are unique in rule testers (eslint#17532)
  feat!: `no-invalid-regexp` make allowConstructorFlags case-sensitive (eslint#17533)
  fix!: no-sequences rule schema correction (eslint#17878)
  feat!: Update `eslint:recommended` configuration (eslint#17716)
  feat!: drop support for string configurations in flat config array (eslint#17717)
  feat!: Remove `SourceCode#getComments()` (eslint#17715)
  feat!: Remove deprecated context methods (eslint#17698)
  feat!: Swap FlatESLint-ESLint, FlatRuleTester-RuleTester in API (eslint#17823)
  feat!: remove formatters except html, json(-with-metadata), and stylish (eslint#17531)
  feat!: Require Node.js `^18.18.0 || ^20.9.0 || >=21.1.0` (eslint#17725)
  fix: allow circular references in config (eslint#17752)
  ...
* main:
  chore: use jsdoc/no-multi-asterisks with allowWhitespace: true (eslint#17900)
  chore: fix getting scope in tests (eslint#17899)
  fix!: Parsing 'exported' comment using parseListConfig (eslint#17675)
  docs: updated examples of `max-lines` rule (eslint#17898)
  feat!: Remove valid-jsdoc and require-jsdoc (eslint#17694)
@mdjermanovic
Copy link
Member

@bmish are you still working on this? This is one of the last changes planned for the upcoming v9.0.0-alpha.0.

  • Fix 35 tests in tests/lib/linter/linter.js

Could we maybe fix those tests by adding linterOptions: { reportUnusedDisableDirectives: 0 } to the configs they're passing to linter.verify()?

@mdjermanovic
Copy link
Member

2. Where does the default need to be documented?

I think here:

https://eslint.org/docs/head/use/configure/configuration-files#reporting-unused-disable-directives

Also here (at some point in the future, we'll maybe clear this redundancy we now have in the docs):

https://eslint.org/docs/head/use/configure/rules#report-unused-eslint-disable-comments

And maybe here, at the end of the Default Value line we could add (defaults to "warn"):

https://eslint.org/docs/head/use/command-line-interface#--report-unused-disable-directives-severity

* main:
  docs: Migrate to v9.0.0 (eslint#17905)
  docs: Switch to flat config by default (eslint#17840)
  docs: Update Create a Plugin for flat config (eslint#17826)
  feat!: add two more cases to `no-implicit-coercion` (eslint#17832)
  docs: Switch shareable config docs to use flat config (eslint#17827)
  chore: remove creating an unused instance of Linter in tests (eslint#17902)
  feat!: Switch Linter to flat config by default (eslint#17851)
@bmish
Copy link
Sponsor Member Author

bmish commented Dec 27, 2023

I moved the default to default-config.js and fixed the existing tests. Thanks for the tips.

I still need to add a test for the new default and update some more docs. Will try to do this today.

@bmish
Copy link
Sponsor Member Author

bmish commented Dec 27, 2023

I have updated the docs and added tests. This is ready for review.

@bmish bmish marked this pull request as ready for review December 27, 2023 16:14
@bmish bmish requested a review from a team as a code owner December 27, 2023 16:14
@mdjermanovic mdjermanovic added the accepted There is consensus among the team that this change meets the criteria for inclusion label Dec 27, 2023
Copy link
Member

@nzakas nzakas left a comment

Choose a reason for hiding this comment

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

Overall LGTM. Just a note on changing rule tests.

@@ -59,8 +59,8 @@ ruleTester.run("capitalized-comments", rule, {
// No options: eslint/istanbul/jshint/jscs/globals?/exported are okay
Copy link
Member

Choose a reason for hiding this comment

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

I think in this and the other rule tests, the intent was to check if directive comments were handled correctly.

Rather than changing all directive comments to non-directive comments, could we instead just turn off reporting of unused directives in those files?

Copy link
Member

Choose a reason for hiding this comment

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

Looks like the old rule tests aren't actually failing now, because FlatRuleTester has its own baseConfig, where linterOptions.reportUnusedDisableDirectives isn't set.

const baseConfig = [
{ files: ["**"] }, // Make sure the default config matches for all files
{
plugins: {
// copy root plugin over
"@": {
/*
* Parsers are wrapped to detect more errors, so this needs
* to be a new object for each call to run(), otherwise the
* parsers will be wrapped multiple times.
*/
parsers: {
...defaultConfig[0].plugins["@"].parsers
},
/*
* The rules key on the default plugin is a proxy to lazy-load
* just the rules that are needed. So, don't create a new object
* here, just use the default one to keep that performance
* enhancement.
*/
rules: defaultConfig[0].plugins["@"].rules
},
"rule-to-test": {
rules: {
[ruleName]: Object.assign({}, rule, {
// Create a wrapper rule that freezes the `context` properties.
create(context) {
freezeDeeply(context.options);
freezeDeeply(context.settings);
freezeDeeply(context.parserOptions);
// freezeDeeply(context.languageOptions);
return rule.create(context);
}
})
}
}
},
languageOptions: {
...defaultConfig[0].languageOptions
}
},
...defaultConfig.slice(1)
];

Now the question is shall we leave it that way?

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

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

Good points. I reverted the rule tests.

On the one hand, it seems like rule tests should generally use the default config? But on the other hand, reporting disable directives seems like it would be unnecessary and even annoying during rule tests, so I'd probably prefer to leave that option off...

Copy link
Member

Choose a reason for hiding this comment

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

I think reporting unused directives is probably best left off for rule tests -- I can't imagine people wanting those warnings intermixed with rule errors.

Copy link
Member

@nzakas nzakas left a comment

Choose a reason for hiding this comment

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

LGTM. Would like @mdjermanovic to confirm before merging.

Copy link
Member

@mdjermanovic mdjermanovic left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion feature This change adds a new feature to ESLint
Projects
Status: Complete
Development

Successfully merging this pull request may close these issues.

Change Request: Enable reportUnusedDisableDirectives config option by default
3 participants