Skip to content

Commit dec1bc1

Browse files
committed
feat(@schematics/angular): reallow --lint-fix for Angular schematics
1 parent 8338f78 commit dec1bc1

File tree

20 files changed

+121
-7
lines changed

20 files changed

+121
-7
lines changed

packages/schematics/angular/component/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
import { InsertChange } from '../utility/change';
3030
import { getWorkspace } from '../utility/config';
3131
import { buildRelativePath, findModuleFromOptions } from '../utility/find-module';
32+
import { applyLintFix } from '../utility/lint-fix';
3233
import { parseName } from '../utility/parse-name';
3334
import { buildDefaultPath } from '../utility/project';
3435
import { validateHtmlSelector, validateName } from '../utility/validation';
@@ -164,6 +165,7 @@ export default function(options: ComponentOptions): Rule {
164165
addDeclarationToNgModule(options),
165166
mergeWith(templateSource),
166167
])),
168+
options.lintFix ? applyLintFix(options.path) : noop(),
167169
]);
168170
};
169171
}

packages/schematics/angular/component/schema.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,8 @@ export interface Schema {
7171
* Specifies if the component is an entry component of declaring module.
7272
*/
7373
entryComponent?: boolean;
74+
/**
75+
* Specifies whether to apply lint fixes after generating the component.
76+
*/
77+
lintFix?: boolean;
7478
}

packages/schematics/angular/component/schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@
103103
"type": "boolean",
104104
"default": false,
105105
"description": "Specifies if the component is an entry component of declaring module."
106+
},
107+
"lintFix": {
108+
"type": "boolean",
109+
"default": false,
110+
"description": "Specifies whether to apply lint fixes after generating the component."
106111
}
107112
},
108113
"required": [],

packages/schematics/angular/directive/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { parseName } from '../utility/parse-name';
2929
import { buildDefaultPath } from '../utility/project';
3030
import { validateHtmlSelector } from '../utility/validation';
3131
import { Schema as DirectiveOptions } from './schema';
32+
import {applyLintFix} from "../utility/lint-fix";
3233

3334

3435
function addDeclarationToNgModule(options: DirectiveOptions): Rule {
@@ -137,6 +138,7 @@ export default function (options: DirectiveOptions): Rule {
137138
addDeclarationToNgModule(options),
138139
mergeWith(templateSource),
139140
])),
141+
options.lintFix ? applyLintFix(options.path) : noop(),
140142
]);
141143
};
142144
}

packages/schematics/angular/directive/schema.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,8 @@ export interface Schema {
4747
* Specifies if declaring module exports the directive.
4848
*/
4949
export?: boolean;
50+
/**
51+
* Specifies whether to apply lint fixes after generating the component.
52+
*/
53+
lintFix?: boolean;
5054
}

packages/schematics/angular/directive/schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@
6868
"type": "boolean",
6969
"default": false,
7070
"description": "Specifies if declaring module exports the directive."
71+
},
72+
"lintFix": {
73+
"type": "boolean",
74+
"default": false,
75+
"description": "Specifies whether to apply lint fixes after generating the directive."
7176
}
7277
},
7378
"required": [],

packages/schematics/angular/guard/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { getWorkspace } from '../utility/config';
2424
import { parseName } from '../utility/parse-name';
2525
import { buildDefaultPath } from '../utility/project';
2626
import { Schema as GuardOptions } from './schema';
27+
import {applyLintFix} from "../utility/lint-fix";
2728

2829

2930
export default function (options: GuardOptions): Rule {
@@ -55,6 +56,7 @@ export default function (options: GuardOptions): Rule {
5556
branchAndMerge(chain([
5657
mergeWith(templateSource),
5758
])),
59+
options.lintFix ? applyLintFix(options.path) : noop(),
5860
]);
5961
};
6062
}

packages/schematics/angular/guard/schema.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@ export interface Schema {
2727
* The name of the project.
2828
*/
2929
project?: string;
30+
/**
31+
* Specifies whether to apply lint fixes after generating the component.
32+
*/
33+
lintFix?: boolean;
3034
}

