Skip to content

Commit 0c51d8f

Browse files
authored
Map ESLint's sourceType: commonjs to script (#17349)
1 parent d3215a2 commit 0c51d8f

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

eslint/babel-eslint-parser/src/configuration.cts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ export = function normalizeESLintConfig(options: any) {
1313
return {
1414
babelOptions: { cwd: process.cwd(), ...babelOptions },
1515
ecmaVersion: ecmaVersion === "latest" ? 1e8 : ecmaVersion,
16-
sourceType,
16+
// https://eslint.org/docs/latest/use/configure/language-options#specifying-javascript-options
17+
// ESLint supports "commonjs" but Babel parser does not.
18+
sourceType: sourceType === "commonjs" ? "script" : sourceType,
1719
requireConfigFile,
1820
...otherOptions,
1921
} as Options;

eslint/babel-eslint-tests/test/integration/eslint/config.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,19 @@ describe("ESLint config", () => {
2020
},
2121
);
2222
});
23+
24+
it('should allow sourceType to be "commonjs"', () => {
25+
// sourceType "commonjs" allows require() calls.
26+
verifyAndAssertMessages(
27+
'require("greetings").hello',
28+
{},
29+
undefined,
30+
undefined,
31+
{
32+
parserOptions: {
33+
sourceType: "commonjs",
34+
},
35+
},
36+
);
37+
});
2338
});

0 commit comments

Comments
 (0)