feat(linter): replace createReactEslintJson with extendReactEslintJson (#13431)

This commit is contained in:
Miroslav Jonaš 2022-11-28 11:54:30 +01:00 committed by GitHub
parent 649e954ea3
commit 9a0db4848f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 36 additions and 52 deletions

View File

@ -122,7 +122,7 @@ describe('react native', () => {
expect(() => { expect(() => {
runCLI(`build ${libName}`); runCLI(`build ${libName}`);
checkFilesExist(`dist/libs/${libName}/index.js`); checkFilesExist(`dist/libs/${libName}/index.js`);
checkFilesExist(`dist/libs/${libName}/index.d.ts`); checkFilesExist(`dist/libs/${libName}/src/index.d.ts`);
}).not.toThrow(); }).not.toThrow();
}); });

View File

@ -6,7 +6,7 @@ import {
Tree, Tree,
updateJson, updateJson,
} from '@nrwl/devkit'; } from '@nrwl/devkit';
import { createReactEslintJson, extraEslintDependencies } from '@nrwl/react'; import { extendReactEslintJson, extraEslintDependencies } from '@nrwl/react';
import { NormalizedSchema } from './normalize-options'; import { NormalizedSchema } from './normalize-options';
export async function addLinting(host: Tree, options: NormalizedSchema) { export async function addLinting(host: Tree, options: NormalizedSchema) {
@ -24,15 +24,10 @@ export async function addLinting(host: Tree, options: NormalizedSchema) {
skipFormat: true, skipFormat: true,
}); });
const reactEslintJson = createReactEslintJson(
options.e2eProjectRoot,
options.setParserOptionsProject
);
updateJson( updateJson(
host, host,
joinPathFragments(options.e2eProjectRoot, '.eslintrc.json'), joinPathFragments(options.e2eProjectRoot, '.eslintrc.json'),
() => reactEslintJson extendReactEslintJson
); );
const installTask = addDependenciesToPackageJson( const installTask = addDependenciesToPackageJson(

View File

@ -6,7 +6,7 @@ import {
Tree, Tree,
updateJson, updateJson,
} from '@nrwl/devkit'; } from '@nrwl/devkit';
import { createReactEslintJson, extraEslintDependencies } from '@nrwl/react'; import { extendReactEslintJson, extraEslintDependencies } from '@nrwl/react';
import type { Linter as ESLintLinter } from 'eslint'; import type { Linter as ESLintLinter } from 'eslint';
export async function addLinting( export async function addLinting(
@ -29,16 +29,12 @@ export async function addLinting(
skipFormat: true, skipFormat: true,
}); });
const reactEslintJson = createReactEslintJson(
appProjectRoot,
setParserOptionsProject
);
updateJson( updateJson(
host, host,
joinPathFragments(appProjectRoot, '.eslintrc.json'), joinPathFragments(appProjectRoot, '.eslintrc.json'),
(json: ESLintLinter.Config) => { (json: ESLintLinter.Config) => {
json = reactEslintJson; json = extendReactEslintJson(json);
json.ignorePatterns = ['!**/*', '.expo', 'node_modules', 'web-build']; json.ignorePatterns = ['!**/*', '.expo', 'node_modules', 'web-build'];
// Find the override that handles both TS and JS files. // Find the override that handles both TS and JS files.

View File

@ -6,7 +6,7 @@ import {
Tree, Tree,
updateJson, updateJson,
} from '@nrwl/devkit'; } from '@nrwl/devkit';
import { createReactEslintJson, extraEslintDependencies } from '@nrwl/react'; import { extendReactEslintJson, extraEslintDependencies } from '@nrwl/react';
import { NormalizedSchema } from './normalize-options'; import { NormalizedSchema } from './normalize-options';
import { runTasksInSerial } from '@nrwl/workspace/src/utilities/run-tasks-in-serial'; import { runTasksInSerial } from '@nrwl/workspace/src/utilities/run-tasks-in-serial';
@ -26,28 +26,24 @@ export async function addLinting(
}); });
if (options.linter === Linter.EsLint) { if (options.linter === Linter.EsLint) {
const reactEslintJson = createReactEslintJson(
options.appProjectRoot,
options.setParserOptionsProject
);
updateJson( updateJson(
host, host,
joinPathFragments(options.appProjectRoot, '.eslintrc.json'), joinPathFragments(options.appProjectRoot, '.eslintrc.json'),
() => { (json) => {
json = extendReactEslintJson(json);
// Turn off @next/next/no-html-link-for-pages since there is an issue with nextjs throwing linting errors // Turn off @next/next/no-html-link-for-pages since there is an issue with nextjs throwing linting errors
// TODO(nicholas): remove after Vercel updates nextjs linter to only lint ["*.ts", "*.tsx", "*.js", "*.jsx"] // TODO(nicholas): remove after Vercel updates nextjs linter to only lint ["*.ts", "*.tsx", "*.js", "*.jsx"]
reactEslintJson.ignorePatterns = [ json.ignorePatterns = [...json.ignorePatterns, '.next/**/*'];
...reactEslintJson.ignorePatterns,
'.next/**/*',
];
reactEslintJson.rules = { json.rules = {
'@next/next/no-html-link-for-pages': 'off', '@next/next/no-html-link-for-pages': 'off',
...reactEslintJson.rules, ...json.rules,
}; };
// Find the override that handles both TS and JS files. // Find the override that handles both TS and JS files.
const commonOverride = reactEslintJson.overrides?.find((o) => const commonOverride = json.overrides?.find((o) =>
['*.ts', '*.tsx', '*.js', '*.jsx'].every((ext) => ['*.ts', '*.tsx', '*.js', '*.jsx'].every((ext) =>
o.files.includes(ext) o.files.includes(ext)
) )
@ -70,23 +66,25 @@ export async function addLinting(
}; };
} }
} }
reactEslintJson.extends ??= [];
if (typeof reactEslintJson.extends === 'string') { json.extends ??= [];
reactEslintJson.extends = [reactEslintJson.extends]; if (typeof json.extends === 'string') {
json.extends = [json.extends];
} }
// add next.js configuration // add next.js configuration
reactEslintJson.extends.unshift(...['next', 'next/core-web-vitals']); json.extends.unshift(...['next', 'next/core-web-vitals']);
// remove nx/react plugin, as it conflicts with the next.js one // remove nx/react plugin, as it conflicts with the next.js one
reactEslintJson.extends = reactEslintJson.extends.filter( json.extends = json.extends.filter(
(name) => name !== 'plugin:@nrwl/nx/react' (name) => name !== 'plugin:@nrwl/nx/react'
); );
reactEslintJson.extends.unshift('plugin:@nrwl/nx/react-typescript'); json.extends.unshift('plugin:@nrwl/nx/react-typescript');
if (!reactEslintJson.env) { if (!json.env) {
reactEslintJson.env = {}; json.env = {};
} }
reactEslintJson.env.jest = true; json.env.jest = true;
return reactEslintJson;
return json;
} }
); );
} }

