Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(linter): do not break migration if eslint file is missing #18762

Merged
merged 1 commit into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions packages/linter/src/generators/init/init-migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,24 @@ export function migrateConfigToMonorepoStyle(
projects.forEach((project) => {
const lintTarget = findLintTarget(project);
if (lintTarget) {
const projectEslintPath = joinPathFragments(
project.root,
lintTarget.options.eslintConfig || findEslintFile(tree, project.root)
);
migrateEslintFile(projectEslintPath, tree);
const eslintFile =
lintTarget.options.eslintConfig || findEslintFile(tree, project.root);
if (eslintFile) {
const projectEslintPath = joinPathFragments(project.root, eslintFile);
migrateEslintFile(projectEslintPath, tree);
}
}
});
}

export function findLintTarget(
project: ProjectConfiguration
): TargetConfiguration {
return Object.entries(project.targets ?? {}).find(
([name, target]) =>
name === 'lint' ||
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very prone to misuse and does not guarantee that the lint uses the ESLint config.

return Object.values(project.targets ?? {}).find(
(target) =>
target.executor === '@nx/linter:eslint' ||
target.executor === '@nrwl/linter:eslint'
)?.[1];
);
}

function migrateEslintFile(projectEslintPath: string, tree: Tree) {
Expand Down Expand Up @@ -99,6 +99,6 @@ function migrateEslintFile(projectEslintPath: string, tree: Tree) {
console.warn('YAML eslint config is not supported yet for migration');
}
if (projectEslintPath.endsWith('.js') || projectEslintPath.endsWith('.cjs')) {
console.warn('YAML eslint config is not supported yet for migration');
console.warn('JS eslint config is not supported yet for migration');
}
}
4 changes: 4 additions & 0 deletions packages/linter/src/generators/utils/eslint-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ export const eslintConfigFileWhitelist = [
];

export const baseEsLintConfigFile = '.eslintrc.base.json';
export const baseEsLintFlatConfigFile = 'eslint.base.config.js';

export function findEslintFile(tree: Tree, projectRoot = ''): string | null {
if (projectRoot === '' && tree.exists(baseEsLintConfigFile)) {
return baseEsLintConfigFile;
}
if (projectRoot === '' && tree.exists(baseEsLintFlatConfigFile)) {
return baseEsLintFlatConfigFile;
}
for (const file of eslintConfigFileWhitelist) {
if (tree.exists(joinPathFragments(projectRoot, file))) {
return file;
Expand Down