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

Bug: flat config with "eslint:all" on a .cjs file recommends function form of "use strict" instead of global form #16304

Closed
1 task
jzinn opened this issue Sep 14, 2022 · 3 comments · Fixed by #16308
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly repro:yes rule Relates to ESLint's core rules
Projects

Comments

@jzinn
Copy link

jzinn commented Sep 14, 2022

Environment

Node version: v16.14.0
npm version: 8.3.1
Local ESLint version: v8.23.1
Global ESLint version: zsh: command not found: eslint
Operating System: Fedora release 36 (Thirty Six)

What parser are you using?

Default (Espree)

What did you do?

package.json

Notice type is module.

{
  "devDependencies": {
    "eslint": "^8.23.1"
  },
  "license": "ISC",
  "name": "eslint-all-cjs",
  "scripts": {
    "lint": "eslint --fix ."
  },
  "type": "module",
  "version": "1.0.0"
}

eslint.config.js

Using the new flat config file format. The default languageOptions sourceType should be commonjs for .cjs files.

export default ["eslint:all"];

index.cjs

A .cjs file that ESLint should consider to be a commonjs file.

module.exports = function identity (value) {

    return value;

};

What did you expect to happen?

Expected: a message to use the global form of "use strict".

Why: the docs for rule strict say:

This rule has a string option:

  • "safe" (default) corresponds either of the following options:
    • "global" if ESLint considers a file to be a CommonJS module
    • "function" otherwise

The file ends with .cjs, so ESLint should recommend "global".

What actually happened?

A message to use the function form:

/path/to/eslint-all-cjs/index.cjs
  1:18  error  Use the function form of 'use strict'  strict

✖ 1 problem (1 error, 0 warnings)

Participation

  • I am willing to submit a pull request for this issue.

Additional comments

No response

@jzinn jzinn added bug ESLint is working incorrectly repro:needed labels Sep 14, 2022
@eslint-github-bot eslint-github-bot bot added this to Needs Triage in Triage Sep 14, 2022
@jzinn
Copy link
Author

jzinn commented Sep 14, 2022

The docs for strict mention two ways to make "safe" equal "global".

The first is to set the environment to node or commonjs. I'm not sure how to do this with the new flat configuration files.

The second is to set globalReturn like this:

export default [
    "eslint:all",
    {
        "languageOptions": {
            "parserOptions": {
                "ecmaFeatures": {
                    "globalReturn": true
                }
            }
        }
    }
];

Setting globalReturn works, so I guess this issue is invalid.

It would be nice if this were a little easier to set up like by specifying the node environment if that's still possible.

Perhaps the .cjs extension should imply that the contents are a commonjs module file instead of only a commonjs file. It's a new (-ish) extension after all.

Just configuring the rule might be best:

export default [
    "eslint:all",
    {
        rules: {
            strict: ["error", "global"]
        }
    }
];

@mdjermanovic mdjermanovic added rule Relates to ESLint's core rules accepted There is consensus among the team that this change meets the criteria for inclusion labels Sep 14, 2022
@mdjermanovic mdjermanovic moved this from Needs Triage to Pull Request Opened in Triage Sep 14, 2022
@mdjermanovic
Copy link
Member

Hi @jzinn, thanks for the issue!

This rule has a string option:

  • "safe" (default) corresponds either of the following options:

    • "global" if ESLint considers a file to be a CommonJS module
    • "function" otherwise

The file ends with .cjs, so ESLint should recommend "global".

You're right, this is a bug.

The new config system works correctly in this case, as it automatically sets sourceType: "commonjs" for .cjs files.

The problem is in the strict rule since its logic around CommonJS is based on parserOptions.ecmaFeatures.globalReturn field, which is in the new config format dropped in favor of languageOptions.sourceType = "commonjs".

I prepared PR #16308 that should fix this problem.

Triage automation moved this from Pull Request Opened to Complete Sep 16, 2022
@oalrbabaa
Copy link

Environment

Node version: v16.14.0

npm version: 8.3.1

Local ESLint version: v8.23.1

Global ESLint version: zsh: command not found: eslint

Operating System: Fedora release 36 (Thirty Six)

What parser are you using?

Default (Espree)

What did you do?

package.json

Notice type is module.

{

  "devDependencies": {

    "eslint": "^8.23.1"

  },

  "license": "ISC",

  "name": "eslint-all-cjs",

  "scripts": {

    "lint": "eslint --fix ."

  },

  "type": "module",

  "version": "1.0.0"

}

eslint.config.js

Using the new flat config file format. The default languageOptions sourceType should be commonjs for .cjs files.

export default ["eslint:all"];

index.cjs

A .cjs file that ESLint should consider to be a commonjs file.

module.exports = function identity (value) {



    return value;



};

What did you expect to happen?

Expected: a message to use the global form of "use strict".

Why: the docs for rule strict say:

This rule has a string option:

  • "safe" (default) corresponds either of the following options:
  • "global" if ESLint considers a file to be a CommonJS module
  • "function" otherwise

The file ends with .cjs, so ESLint should recommend "global".

What actually happened?

A message to use the function form:


/path/to/eslint-all-cjs/index.cjs

  1:18  error  Use the function form of 'use strict'  strict



✖ 1 problem (1 error, 0 warnings)

Participation

  • I am willing to submit a pull request for this issue.

Additional comments

No response

@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Mar 16, 2023
@eslint-github-bot eslint-github-bot bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Mar 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly repro:yes rule Relates to ESLint's core rules
Projects
Archived in project
Triage
Complete
Development

Successfully merging a pull request may close this issue.

3 participants