|
1 | 1 | //// [APISample_linter.ts] |
2 | | - |
3 | | -/* |
4 | | - * Note: This test is a public API sample. The sample sources can be found |
5 | | - at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#traversing-the-ast-with-a-little-linter |
6 | | - * Please log a "breaking change" issue for any API breaking change affecting this issue |
7 | | - */ |
8 | | - |
9 | | -declare var process: any; |
10 | | -declare var console: any; |
11 | | -declare var readFileSync: any; |
12 | | - |
13 | | -import * as ts from "typescript"; |
14 | | - |
15 | | -export function delint(sourceFile: ts.SourceFile) { |
16 | | - delintNode(sourceFile); |
17 | | - |
18 | | - function delintNode(node: ts.Node) { |
19 | | - switch (node.kind) { |
20 | | - case ts.SyntaxKind.ForStatement: |
21 | | - case ts.SyntaxKind.ForInStatement: |
22 | | - case ts.SyntaxKind.WhileStatement: |
23 | | - case ts.SyntaxKind.DoStatement: |
24 | | - if ((<ts.IterationStatement>node).statement.kind !== ts.SyntaxKind.Block) { |
25 | | - report(node, "A looping statement's contents should be wrapped in a block body."); |
26 | | - } |
27 | | - break; |
28 | | - |
29 | | - case ts.SyntaxKind.IfStatement: |
30 | | - let ifStatement = (<ts.IfStatement>node); |
31 | | - if (ifStatement.thenStatement.kind !== ts.SyntaxKind.Block) { |
32 | | - report(ifStatement.thenStatement, "An if statement's contents should be wrapped in a block body."); |
33 | | - } |
34 | | - if (ifStatement.elseStatement && |
35 | | - ifStatement.elseStatement.kind !== ts.SyntaxKind.Block && |
36 | | - ifStatement.elseStatement.kind !== ts.SyntaxKind.IfStatement) { |
37 | | - report(ifStatement.elseStatement, "An else statement's contents should be wrapped in a block body."); |
38 | | - } |
39 | | - break; |
40 | | - |
41 | | - case ts.SyntaxKind.BinaryExpression: |
42 | | - let op = (<ts.BinaryExpression>node).operatorToken.kind; |
43 | | - if (op === ts.SyntaxKind.EqualsEqualsToken || op == ts.SyntaxKind.ExclamationEqualsToken) { |
44 | | - report(node, "Use '===' and '!=='.") |
45 | | - } |
46 | | - break; |
47 | | - } |
48 | | - |
49 | | - ts.forEachChild(node, delintNode); |
50 | | - } |
51 | | - |
52 | | - function report(node: ts.Node, message: string) { |
53 | | - let { line, character } = sourceFile.getLineAndCharacterOfPosition(node.getStart()); |
54 | | - console.log(`${sourceFile.fileName} (${line + 1},${character + 1}): ${message}`); |
55 | | - } |
56 | | -} |
57 | | - |
58 | | -const fileNames = process.argv.slice(2); |
59 | | -fileNames.forEach(fileName => { |
60 | | - // Parse a file |
61 | | - let sourceFile = ts.createSourceFile(fileName, readFileSync(fileName).toString(), ts.ScriptTarget.ES6, /*setParentNodes */ true); |
62 | | - |
63 | | - // delint it |
64 | | - delint(sourceFile); |
| 2 | + |
| 3 | +/* |
| 4 | + * Note: This test is a public API sample. The sample sources can be found |
| 5 | + at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#traversing-the-ast-with-a-little-linter |
| 6 | + * Please log a "breaking change" issue for any API breaking change affecting this issue |
| 7 | + */ |
| 8 | + |
| 9 | +declare var process: any; |
| 10 | +declare var console: any; |
| 11 | +declare var readFileSync: any; |
| 12 | + |
| 13 | +import * as ts from "typescript"; |
| 14 | + |
| 15 | +export function delint(sourceFile: ts.SourceFile) { |
| 16 | + delintNode(sourceFile); |
| 17 | + |
| 18 | + function delintNode(node: ts.Node) { |
| 19 | + switch (node.kind) { |
| 20 | + case ts.SyntaxKind.ForStatement: |
| 21 | + case ts.SyntaxKind.ForInStatement: |
| 22 | + case ts.SyntaxKind.WhileStatement: |
| 23 | + case ts.SyntaxKind.DoStatement: |
| 24 | + if ((<ts.IterationStatement>node).statement.kind !== ts.SyntaxKind.Block) { |
| 25 | + report(node, "A looping statement's contents should be wrapped in a block body."); |
| 26 | + } |
| 27 | + break; |
| 28 | + |
| 29 | + case ts.SyntaxKind.IfStatement: |
| 30 | + let ifStatement = (<ts.IfStatement>node); |
| 31 | + if (ifStatement.thenStatement.kind !== ts.SyntaxKind.Block) { |
| 32 | + report(ifStatement.thenStatement, "An if statement's contents should be wrapped in a block body."); |
| 33 | + } |
| 34 | + if (ifStatement.elseStatement && |
| 35 | + ifStatement.elseStatement.kind !== ts.SyntaxKind.Block && |
| 36 | + ifStatement.elseStatement.kind !== ts.SyntaxKind.IfStatement) { |
| 37 | + report(ifStatement.elseStatement, "An else statement's contents should be wrapped in a block body."); |
| 38 | + } |
| 39 | + break; |
| 40 | + |
| 41 | + case ts.SyntaxKind.BinaryExpression: |
| 42 | + let op = (<ts.BinaryExpression>node).operatorToken.kind; |
| 43 | + if (op === ts.SyntaxKind.EqualsEqualsToken || op == ts.SyntaxKind.ExclamationEqualsToken) { |
| 44 | + report(node, "Use '===' and '!=='.") |
| 45 | + } |
| 46 | + break; |
| 47 | + } |
| 48 | + |
| 49 | + ts.forEachChild(node, delintNode); |
| 50 | + } |
| 51 | + |
| 52 | + function report(node: ts.Node, message: string) { |
| 53 | + let { line, character } = sourceFile.getLineAndCharacterOfPosition(node.getStart()); |
| 54 | + console.log(`${sourceFile.fileName} (${line + 1},${character + 1}): ${message}`); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +const fileNames = process.argv.slice(2); |
| 59 | +fileNames.forEach(fileName => { |
| 60 | + // Parse a file |
| 61 | + let sourceFile = ts.createSourceFile(fileName, readFileSync(fileName).toString(), ts.ScriptTarget.ES6, /*setParentNodes */ true); |
| 62 | + |
| 63 | + // delint it |
| 64 | + delint(sourceFile); |
65 | 65 | }); |
66 | 66 |
|
67 | 67 | //// [APISample_linter.js] |
|
0 commit comments