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): migrate projects using dep-check lint rule to ignore build config files #18882

Merged
merged 1 commit into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions packages/linter/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@
"version": "16.0.0-beta.1",
"description": "Replace @nrwl/linter with @nx/linter",
"implementation": "./src/migrations/update-16-0-0-add-nx-packages/update-16-0-0-add-nx-packages"
},
"update-16-8-0-add-ignored-files": {
"version": "16.8.0",
"description": "update-16-8-0-add-ignored-files",
"implementation": "./src/migrations/update-16-8-0-add-ignored-files/update-16-8-0-add-ignored-files"
}
},
"packageJsonUpdates": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { addProjectConfiguration, readJson, Tree, writeJson } from '@nx/devkit';

import update from './update-16-8-0-add-ignored-files';

describe('update-16-8-0-add-ignored-files migration', () => {
let tree: Tree;

beforeEach(() => {
tree = createTreeWithEmptyWorkspace();
});

it('should run successfully when eslint config is not present', async () => {
addProjectConfiguration(tree, 'my-pkg', {
root: 'packages/my-pkg',
sourceRoot: 'packages/my-pkg/src',
projectType: 'library',
targets: {
build: {
executor: '@nx/vite:build',
options: {},
},
},
});

expect(() => update(tree)).not.toThrow();
});

it.each`
executor | expectedPattern
${'@nx/vite:build'} | ${'{projectRoot}/vite.config.{js,ts,mjs,mts}'}
${'@nx/vite:test'} | ${'{projectRoot}/vite.config.{js,ts,mjs,mts}'}
${'@nx/esbuild:esbuild'} | ${'{projectRoot}/esbuild.config.{js,ts,mjs,mts}'}
${'@nx/rollup:rollup'} | ${'{projectRoot}/rollup.config.{js,ts,mjs,mts}'}
`(
'should add ignoredFiles to projects using vite, esbuild, and rollup',
async ({ executor, expectedPattern }) => {
addProjectConfiguration(tree, 'my-pkg', {
root: 'packages/my-pkg',
sourceRoot: 'packages/my-pkg/src',
projectType: 'library',
targets: {
build: {
executor,
options: {},
},
},
});
writeJson(tree, `packages/my-pkg/.eslintrc.json`, {
root: true,
ignorePatterns: ['!**/*'],
plugins: ['@nx'],
overrides: [
{
files: ['*.json'],
parser: 'jsonc-eslint-parser',
rules: {
'@nx/dependency-checks': 'error',
},
},
],
});

update(tree);

expect(readJson(tree, 'packages/my-pkg/.eslintrc.json')).toEqual({
root: true,
ignorePatterns: ['!**/*'],
plugins: ['@nx'],
overrides: [
{
files: ['*.json'],
parser: 'jsonc-eslint-parser',
rules: {
'@nx/dependency-checks': [
'error',
{
ignoredFiles: [expectedPattern],
},
],
},
},
],
});
}
);

it('should retain existing severity', () => {
addProjectConfiguration(tree, 'my-pkg', {
root: 'packages/my-pkg',
sourceRoot: 'packages/my-pkg/src',
projectType: 'library',
targets: {
build: {
executor: '@nx/vite:build',
options: {},
},
},
});
writeJson(tree, `packages/my-pkg/.eslintrc.json`, {
root: true,
ignorePatterns: ['!**/*'],
plugins: ['@nx'],
overrides: [
{
files: ['*.json'],
parser: 'jsonc-eslint-parser',
rules: {
'@nx/dependency-checks': 'warn',
},
},
],
});

update(tree);

expect(readJson(tree, 'packages/my-pkg/.eslintrc.json')).toEqual({
root: true,
ignorePatterns: ['!**/*'],
plugins: ['@nx'],
overrides: [
{
files: ['*.json'],
parser: 'jsonc-eslint-parser',
rules: {
'@nx/dependency-checks': [
'warn',
{
ignoredFiles: ['{projectRoot}/vite.config.{js,ts,mjs,mts}'],
},
],
},
},
],
});
});

it('should retain existing options', () => {
addProjectConfiguration(tree, 'my-pkg', {
root: 'packages/my-pkg',
sourceRoot: 'packages/my-pkg/src',
projectType: 'library',
targets: {
build: {
executor: '@nx/vite:build',
options: {},
},
},
});
writeJson(tree, `packages/my-pkg/.eslintrc.json`, {
root: true,
ignorePatterns: ['!**/*'],
plugins: ['@nx'],
overrides: [
{
files: ['*.json'],
parser: 'jsonc-eslint-parser',
rules: {
'@nx/dependency-checks': [
'error',
{
checkVersionMismatches: false,
},
],
},
},
],
});

update(tree);

expect(readJson(tree, 'packages/my-pkg/.eslintrc.json')).toEqual({
root: true,
ignorePatterns: ['!**/*'],
plugins: ['@nx'],
overrides: [
{
files: ['*.json'],
parser: 'jsonc-eslint-parser',
rules: {
'@nx/dependency-checks': [
'error',
{
checkVersionMismatches: false,
ignoredFiles: ['{projectRoot}/vite.config.{js,ts,mjs,mts}'],
},
],
},
},
],
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { getProjects, Tree } from '@nx/devkit';
import { forEachExecutorOptions } from '@nx/devkit/src/generators/executor-options-utils';
import {
findEslintFile,
lintConfigHasOverride,
updateOverrideInLintConfig,
} from '../../generators/utils/eslint-file';

// Add `ignoredFiles` pattern to projects using vite, esbuild, and rollup.
// This is needed because the @nx/dependency-checks lint rule will complain
// about dependencies used in build config files, even though they are not
// production dependencies.
export default function update(tree: Tree) {
const projects = getProjects(tree);

const addIgnorePattern =
(ignorePattern: string) => (_options: unknown, projectName: string) => {
const project = projects.get(projectName);
if (!findEslintFile(tree, project.root)) return;
if (
lintConfigHasOverride(
tree,
project.root,
(o) =>
Array.isArray(o.files)
? o.files.some((f) => f.match(/\.json$/))
: !!o.files?.match(/\.json$/),
true
)
) {
updateOverrideInLintConfig(
tree,
project.root,
(o) => !!o.rules?.['@nx/dependency-checks'],
(o) => {
const value = o.rules['@nx/dependency-checks'];
let ruleSeverity: 0 | 1 | 2 | 'error' | 'warn' | 'off';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is no longer correct. The current type doesn't support numbers

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was giving me type errors if I don't include them. I can cast as any as well.

let ruleOptions: any;
if (Array.isArray(value)) {
ruleSeverity = value[0];
ruleOptions = value[1];
} else {
ruleSeverity = value;
ruleOptions = {};
}
ruleOptions.ignoredFiles = [ignorePattern];
o.rules['@nx/dependency-checks'] = [ruleSeverity, ruleOptions];
return o;
}
);
}
};

forEachExecutorOptions(
tree,
'@nx/vite:build',
addIgnorePattern('{projectRoot}/vite.config.{js,ts,mjs,mts}')
);
forEachExecutorOptions(
tree,
'@nx/vite:test',
addIgnorePattern('{projectRoot}/vite.config.{js,ts,mjs,mts}')
);
forEachExecutorOptions(
tree,
'@nx/esbuild:esbuild',
addIgnorePattern('{projectRoot}/esbuild.config.{js,ts,mjs,mts}')
);
forEachExecutorOptions(
tree,
'@nx/rollup:rollup',
addIgnorePattern('{projectRoot}/rollup.config.{js,ts,mjs,mts}')
);
}