fix(linter): enable analyzeSourceFile as needed when generating new lint project (#18769)
This commit is contained in:
parent
a146fcc690
commit
4846ae53ad
@ -1,7 +1,9 @@
|
||||
import {
|
||||
addProjectConfiguration,
|
||||
readProjectConfiguration,
|
||||
updateJson,
|
||||
Tree,
|
||||
readJson,
|
||||
} from '@nx/devkit';
|
||||
|
||||
import { Linter } from '../utils/linter';
|
||||
@ -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,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -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';
|
||||
@ -108,6 +114,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) {
|
||||
@ -163,18 +182,17 @@ function createEsLintConfiguration(
|
||||
files: ['*.js', '*.jsx'],
|
||||
rules: {},
|
||||
},
|
||||
...(isBuildableLibraryProject(projectConfig)
|
||||
? [
|
||||
{
|
||||
];
|
||||
|
||||
if (isBuildableLibraryProject(projectConfig)) {
|
||||
overrides.push({
|
||||
files: ['*.json'],
|
||||
parser: 'jsonc-eslint-parser',
|
||||
rules: {
|
||||
'@nx/dependency-checks': 'error',
|
||||
} as Linter.RulesRecord,
|
||||
},
|
||||
]
|
||||
: []),
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
if (useFlatConfig(tree)) {
|
||||
const isCompatNeeded = addDependencyChecks;
|
||||
@ -204,6 +222,18 @@ function createEsLintConfiguration(
|
||||
}
|
||||
}
|
||||
|
||||
function isJsAnalyzeSourceFilesEnabled(tree: Tree): boolean {
|
||||
const nxJson = readJson<NxJsonConfiguration>(tree, 'nx.json');
|
||||
const jsPluginConfig = nxJson.pluginsConfig?.['@nx/js'] as {
|
||||
analyzeSourceFiles?: boolean;
|
||||
};
|
||||
|
||||
return (
|
||||
jsPluginConfig?.analyzeSourceFiles ??
|
||||
nxJson.extends !== 'nx/presets/npm.json'
|
||||
);
|
||||
}
|
||||
|
||||
function isBuildableLibraryProject(
|
||||
projectConfig: ProjectConfiguration
|
||||
): boolean {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user