-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): enable linter on root projects (#13347)
- Loading branch information
Showing
13 changed files
with
398 additions
and
95 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
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
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
80 changes: 80 additions & 0 deletions
80
packages/linter/src/generators/init/global-eslint-config.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,80 @@ | ||
import { ESLint, Linter as LinterType } from 'eslint'; | ||
|
||
/** | ||
* This configuration is intended to apply to all TypeScript source files. | ||
* See the eslint-plugin-nx package for what is in the referenced shareable config. | ||
*/ | ||
export const globalTypeScriptOverrides = { | ||
files: ['*.ts', '*.tsx'], | ||
extends: ['plugin:@nrwl/nx/typescript'], | ||
/** | ||
* Having an empty rules object present makes it more obvious to the user where they would | ||
* extend things from if they needed to | ||
*/ | ||
rules: {}, | ||
}; | ||
|
||
/** | ||
* This configuration is intended to apply to all JavaScript source files. | ||
* See the eslint-plugin-nx package for what is in the referenced shareable config. | ||
*/ | ||
export const globalJavaScriptOverrides = { | ||
files: ['*.js', '*.jsx'], | ||
extends: ['plugin:@nrwl/nx/javascript'], | ||
/** | ||
* Having an empty rules object present makes it more obvious to the user where they would | ||
* extend things from if they needed to | ||
*/ | ||
rules: {}, | ||
}; | ||
|
||
/** | ||
* This configuration is intended to apply to all "source code" (but not | ||
* markup like HTML, or other custom file types like GraphQL) | ||
*/ | ||
export const moduleBoundariesOverride = { | ||
files: ['*.ts', '*.tsx', '*.js', '*.jsx'], | ||
rules: { | ||
'@nrwl/nx/enforce-module-boundaries': [ | ||
'error', | ||
{ | ||
enforceBuildableLibDependency: true, | ||
allow: [], | ||
depConstraints: [{ sourceTag: '*', onlyDependOnLibsWithTags: ['*'] }], | ||
}, | ||
], | ||
} as LinterType.RulesRecord, | ||
}; | ||
|
||
export const getGlobalEsLintConfiguration = ( | ||
unitTestRunner?: string, | ||
rootProject?: boolean | ||
) => { | ||
const config: ESLint.ConfigData = { | ||
root: true, | ||
ignorePatterns: rootProject ? ['!**/*'] : ['**/*'], | ||
plugins: ['@nrwl/nx'], | ||
/** | ||
* We leverage ESLint's "overrides" capability so that we can set up a root config which will support | ||
* all permutations of Nx workspaces across all frameworks, libraries and tools. | ||
* | ||
* The key point is that we need entirely different ESLint config to apply to different types of files, | ||
* but we still want to share common config where possible. | ||
*/ | ||
overrides: [ | ||
...(rootProject ? [] : [moduleBoundariesOverride]), | ||
globalTypeScriptOverrides, | ||
globalJavaScriptOverrides, | ||
], | ||
}; | ||
if (unitTestRunner === 'jest') { | ||
config.overrides.push({ | ||
files: ['*.spec.ts', '*.spec.tsx', '*.spec.js', '*.spec.jsx'], | ||
env: { | ||
jest: true, | ||
}, | ||
rules: {}, | ||
}); | ||
} | ||
return config; | ||
}; |
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,116 @@ | ||
import { | ||
joinPathFragments, | ||
offsetFromRoot, | ||
ProjectConfiguration, | ||
TargetConfiguration, | ||
Tree, | ||
updateJson, | ||
updateProjectConfiguration, | ||
writeJson, | ||
} from '@nrwl/devkit'; | ||
import { basename, dirname } from 'path'; | ||
import { findEslintFile } from '../utils/eslint-file'; | ||
import { getGlobalEsLintConfiguration } from './global-eslint-config'; | ||
|
||
const FILE_EXTENSION_REGEX = /(?<!(^|\/))(\.[^/.]+)$/; | ||
|
||
export function migrateConfigToMonorepoStyle( | ||
projects: ProjectConfiguration[], | ||
tree: Tree, | ||
unitTestRunner: string | ||
): void { | ||
// copy the root's .eslintrc.json to new name | ||
const rootProject = projects.find((p) => p.root === '.'); | ||
const eslintPath = | ||
rootProject.targets?.lint?.options?.eslintConfig || findEslintFile(tree); | ||
const pathSegments = eslintPath.split(FILE_EXTENSION_REGEX).filter(Boolean); | ||
const rootProjEslintPath = | ||
pathSegments.length > 1 | ||
? pathSegments.join(`.${rootProject.name}`) | ||
: `.${rootProject.name}.${rootProject.name}`; | ||
tree.write(rootProjEslintPath, tree.read(eslintPath)); | ||
|
||
// update root project's configuration | ||
const lintTarget = findLintTarget(rootProject); | ||
lintTarget.options.eslintConfig = rootProjEslintPath; | ||
updateProjectConfiguration(tree, rootProject.name, rootProject); | ||
|
||
// replace root eslint with default global | ||
tree.delete(eslintPath); | ||
writeJson( | ||
tree, | ||
'.eslintrc.json', | ||
getGlobalEsLintConfiguration(unitTestRunner) | ||
); | ||
|
||
// update extens in all projects' eslint configs | ||
projects.forEach((project) => { | ||
const lintTarget = findLintTarget(project); | ||
if (lintTarget) { | ||
const projectEslintPath = joinPathFragments( | ||
project.root, | ||
lintTarget.options.eslintConfig || findEslintFile(tree, project.root) | ||
); | ||
migrateEslintFile(projectEslintPath, tree); | ||
} | ||
}); | ||
} | ||
|
||
export function findLintTarget( | ||
project: ProjectConfiguration | ||
): TargetConfiguration { | ||
return Object.entries(project.targets).find( | ||
([name, target]) => | ||
name === 'lint' || target.executor === '@nrwl/linter:eslint' | ||
)?.[1]; | ||
} | ||
|
||
function migrateEslintFile(projectEslintPath: string, tree: Tree) { | ||
if (projectEslintPath.endsWith('.json')) { | ||
updateJson(tree, projectEslintPath, (json) => { | ||
// we have a new root now | ||
delete json.root; | ||
// remove nrwl/nx plugins | ||
if (json.plugins) { | ||
json.plugins = json.plugins.filter((p) => p !== '@nrwl/nx'); | ||
if (json.plugins.length === 0) { | ||
delete json.plugins; | ||
} | ||
} | ||
// add extends | ||
json.extends = json.extends || []; | ||
const pathToRootConfig = `${offsetFromRoot( | ||
dirname(projectEslintPath) | ||
)}.eslintrc.json`; | ||
if (json.extends.indexOf(pathToRootConfig) === -1) { | ||
json.extends.push(pathToRootConfig); | ||
} | ||
// cleanup overrides | ||
if (json.overrides) { | ||
json.overrides.forEach((override) => { | ||
if (override.extends) { | ||
override.extends = override.extends.filter( | ||
(ext) => | ||
ext !== 'plugin:@nrwl/nx/typescript' && | ||
ext !== 'plugin:@nrwl/nx/javascript' | ||
); | ||
if (override.extends.length === 0) { | ||
delete override.extends; | ||
} | ||
} | ||
}); | ||
} | ||
return json; | ||
}); | ||
return; | ||
} | ||
if ( | ||
projectEslintPath.endsWith('.yml') || | ||
projectEslintPath.endsWith('.yaml') | ||
) { | ||
console.warn('YAML eslint config is not supported yet for migration'); | ||
} | ||
if (projectEslintPath.endsWith('.js') || projectEslintPath.endsWith('.cjs')) { | ||
console.warn('YAML eslint config is not supported yet for migration'); | ||
} | ||
} |
Oops, something went wrong.
1 comment
on commit 110b5f2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
nx-dev – ./
nx-dev-nrwl.vercel.app
nx.dev
nx-five.vercel.app
nx-dev-git-master-nrwl.vercel.app
Why is this necessary?