packages/schematics/angular/guard/schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
"$default": {
3535
"$source": "projectName"
3636
}
37+
},
38+
"lintFix": {
39+
"type": "boolean",
40+
"default": false,
41+
"description": "Specifies whether to apply lint fixes after generating the guard."
3742
}
3843
},
3944
"required": [],

packages/schematics/angular/interface/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ import {
1515
chain,
1616
mergeWith,
1717
move,
18+
noop,
1819
template,
1920
url,
2021
} from '@angular-devkit/schematics';
2122
import { getWorkspace } from '../utility/config';
23+
import { applyLintFix } from '../utility/lint-fix';
2224
import { parseName } from '../utility/parse-name';
2325
import { buildDefaultPath } from '../utility/project';
2426
import { Schema as InterfaceOptions } from './schema';
@@ -55,6 +57,7 @@ export default function (options: InterfaceOptions): Rule {
5557
branchAndMerge(chain([
5658
mergeWith(templateSource),
5759
])),
60+
options.lintFix ? applyLintFix(options.path) : noop(),
5861
]);
5962
};
6063
}

packages/schematics/angular/interface/schema.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@ export interface Schema {
2727
* Specifies the type of interface.
2828
*/
2929
type?: string;
30+
/**
31+
* Specifies whether to apply lint fixes after generating the component.
32+
*/
33+
lintFix?: boolean;
3034
}

packages/schematics/angular/interface/schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
"$source": "argv",
3838
"index": 1
3939
}
40+
},
41+
"lintFix": {
42+
"type": "boolean",
43+
"default": false,
44+
"description": "Specifies whether to apply lint fixes after generating the directive."
4045
}
4146
},
4247
"required": [],

packages/schematics/angular/pipe/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { addDeclarationToModule, addExportToModule } from '../utility/ast-utils'
2525
import { InsertChange } from '../utility/change';
2626
import { getWorkspace } from '../utility/config';
2727
import { buildRelativePath, findModuleFromOptions } from '../utility/find-module';
28+
import { applyLintFix } from '../utility/lint-fix';
2829
import { parseName } from '../utility/parse-name';
2930
import { buildDefaultPath } from '../utility/project';
3031
import { Schema as PipeOptions } from './schema';
@@ -117,6 +118,7 @@ export default function (options: PipeOptions): Rule {
117118
chain([
118119
addDeclarationToNgModule(options),
119120
mergeWith(templateSource),
121+
options.lintFix ? applyLintFix(options.path) : noop(),
120122
]),
121123
);
122124
};

packages/schematics/angular/pipe/schema.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,8 @@ export interface Schema {
3939
* Specifies if declaring module exports the pipe.
4040
*/
4141
export?: boolean;
42+
/**
43+
* Specifies whether to apply lint fixes after generating the component.
44+
*/
45+
lintFix?: boolean;
4246
}

packages/schematics/angular/pipe/schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@
5050
"type": "boolean",
5151
"default": false,
5252
"description": "Specifies if declaring module exports the pipe."
53+
},
54+
"lintFix": {
55+
"type": "boolean",
56+
"default": false,
57+
"description": "Specifies whether to apply lint fixes after generating the pipe."
5358
}
5459
},
5560
"required": [],

packages/schematics/angular/service/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
SchematicsException,
1212
Tree,
1313
apply,
14+
chain,
1415
filter,
1516
mergeWith,
1617
move,
@@ -19,6 +20,7 @@ import {
1920
url,
2021
} from '@angular-devkit/schematics';
2122
import { getWorkspace } from '../utility/config';
23+
import { applyLintFix } from '../utility/lint-fix';
2224
import { parseName } from '../utility/parse-name';
2325
import { buildDefaultPath } from '../utility/project';
2426
import { Schema as ServiceOptions } from './schema';
@@ -49,6 +51,9 @@ export default function (options: ServiceOptions): Rule {
4951
move(parsedPath.path),
5052
]);
5153

