Implement getNameOrDottedNameSpan for new compiler

This commit is contained in:
Sheetal Nandi
2014-10-20 23:34:43 -07:00
parent 4a8a8920a2
commit 308670c9ce
8 changed files with 1540 additions and 254 deletions

View File

@@ -987,10 +987,7 @@ module FourSlash {
private alignmentForExtraInfo = 50;
public getBreakpointStatementLocation(pos: number, prefixString: string) {
this.taoInvalidReason = 'getBreakpointStatementLocation NYI';
var spanInfo = this.languageService.getBreakpointStatementAtPosition(this.activeFile.fileName, pos);
private spanInfoToString(pos: number, spanInfo: TypeScript.TextSpan, prefixString: string) {
var resultString = "SpanInfo: " + JSON.stringify(spanInfo);
if (spanInfo) {
var spanString = this.activeFile.content.substr(spanInfo.start(), spanInfo.length());
@@ -1003,9 +1000,72 @@ module FourSlash {
}
resultString += "\n" + prefixString + ":=> (" + this.getLineColStringAtPosition(spanInfo.start()) + ") to (" + this.getLineColStringAtPosition(spanInfo.end()) + ")";
}
return resultString;
}
private baselineCurrentFileLocations(getSpanAtPos: (pos: number) => TypeScript.TextSpan): string {
var fileLineMap = ts.getLineStarts(this.activeFile.content);
var nextLine = 0;
var resultString = "";
var currentLine: string;
var previousSpanInfo: string;
var startColumn: number;
var length: number;
var prefixString = " >";
var addSpanInfoString = () => {
if (previousSpanInfo) {
resultString += currentLine;
var thisLineMarker = repeatString(startColumn, " ") + repeatString(length, "~");
thisLineMarker += repeatString(this.alignmentForExtraInfo - thisLineMarker.length - prefixString.length + 1, " ");
resultString += thisLineMarker;
resultString += "=> Pos: (" + (pos - length) + " to " + (pos - 1) + ") ";
resultString += " " + previousSpanInfo;
previousSpanInfo = undefined;
}
};
for (var pos = 0; pos < this.activeFile.content.length; pos++) {
if (pos === 0 || pos === fileLineMap[nextLine]) {
nextLine++;
addSpanInfoString();
if (resultString.length) {
resultString += "\n--------------------------------";
}
currentLine = "\n" + nextLine.toString() + repeatString(3 - nextLine.toString().length, " ") + ">" + this.activeFile.content.substring(pos, fileLineMap[nextLine]) + "\n ";
startColumn = 0;
length = 0;
}
var spanInfo = this.spanInfoToString(pos, getSpanAtPos(pos), prefixString);
if (previousSpanInfo && previousSpanInfo !== spanInfo) {
addSpanInfoString();
previousSpanInfo = spanInfo;
startColumn = startColumn + length;
length = 1;
}
else {
previousSpanInfo = spanInfo;
length++;
}
}
addSpanInfoString();
return resultString;
function repeatString(count: number, char: string) {
var result = "";
for (var i = 0; i < count; i++) {
result += char;
}
return result;
}
}
public getBreakpointStatementLocation(pos: number) {
this.taoInvalidReason = 'getBreakpointStatementLocation NYI';
return this.languageService.getBreakpointStatementAtPosition(this.activeFile.fileName, pos);
}
public baselineCurrentFileBreakpointLocations() {
this.taoInvalidReason = 'baselineCurrentFileBreakpointLocations impossible';
@@ -1013,60 +1073,7 @@ module FourSlash {
"Breakpoint Locations for " + this.activeFile.fileName,
this.testData.globalOptions[testOptMetadataNames.baselineFile],
() => {
var fileLineMap = ts.getLineStarts(this.activeFile.content);
var nextLine = 0;
var resultString = "";
var currentLine: string;
var previousSpanInfo: string;
var startColumn: number;
var length: number;
var prefixString = " >";
var addSpanInfoString = () => {
if (previousSpanInfo) {
resultString += currentLine;
var thisLineMarker = repeatString(startColumn, " ") + repeatString(length, "~");
thisLineMarker += repeatString(this.alignmentForExtraInfo - thisLineMarker.length - prefixString.length + 1, " ");
resultString += thisLineMarker;
resultString += "=> Pos: (" + (pos - length) + " to " + (pos - 1) + ") ";
resultString += " " + previousSpanInfo;
previousSpanInfo = undefined;
}
};
for (var pos = 0; pos < this.activeFile.content.length; pos++) {
if (pos === 0 || pos === fileLineMap[nextLine]) {
nextLine++;
addSpanInfoString();
if (resultString.length) {
resultString += "\n--------------------------------";
}
currentLine = "\n" + nextLine.toString() + repeatString(3 - nextLine.toString().length, " ") + ">" + this.activeFile.content.substring(pos, fileLineMap[nextLine]) + "\n ";
startColumn = 0;
length = 0;
}
var spanInfo = this.getBreakpointStatementLocation(pos, prefixString);
if (previousSpanInfo && previousSpanInfo !== spanInfo) {
addSpanInfoString();
previousSpanInfo = spanInfo;
startColumn = startColumn + length;
length = 1;
}
else {
previousSpanInfo = spanInfo;
length++;
}
}
addSpanInfoString();
return resultString;
function repeatString(count: number, char: string) {
var result = "";
for (var i = 0; i < count; i++) {
result += char;
}
return result;
}
return this.baselineCurrentFileLocations(pos => this.getBreakpointStatementLocation(pos));
},
true /* run immediately */);
}
@@ -1114,7 +1121,7 @@ module FourSlash {
}
public printBreakpointLocation(pos: number) {
Harness.IO.log("\n**Pos: " + pos + " " + this.getBreakpointStatementLocation(pos, " "));
Harness.IO.log("\n**Pos: " + pos + " " + this.spanInfoToString(pos, this.getBreakpointStatementLocation(pos), " "));
}
public printBreakpointAtCurrentLocation() {
@@ -1624,10 +1631,10 @@ module FourSlash {
this.taoInvalidReason = 'verifyCurrentNameOrDottedNameSpanText NYI';
var span = this.languageService.getNameOrDottedNameSpan(this.activeFile.fileName, this.currentCaretPosition, this.currentCaretPosition);
if (span === null) {
if (!span) {
this.raiseError('verifyCurrentNameOrDottedNameSpanText\n' +
'\tExpected: "' + text + '"\n' +
'\t Actual: null');
'\t Actual: undefined');
}
var actual = this.languageServiceShimHost.getScriptSnapshot(this.activeFile.fileName).getText(span.start(), span.end());
@@ -1639,12 +1646,8 @@ module FourSlash {
}
private getNameOrDottedNameSpan(pos: number) {
var spanInfo = this.languageService.getNameOrDottedNameSpan(this.activeFile.fileName, pos, pos);
var resultString = "\n**Pos: " + pos + " SpanInfo: " + JSON.stringify(spanInfo) + "\n** Statement: ";
if (spanInfo !== null) {
resultString = resultString + this.languageServiceShimHost.getScriptSnapshot(this.activeFile.fileName).getText(spanInfo.start(), spanInfo.end());
}
return resultString;
this.taoInvalidReason = 'getNameOrDottedNameSpan NYI';
return this.languageService.getNameOrDottedNameSpan(this.activeFile.fileName, pos, pos);
}
public baselineCurrentFileNameOrDottedNameSpans() {
@@ -1654,18 +1657,14 @@ module FourSlash {
"Name OrDottedNameSpans for " + this.activeFile.fileName,
this.testData.globalOptions[testOptMetadataNames.baselineFile],
() => {
var fileLength = this.languageServiceShimHost.getScriptSnapshot(this.activeFile.fileName).getLength();
var resultString = "";
for (var pos = 0; pos < fileLength; pos++) {
resultString = resultString + this.getNameOrDottedNameSpan(pos);
}
return resultString;
return this.baselineCurrentFileLocations(pos =>
this.getNameOrDottedNameSpan(pos));
},
true /* run immediately */);
}
public printNameOrDottedNameSpans(pos: number) {
Harness.IO.log(this.getNameOrDottedNameSpan(pos));
Harness.IO.log(this.spanInfoToString(pos, this.getNameOrDottedNameSpan(pos), "**"));
}
private verifyClassifications(expected: { classificationType: string; text: string; textSpan?: TextSpan }[], actual: ts.ClassifiedSpan[]) {

View File

@@ -1950,18 +1950,32 @@ module ts {
return isLabelOfLabeledStatement(node) || isJumpStatementTarget(node);
}
function isRightSideOfQualifiedName(node: Node) {
return node.parent.kind === SyntaxKind.QualifiedName && (<QualifiedName>node.parent).right === node;
}
function isRightSideOfPropertyAccess(node: Node) {
return node.parent.kind === SyntaxKind.PropertyAccess && (<PropertyAccess>node.parent).right === node;
}
function isCallExpressionTarget(node: Node): boolean {
if (node.parent.kind === SyntaxKind.PropertyAccess && (<PropertyAccess>node.parent).right === node)
if (isRightSideOfPropertyAccess(node)) {
node = node.parent;
}
return node.parent.kind === SyntaxKind.CallExpression && (<CallExpression>node.parent).func === node;
}
function isNewExpressionTarget(node: Node): boolean {
if (node.parent.kind === SyntaxKind.PropertyAccess && (<PropertyAccess>node.parent).right === node)
if (isRightSideOfPropertyAccess(node)) {
node = node.parent;
}
return node.parent.kind === SyntaxKind.NewExpression && (<CallExpression>node.parent).func === node;
}
function isNameOfModuleDeclaration(node: Node) {
return node.parent.kind === SyntaxKind.ModuleDeclaration && (<ModuleDeclaration>node.parent).name === node;
}
function isNameOfFunctionDeclaration(node: Node): boolean {
return node.kind === SyntaxKind.Identifier &&
isAnyFunction(node.parent) && (<FunctionDeclaration>node.parent).name === node;
@@ -1994,7 +2008,7 @@ module ts {
function isNameOfExternalModuleImportOrDeclaration(node: Node): boolean {
return node.kind === SyntaxKind.StringLiteral &&
((node.parent.kind === SyntaxKind.ModuleDeclaration && (<ModuleDeclaration>node.parent).name === node) ||
(isNameOfModuleDeclaration(node) ||
(node.parent.kind === SyntaxKind.ImportDeclaration && (<ImportDeclaration>node.parent).externalModuleName === node));
}
@@ -4528,8 +4542,9 @@ module ts {
}
function isTypeReference(node: Node): boolean {
if (node.parent.kind === SyntaxKind.QualifiedName && (<QualifiedName>node.parent).right === node)
if (isRightSideOfQualifiedName(node)) {
node = node.parent;
}
return node.parent.kind === SyntaxKind.TypeReference;
}
@@ -4679,64 +4694,63 @@ module ts {
}
function getNameOrDottedNameSpan(filename: string, startPos: number, endPos: number): TypeScript.TextSpan {
function getTypeInfoEligiblePath(filename: string, position: number, isConstructorValidPosition: boolean) {
var sourceUnit = syntaxTreeCache.getCurrentFileSyntaxTree(filename).sourceUnit();
filename = ts.normalizeSlashes(filename);
// Get node at the location
var node = getTouchingPropertyName(getCurrentSourceFile(filename), startPos);
var ast = TypeScript.ASTHelpers.getAstAtPosition(sourceUnit, position, /*useTrailingTriviaAsLimChar*/ false, /*forceInclusive*/ true);
if (ast === null) {
return null;
}
if (ast.kind() === TypeScript.SyntaxKind.ParameterList && ast.parent.kind() === TypeScript.SyntaxKind.CallSignature && ast.parent.parent.kind() === TypeScript.SyntaxKind.ConstructorDeclaration) {
ast = ast.parent.parent;
}
switch (ast.kind()) {
default:
return null;
case TypeScript.SyntaxKind.ConstructorDeclaration:
var constructorAST = <TypeScript.ConstructorDeclarationSyntax>ast;
if (!isConstructorValidPosition || !(position >= TypeScript.start(constructorAST) && position <= TypeScript.start(constructorAST) + "constructor".length)) {
return null;
}
else {
return ast;
}
case TypeScript.SyntaxKind.FunctionDeclaration:
return null;
case TypeScript.SyntaxKind.MemberAccessExpression:
case TypeScript.SyntaxKind.QualifiedName:
case TypeScript.SyntaxKind.SuperKeyword:
case TypeScript.SyntaxKind.StringLiteral:
case TypeScript.SyntaxKind.ThisKeyword:
case TypeScript.SyntaxKind.IdentifierName:
return ast;
}
if (!node) {
return;
}
filename = TypeScript.switchToForwardSlashes(filename);
switch (node.kind) {
case SyntaxKind.PropertyAccess:
case SyntaxKind.QualifiedName:
case SyntaxKind.StringLiteral:
case SyntaxKind.FalseKeyword:
case SyntaxKind.TrueKeyword:
case SyntaxKind.NullKeyword:
case SyntaxKind.SuperKeyword:
case SyntaxKind.ThisKeyword:
case SyntaxKind.Identifier:
break;
var node = getTypeInfoEligiblePath(filename, startPos, false);
if (!node) return null;
// Cant create the text span
default:
return;
}
while (node) {
if (TypeScript.ASTHelpers.isNameOfMemberAccessExpression(node) ||
TypeScript.ASTHelpers.isRightSideOfQualifiedName(node)) {
node = node.parent;
var nodeForStartPos = node;
while (true) {
if (isRightSideOfPropertyAccess(nodeForStartPos) || isRightSideOfQualifiedName(nodeForStartPos)) {
// If on the span is in right side of the the property or qualified name, return the span from the qualified name pos to end of this node
nodeForStartPos = nodeForStartPos.parent;
}
else if (isNameOfModuleDeclaration(nodeForStartPos)) {
// If this is name of a module declarations, check if this is right side of dotted module name
// If parent of the module declaration which is parent of this node is module declaration and its body is the module declaration that this node is name of
// Then this name is name from dotted module
if (nodeForStartPos.parent.parent.kind === SyntaxKind.ModuleDeclaration &&
(<ModuleDeclaration>nodeForStartPos.parent.parent).body === nodeForStartPos.parent) {
// Use parent module declarations name for start pos
nodeForStartPos = (<ModuleDeclaration>nodeForStartPos.parent.parent).name;
}
else {
// We have to use this name for start pos
break;
}
}
else {
// Is not a member expression so we have found the node for start pos
break;
}
}
return TypeScript.TextSpan.fromBounds(
TypeScript.start(node),
TypeScript.end(node));
return TypeScript.TextSpan.fromBounds(nodeForStartPos.getStart(), node.getEnd());
}
function getBreakpointStatementAtPosition(filename: string, position: number) {
// doesn't use compiler - no need to synchronize with host
filename = TypeScript.switchToForwardSlashes(filename);
filename = ts.normalizeSlashes(filename);
return BreakpointResolver.spanInSourceFileAtLocation(getCurrentSourceFile(filename), position);
}