mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-15 12:51:30 -05:00
Update APISample tests for 1.5-alpha release
This commit is contained in:
@@ -21,8 +21,9 @@ export function compile(fileNames: string[], options: ts.CompilerOptions): void
|
||||
var allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
|
||||
|
||||
allDiagnostics.forEach(diagnostic => {
|
||||
var lineChar = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
|
||||
console.log(`${diagnostic.file.fileName} (${lineChar.line + 1},${lineChar.character + 1}): ${ts.flattenDiagnosticMessageText(diagnostic.messageText, os.EOL)}`);
|
||||
var { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
|
||||
var message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
|
||||
console.log(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`);
|
||||
});
|
||||
|
||||
var exitCode = emitResult.emitSkipped ? 1 : 0;
|
||||
|
||||
@@ -10,9 +10,9 @@
|
||||
|
||||
declare var process: any;
|
||||
declare var console: any;
|
||||
declare var fs: any;
|
||||
declare var readFileSync: any;
|
||||
|
||||
import ts = require("typescript");
|
||||
import * as ts from "typescript";
|
||||
|
||||
export function delint(sourceFile: ts.SourceFile) {
|
||||
delintNode(sourceFile);
|
||||
@@ -27,21 +27,22 @@ export function delint(sourceFile: ts.SourceFile) {
|
||||
report(node, "A looping statement's contents should be wrapped in a block body.");
|
||||
}
|
||||
break;
|
||||
|
||||
case ts.SyntaxKind.IfStatement:
|
||||
var ifStatement = (<ts.IfStatement>node);
|
||||
let ifStatement = (<ts.IfStatement>node);
|
||||
if (ifStatement.thenStatement.kind !== ts.SyntaxKind.Block) {
|
||||
report(ifStatement.thenStatement, "An if statement's contents should be wrapped in a block body.");
|
||||
}
|
||||
if (ifStatement.elseStatement &&
|
||||
ifStatement.elseStatement.kind !== ts.SyntaxKind.Block && ifStatement.elseStatement.kind !== ts.SyntaxKind.IfStatement) {
|
||||
ifStatement.elseStatement.kind !== ts.SyntaxKind.Block &&
|
||||
ifStatement.elseStatement.kind !== ts.SyntaxKind.IfStatement) {
|
||||
report(ifStatement.elseStatement, "An else statement's contents should be wrapped in a block body.");
|
||||
}
|
||||
break;
|
||||
|
||||
case ts.SyntaxKind.BinaryExpression:
|
||||
var op = (<ts.BinaryExpression>node).operatorToken.kind;
|
||||
|
||||
if (op === ts.SyntaxKind.EqualsEqualsToken || op === ts.SyntaxKind.ExclamationEqualsToken) {
|
||||
let op = (<ts.BinaryExpression>node).operatorToken.kind;
|
||||
if (op === ts.SyntaxKind.EqualsEqualsToken || op == ts.SyntaxKind.ExclamationEqualsToken) {
|
||||
report(node, "Use '===' and '!=='.")
|
||||
}
|
||||
break;
|
||||
@@ -51,16 +52,16 @@ export function delint(sourceFile: ts.SourceFile) {
|
||||
}
|
||||
|
||||
function report(node: ts.Node, message: string) {
|
||||
var lineChar = sourceFile.getLineAndCharacterOfPosition(node.getStart());
|
||||
console.log(`${sourceFile.fileName} (${lineChar.line + 1},${lineChar.character + 1}): ${message}`)
|
||||
let { line, character } = sourceFile.getLineAndCharacterOfPosition(node.getStart());
|
||||
console.log(`${sourceFile.fileName} (${line + 1},${character + 1}): ${message}`);
|
||||
}
|
||||
}
|
||||
|
||||
var fileNames = process.argv.slice(2);
|
||||
const fileNames = process.argv.slice(2);
|
||||
fileNames.forEach(fileName => {
|
||||
// Parse a file
|
||||
var sourceFile = ts.createSourceFile(fileName, fs.readFileSync(fileName).toString(), ts.ScriptTarget.ES6, /*setParentNodes */ true);
|
||||
let sourceFile = ts.createSourceFile(fileName, readFileSync(fileName).toString(), ts.ScriptTarget.ES6, /*setParentNodes */ true);
|
||||
|
||||
// delint it
|
||||
delint(sourceFile);
|
||||
});
|
||||
});
|
||||
@@ -8,61 +8,12 @@
|
||||
* Please log a "breaking change" issue for any API breaking change affecting this issue
|
||||
*/
|
||||
|
||||
declare var process: any;
|
||||
declare var console: any;
|
||||
declare var fs: any;
|
||||
declare var path: any;
|
||||
declare var os: any;
|
||||
|
||||
import ts = require("typescript");
|
||||
import * as ts from "typescript";
|
||||
|
||||
function transform(contents: string, compilerOptions: ts.CompilerOptions = {}) {
|
||||
// Sources
|
||||
var files = {
|
||||
"file.ts": contents,
|
||||
"lib.d.ts": fs.readFileSync(ts.getDefaultLibFilePath(compilerOptions)).toString()
|
||||
};
|
||||
const source = "let x: string = 'string'";
|
||||
|
||||
// Generated outputs
|
||||
var outputs = [];
|
||||
|
||||
// Create a compilerHost object to allow the compiler to read and write files
|
||||
var compilerHost = {
|
||||
getSourceFile: (fileName, target) => {
|
||||
return files[fileName] !== undefined ?
|
||||
ts.createSourceFile(fileName, files[fileName], target) : undefined;
|
||||
},
|
||||
writeFile: (name, text, writeByteOrderMark) => {
|
||||
outputs.push({ name: name, text: text, writeByteOrderMark: writeByteOrderMark });
|
||||
},
|
||||
getDefaultLibFileName: () => "lib.d.ts",
|
||||
useCaseSensitiveFileNames: () => false,
|
||||
getCanonicalFileName: (fileName) => fileName,
|
||||
getCurrentDirectory: () => "",
|
||||
getNewLine: () => "\n"
|
||||
};
|
||||
|
||||
// Create a program from inputs
|
||||
var program = ts.createProgram(["file.ts"], compilerOptions, compilerHost);
|
||||
|
||||
// Query for early errors
|
||||
var errors = ts.getPreEmitDiagnostics(program);
|
||||
var emitResult = program.emit();
|
||||
|
||||
errors = errors.concat(emitResult.diagnostics);
|
||||
|
||||
return {
|
||||
outputs: outputs,
|
||||
errors: errors.map(function (e) {
|
||||
return e.file.fileName + "(" + (e.file.getLineAndCharacterOfPosition(e.start).line + 1) + "): "
|
||||
+ ts.flattenDiagnosticMessageText(e.messageText, os.EOL);
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
// Calling our transform function using a simple TypeScript variable declarations,
|
||||
// and loading the default library like:
|
||||
var source = "var x: number = 'string'";
|
||||
var result = transform(source);
|
||||
let result = ts.transpile(source, { module: ts.ModuleKind.CommonJS });
|
||||
|
||||
console.log(JSON.stringify(result));
|
||||
@@ -13,10 +13,10 @@ declare var console: any;
|
||||
declare var fs: any;
|
||||
declare var path: any;
|
||||
|
||||
import ts = require("typescript");
|
||||
import * as ts from "typescript";
|
||||
|
||||
function watch(rootFileNames: string[], options: ts.CompilerOptions) {
|
||||
var files: ts.Map<{ version: number }> = {};
|
||||
const files: ts.Map<{ version: number }> = {};
|
||||
|
||||
// initialize the list of files
|
||||
rootFileNames.forEach(fileName => {
|
||||
@@ -24,7 +24,7 @@ function watch(rootFileNames: string[], options: ts.CompilerOptions) {
|
||||
});
|
||||
|
||||
// Create the language service host to allow the LS to communicate with the host
|
||||
var servicesHost: ts.LanguageServiceHost = {
|
||||
const servicesHost: ts.LanguageServiceHost = {
|
||||
getScriptFileNames: () => rootFileNames,
|
||||
getScriptVersion: (fileName) => files[fileName] && files[fileName].version.toString(),
|
||||
getScriptSnapshot: (fileName) => {
|
||||
@@ -40,7 +40,7 @@ function watch(rootFileNames: string[], options: ts.CompilerOptions) {
|
||||
};
|
||||
|
||||
// Create the language service files
|
||||
var services = ts.createLanguageService(servicesHost, ts.createDocumentRegistry())
|
||||
const services = ts.createLanguageService(servicesHost, ts.createDocumentRegistry())
|
||||
|
||||
// Now let's watch the files
|
||||
rootFileNames.forEach(fileName => {
|
||||
@@ -65,7 +65,7 @@ function watch(rootFileNames: string[], options: ts.CompilerOptions) {
|
||||
});
|
||||
|
||||
function emitFile(fileName: string) {
|
||||
var output = services.getEmitOutput(fileName);
|
||||
let output = services.getEmitOutput(fileName);
|
||||
|
||||
if (!output.emitSkipped) {
|
||||
console.log(`Emitting ${fileName}`);
|
||||
@@ -81,24 +81,25 @@ function watch(rootFileNames: string[], options: ts.CompilerOptions) {
|
||||
}
|
||||
|
||||
function logErrors(fileName: string) {
|
||||
var allDiagnostics = services.getCompilerOptionsDiagnostics()
|
||||
let allDiagnostics = services.getCompilerOptionsDiagnostics()
|
||||
.concat(services.getSyntacticDiagnostics(fileName))
|
||||
.concat(services.getSemanticDiagnostics(fileName));
|
||||
|
||||
allDiagnostics.forEach(diagnostic => {
|
||||
let message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n");
|
||||
if (diagnostic.file) {
|
||||
var lineChar = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
|
||||
console.log(` Error ${diagnostic.file.fileName} (${lineChar.line + 1},${lineChar.character + 1}): ${ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n")}`);
|
||||
let { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
|
||||
console.log(` Error ${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`);
|
||||
}
|
||||
else {
|
||||
console.log(` Error: ${diagnostic.messageText}`);
|
||||
console.log(` Error: ${message}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize files constituting the program as all .ts files in the current directory
|
||||
var currentDirectoryFiles = fs.readdirSync(process.cwd()).
|
||||
const currentDirectoryFiles = fs.readdirSync(process.cwd()).
|
||||
filter(fileName=> fileName.length >= 3 && fileName.substr(fileName.length - 3, 3) === ".ts");
|
||||
|
||||
// Start the watcher
|
||||
|
||||
Reference in New Issue
Block a user