Skip to content

Commit bab9a56

Browse files
sumitarorahansl
authored andcommitted
fix(@angular/cli): fixing lint error issue added flag --type-check
1 parent a3d2e44 commit bab9a56

File tree

5 files changed

+105
-4
lines changed

5 files changed

+105
-4
lines changed

docs/documentation/lint.md

+10
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@
2626
</p>
2727
</details>
2828

29+
<details>
30+
<summary>type-check</summary>
31+
<p>
32+
`--type-check` _default value: false_
33+
</p>
34+
<p>
35+
Controls the type check for linting.
36+
</p>
37+
</details>
38+
2939
<details>
3040
<summary>format</summary>
3141
<p>

packages/@angular/cli/commands/lint.ts

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const Command = require('../ember-cli/lib/models/command');
55

66
export interface LintCommandOptions {
77
fix?: boolean;
8+
typeCheck?: boolean;
89
format?: string;
910
force?: boolean;
1011
}
@@ -21,6 +22,12 @@ export default Command.extend({
2122
default: false,
2223
description: 'Fixes linting errors (may overwrite linted files).'
2324
},
25+
{
26+
name: 'type-check',
27+
type: Boolean,
28+
default: false,
29+
description: 'Controls the type check for linting.'
30+
},
2431
{
2532
name: 'force',
2633
type: Boolean,

packages/@angular/cli/tasks/lint.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ export default Task.extend({
3232
.map((config) => {
3333
const program: ts.Program = Linter.createProgram(config.project);
3434
const files = getFilesToLint(program, config, Linter);
35-
36-
const linter = new Linter({
35+
const lintOptions = {
3736
fix: commandOptions.fix,
3837
formatter: commandOptions.format
39-
}, program);
38+
};
39+
const lintProgram = commandOptions.typeCheck ? program : undefined;
40+
const linter = new Linter(lintOptions, lintProgram);
4041

4142
files.forEach((file) => {
4243
const sourceFile = program.getSourceFile(file);
@@ -75,7 +76,7 @@ export default Task.extend({
7576
// print formatter output directly for non human-readable formats
7677
if (['prose', 'verbose', 'stylish'].indexOf(commandOptions.format) == -1) {
7778
return (result.failures.length == 0 || commandOptions.force)
78-
? Promise.resolve(0) : Promise.resolve(2);
79+
? Promise.resolve(0) : Promise.resolve(2);
7980
}
8081

8182
if (result.failures.length > 0) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { ng } from '../../utils/process';
2+
import { expectToFail } from '../../utils/utils';
3+
import { writeFile } from '../../utils/fs';
4+
5+
export default function () {
6+
const fileName = 'src/app/foo.ts';
7+
const fileContents = `
8+
const ANIMATION_CSS_VALUE_REGEX = 'asda';
9+
const a = ["asda", 'asda', 'asdasd', "ASDASDAS"];
10+
const b = "asdasd";
11+
const c = {
12+
a: "sadas",
13+
b: {
14+
v: "asdasda",
15+
s: ["asda", "asdas", 10, true, "asda"],
16+
}
17+
};
18+
19+
function check(val: any, fxState: any) {
20+
if (typeof val === "string" && val.indexOf(" ") < 0) {
21+
let r = val.match(ANIMATION_CSS_VALUE_REGEX);
22+
let num = parseFloat(r[1]);
23+
24+
if (!isNaN(num)) {
25+
fxState.num = num + "";
26+
}
27+
fxState.unit = (r[0] !== r[2] ? r[2] : "");
28+
29+
} else if (typeof val === "number") {
30+
fxState.num = val + "";
31+
}
32+
}
33+
34+
`;
35+
36+
return Promise.resolve()
37+
.then(() => writeFile(fileName, fileContents))
38+
.then(() => expectToFail(() => ng('lint', '--fix', '--type-check')));
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { ng } from '../../utils/process';
2+
import { writeFile } from '../../utils/fs';
3+
4+
export default function () {
5+
const fileName = 'src/app/foo.ts';
6+
const fileContents = `
7+
const ANIMATION_CSS_VALUE_REGEX = 'asda';
8+
const a = ["asda", 'asda', 'asdasd', "ASDASDAS"];
9+
const b = "asdasd";
10+
const c = {
11+
a: "sadas",
12+
b: {
13+
v: "asdasda",
14+
s: ["asda", "asdas", 10, true, "asda"],
15+
}
16+
};
17+
18+
function check(val: any, fxState: any) {
19+
if (typeof val === "string" && val.indexOf(" ") < 0) {
20+
let r = val.match(ANIMATION_CSS_VALUE_REGEX);
21+
let num = parseFloat(r[1]);
22+
23+
if (!isNaN(num)) {
24+
fxState.num = num + "";
25+
}
26+
fxState.unit = (r[0] !== r[2] ? r[2] : "");
27+
28+
} else if (typeof val === "number") {
29+
fxState.num = val + "";
30+
}
31+
}
32+
33+
`;
34+
35+
return Promise.resolve()
36+
.then(() => writeFile(fileName, fileContents))
37+
.then(() => ng('lint', '--fix'))
38+
.then(() => ng('lint'))
39+
.then(({ stdout }) => {
40+
if (!stdout.match(/All files pass linting./)) {
41+
throw new Error('All files pass linting.');
42+
}
43+
});
44+
}

0 commit comments

Comments
 (0)