Skip to content

Commit

Permalink
fix(@angular/cli): fixing lint error issue added flag --type-check
Browse files Browse the repository at this point in the history
  • Loading branch information
sumitarora authored and hansl committed Mar 24, 2017
1 parent a3d2e44 commit bab9a56
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 4 deletions.
10 changes: 10 additions & 0 deletions docs/documentation/lint.md
Expand Up @@ -26,6 +26,16 @@
</p>
</details>

<details>
<summary>type-check</summary>
<p>
`--type-check` _default value: false_
</p>
<p>
Controls the type check for linting.
</p>
</details>

<details>
<summary>format</summary>
<p>
Expand Down
7 changes: 7 additions & 0 deletions packages/@angular/cli/commands/lint.ts
Expand Up @@ -5,6 +5,7 @@ const Command = require('../ember-cli/lib/models/command');

export interface LintCommandOptions {
fix?: boolean;
typeCheck?: boolean;
format?: string;
force?: boolean;
}
Expand All @@ -21,6 +22,12 @@ export default Command.extend({
default: false,
description: 'Fixes linting errors (may overwrite linted files).'
},
{
name: 'type-check',
type: Boolean,
default: false,
description: 'Controls the type check for linting.'
},
{
name: 'force',
type: Boolean,
Expand Down
9 changes: 5 additions & 4 deletions packages/@angular/cli/tasks/lint.ts
Expand Up @@ -32,11 +32,12 @@ export default Task.extend({
.map((config) => {
const program: ts.Program = Linter.createProgram(config.project);
const files = getFilesToLint(program, config, Linter);

const linter = new Linter({
const lintOptions = {
fix: commandOptions.fix,
formatter: commandOptions.format
}, program);
};
const lintProgram = commandOptions.typeCheck ? program : undefined;
const linter = new Linter(lintOptions, lintProgram);

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

if (result.failures.length > 0) {
Expand Down
39 changes: 39 additions & 0 deletions tests/e2e/tests/lint/lint-with-type-check-fail.ts
@@ -0,0 +1,39 @@
import { ng } from '../../utils/process';
import { expectToFail } from '../../utils/utils';
import { writeFile } from '../../utils/fs';

export default function () {
const fileName = 'src/app/foo.ts';
const fileContents = `
const ANIMATION_CSS_VALUE_REGEX = 'asda';
const a = ["asda", 'asda', 'asdasd', "ASDASDAS"];
const b = "asdasd";
const c = {
a: "sadas",
b: {
v: "asdasda",
s: ["asda", "asdas", 10, true, "asda"],
}
};
function check(val: any, fxState: any) {
if (typeof val === "string" && val.indexOf(" ") < 0) {
let r = val.match(ANIMATION_CSS_VALUE_REGEX);
let num = parseFloat(r[1]);
if (!isNaN(num)) {
fxState.num = num + "";
}
fxState.unit = (r[0] !== r[2] ? r[2] : "");
} else if (typeof val === "number") {
fxState.num = val + "";
}
}
`;

return Promise.resolve()
.then(() => writeFile(fileName, fileContents))
.then(() => expectToFail(() => ng('lint', '--fix', '--type-check')));
}
44 changes: 44 additions & 0 deletions tests/e2e/tests/lint/lint-with-type-check.ts
@@ -0,0 +1,44 @@
import { ng } from '../../utils/process';
import { writeFile } from '../../utils/fs';

export default function () {
const fileName = 'src/app/foo.ts';
const fileContents = `
const ANIMATION_CSS_VALUE_REGEX = 'asda';
const a = ["asda", 'asda', 'asdasd', "ASDASDAS"];
const b = "asdasd";
const c = {
a: "sadas",
b: {
v: "asdasda",
s: ["asda", "asdas", 10, true, "asda"],
}
};
function check(val: any, fxState: any) {
if (typeof val === "string" && val.indexOf(" ") < 0) {
let r = val.match(ANIMATION_CSS_VALUE_REGEX);
let num = parseFloat(r[1]);
if (!isNaN(num)) {
fxState.num = num + "";
}
fxState.unit = (r[0] !== r[2] ? r[2] : "");
} else if (typeof val === "number") {
fxState.num = val + "";
}
}
`;

return Promise.resolve()
.then(() => writeFile(fileName, fileContents))
.then(() => ng('lint', '--fix'))
.then(() => ng('lint'))
.then(({ stdout }) => {
if (!stdout.match(/All files pass linting./)) {
throw new Error('All files pass linting.');
}
});
}

0 comments on commit bab9a56

Please sign in to comment.