Skip to content

Commit

Permalink
feat(linter): handle node
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Aug 9, 2023
1 parent 8db43d7 commit 12dcc4a
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 58 deletions.
16 changes: 8 additions & 8 deletions packages/next/src/generators/application/lib/add-linting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import {
Tree,
updateJson,
} from '@nx/devkit';
import {
extendReactEslintJson,
extraEslintDependencies,
} from '@nx/react/src/utils/lint';
import { extraEslintDependencies } from '@nx/react/src/utils/lint';
import { NormalizedSchema } from './normalize-options';
import {
addExtendsToLintConfig,
addIgnoresToLintConfig,
} from '@nx/linter/src/generators/utils/eslint-file';

export async function addLinting(
host: Tree,
Expand All @@ -30,17 +31,16 @@ export async function addLinting(
});

if (options.linter === Linter.EsLint) {
addExtendsToLintConfig(host, options.appProjectRoot, 'plugin:@nx/react');
addIgnoresToLintConfig(host, options.appProjectRoot, ['.next/**/*']);

updateJson(
host,
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
// TODO(nicholas): remove after Vercel updates nextjs linter to only lint ["*.ts", "*.tsx", "*.js", "*.jsx"]

json.ignorePatterns = [...json.ignorePatterns, '.next/**/*'];

json.rules = {
'@next/next/no-html-link-for-pages': 'off',
...json.rules,
Expand Down
28 changes: 1 addition & 27 deletions packages/node/src/generators/e2e-project/e2e-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
javaScriptOverride,
typeScriptOverride,
} from '@nx/linter/src/generators/init/global-eslint-config';
import { addOverrideToLintConfig } from '@nx/linter/src/generators/utils/eslint-file';

export async function e2eProjectGenerator(host: Tree, _options: Schema) {
const tasks: GeneratorCallback[] = [];
Expand Down Expand Up @@ -110,33 +111,6 @@ export async function e2eProjectGenerator(host: Tree, _options: Schema) {
rootProject: options.rootProject,
});
tasks.push(linterTask);

updateJson(host, join(options.e2eProjectRoot, '.eslintrc.json'), (json) => {
if (options.rootProject) {
json.plugins = ['@nx'];
json.extends = [];
}
json.overrides = [
...(options.rootProject
? [typeScriptOverride, javaScriptOverride]
: []),
/**
* In order to ensure maximum efficiency when typescript-eslint generates TypeScript Programs
* behind the scenes during lint runs, we need to make sure the project is configured to use its
* own specific tsconfigs, and not fall back to the ones in the root of the workspace.
*/
{
files: ['*.ts', '*.tsx', '*.js', '*.jsx'],
/**
* Having an empty rules object present makes it more obvious to the user where they would
* extend things from if they needed to
*/
rules: {},
},
];

return json;
});
}

if (!options.skipFormat) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,10 @@ function projectHasKarmaConfig(
function projectHasEslintConfig(
project: AngularJsonProjectConfiguration
): boolean {
return fileExists(join(project.root, '.eslintrc.json'));
return (
fileExists(join(project.root, '.eslintrc.json')) ||
fileExists(join(project.root, 'eslint.config.js'))
);
}

function replaceNgWithNxInPackageJsonScripts(repoRoot: string): void {
Expand Down
18 changes: 11 additions & 7 deletions packages/react/src/utils/lint.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Linter } from 'eslint';
import {
eslintPluginImportVersion,
eslintPluginReactVersion,
Expand All @@ -15,11 +16,14 @@ export const extraEslintDependencies = {
},
};

// export const extendReactEslintJson = (json: Linter.Config) => {
// const { extends: pluginExtends, ...config } = json;
/**
* @deprecated Use `addExtendsToLintConfig` from `@nx/linter` instead.
*/
export const extendReactEslintJson = (json: Linter.Config) => {
const { extends: pluginExtends, ...config } = json;

// return {
// extends: ['plugin:@nx/react', ...(pluginExtends || [])],
// ...config,
// };
// };
return {
extends: ['plugin:@nx/react', ...(pluginExtends || [])],
...config,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { Linter } from '../../../utils/lint';
import { NormalizedSchema } from '../schema';
import { updateEslintrcJson } from './update-eslintrc-json';
import { updateEslintConfig } from './update-eslint-config';

// nx-ignore-next-line
const { libraryGenerator } = require('@nx/js');
Expand Down Expand Up @@ -38,7 +38,7 @@ describe('updateEslint', () => {
const projectConfig = readProjectConfiguration(tree, 'my-lib');

expect(() => {
updateEslintrcJson(tree, schema, projectConfig);
updateEslintConfig(tree, schema, projectConfig);
}).not.toThrow();
});

Expand All @@ -54,7 +54,7 @@ describe('updateEslint', () => {
);
const projectConfig = readProjectConfiguration(tree, 'my-lib');

updateEslintrcJson(tree, schema, projectConfig);
updateEslintConfig(tree, schema, projectConfig);

expect(
readJson(tree, '/libs/shared/my-destination/.eslintrc.json')
Expand Down Expand Up @@ -84,7 +84,7 @@ describe('updateEslint', () => {
relativeToRootDestination: 'libs/test',
};

updateEslintrcJson(tree, newSchema, projectConfig);
updateEslintConfig(tree, newSchema, projectConfig);

expect(readJson(tree, '/libs/test/.eslintrc.json')).toEqual(
expect.objectContaining({
Expand Down Expand Up @@ -113,7 +113,7 @@ describe('updateEslint', () => {
);
const projectConfig = readProjectConfiguration(tree, 'my-lib');

updateEslintrcJson(tree, schema, projectConfig);
updateEslintConfig(tree, schema, projectConfig);

expect(
readJson(tree, '/libs/shared/my-destination/.eslintrc.json')
Expand Down Expand Up @@ -141,7 +141,7 @@ describe('updateEslint', () => {
);
const projectConfig = readProjectConfiguration(tree, 'my-lib');

updateEslintrcJson(tree, schema, projectConfig);
updateEslintConfig(tree, schema, projectConfig);

expect(
readJson(tree, '/libs/shared/my-destination/.eslintrc.json')
Expand Down Expand Up @@ -181,7 +181,7 @@ describe('updateEslint', () => {
);
const projectConfig = readProjectConfiguration(tree, 'my-lib');

updateEslintrcJson(tree, schema, projectConfig);
updateEslintConfig(tree, schema, projectConfig);

expect(
readJson(tree, '/libs/shared/my-destination/.eslintrc.json')
Expand Down Expand Up @@ -222,7 +222,7 @@ describe('updateEslint', () => {
);
const projectConfig = readProjectConfiguration(tree, 'my-lib');

updateEslintrcJson(tree, schema, projectConfig);
updateEslintConfig(tree, schema, projectConfig);

expect(
readJson(tree, '/libs/shared/my-destination/.eslintrc.json').overrides[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,20 @@ function offsetFilePath(
*
* @param schema The options provided to the schematic
*/
export function updateEslintrcJson(
export function updateEslintConfig(
tree: Tree,
schema: NormalizedSchema,
project: ProjectConfiguration
) {
const offset = offsetFromRoot(schema.relativeToRootDestination);

const eslintRcPath = join(schema.relativeToRootDestination, '.eslintrc.json');

if (!tree.exists(eslintRcPath)) {
// no .eslintrc found. nothing to do
return;
}

const offset = offsetFromRoot(schema.relativeToRootDestination);

updateJson<PartialEsLintRcJson>(tree, eslintRcPath, (eslintRcJson) => {
if (typeof eslintRcJson.extends === 'string') {
eslintRcJson.extends = offsetFilePath(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function updateFilesForRootProjects(
if (!allowedExt.includes(ext)) {
continue;
}
if (file === '.eslintrc.json') {
if (file === '.eslintrc.json' || file === 'eslint.config.js') {
continue;
}

Expand Down Expand Up @@ -108,7 +108,7 @@ export function updateFilesForNonRootProjects(
if (!allowedExt.includes(ext)) {
continue;
}
if (file === '.eslintrc.json') {
if (file === '.eslintrc.json' || file === 'eslint.config.js') {
continue;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/workspace/src/generators/move/move.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { normalizeSchema } from './lib/normalize-schema';
import { updateBuildTargets } from './lib/update-build-targets';
import { updateCypressConfig } from './lib/update-cypress-config';
import { updateDefaultProject } from './lib/update-default-project';
import { updateEslintrcJson } from './lib/update-eslintrc-json';
import { updateEslintConfig } from './lib/update-eslint-config';
import { updateImplicitDependencies } from './lib/update-implicit-dependencies';
import { updateImports } from './lib/update-imports';
import { updateJestConfig } from './lib/update-jest-config';
Expand Down Expand Up @@ -48,7 +48,7 @@ export async function moveGenerator(tree: Tree, rawSchema: Schema) {
updateCypressConfig(tree, schema, projectConfig);
updateJestConfig(tree, schema, projectConfig);
updateStorybookConfig(tree, schema, projectConfig);
updateEslintrcJson(tree, schema, projectConfig);
updateEslintConfig(tree, schema, projectConfig);
updateReadme(tree, schema);
updatePackageJson(tree, schema);
updateBuildTargets(tree, schema);
Expand Down

0 comments on commit 12dcc4a

Please sign in to comment.