Skip to content

Commit

Permalink
feat(linter): improve support for mixed linters when using IDEs (nrwl…
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo authored Mar 6, 2020
1 parent 13302f0 commit fef54fa
Show file tree
Hide file tree
Showing 13 changed files with 276 additions and 62 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ tmp
node_modules
/package.json
packages/workspace/src/schematics/**/files/**/*.json
packages/workspace/src/core/dep-graph/vendor.js
packages/web/src/schematics/**/files/**/*.json
packages/node/src/schematics/**/files/**/*.json
packages/express/src/schematics/**/files/**/*.json
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
"cz-customizable": "^6.2.0",
"document-register-element": "^1.13.1",
"dotenv": "6.2.0",
"eslint": "6.1.0",
"eslint": "6.8.0",
"eslint-config-prettier": "6.0.0",
"eslint-plugin-import": "^2.20.1",
"eslint-plugin-jsx-a11y": "^6.2.3",
Expand Down Expand Up @@ -223,7 +223,7 @@
"tsconfig-paths-webpack-plugin": "^3.2.0",
"tsickle": "^0.37.0",
"tslib": "^1.9.3",
"tslint": "5.11.0",
"tslint": "6.0.0",
"typescript": "~3.7.4",
"url-loader": "^3.0.0",
"verdaccio": "^4.4.2",
Expand Down
3 changes: 3 additions & 0 deletions packages/angular/src/schematics/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,9 @@ function updateLinting(options: NormalizedSchema): Rule {
}),
updateJsonInTree(`${options.appProjectRoot}/tslint.json`, json => {
json.extends = `${offsetFromRoot(options.appProjectRoot)}tslint.json`;
json.linterOptions = {
exclude: ['!**/*']
};
return json;
})
]);
Expand Down
5 changes: 4 additions & 1 deletion packages/angular/src/schematics/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,10 @@ function updateProject(options: NormalizedSchema): Rule {
updateJsonInTree(`${options.projectRoot}/tslint.json`, json => {
return {
...json,
extends: `${offsetFromRoot(options.projectRoot)}tslint.json`
extends: `${offsetFromRoot(options.projectRoot)}tslint.json`,
linterOptions: {
exclude: ['!**/*']
}
};
}),
updateJsonInTree(`/nx.json`, json => {
Expand Down
19 changes: 15 additions & 4 deletions packages/workspace/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,13 @@
},
"update-9-1-0": {
"version": "9.1.0-beta.1",
"description": "Update prettier to 1.19.1 with support for typescript 3.7",
"factory": "./src/migrations/update-9-0-0/update-9-0-0"
"description": "Update prettier to 1.19.1 with support for typescript 3.7; Update eslint and tslint",
"factory": "./src/migrations/update-9-1-0/update-9-1-0"
},
"update-lint-config-9-1-0": {
"version": "9.1.0-beta.1",
"description": "Update eslint and tslint config to provide better IDE support",
"factory": "./src/migrations/update-9-1-0/update-lint-config"
}
},
"packageJsonUpdates": {
Expand Down Expand Up @@ -396,11 +401,17 @@
}
}
},
"9.0.0": {
"version": "9.0.0-beta.1",
"9.1.0": {
"version": "9.1.0-beta.1",
"packages": {
"eslint": {
"version": "6.8.0"
},
"prettier": {
"version": "1.19.1"
},
"tslint": {
"version": "6.0.0"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const updatePackages = updatePackagesInPackageJson(
join(__dirname, '../../../', 'migrations.json'),
'9.1.0'
);

export default function() {
return chain([updatePackages]);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import { chain, Tree } from '@angular-devkit/schematics';
import { readJsonInTree, updateJsonInTree } from '@nrwl/workspace';
import { createEmptyWorkspace } from '@nrwl/workspace/testing';
import { callRule, runMigration } from '../../utils/testing';
import { updateWorkspace } from '@nrwl/workspace/src/utils/workspace';

describe('Update 9.1.0', () => {
let tree: Tree;

beforeEach(async () => {
tree = Tree.empty();
tree = createEmptyWorkspace(tree);
});

it('should update tslint.json', async () => {
tree = await callRule(
chain([
updateWorkspace(workspace => {
workspace.projects.add({
name: 'proj1',
root: 'proj1',
architect: {
lint: {
builder: '@angular-devkit/build-angular:tslint'
}
}
});
workspace.projects.add({
name: 'proj2',
root: 'proj2',
architect: {
lint: {
builder: '@angular-devkit/build-angular:tslint'
}
}
});
}),
updateJsonInTree('proj1/tslint.json', () => ({
rules: {}
})),
updateJsonInTree('proj2/tslint.json', () => ({
rules: {},
linterOptions: {
exclude: ['whatever']
}
}))
]),
tree
);

const result = await runMigration('update-lint-config-9-1-0', {}, tree);

expect(readJsonInTree(result, 'proj1/tslint.json')).toEqual({
rules: {},
linterOptions: {
exclude: ['!**/*']
}
});

expect(readJsonInTree(result, 'proj2/tslint.json')).toEqual({
rules: {},
linterOptions: {
exclude: ['!**/*', 'whatever']
}
});
});

it('should update .eslintrc', async () => {
tree = await callRule(
chain([
updateWorkspace(workspace => {
workspace.projects.add({
name: 'proj1',
root: 'proj1',
architect: {
lint: {
builder: '@nrwl/linter:lint',
options: {
config: 'proj1/.eslintrc'
}
}
}
});
workspace.projects.add({
name: 'proj2',
root: 'proj2',
architect: {
lint: {
builder: '@nrwl/linter:lint',
options: {
config: 'proj2/.eslintrc'
}
}
}
});
}),
updateJsonInTree('proj1/.eslintrc', () => ({
rules: {}
})),
updateJsonInTree('proj2/.eslintrc', () => ({
rules: {},
ignorePatterns: ['whatever']
}))
]),
tree
);

const result = await runMigration('update-lint-config-9-1-0', {}, tree);

expect(readJsonInTree(result, 'proj1/.eslintrc')).toEqual({
rules: {},
ignorePatterns: ['!**/*']
});

expect(readJsonInTree(result, 'proj2/.eslintrc')).toEqual({
rules: {},
ignorePatterns: ['!**/*', 'whatever']
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { SchematicContext, Tree } from '@angular-devkit/schematics';
import { readWorkspace, updateJsonInTree } from '@nrwl/workspace';

function updateLintConfigurations(host: Tree, context: SchematicContext) {
const workspaceJson = readWorkspace(host);
Object.values(workspaceJson.projects).forEach((config: any) => {
if (!config.architect || !config.architect.lint) return;
if (config.architect.lint.builder === '@nrwl/linter:lint') {
updateJson(
json => {
// Prefix it so that previously ignored files will override the whitelist.
json.ignorePatterns = ['!**/*', ...(json.ignorePatterns || [])];
return json;
},
config.architect.lint.options.config,
host,
context
);
}
if (
config.architect.lint.builder === '@angular-devkit/build-angular:tslint'
) {
updateJson(
json => {
json.linterOptions = {
...json.linterOptions,
// Prefix it so that previously ignored files will override the whitelist.
exclude: [
'!**/*',
...((json.linterOptions && json.linterOptions.exclude) || [])
]
};
return json;
},
`${config.root}/tslint.json`,
host,
context
);
}
});
}

function updateJson(visitor, path, host, context) {
try {
if (host.exists(path)) {
// In case tslint.json does not exist we don't want to create it.
updateJsonInTree(path, visitor)(host, context);
}
} catch {
context.logger.warn(`Could not update ${path}`);
}
}

export default function() {
return updateLintConfigurations;
}
5 changes: 4 additions & 1 deletion packages/workspace/src/schematics/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,10 @@ describe('lib', () => {
const tslintJson = readJsonInTree(tree, 'libs/my-dir/my-lib/tslint.json');
expect(tslintJson).toEqual({
extends: '../../../tslint.json',
rules: {}
rules: {},
linterOptions: {
exclude: ['!**/*']
}
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"@types/node": "~8.9.4",
"dotenv": "6.2.0",
"ts-node": "~7.0.0",
"tslint": "~5.11.0",
"tslint": "~6.0.0",
"eslint": "<%= eslintVersion %>",
"typescript": "<%= typescriptVersion %>",
"prettier": "<%= prettierVersion %>"
Expand Down
10 changes: 10 additions & 0 deletions packages/workspace/src/utils/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ export function addLintFiles(
join(projectRoot as any, `tslint.json`),
JSON.stringify({
extends: `${offsetFromRoot(projectRoot)}tslint.json`,
// Include project files to be linted since the global one excludes all files.
linterOptions: {
exclude: ['!**/*']
},
rules: {}
})
);
Expand Down Expand Up @@ -134,6 +138,8 @@ export function addLintFiles(
rules: {}
};
}
// Include all project files to be linted (since they are turned off in the root eslintrc file).
configJson.ignorePatterns = ['!**/*'];
host.create(
join(projectRoot as any, `.eslintrc`),
JSON.stringify(configJson)
Expand All @@ -149,6 +155,9 @@ export function addLintFiles(
const globalTsLint = `
{
"rulesDirectory": ["node_modules/@nrwl/workspace/src/tslint"],
"linterOptions": {
"exclude": ["**/*"]
},
"rules": {
"arrow-return-shorthand": true,
"callable-types": true,
Expand Down Expand Up @@ -220,6 +229,7 @@ const globalESLint = `
"sourceType": "module",
"project": "./tsconfig.json"
},
"ignorePatterns": ["**/*"],
"plugins": ["@typescript-eslint", "@nrwl/nx"],
"extends": [
'eslint:recommended',
Expand Down
2 changes: 1 addition & 1 deletion packages/workspace/src/utils/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ export const angularCliVersion = '9.0.1';
export const typescriptVersion = '~3.7.4';
export const prettierVersion = '1.19.1';
export const typescriptESLintVersion = '2.19.2';
export const eslintVersion = '6.1.0';
export const eslintVersion = '6.8.0';
export const eslintConfigPrettierVersion = '6.0.0';
Loading

0 comments on commit fef54fa

Please sign in to comment.