Skip to content

Commit

Permalink
fix(linter): Generator creating .eslintrc.json at the root path even …
Browse files Browse the repository at this point in the history
…when .eslintrc.js already exist (#10080)

* feat(devkit): feat(devkit): don't replace begining undescore when creating file

Underscore is a character usually used for sorting purposes and is absolutely legit. There is no
reason to replace it.

ISSUES CLOSED: #8875

* fix(linter): generator creating .eslintrc.json at the root path even when .eslintrc.js
  • Loading branch information
AloisH committed May 11, 2022
1 parent 1aed5ac commit e5bde2f
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 8 deletions.
13 changes: 11 additions & 2 deletions packages/linter/src/generators/init/init.spec.ts
@@ -1,7 +1,6 @@
import { Linter } from '../utils/linter';
import { Tree } from '@nrwl/devkit';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';

import { Linter } from '../utils/linter';
import { lintInitGenerator } from './init';

describe('@nrwl/linter:init', () => {
Expand All @@ -20,6 +19,16 @@ describe('@nrwl/linter:init', () => {

expect(tree.read('.eslintrc.json', 'utf-8')).toMatchSnapshot();
});

it('should not generate the global eslint config if it already exist', async () => {
tree.write('.eslintrc.js', '{}');

await lintInitGenerator(tree, {
linter: Linter.EsLint,
});

expect(tree.exists('.eslintrc.json')).toBe(false);
});
});

describe('tslint', () => {
Expand Down
6 changes: 4 additions & 2 deletions packages/linter/src/generators/init/init.ts
@@ -1,10 +1,10 @@
import type { GeneratorCallback, Tree } from '@nrwl/devkit';
import {
addDependenciesToPackageJson,
removeDependenciesFromPackageJson,
updateJson,
writeJson,
} from '@nrwl/devkit';
import type { GeneratorCallback, Tree } from '@nrwl/devkit';
import {
buildAngularVersion,
eslintConfigPrettierVersion,
Expand All @@ -13,7 +13,9 @@ import {
tslintVersion,
typescriptESLintVersion,
} from '../../utils/versions';

import { Linter } from '../utils/linter';
import { containsEslint } from '../utils/eslint-file';

export interface LinterInitOptions {
linter?: Linter;
Expand Down Expand Up @@ -165,7 +167,7 @@ function initTsLint(tree: Tree, options: LinterInitOptions): GeneratorCallback {
}

function initEsLint(tree: Tree, options: LinterInitOptions): GeneratorCallback {
if (tree.exists('/.eslintrc.json')) {
if (containsEslint(tree)) {
return () => {};
}

Expand Down
@@ -1,5 +1,42 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`@nrwl/linter:lint-project --linter eslint should extend to .eslintrc.js when an .eslintrc.js already exist 1`] = `
"{
\\"extends\\": [
\\"../../.eslintrc.js\\"
],
\\"ignorePatterns\\": [
\\"!**/*\\"
],
\\"overrides\\": [
{
\\"files\\": [
\\"*.ts\\",
\\"*.tsx\\",
\\"*.js\\",
\\"*.jsx\\"
],
\\"rules\\": {}
},
{
\\"files\\": [
\\"*.ts\\",
\\"*.tsx\\"
],
\\"rules\\": {}
},
{
\\"files\\": [
\\"*.js\\",
\\"*.jsx\\"
],
\\"rules\\": {}
}
]
}
"
`;

exports[`@nrwl/linter:lint-project --linter eslint should generate a eslint config 1`] = `
"{
\\"extends\\": [
Expand Down
20 changes: 18 additions & 2 deletions packages/linter/src/generators/lint-project/lint-project.spec.ts
@@ -1,11 +1,11 @@
import {
Tree,
addProjectConfiguration,
readProjectConfiguration,
Tree,
} from '@nrwl/devkit';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';

import { Linter } from '../utils/linter';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import { lintProjectGenerator } from './lint-project';

describe('@nrwl/linter:lint-project', () => {
Expand Down Expand Up @@ -63,6 +63,22 @@ describe('@nrwl/linter:lint-project', () => {
}
`);
});

it('should extend to .eslintrc.js when an .eslintrc.js already exist', async () => {
tree.write('.eslintrc.js', '{}');

await lintProjectGenerator(tree, {
...defaultOptions,
linter: Linter.EsLint,
eslintFilePatterns: ['**/*.ts'],
project: 'test-lib',
setParserOptionsProject: false,
});

expect(
tree.read('libs/test-lib/.eslintrc.json', 'utf-8')
).toMatchSnapshot();
});
});

describe('tslint', () => {
Expand Down
6 changes: 4 additions & 2 deletions packages/linter/src/generators/lint-project/lint-project.ts
Expand Up @@ -6,8 +6,10 @@ import {
updateProjectConfiguration,
writeJson,
} from '@nrwl/devkit';
import { join } from 'path';

import { Linter } from '../utils/linter';
import { findEslintFile } from '../utils/eslint-file';
import { join } from 'path';
import { lintInitGenerator } from '../init/init';

interface LintProjectOptions {
Expand Down Expand Up @@ -40,7 +42,7 @@ function createEsLintConfiguration(
setParserOptionsProject: boolean
) {
writeJson(tree, join(projectConfig.root, `.eslintrc.json`), {
extends: [`${offsetFromRoot(projectConfig.root)}.eslintrc.json`],
extends: [`${offsetFromRoot(projectConfig.root)}${findEslintFile(tree)}`],
// Include project files to be linted since the global one excludes all files.
ignorePatterns: ['!**/*'],
overrides: [
Expand Down
55 changes: 55 additions & 0 deletions packages/linter/src/generators/utils/eslint-file.spec.ts
@@ -0,0 +1,55 @@
import { containsEslint, findEslintFile } from './eslint-file';

import { Tree } from '@nrwl/devkit';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';

describe('@nrwl/linter:eslint-file', () => {
let tree: Tree;

beforeEach(() => {
tree = createTreeWithEmptyWorkspace();
});

describe('containsEslint', () => {
it('should return false when calling containsEslint without a eslint config', () => {
expect(containsEslint(tree)).toBe(false);
});

it('should return true when calling containsEslint with a .eslintrc.json config', () => {
tree.write('.eslintrc.json', '{}');
expect(containsEslint(tree)).toBe(true);
});

it('should return true when calling containsEslint with a .eslintrc.js config', () => {
tree.write('.eslintrc.js', '{}');
expect(containsEslint(tree)).toBe(true);
});

it('should return false when calling containsEslint witn an incorrect eslint file name', () => {
tree.write('.eslintrc.yaml', '{}');
expect(containsEslint(tree)).toBe(false);
});
});

describe('findEslintFile', () => {
it('should return default name when calling findEslintFile when no eslint is found', () => {
expect(findEslintFile(tree)).toBe('eslintrc.json');
});

it('should return the name of the eslint config when calling findEslintFile', () => {
tree.write('.eslintrc.json', '{}');
expect(findEslintFile(tree)).toBe('.eslintrc.json');
});

it('should return the name of the eslint config when calling findEslintFile', () => {
tree.write('.eslintrc.js', '{}');
expect(findEslintFile(tree)).toBe('.eslintrc.js');
});

it('should return default name when calling findEslintFile when no eslint is found', () => {
tree.write('.eslintrc.yaml', '{}');

expect(findEslintFile(tree)).toBe('eslintrc.json');
});
});
});
22 changes: 22 additions & 0 deletions packages/linter/src/generators/utils/eslint-file.ts
@@ -0,0 +1,22 @@
import type { Tree } from '@nrwl/devkit';

const eslintFileList = ['.eslintrc.json', '.eslintrc.js'];

export function containsEslint(tree: Tree): boolean {
for (const file of eslintFileList) {
if (tree.exists(file)) {
return true;
}
}
return false;
}

export function findEslintFile(tree: Tree): string {
for (const file of eslintFileList) {
if (tree.exists(file)) {
return file;
}
}
// Default file
return 'eslintrc.json';
}

0 comments on commit e5bde2f

Please sign in to comment.