nx/packages/angular/src/migrations/update-20-2-0/remove-angular-eslint-rules.ts
Leosvel Pérez Espinosa df77fde81f
fix(angular): handle removed angular-eslint rules in root eslint config files and update package (#29262)
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

The migration to remove Angular ESLint rules that were removed in v19
does not handle these rules in the root eslint config files.

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

The migration to remove Angular ESLint rules that were removed in v19
should handle these rules in the root eslint config files.

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
2024-12-10 14:10:53 +00:00

78 lines
2.2 KiB
TypeScript

import { formatFiles, type Tree } from '@nx/devkit';
import {
findEslintFile,
isEslintConfigSupported,
lintConfigHasOverride,
updateOverrideInLintConfig,
} from '@nx/eslint/src/generators/utils/eslint-file';
import { getProjectsFilteredByDependencies } from '../utils/projects';
export const rulesToRemove = [
'@angular-eslint/no-host-metadata-property',
'@angular-eslint/sort-ngmodule-metadata-arrays',
'@angular-eslint/prefer-standalone-component',
];
export default async function (tree: Tree) {
const projects = await getProjectsFilteredByDependencies(tree, [
'npm:@angular/core',
]);
let hasRootProject = false;
for (const {
project: { root },
} of projects) {
if (!isEslintConfigSupported(tree, root)) {
// ESLint config is not supported, skip
continue;
}
if (root === '.') {
hasRootProject = true;
}
removeRules(tree, root);
}
/**
* We need to handle both a root config file (e.g. eslint.config.js) and a
* potential base config file (e.g. eslint.base.config.js). We can't use
* `findEslintFile` because it would return only one or the other depending
* on whether a root is provided and the existence of the files. So, we
* handle each of them separately.
*/
// check root config, provide a root so it doesn't try to lookup a base config
if (!hasRootProject) {
// if there is no root project the root eslint config has not been processed
if (isEslintConfigSupported(tree, '')) {
removeRules(tree, '');
}
}
// handle root base config, not providing a root will prioritize a base config
const baseEslintConfig = findEslintFile(tree);
if (baseEslintConfig && baseEslintConfig.includes('.base.')) {
removeRules(tree, baseEslintConfig);
}
await formatFiles(tree);
}
function removeRules(tree: Tree, root: string): void {
for (const rule of rulesToRemove) {
const lookup: Parameters<typeof lintConfigHasOverride>[2] = (o) =>
!!o.rules?.[rule];
if (!lintConfigHasOverride(tree, root, lookup)) {
// it's not using the rule, skip
continue;
}
// there is an override containing the rule, remove the rule
updateOverrideInLintConfig(tree, root, lookup, (o) => {
delete o.rules[rule];
return o;
});
}
}