View File

@ -6,7 +6,7 @@ import {
Tree, Tree,
updateJson, updateJson,
} from '@nrwl/devkit'; } from '@nrwl/devkit';
import { createReactEslintJson, extraEslintDependencies } from '@nrwl/react'; import { extendReactEslintJson, extraEslintDependencies } from '@nrwl/react';
import type { Linter as ESLintLinter } from 'eslint'; import type { Linter as ESLintLinter } from 'eslint';
interface NormalizedSchema { interface NormalizedSchema {
@ -30,16 +30,12 @@ export async function addLinting(host: Tree, options: NormalizedSchema) {
skipFormat: true, skipFormat: true,
}); });
const reactEslintJson = createReactEslintJson(
options.projectRoot,
options.setParserOptionsProject
);
updateJson( updateJson(
host, host,
joinPathFragments(options.projectRoot, '.eslintrc.json'), joinPathFragments(options.projectRoot, '.eslintrc.json'),
(json: ESLintLinter.Config) => { (json: ESLintLinter.Config) => {
json = reactEslintJson; json = extendReactEslintJson(json);
json.ignorePatterns = ['!**/*', 'public', '.cache', 'node_modules']; json.ignorePatterns = ['!**/*', 'public', '.cache', 'node_modules'];
// Find the override that handles both TS and JS files. // Find the override that handles both TS and JS files.

View File

@ -1,6 +1,7 @@
export { export {
extraEslintDependencies, extraEslintDependencies,
createReactEslintJson, createReactEslintJson,
extendReactEslintJson,
} from './src/utils/lint'; } from './src/utils/lint';
export { CSS_IN_JS_DEPENDENCIES } from './src/utils/styled'; export { CSS_IN_JS_DEPENDENCIES } from './src/utils/styled';
export { assertValidStyle } from './src/utils/assertion'; export { assertValidStyle } from './src/utils/assertion';

View File

@ -34,7 +34,7 @@ import {
findComponentImportPath, findComponentImportPath,
} from '../../utils/ast-utils'; } from '../../utils/ast-utils';
import { import {
createReactEslintJson, extendReactEslintJson,
extraEslintDependencies, extraEslintDependencies,
} from '../../utils/lint'; } from '../../utils/lint';
import { import {
@ -195,15 +195,10 @@ async function addLinting(host: Tree, options: NormalizedSchema) {
skipPackageJson: options.skipPackageJson, skipPackageJson: options.skipPackageJson,
}); });
const reactEslintJson = createReactEslintJson(
options.projectRoot,
options.setParserOptionsProject
);
updateJson( updateJson(
host, host,
joinPathFragments(options.projectRoot, '.eslintrc.json'), joinPathFragments(options.projectRoot, '.eslintrc.json'),
() => reactEslintJson extendReactEslintJson
); );
let installTask = () => {}; let installTask = () => {};

View File

@ -26,6 +26,9 @@ export const extendReactEslintJson = (json: Linter.Config) => {
}; };
}; };
/**
* @deprecated Use {@link extendReactEslintJson} instead.
*/
export const createReactEslintJson = ( export const createReactEslintJson = (
projectRoot: string, projectRoot: string,
setParserOptionsProject: boolean setParserOptionsProject: boolean