Skip to content

Commit aeb4ff5

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

File tree

23 files changed

+138
-6
lines changed

23 files changed

+138
-6
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
@@ -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 { validateHtmlSelector } from '../utility/validation';
@@ -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/enum/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ import {
1616
chain,
1717
mergeWith,
1818
move,
19+
noop,
1920
template,
2021
url,
2122
} from '@angular-devkit/schematics';
2223
import { getWorkspace } from '../utility/config';
24+
import { applyLintFix } from '../utility/lint-fix';
2325
import { parseName } from '../utility/parse-name';
2426
import { buildDefaultPath } from '../utility/project';
2527
import { Schema as EnumOptions } from './schema';
@@ -53,6 +55,7 @@ export default function (options: EnumOptions): Rule {
5355
branchAndMerge(chain([
5456
mergeWith(templateSource),
5557
])),
58+
options.lintFix ? applyLintFix(options.path) : noop(),
5659
]);
5760
};
5861
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,8 @@ export interface Schema {
1919
* The name of the project.
2020
*/
2121
project?: string;
22+
/**
23+
* Specifies whether to apply lint fixes after generating the component.
24+
*/
25+
lintFix?: boolean;
2226
}

packages/schematics/angular/enum/schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
"$default": {
2525
"$source": "projectName"
2626
}
27+
},
28+
"lintFix": {
29+
"type": "boolean",
30+
"default": false,
31+
"description": "Specifies whether to apply lint fixes after generating the enum."
2732
}
2833
},
2934
"required": [],

packages/schematics/angular/guard/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
url,
2222
} from '@angular-devkit/schematics';
2323
import { getWorkspace } from '../utility/config';
24+
import { applyLintFix } from '../utility/lint-fix';
2425
import { parseName } from '../utility/parse-name';
2526
import { buildDefaultPath } from '../utility/project';
2627
import { Schema as GuardOptions } from './schema';
@@ -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: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 {
9+
DirEntry,
10+
Rule,
11+
SchematicContext,
12+
SchematicsException,
13+
Tree,
14+
} from '@angular-devkit/schematics';
15+
import { TslintFixTask } from '@angular-devkit/schematics/tasks';
16+
17+
export function applyLintFix(path = '/'): Rule {
18+
return (tree: Tree, context: SchematicContext) => {
19+
// Find the closest tsling.json
20+
let dir: DirEntry | null = tree.getDir(path.substr(0, path.lastIndexOf('/')));
21+
22+
do {
23+
if ((dir.subfiles as string[]).includes('tslint.json')) {
24+
break;
25+
}
26+
27+
dir = dir.parent;
28+
} while (dir !== null);
29+
30+
if (dir === null) {
31+
throw new SchematicsException('Asked to run lint fixes, but could not find a tslint.json.');
32+
}
33+
34+
// Only include files that have been touched.
35+
const files = tree.actions.reduce((acc: Set<string>, action) => {
36+
const path = action.path.substr(1); // Remove the starting '/'.
37+
if (path.endsWith('.ts') && dir && action.path.startsWith(dir.path)) {
38+
acc.add(path);
39+
}
40+
41+
return acc;
42+
}, new Set<string>());
43+
44+
context.addTask(new TslintFixTask({
45+
ignoreErrors: true,
46+
tsConfigPath: 'tsconfig.json',
47+
files: [...files],
48+
}));
49+
};
50+
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ 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
@@ -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)