Skip to content

Commit

Permalink
feat(core): Add support for ESM config file
Browse files Browse the repository at this point in the history
  • Loading branch information
ndcunningham committed Jan 15, 2025
1 parent 7ef4fa7 commit 435a1a5
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,10 @@ describe('convertEslintJsonToFlatConfig', () => {
);

expect(content).toMatchInlineSnapshot(`
"const { FlatCompat } = require("@eslint/eslintrc");
const js = require("@eslint/js");
const nxEslintPlugin = require("@nx/eslint-plugin");
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
});
module.exports = [
"import { FlatCompat } from "@eslint/eslintrc";
import js from "@eslint/js";
import nxEslintPlugin from "@nx/eslint-plugin";
export const myExports = [
{
ignores: [
"**/dist"
Expand Down Expand Up @@ -172,6 +166,12 @@ describe('convertEslintJsonToFlatConfig', () => {
]
}
];
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
});
export default myExports;
"
`);
});
Expand Down Expand Up @@ -232,17 +232,11 @@ describe('convertEslintJsonToFlatConfig', () => {
);

expect(content).toMatchInlineSnapshot(`
"const { FlatCompat } = require("@eslint/eslintrc");
const js = require("@eslint/js");
const baseConfig = require("../../eslint.config.cjs");
const globals = require("globals");
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
});
module.exports = [
"import { FlatCompat } from "@eslint/eslintrc";
import js from "@eslint/js";
import baseConfig from "../../eslint.config.mjs";
import globals from "globals";
export const myExports = [
{
ignores: [
"**/dist"
Expand Down Expand Up @@ -294,7 +288,7 @@ describe('convertEslintJsonToFlatConfig', () => {
"@nx/dependency-checks": "error"
},
languageOptions: {
parser: require("jsonc-eslint-parser")
parser: await import("jsonc-eslint-parser")
}
},
{
Expand All @@ -308,6 +302,12 @@ describe('convertEslintJsonToFlatConfig', () => {
]
}
];
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
});
export default myExports;
"
`);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ function addExtends(
configBlocks.push(generateSpreadElement(localName));
const newImport = imp.replace(
/^(.*)\.eslintrc(.base)?\.json$/,
'$1eslint$2.config.cjs'
'$1eslint$2.config.mjs'
);
importsMap.set(newImport, localName);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,17 @@ describe('convert-to-flat-config generator', () => {

expect(tree.exists('eslint.config.cjs')).toBeTruthy();
expect(tree.read('eslint.config.cjs', 'utf-8')).toMatchSnapshot();
expect(tree.exists('libs/test-lib/eslint.config.cjs')).toBeTruthy();
expect(tree.exists('libs/test-lib/eslint.config.mjs')).toBeTruthy();
expect(
tree.read('libs/test-lib/eslint.config.cjs', 'utf-8')
).toMatchSnapshot();
// check nx.json changes
const nxJson = readJson(tree, 'nx.json');
expect(nxJson.targetDefaults.lint.inputs).toContain(
'{workspaceRoot}/eslint.config.cjs'
'{workspaceRoot}/eslint.config.mjs'
);
expect(nxJson.namedInputs.production).toContain(
'!{projectRoot}/eslint.config.cjs'
'!{projectRoot}/eslint.config.mjs'
);
});

Expand All @@ -112,19 +112,19 @@ describe('convert-to-flat-config generator', () => {

await convertToFlatConfigGenerator(tree, options);

expect(tree.exists('eslint.config.cjs')).toBeTruthy();
expect(tree.read('eslint.config.cjs', 'utf-8')).toMatchSnapshot();
expect(tree.exists('libs/test-lib/eslint.config.cjs')).toBeTruthy();
expect(tree.exists('eslint.config.mjs')).toBeTruthy();
expect(tree.read('eslint.config.mjs', 'utf-8')).toMatchSnapshot();
expect(tree.exists('libs/test-lib/eslint.config.mjs')).toBeTruthy();
expect(
tree.read('libs/test-lib/eslint.config.cjs', 'utf-8')
tree.read('libs/test-lib/eslint.config.mjs', 'utf-8')
).toMatchSnapshot();
// check nx.json changes
const nxJson = readJson(tree, 'nx.json');
expect(nxJson.targetDefaults.lint.inputs).toContain(
'{workspaceRoot}/eslint.config.cjs'
);
expect(nxJson.namedInputs.production).toContain(
'!{projectRoot}/eslint.config.cjs'
'!{projectRoot}/eslint.config.mjs'
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ export default convertToFlatConfigGenerator;

function convertRootToFlatConfig(tree: Tree, eslintFile: string) {
if (/\.base\.(js|json|yml|yaml)$/.test(eslintFile)) {
convertConfigToFlatConfig(tree, '', eslintFile, 'eslint.base.config.cjs');
convertConfigToFlatConfig(tree, '', eslintFile, 'eslint.base.config.mjs');
}
convertConfigToFlatConfig(
tree,
'',
eslintFile.replace('.base.', '.'),
'eslint.config.cjs'
'eslint.config.mjs'
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ function createEsLintConfiguration(
});
const nodeList = createNodeList(importMap, nodes);
const content = stringifyNodeList(nodeList);
tree.write(join(projectConfig.root, `eslint.config.cjs`), content);
tree.write(join(projectConfig.root, `eslint.config.mjs`), content);
} else {
writeJson(tree, join(projectConfig.root, `.eslintrc.json`), {
extends: extendedRootConfig ? [pathToRootConfig] : undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ describe('ast-utils', () => {

describe('addBlockToFlatConfigExport', () => {
it('should inject block to the end of the file', () => {
const content = `const baseConfig = require("../../eslint.config.cjs");
module.exports = [
const content = `const baseConfig = import("../../eslint.config.mjs");
export default myExports = [
...baseConfig,
{
files: [
Expand Down
Loading

0 comments on commit 435a1a5

Please sign in to comment.