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

feat(linter): add generator for converting to flat config #17532

Merged
merged 25 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
a990a23
feat(linter): add generator for converting to flat config
meeroslav Jun 9, 2023
517373a
feat(linter): use ts printer to generate js file
meeroslav Jul 19, 2023
edad8a7
fix(linter): simplify source generation
meeroslav Jul 20, 2023
1aad834
feat(linter): add parsers
meeroslav Jul 20, 2023
51292bf
feat(linter): add preparation for language agnostic parsing
meeroslav Jul 20, 2023
06ed614
feat(linter): add support for plugins
meeroslav Aug 1, 2023
c7c8401
feat(linter): add other parsers
meeroslav Aug 1, 2023
dd46c5d
feat(linter): convert require to import
meeroslav Aug 1, 2023
48c1cf3
fix(linter): update config to latest migration changes
meeroslav Aug 3, 2023
e7da0c8
fix(linter): fix format
meeroslav Aug 3, 2023
08f0d56
feat(linter): add e2e
meeroslav Aug 3, 2023
0c6a0d5
fix(linter): improve ignores message
meeroslav Aug 4, 2023
33cefa7
fix(linter): remove compat where not needed
meeroslav Aug 4, 2023
0564d98
fix(linter): fix base json import
meeroslav Aug 4, 2023
507fd39
fix(linter): expect eslintrc to be added
meeroslav Aug 4, 2023
40ada11
fix(linter): ensure @eslint/js is available
meeroslav Aug 4, 2023
b0754ad
fix(linter): fix globals and env
meeroslav Aug 4, 2023
7d67d72
feat(repo): improve tests
meeroslav Aug 4, 2023
d75a4b6
feat(linter): fix e2e test partially
meeroslav Aug 4, 2023
39873cf
feat(linter): improve ignores
meeroslav Aug 7, 2023
9a35950
fix(linter): fix import
meeroslav Aug 7, 2023
284562a
fix(linter): fix import of eslint/js
meeroslav Aug 7, 2023
0bbd43b
fix(linter): fix override extensions
meeroslav Aug 7, 2023
0b21c8b
fix(linter): revert project.json changes
meeroslav Aug 8, 2023
03454cc
feat(linter): include pr review notes
meeroslav Aug 8, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/generated/manifests/menus.json
Original file line number Diff line number Diff line change
Expand Up @@ -6344,6 +6344,14 @@
"children": [],
"isExternal": false,
"disableCollapsible": false
},
{
"id": "convert-to-flat-config",
"path": "/packages/linter/generators/convert-to-flat-config",
"name": "convert-to-flat-config",
"children": [],
"isExternal": false,
"disableCollapsible": false
}
],
"isExternal": false,
Expand Down
9 changes: 9 additions & 0 deletions docs/generated/manifests/packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,15 @@
"originalFilePath": "/packages/linter/src/generators/workspace-rule/schema.json",
"path": "/packages/linter/generators/workspace-rule",
"type": "generator"
},
"/packages/linter/generators/convert-to-flat-config": {
"description": "Convert an Nx workspace to a Flat ESLint config.",
"file": "generated/packages/linter/generators/convert-to-flat-config.json",
"hidden": false,
"name": "convert-to-flat-config",
"originalFilePath": "/packages/linter/src/generators/convert-to-flat-config/schema.json",
"path": "/packages/linter/generators/convert-to-flat-config",
"type": "generator"
}
},
"path": "/packages/linter"
Expand Down
9 changes: 9 additions & 0 deletions docs/generated/packages-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,15 @@
"originalFilePath": "/packages/linter/src/generators/workspace-rule/schema.json",
"path": "linter/generators/workspace-rule",
"type": "generator"
},
{
"description": "Convert an Nx workspace to a Flat ESLint config.",
"file": "generated/packages/linter/generators/convert-to-flat-config.json",
"hidden": false,
"name": "convert-to-flat-config",
"originalFilePath": "/packages/linter/src/generators/convert-to-flat-config/schema.json",
"path": "linter/generators/convert-to-flat-config",
"type": "generator"
}
],
"githubRoot": "https://github.com/nrwl/nx/blob/master",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "convert-to-flat-config",
"factory": "./src/generators/convert-to-flat-config/generator",
"schema": {
"$schema": "http://json-schema.org/schema",
"$id": "ConvertToFlatConfig",
"cli": "nx",
"description": "Convert an Nx workspace to a Flat ESLint config.",
"type": "object",
"properties": {
"skipFormat": {
"type": "boolean",
"description": "Skip formatting files.",
"default": false,
"x-priority": "internal"
}
},
"additionalProperties": false,
"required": [],
"presets": []
},
"description": "Convert an Nx workspace to a Flat ESLint config.",
"implementation": "/packages/linter/src/generators/convert-to-flat-config/generator.ts",
"aliases": [],
"hidden": false,
"path": "/packages/linter/src/generators/convert-to-flat-config/schema.json",
"type": "generator"
}
1 change: 1 addition & 0 deletions docs/shared/reference/sitemap.md
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@
- [generators](/packages/linter/generators)
- [workspace-rules-project](/packages/linter/generators/workspace-rules-project)
- [workspace-rule](/packages/linter/generators/workspace-rule)
- [convert-to-flat-config](/packages/linter/generators/convert-to-flat-config)
- [nest](/packages/nest)
- [documents](/packages/nest/documents)
- [Overview](/packages/nest/documents/overview)
Expand Down
75 changes: 75 additions & 0 deletions e2e/linter/src/linter.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import * as path from 'path';
import {
checkFilesDoNotExist,
checkFilesExist,
cleanupProject,
createFile,
getSelectedPackageManager,
newProject,
readFile,
readJson,
runCLI,
runCreateWorkspace,
uniq,
updateFile,
updateJson,
Expand Down Expand Up @@ -537,6 +540,78 @@ describe('Linter', () => {
});
});

