Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix getTypeAtLocation for dotted implements clauses #39363

Merged
merged 2 commits into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14381,11 +14381,12 @@ namespace ts {
return getTypeFromInferTypeNode(<InferTypeNode>node);
case SyntaxKind.ImportType:
return getTypeFromImportTypeNode(<ImportTypeNode>node);
// This function assumes that an identifier or qualified name is a type expression
// This function assumes that an identifier, qualified name, or proprety access expression is a type expression
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// This function assumes that an identifier, qualified name, or proprety access expression is a type expression
// This function assumes that an identifier, qualified name, or property access expression is a type expression

// Callers should first ensure this by calling `isPartOfTypeNode`
// TODO(rbuckton): These aren't valid TypeNodes, but we treat them as such because of `isPartOfTypeNode`, which returns `true` for things that aren't `TypeNode`s.
case SyntaxKind.Identifier as TypeNodeSyntaxKind:
case SyntaxKind.QualifiedName as TypeNodeSyntaxKind:
case SyntaxKind.PropertyAccessExpression as TypeNodeSyntaxKind:
const symbol = getSymbolAtLocation(node);
return symbol ? getDeclaredTypeOfSymbol(symbol) : errorType;
default:
Expand Down
28 changes: 28 additions & 0 deletions src/testRunner/unittests/publicApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,31 @@ describe("unittests:: Public APIs:: isPropertyName", () => {
assert.isTrue(ts.isPropertyName(prop), "PrivateIdentifier must be a valid property name.");
});
});

describe("unittests:: Public APIs:: getTypeAtLocation", () => {
it("works on PropertyAccessExpression in implements clause", () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would this be easier as a fourslash test? I mean, the answer is probably No, but I thought that quickinfo used getTypeAtLocation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmmmmaaybe? I didn’t think about quick info. I had hoped that the type baseliner would do the job, but it didn’t drill all the way down to the problematic node. Quick info also seems susceptible to future changes in which nodes it calls getTypeAtLocation on, even if it works now. I do wish it was easier to set up this kind of test.

const content = `namespace Test {
export interface Test {}
}
class Foo implements Test.Test {}`;

const host = new fakes.CompilerHost(vfs.createFromFileSystem(
Harness.IO,
/*ignoreCase*/ true,
{ documents: [new documents.TextDocument("/file.ts", content)], cwd: "/" }));

const program = ts.createProgram({
host,
rootNames: ["/file.ts"],
options: { noLib: true }
});

const checker = program.getTypeChecker();
const file = program.getSourceFile("/file.ts")!;
const classDeclaration = file.statements.find(ts.isClassDeclaration)!;
const propertyAccess = classDeclaration.heritageClauses![0].types[0].expression as ts.PropertyAccessExpression;
const type = checker.getTypeAtLocation(propertyAccess);
assert.ok(!(type.flags & ts.TypeFlags.Any));
assert.equal(type, checker.getTypeAtLocation(propertyAccess.name));
});
});