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 {
|
import {
|
||||||
addProjectConfiguration,
|
addProjectConfiguration,
|
||||||
readProjectConfiguration,
|
readProjectConfiguration,
|
||||||
|
updateJson,
|
||||||
Tree,
|
Tree,
|
||||||
|
readJson,
|
||||||
} from '@nx/devkit';
|
} from '@nx/devkit';
|
||||||
|
|
||||||
import { Linter } from '../utils/linter';
|
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 {
|
import {
|
||||||
formatFiles,
|
formatFiles,
|
||||||
offsetFromRoot,
|
offsetFromRoot,
|
||||||
|
readJson,
|
||||||
readProjectConfiguration,
|
readProjectConfiguration,
|
||||||
|
updateJson,
|
||||||
updateProjectConfiguration,
|
updateProjectConfiguration,
|
||||||
writeJson,
|
writeJson,
|
||||||
} from '@nx/devkit';
|
} 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);
|
updateProjectConfiguration(tree, options.project, projectConfig);
|
||||||
|
|
||||||
if (!options.skipFormat) {
|
if (!options.skipFormat) {
|
||||||
@ -163,19 +182,18 @@ function createEsLintConfiguration(
|
|||||||
files: ['*.js', '*.jsx'],
|
files: ['*.js', '*.jsx'],
|
||||||
rules: {},
|
rules: {},
|
||||||
},
|
},
|
||||||
...(isBuildableLibraryProject(projectConfig)
|
|
||||||
? [
|
|
||||||
{
|
|
||||||
files: ['*.json'],
|
|
||||||
parser: 'jsonc-eslint-parser',
|
|
||||||
rules: {
|
|
||||||
'@nx/dependency-checks': 'error',
|
|
||||||
} as Linter.RulesRecord,
|
|
||||||
},
|
|
||||||
]
|
|
||||||
: []),
|
|
||||||
];
|
];
|
||||||
|
|
||||||
|
if (isBuildableLibraryProject(projectConfig)) {
|
||||||
|
overrides.push({
|
||||||
|
files: ['*.json'],
|
||||||
|
parser: 'jsonc-eslint-parser',
|
||||||
|
rules: {
|
||||||
|
'@nx/dependency-checks': 'error',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (useFlatConfig(tree)) {
|
if (useFlatConfig(tree)) {
|
||||||
const isCompatNeeded = addDependencyChecks;
|
const isCompatNeeded = addDependencyChecks;
|
||||||
const nodes = [];
|
const nodes = [];
|
||||||
@ -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(
|
function isBuildableLibraryProject(
|
||||||
projectConfig: ProjectConfiguration
|
projectConfig: ProjectConfiguration
|
||||||
): boolean {
|
): boolean {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user