describe('Flat config', () => {
const packageManager = getSelectedPackageManager() || 'pnpm';

afterEach(() => cleanupProject());

it('should convert integrated to flat config', () => {
const myapp = uniq('myapp');
const mylib = uniq('mylib');

runCreateWorkspace(myapp, {
preset: 'react-monorepo',
appName: myapp,
style: 'css',
packageManager,
bundler: 'vite',
e2eTestRunner: 'none',
});
runCLI(`generate @nx/js:lib ${mylib}`);

// migrate to flat structure
runCLI(`generate @nx/linter:convert-to-flat-config`);
checkFilesExist(
'eslint.config.js',
`apps/${myapp}/eslint.config.js`,
`libs/${mylib}/eslint.config.js`
);
checkFilesDoNotExist(
'.eslintrc.json',
`apps/${myapp}/.eslintrc.json`,
`libs/${mylib}/.eslintrc.json`
);

const outFlat = runCLI(`affected -t lint`, {
silenceError: true,
});
expect(outFlat).toContain('All files pass linting');
}, 1000000);

it('should convert standalone to flat config', () => {
const myapp = uniq('myapp');
const mylib = uniq('mylib');

runCreateWorkspace(myapp, {
preset: 'react-standalone',
appName: myapp,
style: 'css',
packageManager,
bundler: 'vite',
e2eTestRunner: 'none',
});
runCLI(`generate @nx/js:lib ${mylib}`);

// migrate to flat structure
runCLI(`generate @nx/linter:convert-to-flat-config`);
checkFilesExist(
'eslint.config.js',
`${mylib}/eslint.config.js`,
'eslint.base.config.js'
);
checkFilesDoNotExist(
'.eslintrc.json',
`${mylib}/.eslintrc.json`,
'.eslintrc.base.json'
);

const outFlat = runCLI(`affected -t lint`, {
silenceError: true,
});
expect(outFlat).toContain('All files pass linting');
}, 1000000);
});

describe('Root projects migration', () => {
beforeEach(() => newProject());
afterEach(() => cleanupProject());
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
"@babel/preset-react": "^7.22.5",
"@babel/preset-typescript": "^7.22.5",
"@babel/runtime": "^7.22.6",
"@eslint/eslintrc": "^2.1.1",
"@eslint/js": "^8.46.0",
"@floating-ui/react": "0.19.2",
"@jest/reporters": "^29.4.1",
"@jest/test-result": "^29.4.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ exports[`workspace move to nx layout should create nx.json 1`] = `
"!{projectRoot}/**/*.spec.[jt]s",
"!{projectRoot}/karma.conf.js",
"!{projectRoot}/.eslintrc.json",
"!{projectRoot}/eslint.config.js",
],
"sharedGlobals": [],
},
Expand All @@ -118,6 +119,7 @@ exports[`workspace move to nx layout should create nx.json 1`] = `
"inputs": [
"default",
"{workspaceRoot}/.eslintrc.json",
"{workspaceRoot}/eslint.config.js",
],
},
"test": {
Expand Down
10 changes: 8 additions & 2 deletions packages/angular/src/generators/ng-add/utilities/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ export function createNxJson(
'!{projectRoot}/karma.conf.js',
]
: []),
targets.lint ? '!{projectRoot}/.eslintrc.json' : undefined,
...(targets.lint
? ['!{projectRoot}/.eslintrc.json', '!{projectRoot}/eslint.config.js']
: []),
].filter(Boolean),
},
targetDefaults: {
Expand All @@ -91,7 +93,11 @@ export function createNxJson(
: undefined,
lint: targets.lint
? {
inputs: ['default', '{workspaceRoot}/.eslintrc.json'],
inputs: [
'default',
'{workspaceRoot}/.eslintrc.json',
'{workspaceRoot}/eslint.config.js',
],
}
: undefined,
e2e: targets.e2e
Expand Down
5 changes: 5 additions & 0 deletions packages/linter/generators.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
"factory": "./src/generators/workspace-rule/workspace-rule#lintWorkspaceRuleGenerator",
"schema": "./src/generators/workspace-rule/schema.json",
"description": "Create a new Workspace ESLint rule."
},
"convert-to-flat-config": {
"factory": "./src/generators/convert-to-flat-config/generator",
"schema": "./src/generators/convert-to-flat-config/schema.json",
"description": "Convert an Nx workspace to a Flat ESLint config."
}
}
}
3 changes: 2 additions & 1 deletion packages/linter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"tmp": "~0.2.1",
"tslib": "^2.3.0",
"@nx/devkit": "file:../devkit",
"@nx/js": "file:../js"
"@nx/js": "file:../js",
"typescript": "~5.1.3"
},
"peerDependenciesMeta": {
"eslint": {
Expand Down
5 changes: 4 additions & 1 deletion packages/linter/src/executors/eslint/lint.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,13 @@ Please see https://nx.dev/guides/eslint for full guidance on how to resolve this
.filter((pattern) => !!pattern)
.map((pattern) => `- '${pattern}'`);
if (ignoredPatterns.length) {
const ignoreSection = useFlatConfig
? `'ignores' configuration`
: `'.eslintignore' file`;
throw new Error(
`All files matching the following patterns are ignored:\n${ignoredPatterns.join(
'\n'
)}\n\nPlease check your '.eslintignore' file.`
)}\n\nPlease check your ${ignoreSection}.`
);
}
throw new Error(
Expand Down
Loading