Skip to content

Commit

Permalink
feat(linter): support yaml for flat config conversion (nrwl#20022)
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav authored Nov 22, 2023
1 parent 31df83b commit d1a213f
Show file tree
Hide file tree
Showing 10 changed files with 362 additions and 91 deletions.
6 changes: 5 additions & 1 deletion packages/eslint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"executors": "./executors.json",
"generators": "./generators.json",
"peerDependencies": {
"eslint": "^8.0.0"
"eslint": "^8.0.0",
"js-yaml": "4.1.0"
},
"dependencies": {
"tslib": "^2.3.0",
Expand All @@ -41,6 +42,9 @@
"peerDependenciesMeta": {
"eslint": {
"optional": true
},
"js-yaml": {
"optional": true
}
},
"publishConfig": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ exports[`convert-to-flat-config generator should add env configuration 1`] = `
const nxEslintPlugin = require('@nx/eslint-plugin');
const globals = require('globals');
const js = require('@eslint/js');
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
});
module.exports = [
{ plugins: { '@nx': nxEslintPlugin } },
{ languageOptions: { globals: { ...globals.browser, ...globals.node } } },
Expand Down Expand Up @@ -49,10 +51,12 @@ exports[`convert-to-flat-config generator should add global and env configuratio
const nxEslintPlugin = require('@nx/eslint-plugin');
const globals = require('globals');
const js = require('@eslint/js');
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
});
module.exports = [
{ plugins: { '@nx': nxEslintPlugin } },
{
Expand Down Expand Up @@ -96,10 +100,12 @@ exports[`convert-to-flat-config generator should add global configuration 1`] =
"const { FlatCompat } = require('@eslint/eslintrc');
const nxEslintPlugin = require('@nx/eslint-plugin');
const js = require('@eslint/js');
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
});
module.exports = [
{ plugins: { '@nx': nxEslintPlugin } },
{ languageOptions: { globals: { myCustomGlobal: 'readonly' } } },
Expand Down Expand Up @@ -139,10 +145,12 @@ exports[`convert-to-flat-config generator should add global eslintignores 1`] =
"const { FlatCompat } = require('@eslint/eslintrc');
const nxEslintPlugin = require('@nx/eslint-plugin');
const js = require('@eslint/js');
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
});
module.exports = [
{ plugins: { '@nx': nxEslintPlugin } },
{
Expand Down Expand Up @@ -183,10 +191,12 @@ exports[`convert-to-flat-config generator should add parser 1`] = `
const nxEslintPlugin = require('@nx/eslint-plugin');
const typescriptEslintParser = require('@typescript-eslint/parser');
const js = require('@eslint/js');
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
});
module.exports = [
{ plugins: { '@nx': nxEslintPlugin } },
{ languageOptions: { parser: typescriptEslintParser } },
Expand Down Expand Up @@ -229,10 +239,12 @@ const eslintPluginSingleName = require('eslint-plugin-single-name');
const scopeEslintPluginWithName = require('@scope/eslint-plugin-with-name');
const justScopeEslintPlugin = require('@just-scope/eslint-plugin');
const js = require('@eslint/js');
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
});
module.exports = [
{
plugins: {
Expand Down Expand Up @@ -278,10 +290,12 @@ exports[`convert-to-flat-config generator should add settings 1`] = `
"const { FlatCompat } = require('@eslint/eslintrc');
const nxEslintPlugin = require('@nx/eslint-plugin');
const js = require('@eslint/js');
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
});
module.exports = [
{ plugins: { '@nx': nxEslintPlugin } },
{ settings: { sharedData: 'Hello' } },
Expand Down Expand Up @@ -317,8 +331,123 @@ module.exports = [
"
`;

exports[`convert-to-flat-config generator should handle custom eslintignores 1`] = `
exports[`convert-to-flat-config generator should convert json successfully 1`] = `
"const { FlatCompat } = require('@eslint/eslintrc');
const nxEslintPlugin = require('@nx/eslint-plugin');
const js = require('@eslint/js');
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
});
module.exports = [
{ plugins: { '@nx': nxEslintPlugin } },
{
files: ['**/*.ts', '**/*.tsx', '**/*.js', '**/*.jsx'],
rules: {
'@nx/enforce-module-boundaries': [
'error',
{
enforceBuildableLibDependency: true,
allow: [],
depConstraints: [
{
sourceTag: '*',
onlyDependOnLibsWithTags: ['*'],
},
],
},
],
},
},
...compat.config({ extends: ['plugin:@nx/typescript'] }).map((config) => ({
...config,
files: ['**/*.ts', '**/*.tsx'],
rules: {},
})),
...compat.config({ extends: ['plugin:@nx/javascript'] }).map((config) => ({
...config,
files: ['**/*.js', '**/*.jsx'],
rules: {},
})),
];
"
`;

exports[`convert-to-flat-config generator should convert json successfully 2`] = `
"const baseConfig = require('../../eslint.config.js');
module.exports = [
...baseConfig,
{
files: [
'libs/test-lib/**/*.ts',
'libs/test-lib/**/*.tsx',
'libs/test-lib/**/*.js',
'libs/test-lib/**/*.jsx',
],
rules: {},
},
{
files: ['libs/test-lib/**/*.ts', 'libs/test-lib/**/*.tsx'],
rules: {},
},
{
files: ['libs/test-lib/**/*.js', 'libs/test-lib/**/*.jsx'],
rules: {},
},
];
"
`;

exports[`convert-to-flat-config generator should convert yaml successfully 1`] = `
"const { FlatCompat } = require('@eslint/eslintrc');
const nxEslintPlugin = require('@nx/eslint-plugin');
const js = require('@eslint/js');
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
});
module.exports = [
{ plugins: { '@nx': nxEslintPlugin } },
{
files: ['**/*.ts', '**/*.tsx', '**/*.js', '**/*.jsx'],
rules: {
'@nx/enforce-module-boundaries': [
'error',
{
enforceBuildableLibDependency: true,
allow: [],
depConstraints: [
{
sourceTag: '*',
onlyDependOnLibsWithTags: ['*'],
},
],
},
],
},
},
...compat.config({ extends: ['plugin:@nx/typescript'] }).map((config) => ({
...config,
files: ['**/*.ts', '**/*.tsx'],
rules: {},
})),
...compat.config({ extends: ['plugin:@nx/javascript'] }).map((config) => ({
...config,
files: ['**/*.js', '**/*.jsx'],
rules: {},
})),
];
"
`;

exports[`convert-to-flat-config generator should convert yaml successfully 2`] = `
"const baseConfig = require('../../eslint.config.js');
module.exports = [
...baseConfig,
{
Expand All @@ -338,20 +467,20 @@ module.exports = [
files: ['libs/test-lib/**/*.js', 'libs/test-lib/**/*.jsx'],
rules: {},
},
{ ignores: ['libs/test-lib/ignore/me'] },
{ ignores: ['libs/test-lib/ignore/me/as/well'] },
];
"
`;

exports[`convert-to-flat-config generator should run successfully 1`] = `
exports[`convert-to-flat-config generator should convert yml successfully 1`] = `
"const { FlatCompat } = require('@eslint/eslintrc');
const nxEslintPlugin = require('@nx/eslint-plugin');
const js = require('@eslint/js');
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
});
module.exports = [
{ plugins: { '@nx': nxEslintPlugin } },
{
Expand Down Expand Up @@ -386,8 +515,35 @@ module.exports = [
"
`;

exports[`convert-to-flat-config generator should run successfully 2`] = `
exports[`convert-to-flat-config generator should convert yml successfully 2`] = `
"const baseConfig = require('../../eslint.config.js');
module.exports = [
...baseConfig,
{
files: [
'libs/test-lib/**/*.ts',
'libs/test-lib/**/*.tsx',
'libs/test-lib/**/*.js',
'libs/test-lib/**/*.jsx',
],
rules: {},
},
{
files: ['libs/test-lib/**/*.ts', 'libs/test-lib/**/*.tsx'],
rules: {},
},
{
files: ['libs/test-lib/**/*.js', 'libs/test-lib/**/*.jsx'],
rules: {},
},
];
"
`;

exports[`convert-to-flat-config generator should handle custom eslintignores 1`] = `
"const baseConfig = require('../../eslint.config.js');
module.exports = [
...baseConfig,
{
Expand All @@ -407,6 +563,8 @@ module.exports = [
files: ['libs/test-lib/**/*.js', 'libs/test-lib/**/*.jsx'],
rules: {},
},
{ ignores: ['libs/test-lib/ignore/me'] },
{ ignores: ['libs/test-lib/ignore/me/as/well'] },
];
"
`;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Tree } from '@nx/devkit';
import { Tree, readJson } from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { convertEslintJsonToFlatConfig } from './json-converter';

Expand Down Expand Up @@ -58,22 +58,23 @@ describe('convertEslintJsonToFlatConfig', () => {

tree.write('.eslintignore', 'node_modules\nsomething/else');

convertEslintJsonToFlatConfig(
const { content } = convertEslintJsonToFlatConfig(
tree,
'',
'.eslintrc.json',
'eslint.config.js',
readJson(tree, '.eslintrc.json'),
['.eslintignore']
);

expect(tree.read('eslint.config.js', 'utf-8')).toMatchInlineSnapshot(`
expect(content).toMatchInlineSnapshot(`
"const { FlatCompat } = require("@eslint/eslintrc");
const nxEslintPlugin = require("@nx/eslint-plugin");
const js = require("@eslint/js");
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
});
module.exports = [
{ plugins: { "@nx": nxEslintPlugin } },
{
Expand Down Expand Up @@ -118,8 +119,6 @@ describe('convertEslintJsonToFlatConfig', () => {
];
"
`);

expect(tree.exists('.eslintrc.json')).toBeFalsy();
});

it('should convert project configs', async () => {
Expand Down Expand Up @@ -170,23 +169,24 @@ describe('convertEslintJsonToFlatConfig', () => {

tree.write('mylib/.eslintignore', 'node_modules\nsomething/else');

convertEslintJsonToFlatConfig(
const { content } = convertEslintJsonToFlatConfig(
tree,
'mylib',
'.eslintrc.json',
'eslint.config.js',
readJson(tree, 'mylib/.eslintrc.json'),
['mylib/.eslintignore']
);

expect(tree.read('mylib/eslint.config.js', 'utf-8')).toMatchInlineSnapshot(`
expect(content).toMatchInlineSnapshot(`
"const { FlatCompat } = require("@eslint/eslintrc");
const baseConfig = require("../../eslint.config.js");
const globals = require("globals");
const js = require("@eslint/js");
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
});
module.exports = [
...baseConfig,
...compat.extends("plugin:@nx/react-typescript", "next", "next/core-web-vitals"),
Expand Down Expand Up @@ -228,7 +228,5 @@ describe('convertEslintJsonToFlatConfig', () => {
];
"
`);

expect(tree.exists('mylib/.eslintrc.json')).toBeFalsy();
});
});
Loading

0 comments on commit d1a213f

Please sign in to comment.