52-
return mergeWith(templateSource);
54+
return chain([
55+
mergeWith(templateSource),
56+
options.lintFix ? applyLintFix(options.path) : noop(),
57+
]);
5358
};
5459
}

packages/schematics/angular/service/schema.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@ export interface Schema {
2727
* Specifies if a spec file is generated.
2828
*/
2929
spec?: boolean;
30+
/**
31+
* Specifies whether to apply lint fixes after generating the component.
32+
*/
33+
lintFix?: boolean;
3034
}

packages/schematics/angular/service/schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
"type": "boolean",
3535
"default": true,
3636
"description": "Specifies if a spec file is generated."
37+
},
38+
"lintFix": {
39+
"type": "boolean",
40+
"default": false,
41+
"description": "Specifies whether to apply lint fixes after generating the pipe."
3742
}
3843
},
3944
"required": [],
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import { DirEntry, Rule, SchematicContext, SchematicsException, Tree } from '@angular-devkit/schematics';
9+
import { TslintFixTask } from '@angular-devkit/schematics/tasks';
10+
11+
export function applyLintFix(path = '/'): Rule {
12+
return (tree: Tree, context: SchematicContext) => {
13+
// Find the closest tsling.json
14+
let dir: DirEntry | null = tree.getDir(path.substr(0, path.lastIndexOf('/')));
15+
16+
do {
17+
if ((dir.subfiles as string[]).includes('tslint.json')) {
18+
break;
19+
}
20+
21+
dir = dir.parent;
22+
} while (dir !== null);
23+
24+
if (dir === null) {
25+
throw new SchematicsException('Asked to run lint fixes, but could not find a tslint.json.');
26+
}
27+
28+
// Only include files that have been touched.
29+
const files = tree.actions.reduce((acc: Set<string>, action) => {
30+
const path = action.path.substr(1); // Remove the starting '/'.
31+
if (path.endsWith('.ts') && dir && action.path.startsWith(dir.path)) {
32+
acc.add(path);
33+
}
34+
35+
return acc;
36+
}, new Set<string>());
37+
38+
context.addTask(new TslintFixTask('tslint.json', {
39+
ignoreErrors: true,
40+
tsConfigPath: 'tsconfig.json',
41+
files: [...files],
42+
}));
43+
};
44+
}

tests/legacy-cli/e2e/tests/generate/lint-fix.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,10 @@ export default function () {
1414
}
1515
}`;
1616

17-
// TODO(architect): reenable after figuring out what happens to --lint-fix.
18-
return;
19-
2017
return Promise.resolve()
2118

2219
// setup a double-quote tslint config
23-
.then(() => writeFile('src/app/tslint.json', nestedConfigContent))
20+
.then(() => writeFile('tslint.json', nestedConfigContent))
2421

2522
// Generate a fixed new component but don't fix rest of app
2623
.then(() => ng('generate', 'component', 'test-component1', '--lint-fix'))
@@ -32,10 +29,13 @@ export default function () {
3229
.then(() => ng('lint'))
3330

3431
// Enable default option and generate all other module related blueprints
35-
.then(() => ng('config', 'defaults.lintFix', 'true'))
32+
.then(() => ng('config', 'schematics.@schematics/angular.directive.lintFix', 'true'))
33+
.then(() => ng('config', 'schematics.@schematics/angular.service.lintFix', 'true'))
34+
.then(() => ng('config', 'schematics.@schematics/angular.pipe.lintFix', 'true'))
35+
.then(() => ng('config', 'schematics.@schematics/angular.guard.lintFix', 'true'))
3636
.then(() => ng('generate', 'directive', 'test-directive'))
3737
.then(() => ng('generate', 'service', 'test-service'))
3838
.then(() => ng('generate', 'pipe', 'test-pipe'))
39-
.then(() => ng('generate', 'guard', 'test-guard', '--module', 'app.module.ts'))
39+
.then(() => ng('generate', 'guard', 'test-guard'))
4040
.then(() => ng('lint'));
4141
}

0 commit comments

Comments
 (0)