Skip to content

Commit

Permalink
fix(linter): enable analyzeSourceFile as needed when generating new l…
Browse files Browse the repository at this point in the history
…int project

Fixes #18704
  • Loading branch information
jaysoo committed Aug 22, 2023
1 parent a25cc29 commit c5753b4
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 14 deletions.
46 changes: 46 additions & 0 deletions packages/linter/src/generators/lint-project/lint-project.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {
addProjectConfiguration,
readProjectConfiguration,
updateJson,
Tree,
readJson,
} from '@nx/devkit';

import { Linter } from '../utils/linter';
Expand Down Expand Up @@ -175,4 +177,48 @@ describe('@nx/linter:lint-project', () => {
"
`);
});

it('should update nx.json to enable source analysis when using npm.json preset', async () => {
updateJson(tree, 'nx.json', (json) => {
// npm preset disables source analysis
json.extends = 'nx/presets/npm.json';
return json;
});

await lintProjectGenerator(tree, {
...defaultOptions,
linter: Linter.EsLint,
eslintFilePatterns: ['libs/buildable-lib/**/*.ts'],
project: 'buildable-lib',
setParserOptionsProject: false,
});

expect(readJson(tree, 'nx.json').pluginsConfig['@nx/js']).toEqual({
analyzeSourceFiles: true,
});
});

it('should update nx.json to enable source analysis when it is disabled', async () => {
updateJson(tree, 'nx.json', (json) => {
// npm preset disables source analysis
json.pluginsConfig = {
'@nx/js': {
analyzeSourceFiles: false,
},
};
return json;
});

await lintProjectGenerator(tree, {
...defaultOptions,
linter: Linter.EsLint,
eslintFilePatterns: ['libs/buildable-lib/**/*.ts'],
project: 'buildable-lib',
setParserOptionsProject: false,
});

expect(readJson(tree, 'nx.json').pluginsConfig['@nx/js']).toEqual({
analyzeSourceFiles: true,
});
});
});
61 changes: 47 additions & 14 deletions packages/linter/src/generators/lint-project/lint-project.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import type { ProjectConfiguration, Tree } from '@nx/devkit';
import type {
NxJsonConfiguration,
ProjectConfiguration,
Tree,
} from '@nx/devkit';
import {
formatFiles,
offsetFromRoot,
readJson,
readProjectConfiguration,
updateJson,
updateProjectConfiguration,
writeJson,
} from '@nx/devkit';
Expand Down Expand Up @@ -96,6 +102,19 @@ export async function lintProjectGenerator(
);
}

// Buildable libs need source analysis enabled for linting `package.json`.
if (
isBuildableLibraryProject(projectConfig) &&
!isJsAnalyzeSourceFilesEnabled(tree)
) {
updateJson(tree, 'nx.json', (json) => {
json.pluginsConfig ??= {};
json.pluginsConfig['@nx/js'] ??= {};
json.pluginsConfig['@nx/js'].analyzeSourceFiles = true;
return json;
});
}

updateProjectConfiguration(tree, options.project, projectConfig);

if (!options.skipFormat) {
Expand All @@ -111,7 +130,7 @@ function createEsLintConfiguration(
setParserOptionsProject: boolean
) {
const eslintConfig = findEslintFile(tree);
writeJson(tree, join(projectConfig.root, `.eslintrc.json`), {
const eslintRc: any = {
extends: eslintConfig
? [`${offsetFromRoot(projectConfig.root)}${eslintConfig}`]
: undefined,
Expand Down Expand Up @@ -152,19 +171,18 @@ function createEsLintConfiguration(
files: ['*.js', '*.jsx'],
rules: {},
},
...(isBuildableLibraryProject(projectConfig)
? [
{
files: ['*.json'],
parser: 'jsonc-eslint-parser',
rules: {
'@nx/dependency-checks': 'error',
},
},
]
: []),
],
});
};
if (isBuildableLibraryProject(projectConfig)) {
eslintRc.overrides.push({
files: ['*.json'],
parser: 'jsonc-eslint-parser',
rules: {
'@nx/dependency-checks': 'error',
},
});
}
writeJson(tree, join(projectConfig.root, `.eslintrc.json`), eslintRc);
}

function isBuildableLibraryProject(
Expand All @@ -177,6 +195,21 @@ function isBuildableLibraryProject(
);
}

function isJsAnalyzeSourceFilesEnabled(tree: Tree): boolean {
const nxJson = readJson<NxJsonConfiguration>(tree, 'nx.json');
const jsPluginConfig = nxJson.pluginsConfig?.['@nx/js'] as {
analyzeSourceFiles?: boolean;
};

if (nxJson.extends === 'nx/presets/npm.json') {
// npm preset turns off analyzeSourceFiles, but check if user has overridden it.
return jsPluginConfig?.analyzeSourceFiles ?? false;
}

// By default the JS plugin enables analyzeSourceFiles as long as user did not override it.
return jsPluginConfig?.analyzeSourceFiles ?? true;
}

/**
* Detect based on the state of lint target configuration of the root project
* if we should migrate eslint configs to monorepo style
Expand Down

0 comments on commit c5753b4

Please sign in to comment.