Skip to content

Commit

Permalink
fix(linter): eslint flat config not working correctly (#18379)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertIsaac authored Aug 7, 2023
1 parent 85f4737 commit 006180b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
32 changes: 31 additions & 1 deletion packages/linter/src/executors/eslint/lint.impl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ jest.mock('./utility/eslint-utils', () => {
};
});
import lintExecutor from './lint.impl';
import { resolve } from 'path';

let mockChdir = jest.fn().mockImplementation(() => {});

Expand Down Expand Up @@ -150,7 +151,7 @@ describe('Linter Builder', () => {
mockContext
);
expect(mockResolveAndInstantiateESLint).toHaveBeenCalledWith(
'/root/.eslintrc.json',
resolve('/root', '.eslintrc.json'),
{
lintFilePatterns: [],
eslintConfig: './.eslintrc.json',
Expand Down Expand Up @@ -649,4 +650,33 @@ Please see https://nx.dev/guides/eslint for full guidance on how to resolve this
expect(console.log).toHaveBeenCalledWith('{\n "file": "test-source.ts"\n}');
expect(result).toEqual({ success: true });
});

it('should pass path to eslint.config.js to resolveAndInstantiateESLint if it is unspecified and we are using flag configuration', async () => {
setupMocks();
jest.spyOn(fs, 'existsSync').mockReturnValue(true);
await lintExecutor(createValidRunBuilderOptions(), mockContext);
expect(mockResolveAndInstantiateESLint).toHaveBeenCalledWith(
'apps/proj/eslint.config.js',
{
lintFilePatterns: [],
eslintConfig: null,
fix: true,
cache: true,
cacheLocation: 'cacheLocation1/proj',
cacheStrategy: 'content',
format: 'stylish',
force: false,
silent: false,
ignorePath: null,
maxWarnings: -1,
outputFile: null,
quiet: false,
noEslintrc: false,
rulesdir: [],
resolvePluginsRelativeTo: null,
reportUnusedDisableDirectives: null,
},
true
);
});
});
18 changes: 14 additions & 4 deletions packages/linter/src/executors/eslint/lint.impl.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ExecutorContext, joinPathFragments } from '@nx/devkit';
import { ESLint } from 'eslint';
import { existsSync, mkdirSync, writeFileSync } from 'fs';
import { dirname, join, resolve } from 'path';
import { dirname, resolve } from 'path';

import type { Schema } from './schema';
import { resolveAndInstantiateESLint } from './utility/eslint-utils';
Expand Down Expand Up @@ -31,12 +31,12 @@ export default async function run(
* We want users to have the option of not specifying the config path, and let
* eslint automatically resolve the `.eslintrc.json` files in each folder.
*/
const eslintConfigPath = options.eslintConfig
let eslintConfigPath = options.eslintConfig
? resolve(systemRoot, options.eslintConfig)
: undefined;

options.cacheLocation = options.cacheLocation
? join(options.cacheLocation, projectName)
? joinPathFragments(options.cacheLocation, projectName)
: undefined;

const { printConfig, ...normalizedOptions } = options;
Expand All @@ -49,6 +49,13 @@ export default async function run(
const useFlatConfig = existsSync(
joinPathFragments(systemRoot, 'eslint.config.js')
);

if (!eslintConfigPath && useFlatConfig) {
const projectRoot =
context.projectsConfigurations.projects[context.projectName].root;
eslintConfigPath = joinPathFragments(projectRoot, 'eslint.config.js');
}

const { eslint, ESLint } = await resolveAndInstantiateESLint(
eslintConfigPath,
normalizedOptions,
Expand Down Expand Up @@ -158,7 +165,10 @@ Please see https://nx.dev/guides/eslint for full guidance on how to resolve this
const formattedResults = await formatter.format(lintResults);

if (normalizedOptions.outputFile) {
const pathToOutputFile = join(context.root, normalizedOptions.outputFile);
const pathToOutputFile = joinPathFragments(
context.root,
normalizedOptions.outputFile
);
mkdirSync(dirname(pathToOutputFile), { recursive: true });
writeFileSync(pathToOutputFile, formattedResults);
} else {
Expand Down

1 comment on commit 006180b

@vercel
Copy link

@vercel vercel bot commented on 006180b Aug 7, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-git-master-nrwl.vercel.app
nx-dev-nrwl.vercel.app
nx.dev
nx-five.vercel.app

Please sign in to comment.