Skip to content

Commit

Permalink
feat(linter): include pr review notes
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Aug 8, 2023
1 parent ec85a5d commit aee4e4d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
48 changes: 31 additions & 17 deletions packages/linter/src/generators/convert-to-flat-config/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
getProjects,
NxJsonConfiguration,
ProjectConfiguration,
readNxJson,
Tree,
updateJson,
updateProjectConfiguration,
Expand All @@ -26,11 +27,11 @@ export async function convertToFlatConfigGenerator(
}

// rename root eslint config to eslint.config.js
convertRootToFlatConfig(tree);
convertRootToFlatConfig(tree, eslintFile);
// rename and map files
const projects = getProjects(tree);
for (const [project, projectConfig] of projects) {
convertProjectToFlatConfig(tree, project, projectConfig);
convertProjectToFlatConfig(tree, project, projectConfig, readNxJson(tree));
}
// replace references in nx.json
updateNxJsonConfig(tree);
Expand All @@ -43,45 +44,51 @@ export async function convertToFlatConfigGenerator(

export default convertToFlatConfigGenerator;

function convertRootToFlatConfig(tree: Tree) {
if (tree.exists('.eslintrc.base.json')) {
function convertRootToFlatConfig(tree: Tree, eslintFile: string) {
if (eslintFile.endsWith('.base.json')) {
convertConfigToFlatConfig(
tree,
'',
'.eslintrc.base.json',
'eslint.base.config.js'
);
}
if (tree.exists('.eslintrc.json')) {
convertConfigToFlatConfig(tree, '', '.eslintrc.json', 'eslint.config.js');
}
convertConfigToFlatConfig(tree, '', '.eslintrc.json', 'eslint.config.js');
}

function convertProjectToFlatConfig(
tree: Tree,
project: string,
projectConfig: ProjectConfiguration
projectConfig: ProjectConfiguration,
nxJson: NxJsonConfiguration
) {
if (tree.exists(`${projectConfig.root}/.eslintrc.json`)) {
if (projectConfig.targets) {
const eslintTargets = Object.keys(projectConfig.targets).filter(
const eslintTargets = Object.keys(projectConfig.targets || {}).filter(
(t) => projectConfig.targets[t].executor === '@nx/linter:eslint'
);
for (const target of eslintTargets) {
// remove any obsolete `eslintConfig` options pointing to the old config file
if (projectConfig.targets[target].options.eslintConfig) {
if (projectConfig.targets[target].options?.eslintConfig) {
delete projectConfig.targets[target].options.eslintConfig;
}
updateProjectConfiguration(tree, project, projectConfig);
}
const nxHasLintTargets = Object.keys(nxJson.targetDefaults || {}).some(
(t) =>
(t === '@nx/linter:eslint' ||
nxJson.targetDefaults[t].executor === '@nx/linter:eslint') &&
projectConfig.targets?.[t]
);
if (nxHasLintTargets || eslintTargets.length > 0) {
convertConfigToFlatConfig(
tree,
projectConfig.root,
'.eslintrc.json',
'eslint.config.js'
);
}
}

convertConfigToFlatConfig(
tree,
projectConfig.root,
'.eslintrc.json',
'eslint.config.js'
);
}
}

Expand All @@ -95,6 +102,13 @@ function updateNxJsonConfig(tree: Tree) {
inputSet.add('{workspaceRoot}/eslint.config.js');
json.targetDefaults.lint.inputs = Array.from(inputSet);
}
if (json.targetDefaults?.['@nx/linter:eslint']?.inputs) {
const inputSet = new Set(
json.targetDefaults['@nx/linter:eslint'].inputs
);
inputSet.add('{workspaceRoot}/eslint.config.js');
json.targetDefaults['@nx/linter:eslint'].inputs = Array.from(inputSet);
}
if (json.namedInputs?.production) {
const inputSet = new Set(json.namedInputs.production);
inputSet.add('!{projectRoot}/eslint.config.js');
Expand Down
4 changes: 2 additions & 2 deletions packages/linter/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface LinterInitOptions {
rootProject?: boolean;
}

function addTargetDefaults(tree: Tree, isRootProject: boolean) {
function addTargetDefaults(tree: Tree) {
const nxJson = readNxJson(tree);

const productionFileSet = nxJson.namedInputs?.production;
Expand Down Expand Up @@ -67,7 +67,7 @@ function initEsLint(tree: Tree, options: LinterInitOptions): GeneratorCallback {
getGlobalEsLintConfiguration(options.unitTestRunner, options.rootProject)
);
tree.write('.eslintignore', 'node_modules\n');
addTargetDefaults(tree, options.rootProject);
addTargetDefaults(tree);

if (tree.exists('.vscode/extensions.json')) {
updateJson(tree, '.vscode/extensions.json', (json) => {
Expand Down

0 comments on commit aee4e4d

Please sign in to comment.