303 lines
11 KiB
Text
303 lines
11 KiB
Text
=== tests/cases/compiler/APISample_linter.ts ===
|
|
|
|
/*
|
|
* Note: This test is a public API sample. The sample sources can be found
|
|
at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#traversing-the-ast-with-a-little-linter
|
|
* Please log a "breaking change" issue for any API breaking change affecting this issue
|
|
*/
|
|
|
|
declare var process: any;
|
|
>process : any
|
|
|
|
declare var console: any;
|
|
>console : any
|
|
|
|
declare var readFileSync: any;
|
|
>readFileSync : any
|
|
|
|
import * as ts from "typescript";
|
|
>ts : typeof ts
|
|
|
|
export function delint(sourceFile: ts.SourceFile) {
|
|
>delint : (sourceFile: ts.SourceFile) => void
|
|
>sourceFile : ts.SourceFile
|
|
>ts : unknown
|
|
>SourceFile : ts.SourceFile
|
|
|
|
delintNode(sourceFile);
|
|
>delintNode(sourceFile) : void
|
|
>delintNode : (node: ts.Node) => void
|
|
>sourceFile : ts.SourceFile
|
|
|
|
function delintNode(node: ts.Node) {
|
|
>delintNode : (node: ts.Node) => void
|
|
>node : ts.Node
|
|
>ts : unknown
|
|
>Node : ts.Node
|
|
|
|
switch (node.kind) {
|
|
>node.kind : ts.SyntaxKind
|
|
>node : ts.Node
|
|
>kind : ts.SyntaxKind
|
|
|
|
case ts.SyntaxKind.ForStatement:
|
|
>ts.SyntaxKind.ForStatement : ts.SyntaxKind
|
|
>ts.SyntaxKind : typeof ts.SyntaxKind
|
|
>ts : typeof ts
|
|
>SyntaxKind : typeof ts.SyntaxKind
|
|
>ForStatement : ts.SyntaxKind
|
|
|
|
case ts.SyntaxKind.ForInStatement:
|
|
>ts.SyntaxKind.ForInStatement : ts.SyntaxKind
|
|
>ts.SyntaxKind : typeof ts.SyntaxKind
|
|
>ts : typeof ts
|
|
>SyntaxKind : typeof ts.SyntaxKind
|
|
>ForInStatement : ts.SyntaxKind
|
|
|
|
case ts.SyntaxKind.WhileStatement:
|
|
>ts.SyntaxKind.WhileStatement : ts.SyntaxKind
|
|
>ts.SyntaxKind : typeof ts.SyntaxKind
|
|
>ts : typeof ts
|
|
>SyntaxKind : typeof ts.SyntaxKind
|
|
>WhileStatement : ts.SyntaxKind
|
|
|
|
case ts.SyntaxKind.DoStatement:
|
|
>ts.SyntaxKind.DoStatement : ts.SyntaxKind
|
|
>ts.SyntaxKind : typeof ts.SyntaxKind
|
|
>ts : typeof ts
|
|
>SyntaxKind : typeof ts.SyntaxKind
|
|
>DoStatement : ts.SyntaxKind
|
|
|
|
if ((<ts.IterationStatement>node).statement.kind !== ts.SyntaxKind.Block) {
|
|
>(<ts.IterationStatement>node).statement.kind !== ts.SyntaxKind.Block : boolean
|
|
>(<ts.IterationStatement>node).statement.kind : ts.SyntaxKind
|
|
>(<ts.IterationStatement>node).statement : ts.Statement
|
|
>(<ts.IterationStatement>node) : ts.IterationStatement
|
|
><ts.IterationStatement>node : ts.IterationStatement
|
|
>ts : unknown
|
|
>IterationStatement : ts.IterationStatement
|
|
>node : ts.Node
|
|
>statement : ts.Statement
|
|
>kind : ts.SyntaxKind
|
|
>ts.SyntaxKind.Block : ts.SyntaxKind
|
|
>ts.SyntaxKind : typeof ts.SyntaxKind
|
|
>ts : typeof ts
|
|
>SyntaxKind : typeof ts.SyntaxKind
|
|
>Block : ts.SyntaxKind
|
|
|
|
report(node, "A looping statement's contents should be wrapped in a block body.");
|
|
>report(node, "A looping statement's contents should be wrapped in a block body.") : void
|
|
>report : (node: ts.Node, message: string) => void
|
|
>node : ts.Node
|
|
}
|
|
break;
|
|
|
|
case ts.SyntaxKind.IfStatement:
|
|
>ts.SyntaxKind.IfStatement : ts.SyntaxKind
|
|
>ts.SyntaxKind : typeof ts.SyntaxKind
|
|
>ts : typeof ts
|
|
>SyntaxKind : typeof ts.SyntaxKind
|
|
>IfStatement : ts.SyntaxKind
|
|
|
|
let ifStatement = (<ts.IfStatement>node);
|
|
>ifStatement : ts.IfStatement
|
|
>(<ts.IfStatement>node) : ts.IfStatement
|
|
><ts.IfStatement>node : ts.IfStatement
|
|
>ts : unknown
|
|
>IfStatement : ts.IfStatement
|
|
>node : ts.Node
|
|
|
|
if (ifStatement.thenStatement.kind !== ts.SyntaxKind.Block) {
|
|
>ifStatement.thenStatement.kind !== ts.SyntaxKind.Block : boolean
|
|
>ifStatement.thenStatement.kind : ts.SyntaxKind
|
|
>ifStatement.thenStatement : ts.Statement
|
|
>ifStatement : ts.IfStatement
|
|
>thenStatement : ts.Statement
|
|
>kind : ts.SyntaxKind
|
|
>ts.SyntaxKind.Block : ts.SyntaxKind
|
|
>ts.SyntaxKind : typeof ts.SyntaxKind
|
|
>ts : typeof ts
|
|
>SyntaxKind : typeof ts.SyntaxKind
|
|
>Block : ts.SyntaxKind
|
|
|
|
report(ifStatement.thenStatement, "An if statement's contents should be wrapped in a block body.");
|
|
>report(ifStatement.thenStatement, "An if statement's contents should be wrapped in a block body.") : void
|
|
>report : (node: ts.Node, message: string) => void
|
|
>ifStatement.thenStatement : ts.Statement
|
|
>ifStatement : ts.IfStatement
|
|
>thenStatement : ts.Statement
|
|
}
|
|
if (ifStatement.elseStatement &&
|
|
>ifStatement.elseStatement && ifStatement.elseStatement.kind !== ts.SyntaxKind.Block && ifStatement.elseStatement.kind !== ts.SyntaxKind.IfStatement : boolean
|
|
>ifStatement.elseStatement && ifStatement.elseStatement.kind !== ts.SyntaxKind.Block : boolean
|
|
>ifStatement.elseStatement : ts.Statement
|
|
>ifStatement : ts.IfStatement
|
|
>elseStatement : ts.Statement
|
|
|
|
ifStatement.elseStatement.kind !== ts.SyntaxKind.Block &&
|
|
>ifStatement.elseStatement.kind !== ts.SyntaxKind.Block : boolean
|
|
>ifStatement.elseStatement.kind : ts.SyntaxKind
|
|
>ifStatement.elseStatement : ts.Statement
|
|
>ifStatement : ts.IfStatement
|
|
>elseStatement : ts.Statement
|
|
>kind : ts.SyntaxKind
|
|
>ts.SyntaxKind.Block : ts.SyntaxKind
|
|
>ts.SyntaxKind : typeof ts.SyntaxKind
|
|
>ts : typeof ts
|
|
>SyntaxKind : typeof ts.SyntaxKind
|
|
>Block : ts.SyntaxKind
|
|
|
|
ifStatement.elseStatement.kind !== ts.SyntaxKind.IfStatement) {
|
|
>ifStatement.elseStatement.kind !== ts.SyntaxKind.IfStatement : boolean
|
|
>ifStatement.elseStatement.kind : ts.SyntaxKind
|
|
>ifStatement.elseStatement : ts.Statement
|
|
>ifStatement : ts.IfStatement
|
|
>elseStatement : ts.Statement
|
|
>kind : ts.SyntaxKind
|
|
>ts.SyntaxKind.IfStatement : ts.SyntaxKind
|
|
>ts.SyntaxKind : typeof ts.SyntaxKind
|
|
>ts : typeof ts
|
|
>SyntaxKind : typeof ts.SyntaxKind
|
|
>IfStatement : ts.SyntaxKind
|
|
|
|
report(ifStatement.elseStatement, "An else statement's contents should be wrapped in a block body.");
|
|
>report(ifStatement.elseStatement, "An else statement's contents should be wrapped in a block body.") : void
|
|
>report : (node: ts.Node, message: string) => void
|
|
>ifStatement.elseStatement : ts.Statement
|
|
>ifStatement : ts.IfStatement
|
|
>elseStatement : ts.Statement
|
|
}
|
|
break;
|
|
|
|
case ts.SyntaxKind.BinaryExpression:
|
|
>ts.SyntaxKind.BinaryExpression : ts.SyntaxKind
|
|
>ts.SyntaxKind : typeof ts.SyntaxKind
|
|
>ts : typeof ts
|
|
>SyntaxKind : typeof ts.SyntaxKind
|
|
>BinaryExpression : ts.SyntaxKind
|
|
|
|
let op = (<ts.BinaryExpression>node).operatorToken.kind;
|
|
>op : ts.SyntaxKind
|
|
>(<ts.BinaryExpression>node).operatorToken.kind : ts.SyntaxKind
|
|
>(<ts.BinaryExpression>node).operatorToken : ts.Node
|
|
>(<ts.BinaryExpression>node) : ts.BinaryExpression
|
|
><ts.BinaryExpression>node : ts.BinaryExpression
|
|
>ts : unknown
|
|
>BinaryExpression : ts.BinaryExpression
|
|
>node : ts.Node
|
|
>operatorToken : ts.Node
|
|
>kind : ts.SyntaxKind
|
|
|
|
if (op === ts.SyntaxKind.EqualsEqualsToken || op == ts.SyntaxKind.ExclamationEqualsToken) {
|
|
>op === ts.SyntaxKind.EqualsEqualsToken || op == ts.SyntaxKind.ExclamationEqualsToken : boolean
|
|
>op === ts.SyntaxKind.EqualsEqualsToken : boolean
|
|
>op : ts.SyntaxKind
|
|
>ts.SyntaxKind.EqualsEqualsToken : ts.SyntaxKind
|
|
>ts.SyntaxKind : typeof ts.SyntaxKind
|
|
>ts : typeof ts
|
|
>SyntaxKind : typeof ts.SyntaxKind
|
|
>EqualsEqualsToken : ts.SyntaxKind
|
|
>op == ts.SyntaxKind.ExclamationEqualsToken : boolean
|
|
>op : ts.SyntaxKind
|
|
>ts.SyntaxKind.ExclamationEqualsToken : ts.SyntaxKind
|
|
>ts.SyntaxKind : typeof ts.SyntaxKind
|
|
>ts : typeof ts
|
|
>SyntaxKind : typeof ts.SyntaxKind
|
|
>ExclamationEqualsToken : ts.SyntaxKind
|
|
|
|
report(node, "Use '===' and '!=='.")
|
|
>report(node, "Use '===' and '!=='.") : void
|
|
>report : (node: ts.Node, message: string) => void
|
|
>node : ts.Node
|
|
}
|
|
break;
|
|
}
|
|
|
|
ts.forEachChild(node, delintNode);
|
|
>ts.forEachChild(node, delintNode) : void
|
|
>ts.forEachChild : <T>(node: ts.Node, cbNode: (node: ts.Node) => T, cbNodeArray?: (nodes: ts.Node[]) => T) => T
|
|
>ts : typeof ts
|
|
>forEachChild : <T>(node: ts.Node, cbNode: (node: ts.Node) => T, cbNodeArray?: (nodes: ts.Node[]) => T) => T
|
|
>node : ts.Node
|
|
>delintNode : (node: ts.Node) => void
|
|
}
|
|
|
|
function report(node: ts.Node, message: string) {
|
|
>report : (node: ts.Node, message: string) => void
|
|
>node : ts.Node
|
|
>ts : unknown
|
|
>Node : ts.Node
|
|
>message : string
|
|
|
|
let { line, character } = sourceFile.getLineAndCharacterOfPosition(node.getStart());
|
|
>line : number
|
|
>character : number
|
|
>sourceFile.getLineAndCharacterOfPosition(node.getStart()) : ts.LineAndCharacter
|
|
>sourceFile.getLineAndCharacterOfPosition : (pos: number) => ts.LineAndCharacter
|
|
>sourceFile : ts.SourceFile
|
|
>getLineAndCharacterOfPosition : (pos: number) => ts.LineAndCharacter
|
|
>node.getStart() : number
|
|
>node.getStart : (sourceFile?: ts.SourceFile) => number
|
|
>node : ts.Node
|
|
>getStart : (sourceFile?: ts.SourceFile) => number
|
|
|
|
console.log(`${sourceFile.fileName} (${line + 1},${character + 1}): ${message}`);
|
|
>console.log(`${sourceFile.fileName} (${line + 1},${character + 1}): ${message}`) : any
|
|
>console.log : any
|
|
>console : any
|
|
>log : any
|
|
>sourceFile.fileName : string
|
|
>sourceFile : ts.SourceFile
|
|
>fileName : string
|
|
>line + 1 : number
|
|
>line : number
|
|
>character + 1 : number
|
|
>character : number
|
|
>message : string
|
|
}
|
|
}
|
|
|
|
const fileNames = process.argv.slice(2);
|
|
>fileNames : any
|
|
>process.argv.slice(2) : any
|
|
>process.argv.slice : any
|
|
>process.argv : any
|
|
>process : any
|
|
>argv : any
|
|
>slice : any
|
|
|
|
fileNames.forEach(fileName => {
|
|
>fileNames.forEach(fileName => { // Parse a file let sourceFile = ts.createSourceFile(fileName, readFileSync(fileName).toString(), ts.ScriptTarget.ES6, /*setParentNodes */ true); // delint it delint(sourceFile);}) : any
|
|
>fileNames.forEach : any
|
|
>fileNames : any
|
|
>forEach : any
|
|
>fileName => { // Parse a file let sourceFile = ts.createSourceFile(fileName, readFileSync(fileName).toString(), ts.ScriptTarget.ES6, /*setParentNodes */ true); // delint it delint(sourceFile);} : (fileName: any) => void
|
|
>fileName : any
|
|
|
|
// Parse a file
|
|
let sourceFile = ts.createSourceFile(fileName, readFileSync(fileName).toString(), ts.ScriptTarget.ES6, /*setParentNodes */ true);
|
|
>sourceFile : ts.SourceFile
|
|
>ts.createSourceFile(fileName, readFileSync(fileName).toString(), ts.ScriptTarget.ES6, /*setParentNodes */ true) : ts.SourceFile
|
|
>ts.createSourceFile : (fileName: string, sourceText: string, languageVersion: ts.ScriptTarget, setParentNodes?: boolean) => ts.SourceFile
|
|
>ts : typeof ts
|
|
>createSourceFile : (fileName: string, sourceText: string, languageVersion: ts.ScriptTarget, setParentNodes?: boolean) => ts.SourceFile
|
|
>fileName : any
|
|
>readFileSync(fileName).toString() : any
|
|
>readFileSync(fileName).toString : any
|
|
>readFileSync(fileName) : any
|
|
>readFileSync : any
|
|
>fileName : any
|
|
>toString : any
|
|
>ts.ScriptTarget.ES6 : ts.ScriptTarget
|
|
>ts.ScriptTarget : typeof ts.ScriptTarget
|
|
>ts : typeof ts
|
|
>ScriptTarget : typeof ts.ScriptTarget
|
|
>ES6 : ts.ScriptTarget
|
|
|
|
// delint it
|
|
delint(sourceFile);
|
|
>delint(sourceFile) : void
|
|
>delint : (sourceFile: ts.SourceFile) => void
|
|
>sourceFile : ts.SourceFile
|
|
|
|
});
|