Skip to content

Support semantic classification of alias #20012

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

Merged
2 commits merged into from
Nov 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
97 changes: 54 additions & 43 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,52 +16,63 @@ namespace ts {
referenced: boolean;
}

export function getModuleInstanceState(node: Node): ModuleInstanceState {
export function getModuleInstanceState(node: ModuleDeclaration): ModuleInstanceState {
return node.body ? getModuleInstanceStateWorker(node.body) : ModuleInstanceState.Instantiated;
}

function getModuleInstanceStateWorker(node: Node): ModuleInstanceState {
// A module is uninstantiated if it contains only
// 1. interface declarations, type alias declarations
if (node.kind === SyntaxKind.InterfaceDeclaration || node.kind === SyntaxKind.TypeAliasDeclaration) {
return ModuleInstanceState.NonInstantiated;
}
// 2. const enum declarations
else if (isConstEnumDeclaration(node)) {
return ModuleInstanceState.ConstEnumOnly;
}
// 3. non-exported import declarations
else if ((node.kind === SyntaxKind.ImportDeclaration || node.kind === SyntaxKind.ImportEqualsDeclaration) && !(hasModifier(node, ModifierFlags.Export))) {
return ModuleInstanceState.NonInstantiated;
}
// 4. other uninstantiated module declarations.
else if (node.kind === SyntaxKind.ModuleBlock) {
let state = ModuleInstanceState.NonInstantiated;
forEachChild(node, n => {
switch (getModuleInstanceState(n)) {
case ModuleInstanceState.NonInstantiated:
// child is non-instantiated - continue searching
return false;
case ModuleInstanceState.ConstEnumOnly:
// child is const enum only - record state and continue searching
state = ModuleInstanceState.ConstEnumOnly;
return false;
case ModuleInstanceState.Instantiated:
// child is instantiated - record state and stop
state = ModuleInstanceState.Instantiated;
return true;
switch (node.kind) {
// 1. interface declarations, type alias declarations
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.TypeAliasDeclaration:
return ModuleInstanceState.NonInstantiated;
// 2. const enum declarations
case SyntaxKind.EnumDeclaration:
if (isConst(node)) {
return ModuleInstanceState.ConstEnumOnly;
}
break;
// 3. non-exported import declarations
case SyntaxKind.ImportDeclaration:
case SyntaxKind.ImportEqualsDeclaration:
if (!(hasModifier(node, ModifierFlags.Export))) {
return ModuleInstanceState.NonInstantiated;
}
break;
// 4. other uninstantiated module declarations.
case SyntaxKind.ModuleBlock: {
let state = ModuleInstanceState.NonInstantiated;
forEachChild(node, n => {
const childState = getModuleInstanceStateWorker(n);
switch (childState) {
case ModuleInstanceState.NonInstantiated:
// child is non-instantiated - continue searching
return;
case ModuleInstanceState.ConstEnumOnly:
// child is const enum only - record state and continue searching
state = ModuleInstanceState.ConstEnumOnly;
return;
case ModuleInstanceState.Instantiated:
// child is instantiated - record state and stop
state = ModuleInstanceState.Instantiated;
return true;
default:
Debug.assertNever(childState);
}
});
return state;
}
case SyntaxKind.ModuleDeclaration:
return getModuleInstanceState(node as ModuleDeclaration);
case SyntaxKind.Identifier:
// Only jsdoc typedef definition can exist in jsdoc namespace, and it should
// be considered the same as type alias
if ((<Identifier>node).isInJSDocNamespace) {
return ModuleInstanceState.NonInstantiated;
}
});
return state;
}
else if (node.kind === SyntaxKind.ModuleDeclaration) {
const body = (<ModuleDeclaration>node).body;
return body ? getModuleInstanceState(body) : ModuleInstanceState.Instantiated;
}
// Only jsdoc typedef definition can exist in jsdoc namespace, and it should
// be considered the same as type alias
else if (node.kind === SyntaxKind.Identifier && (<Identifier>node).isInJSDocNamespace) {
return ModuleInstanceState.NonInstantiated;
}
else {
return ModuleInstanceState.Instantiated;
}
return ModuleInstanceState.Instantiated;
}

const enum ContainerFlags {
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20178,7 +20178,7 @@ namespace ts {
case SyntaxKind.JSDocTypedefTag:
return DeclarationSpaces.ExportType;
case SyntaxKind.ModuleDeclaration:
return isAmbientModule(d) || getModuleInstanceState(d) !== ModuleInstanceState.NonInstantiated
return isAmbientModule(d as ModuleDeclaration) || getModuleInstanceState(d as ModuleDeclaration) !== ModuleInstanceState.NonInstantiated
? DeclarationSpaces.ExportNamespace | DeclarationSpaces.ExportValue
: DeclarationSpaces.ExportNamespace;
case SyntaxKind.ClassDeclaration:
Expand Down Expand Up @@ -21126,7 +21126,7 @@ namespace ts {
}

// Uninstantiated modules shouldnt do this check
if (node.kind === SyntaxKind.ModuleDeclaration && getModuleInstanceState(node) !== ModuleInstanceState.Instantiated) {
if (isModuleDeclaration(node) && getModuleInstanceState(node) !== ModuleInstanceState.Instantiated) {
return;
}

Expand All @@ -21145,7 +21145,7 @@ namespace ts {
}

// Uninstantiated modules shouldnt do this check
if (node.kind === SyntaxKind.ModuleDeclaration && getModuleInstanceState(node) !== ModuleInstanceState.Instantiated) {
if (isModuleDeclaration(node) && getModuleInstanceState(node) !== ModuleInstanceState.Instantiated) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3107,7 +3107,7 @@ namespace ts {
/* @internal */
// The set of things we consider semantically classifiable. Used to speed up the LS during
// classification.
Classifiable = Class | Enum | TypeAlias | Interface | TypeParameter | Module,
Classifiable = Class | Enum | TypeAlias | Interface | TypeParameter | Module | Alias,

/* @internal */
LateBindingContainer = Class | Interface | TypeLiteral | ObjectLiteral,
Expand Down
6 changes: 3 additions & 3 deletions src/services/breakpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ namespace ts.BreakpointResolver {

case SyntaxKind.ModuleDeclaration:
// span on complete module if it is instantiated
if (getModuleInstanceState(node) !== ModuleInstanceState.Instantiated) {
if (getModuleInstanceState(node as ModuleDeclaration) !== ModuleInstanceState.Instantiated) {
return undefined;
}
// falls through
Expand Down Expand Up @@ -471,7 +471,7 @@ namespace ts.BreakpointResolver {
function spanInBlock(block: Block): TextSpan {
switch (block.parent.kind) {
case SyntaxKind.ModuleDeclaration:
if (getModuleInstanceState(block.parent) !== ModuleInstanceState.Instantiated) {
if (getModuleInstanceState(block.parent as ModuleDeclaration) !== ModuleInstanceState.Instantiated) {
return undefined;
}
// falls through
Expand Down Expand Up @@ -581,7 +581,7 @@ namespace ts.BreakpointResolver {
switch (node.parent.kind) {
case SyntaxKind.ModuleBlock:
// If this is not an instantiated module block, no bp span
if (getModuleInstanceState(node.parent.parent) !== ModuleInstanceState.Instantiated) {
if (getModuleInstanceState(node.parent.parent as ModuleDeclaration) !== ModuleInstanceState.Instantiated) {
return undefined;
}
// falls through
Expand Down
128 changes: 55 additions & 73 deletions src/services/classifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,89 +488,71 @@ namespace ts {

/* @internal */
export function getEncodedSemanticClassifications(typeChecker: TypeChecker, cancellationToken: CancellationToken, sourceFile: SourceFile, classifiableNames: UnderscoreEscapedMap<true>, span: TextSpan): Classifications {
const result: number[] = [];
processNode(sourceFile);

return { spans: result, endOfLineState: EndOfLineState.None };

function pushClassification(start: number, length: number, type: ClassificationType) {
result.push(start);
result.push(length);
result.push(type);
}

function classifySymbol(symbol: Symbol, meaningAtPosition: SemanticMeaning): ClassificationType {
const flags = symbol.getFlags();
if ((flags & SymbolFlags.Classifiable) === SymbolFlags.None) {
const spans: number[] = [];
sourceFile.forEachChild(function cb(node: Node): void {
// Only walk into nodes that intersect the requested span.
if (!node || !textSpanIntersectsWith(span, node.pos, node.getFullWidth())) {
return;
}

if (flags & SymbolFlags.Class) {
return ClassificationType.className;
}
else if (flags & SymbolFlags.Enum) {
return ClassificationType.enumName;
}
else if (flags & SymbolFlags.TypeAlias) {
return ClassificationType.typeAliasName;
}
else if (meaningAtPosition & SemanticMeaning.Type) {
if (flags & SymbolFlags.Interface) {
return ClassificationType.interfaceName;
}
else if (flags & SymbolFlags.TypeParameter) {
return ClassificationType.typeParameterName;
}
}
else if (flags & SymbolFlags.Module) {
// Only classify a module as such if
// - It appears in a namespace context.
// - There exists a module declaration which actually impacts the value side.
if (meaningAtPosition & SemanticMeaning.Namespace ||
(meaningAtPosition & SemanticMeaning.Value && hasValueSideModule(symbol))) {
return ClassificationType.moduleName;
checkForClassificationCancellation(cancellationToken, node.kind);
// Only bother calling into the typechecker if this is an identifier that
// could possibly resolve to a type name. This makes classification run
// in a third of the time it would normally take.
if (isIdentifier(node) && !nodeIsMissing(node) && classifiableNames.has(node.escapedText)) {
const symbol = typeChecker.getSymbolAtLocation(node);
const type = symbol && classifySymbol(symbol, getMeaningFromLocation(node), typeChecker);
if (type) {
pushClassification(node.getStart(sourceFile), node.getEnd(), type);
}
}

return undefined;
node.forEachChild(cb);
});
return { spans, endOfLineState: EndOfLineState.None };

/**
* Returns true if there exists a module that introduces entities on the value side.
*/
function hasValueSideModule(symbol: Symbol): boolean {
return forEach(symbol.declarations, declaration => {
return declaration.kind === SyntaxKind.ModuleDeclaration &&
getModuleInstanceState(declaration) === ModuleInstanceState.Instantiated;
});
}
function pushClassification(start: number, end: number, type: ClassificationType): void {
spans.push(start);
spans.push(end - start);
spans.push(type);
}
}

function processNode(node: Node) {
// Only walk into nodes that intersect the requested span.
if (node && textSpanIntersectsWith(span, node.getFullStart(), node.getFullWidth())) {
const kind = node.kind;
checkForClassificationCancellation(cancellationToken, kind);

if (kind === SyntaxKind.Identifier && !nodeIsMissing(node)) {
const identifier = <Identifier>node;

// Only bother calling into the typechecker if this is an identifier that
// could possibly resolve to a type name. This makes classification run
// in a third of the time it would normally take.
if (classifiableNames.has(identifier.escapedText)) {
const symbol = typeChecker.getSymbolAtLocation(node);
if (symbol) {
const type = classifySymbol(symbol, getMeaningFromLocation(node));
if (type) {
pushClassification(node.getStart(), node.getWidth(), type);
}
}
}
}

forEachChild(node, processNode);
}
function classifySymbol(symbol: Symbol, meaningAtPosition: SemanticMeaning, checker: TypeChecker): ClassificationType | undefined {
const flags = symbol.getFlags();
if ((flags & SymbolFlags.Classifiable) === SymbolFlags.None) {
return undefined;
}
else if (flags & SymbolFlags.Class) {
return ClassificationType.className;
}
else if (flags & SymbolFlags.Enum) {
return ClassificationType.enumName;
}
else if (flags & SymbolFlags.TypeAlias) {
return ClassificationType.typeAliasName;
}
else if (flags & SymbolFlags.Module) {
// Only classify a module as such if
// - It appears in a namespace context.
// - There exists a module declaration which actually impacts the value side.
return meaningAtPosition & SemanticMeaning.Namespace || meaningAtPosition & SemanticMeaning.Value && hasValueSideModule(symbol) ? ClassificationType.moduleName : undefined;
}
else if (flags & SymbolFlags.Alias) {
return classifySymbol(checker.getAliasedSymbol(symbol), meaningAtPosition, checker);
}
else if (meaningAtPosition & SemanticMeaning.Type) {
return flags & SymbolFlags.Interface ? ClassificationType.interfaceName : flags & SymbolFlags.TypeParameter ? ClassificationType.typeParameterName : undefined;
}
else {
return undefined;
}
}

/** Returns true if there exists a module that introduces entities on the value side. */
function hasValueSideModule(symbol: Symbol): boolean {
return some(symbol.declarations, declaration =>
isModuleDeclaration(declaration) && getModuleInstanceState(declaration) === ModuleInstanceState.Instantiated);
}

function getClassificationTypeName(type: ClassificationType): ClassificationTypeNames {
Expand Down
2 changes: 1 addition & 1 deletion src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ namespace ts {
if (isAmbientModule(<ModuleDeclaration>node)) {
return SemanticMeaning.Namespace | SemanticMeaning.Value;
}
else if (getModuleInstanceState(node) === ModuleInstanceState.Instantiated) {
else if (getModuleInstanceState(node as ModuleDeclaration) === ModuleInstanceState.Instantiated) {
return SemanticMeaning.Namespace | SemanticMeaning.Value;
}
else {
Expand Down
20 changes: 20 additions & 0 deletions tests/cases/fourslash/semanticClassificationAlias.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/// <reference path="fourslash.ts"/>

// @Filename: /a.ts
////export type x = number;
////export class y {};

// @Filename: /b.ts
////import { /*0*/x, /*1*/y } from "./a";
////const v: /*2*/x = /*3*/y;

goTo.file("/b.ts");

const [m0, m1, m2, m3] = test.markers();
const c = classification;
verify.semanticClassificationsAre(
c.typeAliasName("x", m0.position),
c.className("y", m1.position),
c.typeAliasName("x", m2.position),
c.className("y", m3.position),
);