mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-07-02 14:48:32 -05:00
Make the harness invoke typeWriter
This commit is contained in:
@@ -52,6 +52,8 @@ class CompilerBaselineRunner extends RunnerBase {
|
||||
var rootDir = lastUnit.originalFilePath.indexOf('conformance') === -1 ? 'tests/cases/compiler/' : lastUnit.originalFilePath.substring(0, lastUnit.originalFilePath.lastIndexOf('/')) + '/';
|
||||
|
||||
var result: Harness.Compiler.CompilerResult;
|
||||
var program: ts.Program;
|
||||
var checker: ts.TypeChecker;
|
||||
var options: ts.CompilerOptions;
|
||||
// equivalent to the files that will be passed on the command line
|
||||
var toBeCompiled: { unitName: string; content: string }[];
|
||||
@@ -85,8 +87,11 @@ class CompilerBaselineRunner extends RunnerBase {
|
||||
});
|
||||
}
|
||||
|
||||
options = harnessCompiler.compileFiles(toBeCompiled, otherFiles, function (compileResult) {
|
||||
options = harnessCompiler.compileFiles(toBeCompiled, otherFiles, function (compileResult, _program, _checker) {
|
||||
result = compileResult;
|
||||
// The program and checker will be used by typeWriter
|
||||
program = _program;
|
||||
checker = _checker;
|
||||
}, function (settings) {
|
||||
harnessCompiler.setCompilerSettings(tcSettings);
|
||||
});
|
||||
@@ -97,6 +102,7 @@ class CompilerBaselineRunner extends RunnerBase {
|
||||
a fresh compiler instance for themselves and then create a fresh one for the next test. Would be nice to get dev fixes
|
||||
eventually to remove this limitation. */
|
||||
for (var i = 0; i < tcSettings.length; ++i) {
|
||||
// noImplicitAny is passed to getCompiler, but target is just passed in the settings blob to setCompilerSettings
|
||||
if (!createNewInstance && (tcSettings[i].flag == "noimplicitany" || tcSettings[i].flag === 'target')) {
|
||||
harnessCompiler = Harness.Compiler.getCompiler({
|
||||
useExistingInstance: false,
|
||||
@@ -251,32 +257,15 @@ class CompilerBaselineRunner extends RunnerBase {
|
||||
|
||||
it('Correct type baselines for ' + fileName, () => {
|
||||
// NEWTODO: Type baselines
|
||||
if (/* ! */ false && /* ! */ result.errors.length === 0) {
|
||||
if (result.errors.length === 0) {
|
||||
Harness.Baseline.runBaseline('Correct expression types for ' + fileName, justName.replace(/\.ts/, '.types'), () => {
|
||||
// TODO: Rewrite this part
|
||||
//var compiler = new TypeScript.TypeScriptCompiler(
|
||||
// new TypeScript.NullLogger(), TypeScript.ImmutableCompilationSettings.defaultSettings());
|
||||
|
||||
//compiler.addFile('lib.d.ts', TypeScript.ScriptSnapshot.fromString(Harness.Compiler.libTextMinimal),
|
||||
// TypeScript.ByteOrderMark.None, /*version:*/ "0", /*isOpen:*/ true);
|
||||
|
||||
//var allFiles = toBeCompiled.concat(otherFiles);
|
||||
//allFiles.forEach(file => {
|
||||
// compiler.addFile(file.unitName, TypeScript.ScriptSnapshot.fromString(file.content),
|
||||
// TypeScript.ByteOrderMark.None, /*version:*/ "0", /*isOpen:*/ true);
|
||||
//});
|
||||
|
||||
var allFiles: any[] = [];
|
||||
var compiler: any = undefined;
|
||||
|
||||
var typeBaselineText = '';
|
||||
var allFiles = toBeCompiled.concat(otherFiles);
|
||||
var typeLines: string[] = [];
|
||||
var typeMap: { [fileName: string]: { [lineNum: number]: string[]; } } = {};
|
||||
var walker = new TypeWriterWalker(program, checker);
|
||||
allFiles.forEach(file => {
|
||||
var codeLines = file.content.split('\n');
|
||||
var walker = new TypeWriterWalker(file.unitName, compiler);
|
||||
walker.run();
|
||||
walker.results.forEach(result => {
|
||||
walker.getTypes(file.unitName).forEach(result => {
|
||||
var formattedLine = result.identifierName + " : " + result.type;
|
||||
if (!typeMap[file.unitName]) {
|
||||
typeMap[file.unitName] = {};
|
||||
@@ -290,23 +279,6 @@ class CompilerBaselineRunner extends RunnerBase {
|
||||
typeMap[file.unitName][result.line] = typeInfo;
|
||||
});
|
||||
|
||||
var typeBaselineText = '';
|
||||
var typeLines: string[] = [];
|
||||
var typeMap: { [fileName: string]: { [lineNum: number]: string[]; } } = {};
|
||||
allFiles.forEach(file => {
|
||||
var codeLines = file.content.split('\n');
|
||||
var walker = new TypeWriterWalker(file.unitName, compiler);
|
||||
walker.run();
|
||||
walker.results.forEach(result => {
|
||||
var formattedLine = result.identifierName + " : " + result.type;
|
||||
if (!typeMap[file.unitName]) {
|
||||
typeMap[file.unitName] = {};
|
||||
} else {
|
||||
typeLines.push('No type information for this code.');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
typeLines.push('=== ' + file.unitName + ' ===\r\n');
|
||||
for (var i = 0; i < codeLines.length; i++) {
|
||||
var currentCodeLine = codeLines[i];
|
||||
|
||||
@@ -609,7 +609,7 @@ module Harness {
|
||||
|
||||
public compileFiles(inputFiles: { unitName: string; content: string }[],
|
||||
otherFiles: { unitName: string; content?: string }[],
|
||||
onComplete: (result: CompilerResult) => void,
|
||||
onComplete: (result: CompilerResult, program: ts.Program, checker: ts.TypeChecker) => void,
|
||||
settingsCallback?: (settings: ts.CompilerOptions) => void,
|
||||
options?: ts.CompilerOptions) {
|
||||
|
||||
@@ -755,7 +755,7 @@ module Harness {
|
||||
var result = new CompilerResult(fileOutputs, errors, []);
|
||||
// Covert the source Map data into the baseline
|
||||
result.updateSourceMapRecord(program, emitResult ? emitResult.sourceMaps : undefined);
|
||||
onComplete(result);
|
||||
onComplete(result, program, checker);
|
||||
|
||||
// reset what newline means in case the last test changed it
|
||||
sys.newLine = '\r\n';
|
||||
|
||||
@@ -1,180 +1,15 @@
|
||||
interface TypeWriterResult {
|
||||
line: number;
|
||||
column: number;
|
||||
syntaxKind: string;
|
||||
identifierName: string;
|
||||
type: string;
|
||||
}
|
||||
|
||||
/** TODO: Rewrite entirely **/
|
||||
class TypeWriterWalker {
|
||||
constructor(public filename: string, public compiler: any) {
|
||||
constructor(public program: ts.Program, public checker: ts.TypeChecker) {
|
||||
}
|
||||
public run() { }
|
||||
public results: any[];
|
||||
}
|
||||
|
||||
/*
|
||||
class TypeWriterWalker extends TypeScript.SyntaxWalker {
|
||||
private document: TypeScript.Document;
|
||||
private syntaxTree: TypeScript.SyntaxTree;
|
||||
private text: TypeScript.ISimpleText;
|
||||
private currentPosition = 0;
|
||||
|
||||
public results: {
|
||||
line: number;
|
||||
column: number;
|
||||
syntaxKind: string;
|
||||
identifierName: string;
|
||||
type: string;
|
||||
}[] = [];
|
||||
|
||||
constructor(public filename: string, public compiler: TypeScript.TypeScriptCompiler) {
|
||||
super();
|
||||
|
||||
this.document = compiler.getDocument(filename);
|
||||
this.syntaxTree = this.document.syntaxTree();
|
||||
this.text = this.syntaxTree.text;
|
||||
}
|
||||
|
||||
public run() {
|
||||
TypeScript.visitNodeOrToken(this, this.syntaxTree.sourceUnit());
|
||||
}
|
||||
|
||||
private isName(token: TypeScript.ISyntaxToken) {
|
||||
var parent = token.parent;
|
||||
|
||||
switch (parent.kind()) {
|
||||
case TypeScript.SyntaxKind.ContinueStatement:
|
||||
return (<TypeScript.ContinueStatementSyntax>parent).identifier === token;
|
||||
case TypeScript.SyntaxKind.BreakStatement:
|
||||
return (<TypeScript.BreakStatementSyntax>parent).identifier === token;
|
||||
case TypeScript.SyntaxKind.LabeledStatement:
|
||||
return (<TypeScript.LabeledStatementSyntax>parent).identifier === token;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public visitToken(token: TypeScript.ISyntaxToken) {
|
||||
if (token.kind() === TypeScript.SyntaxKind.IdentifierName) {
|
||||
if (!this.isName(token)) {
|
||||
this.log(token);
|
||||
}
|
||||
} else if (token.kind() === TypeScript.SyntaxKind.ThisKeyword) {
|
||||
this.log(token);
|
||||
}
|
||||
return super.visitToken(token);
|
||||
}
|
||||
|
||||
public visitNode(node: TypeScript.ISyntaxNode) {
|
||||
return super.visitNode(node);
|
||||
}
|
||||
|
||||
private getAstForElement(element: TypeScript.ISyntaxElement) {
|
||||
if (!TypeScript.isShared(element)) {
|
||||
return element;
|
||||
}
|
||||
}
|
||||
|
||||
private getEnclosingScopeSymbol(ast: TypeScript.ISyntaxElement): TypeScript.PullSymbol {
|
||||
var enclosingScopeAST = TypeScript.DeclarationEmitter.getEnclosingContainer(ast);
|
||||
if (enclosingScopeAST) {
|
||||
var typeInfo = this.compiler.pullGetSymbolInformationFromAST(enclosingScopeAST, this.document);
|
||||
return typeInfo ? typeInfo.symbol : null;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private getTypeOfElement(element: TypeScript.ISyntaxElement) {
|
||||
var ast = this.getAstForElement(element);
|
||||
if (ast) {
|
||||
var typeInfo = this.compiler.pullGetSymbolInformationFromAST(ast, this.document);
|
||||
if (typeInfo.symbol && typeInfo.symbol.type) {
|
||||
var enclosingScope = this.getEnclosingScopeSymbol(ast);
|
||||
return typeInfo.symbol.type.toString(enclosingScope);
|
||||
}
|
||||
}
|
||||
|
||||
return "<unknown>";
|
||||
}
|
||||
|
||||
public visitPrefixUnaryExpression(node: TypeScript.PrefixUnaryExpressionSyntax) {
|
||||
this.log(node);
|
||||
return super.visitPrefixUnaryExpression(node);
|
||||
}
|
||||
public visitArrayLiteralExpression(node: TypeScript.ArrayLiteralExpressionSyntax) {
|
||||
this.log(node);
|
||||
return super.visitArrayLiteralExpression(node);
|
||||
}
|
||||
public visitOmittedExpression(node: TypeScript.OmittedExpressionSyntax) {
|
||||
this.log(node);
|
||||
return super.visitOmittedExpression(node);
|
||||
}
|
||||
public visitParenthesizedExpression(node: TypeScript.ParenthesizedExpressionSyntax) {
|
||||
this.log(node);
|
||||
return super.visitParenthesizedExpression(node);
|
||||
}
|
||||
public visitSimpleArrowFunctionExpression(node: TypeScript.SimpleArrowFunctionExpressionSyntax) {
|
||||
this.log(node);
|
||||
return super.visitSimpleArrowFunctionExpression(node);
|
||||
}
|
||||
public visitParenthesizedArrowFunctionExpression(node: TypeScript.ParenthesizedArrowFunctionExpressionSyntax) {
|
||||
this.log(node);
|
||||
return super.visitParenthesizedArrowFunctionExpression(node);
|
||||
}
|
||||
public visitObjectCreationExpression(node: TypeScript.ObjectCreationExpressionSyntax) {
|
||||
this.log(node);
|
||||
return super.visitObjectCreationExpression(node);
|
||||
}
|
||||
public visitCastExpression(node: TypeScript.CastExpressionSyntax) {
|
||||
this.log(node);
|
||||
return super.visitCastExpression(node);
|
||||
}
|
||||
public visitObjectLiteralExpression(node: TypeScript.ObjectLiteralExpressionSyntax) {
|
||||
this.log(node);
|
||||
return super.visitObjectLiteralExpression(node);
|
||||
}
|
||||
public visitFunctionExpression(node: TypeScript.FunctionExpressionSyntax) {
|
||||
this.log(node);
|
||||
return super.visitFunctionExpression(node);
|
||||
}
|
||||
public visitTypeOfExpression(node: TypeScript.TypeOfExpressionSyntax) {
|
||||
this.log(node);
|
||||
return super.visitTypeOfExpression(node);
|
||||
}
|
||||
public visitDeleteExpression(node: TypeScript.DeleteExpressionSyntax) {
|
||||
this.log(node);
|
||||
return super.visitDeleteExpression(node);
|
||||
}
|
||||
public visitVoidExpression(node: TypeScript.VoidExpressionSyntax) {
|
||||
this.log(node);
|
||||
return super.visitVoidExpression(node);
|
||||
}
|
||||
public visitMemberAccessExpression(node: TypeScript.MemberAccessExpressionSyntax) {
|
||||
this.log(node);
|
||||
return super.visitMemberAccessExpression(node);
|
||||
}
|
||||
public visitPostfixUnaryExpression(node: TypeScript.PostfixUnaryExpressionSyntax) {
|
||||
this.log(node);
|
||||
return super.visitPostfixUnaryExpression(node);
|
||||
}
|
||||
public visitElementAccessExpression(node: TypeScript.ElementAccessExpressionSyntax) {
|
||||
this.log(node);
|
||||
return super.visitElementAccessExpression(node);
|
||||
}
|
||||
public visitInvocationExpression(node: TypeScript.InvocationExpressionSyntax) {
|
||||
this.log(node);
|
||||
return super.visitInvocationExpression(node);
|
||||
}
|
||||
public visitBinaryExpression(node: TypeScript.BinaryExpressionSyntax) {
|
||||
this.log(node);
|
||||
return super.visitBinaryExpression(node);
|
||||
}
|
||||
public visitConditionalExpression(node: TypeScript.ConditionalExpressionSyntax) {
|
||||
this.log(node);
|
||||
return super.visitConditionalExpression(node);
|
||||
}
|
||||
|
||||
public log(node: TypeScript.ISyntaxNodeOrToken) {
|
||||
var _fullStart = TypeScript.fullStart(node);
|
||||
if (_fullStart >= 0) {
|
||||
var pos = this.document.lineMap().getLineAndCharacterFromPosition(_fullStart);
|
||||
this.results.push({ line: pos.line(), column: pos.character(), syntaxKind: TypeScript.SyntaxKind[node.kind()], identifierName: TypeScript.fullText(node, this.text).trim(), type: this.getTypeOfElement(node) });
|
||||
}
|
||||
public getTypes(fileName: string): TypeWriterResult[] {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
*/
|
||||
Reference in New Issue
Block a user