-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(linter): migrate projects using dep-check lint rule to ignore bu…
…ild config files
- Loading branch information
Showing
3 changed files
with
272 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
193 changes: 193 additions & 0 deletions
193
...er/src/migrations/update-16-8-0-add-ignored-files/update-16-8-0-add-ignored-files.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}'], | ||
}, | ||
], | ||
}, | ||
}, | ||
], | ||
}); | ||
}); | ||
}); |
74 changes: 74 additions & 0 deletions
74
.../linter/src/migrations/update-16-8-0-add-ignored-files/update-16-8-0-add-ignored-files.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
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}') | ||
); | ||
} |