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

can .eslintrc.cjs be a .js instead? #371

Open
zepumph opened this issue Feb 21, 2023 · 2 comments
Open

can .eslintrc.cjs be a .js instead? #371

zepumph opened this issue Feb 21, 2023 · 2 comments
Assignees

Comments

@zepumph
Copy link
Member

zepumph commented Feb 21, 2023

@liammulh why is this a cjs file? It wasn't being linted. Do you have a preference this way? None of our other eslint files are that file extension. Also, sublime doesn't auto recognize the file extension, so although I could google it to find out that it is a commonJS file, it doesn't seem too standard.

@liammulh
Copy link
Member

Rosetta's .eslintrc.cjs extends Chipper's:

rosetta/.eslintrc.cjs

Lines 6 to 11 in 35bb0b7

"extends": [
"plugin:react/recommended",
"../chipper/eslint/sim_eslintrc.js",
"../chipper/eslint/node_eslintrc.js",
"../chipper/eslint/format_eslintrc.js"
],

Chipper is configured to use CommonJS modules, but Rosetta is configured to use ECMAScript modules. So in Chipper, .js modules are interpreted as CommonJS modules whereas in Rosetta .js files are interpreted as ECMAScript modules.

In Rosetta we have to use .cjs to specify that the ESLint config file should be interpreted as a CommonJS module rather than an ECMAScript module. Otherwise you'd get an error like:

Error
Running "lint" task
Fatal error: Perennial task failed:
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/liam/,/programs/phet/rosetta/.eslintrc.js from /Users/liam/,/programs/phet/chipper/node_modules/@eslint/eslintrc/dist/eslintrc.cjs not supported.
.eslintrc.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead rename .eslintrc.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /Users/liam/,/programs/phet/rosetta/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

    at Object.module.exports [as default] (/Users/liam/,/programs/phet/chipper/node_modules/import-fresh/index.js:32:59)
    at loadJSConfigFile (/Users/liam/,/programs/phet/chipper/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:2562:47)
    at loadConfigFile (/Users/liam/,/programs/phet/chipper/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:2646:20)
    at ConfigArrayFactory.loadInDirectory (/Users/liam/,/programs/phet/chipper/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:2856:34)
    at CascadingConfigArrayFactory._loadConfigInAncestors (/Users/liam/,/programs/phet/chipper/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3848:46)
    at CascadingConfigArrayFactory.getConfigArrayForFile (/Users/liam/,/programs/phet/chipper/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3769:18)
    at FileEnumerator._iterateFilesRecursive (/Users/liam/,/programs/phet/chipper/node_modules/eslint/lib/cli-engine/file-enumerator.js:448:49)
    at _iterateFilesRecursive.next (<anonymous>)
    at FileEnumerator.iterateFiles (/Users/liam/,/programs/phet/chipper/node_modules/eslint/lib/cli-engine/file-enumerator.js:299:49)
    at iterateFiles.next (<anonymous>)
    at CLIEngine.executeOnFiles (/Users/liam/,/programs/phet/chipper/node_modules/eslint/lib/cli-engine/cli-engine.js:786:48)
    at ESLint.lintFiles (/Users/liam/,/programs/phet/chipper/node_modules/eslint/lib/eslint/eslint.js:551:23)
    at lintOneRepo (/Users/liam/,/programs/phet/chipper/js/grunt/lint.js:120:32)
    at lint (/Users/liam/,/programs/phet/chipper/js/grunt/lint.js:170:27)
    at /Users/liam/,/programs/phet/chipper/js/grunt/Gruntfile.js:361:37
    at Object.<anonymous> (/Users/liam/,/programs/phet/chipper/js/grunt/Gruntfile.js:94:13)
    at Object.thisTask.fn (/Users/liam/,/programs/phet/rosetta/node_modules/grunt/lib/grunt/task.js:70:16)
    at Object.<anonymous> (/Users/liam/,/programs/phet/rosetta/node_modules/grunt/lib/util/task.js:294:30)
    at Task.runTaskFn (/Users/liam/,/programs/phet/rosetta/node_modules/grunt/lib/util/task.js:244:24)
    at Task.<anonymous> (/Users/liam/,/programs/phet/rosetta/node_modules/grunt/lib/util/task.js:293:12)
    at Task.start (/Users/liam/,/programs/phet/rosetta/node_modules/grunt/lib/util/task.js:302:5)
    at Object.grunt.tasks (/Users/liam/,/programs/phet/rosetta/node_modules/grunt/lib/grunt.js:173:8)
    at Liftoff.<anonymous> (/Users/liam/,/programs/phet/rosetta/node_modules/grunt-cli/bin/grunt:68:15)
    at Liftoff.execute (/Users/liam/,/programs/phet/rosetta/node_modules/liftup/index.js:202:12)
    at module.exports (/Users/liam/,/programs/phet/rosetta/node_modules/flagged-respawn/index.js:51:3)
    at Liftoff.<anonymous> (/Users/liam/,/programs/phet/rosetta/node_modules/liftup/index.js:192:5)
    at Liftoff.<anonymous> (/Users/liam/,/programs/phet/rosetta/node_modules/liftup/index.js:155:7)
Full Error details:
Error [ERR_REQUIRE_ESM]: Cannot read config file: /Users/liam/,/programs/phet/rosetta/.eslintrc.js
Error: require() of ES Module /Users/liam/,/programs/phet/rosetta/.eslintrc.js from /Users/liam/,/programs/phet/chipper/node_modules/@eslint/eslintrc/dist/eslintrc.cjs not supported.
.eslintrc.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead rename .eslintrc.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /Users/liam/,/programs/phet/rosetta/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

@zepumph
Copy link
Member Author

zepumph commented Feb 21, 2023

Sounds good, thanks for explaining. With that in mind:

  • let's go ahead and add .mjs and .cjs as file extensions that should be linted over in CHIPPER/lint.js
  • Run grunt lint-everything to make sure that no errors sneak in from other cjs files.
  • Please add notes about .cjs in rosetta to some documentation, likely the implementation-notes.md

How does that sound?

@zepumph zepumph removed their assignment Feb 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants