Skip to content

Commit 655039c

Browse files
committed
Merge pull request #473 from Microsoft/getReferences
Wire getReferences to use the new compiler
2 parents 4fbc003 + dc0560a commit 655039c

File tree

80 files changed

+2103
-497
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+2103
-497
lines changed

src/compiler/checker.ts

Lines changed: 44 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ module ts {
1111
var nextNodeId = 1;
1212
var nextMergeId = 1;
1313

14+
export function getDeclarationOfKind(symbol: Symbol, kind: SyntaxKind): Declaration {
15+
var declarations = symbol.declarations;
16+
for (var i = 0; i < declarations.length; i++) {
17+
var declaration = declarations[i];
18+
if (declaration.kind === kind) {
19+
return declaration;
20+
}
21+
}
22+
23+
return undefined;
24+
}
25+
1426
/// fullTypeCheck denotes if this instance of the typechecker will be used to get semantic diagnostics.
1527
/// If fullTypeCheck === true - then typechecker should do every possible check to produce all errors
1628
/// If fullTypeCheck === false - typechecker can shortcut and skip checks that only produce errors.
@@ -49,7 +61,9 @@ module ts {
4961
getApparentType: getApparentType,
5062
typeToString: typeToString,
5163
symbolToString: symbolToString,
52-
getAugmentedPropertiesOfApparentType: getAugmentedPropertiesOfApparentType
64+
getAugmentedPropertiesOfApparentType: getAugmentedPropertiesOfApparentType,
65+
getRootSymbol: getRootSymbol,
66+
getContextualType: getContextualType
5367
};
5468

5569
var undefinedSymbol = createSymbol(SymbolFlags.Property | SymbolFlags.Transient, "undefined");
@@ -568,18 +582,6 @@ module ts {
568582
return false;
569583
}
570584

571-
function getDeclarationOfKind(symbol: Symbol, kind: SyntaxKind): Declaration {
572-
var declarations = symbol.declarations;
573-
for (var i = 0; i < declarations.length; i++) {
574-
var declaration = declarations[i];
575-
if (declaration.kind === kind) {
576-
return declaration;
577-
}
578-
}
579-
580-
return undefined;
581-
}
582-
583585
function findConstructorDeclaration(node: ClassDeclaration): ConstructorDeclaration {
584586
var members = node.members;
585587
for (var i = 0; i < members.length; i++) {
@@ -3148,6 +3150,7 @@ module ts {
31483150
symbol.declarations = p.declarations;
31493151
symbol.parent = p.parent;
31503152
symbol.type = widenedTypes[index++];
3153+
symbol.target = p;
31513154
if (p.valueDeclaration) symbol.valueDeclaration = p.valueDeclaration;
31523155
members[symbol.name] = symbol;
31533156
});
@@ -3821,6 +3824,7 @@ module ts {
38213824
prop.parent = member.parent;
38223825
if (member.valueDeclaration) prop.valueDeclaration = member.valueDeclaration;
38233826
prop.type = type;
3827+
prop.target = member;
38243828
member = prop;
38253829
}
38263830
else {
@@ -6584,24 +6588,6 @@ module ts {
65846588
(<Declaration>name.parent).name === name;
65856589
}
65866590

6587-
// True if the given identifier, string literal, or number literal is the name of a declaration node
6588-
function isDeclarationOrFunctionExpressionOrCatchVariableName(name: Node): boolean {
6589-
if (name.kind !== SyntaxKind.Identifier && name.kind !== SyntaxKind.StringLiteral && name.kind !== SyntaxKind.NumericLiteral) {
6590-
return false;
6591-
}
6592-
6593-
var parent = name.parent;
6594-
if (isDeclaration(parent) || parent.kind === SyntaxKind.FunctionExpression) {
6595-
return (<Declaration>parent).name === name;
6596-
}
6597-
6598-
if (parent.kind === SyntaxKind.CatchBlock) {
6599-
return (<CatchBlock>parent).variable === name;
6600-
}
6601-
6602-
return false;
6603-
}
6604-
66056591
function isTypeDeclaration(node: Node): boolean {
66066592
switch (node.kind) {
66076593
case SyntaxKind.TypeParameter:
@@ -6612,28 +6598,6 @@ module ts {
66126598
}
66136599
}
66146600

6615-
function isDeclaration(node: Node): boolean {
6616-
switch (node.kind) {
6617-
case SyntaxKind.TypeParameter:
6618-
case SyntaxKind.Parameter:
6619-
case SyntaxKind.VariableDeclaration:
6620-
case SyntaxKind.Property:
6621-
case SyntaxKind.PropertyAssignment:
6622-
case SyntaxKind.EnumMember:
6623-
case SyntaxKind.Method:
6624-
case SyntaxKind.FunctionDeclaration:
6625-
case SyntaxKind.GetAccessor:
6626-
case SyntaxKind.SetAccessor:
6627-
case SyntaxKind.ClassDeclaration:
6628-
case SyntaxKind.InterfaceDeclaration:
6629-
case SyntaxKind.EnumDeclaration:
6630-
case SyntaxKind.ModuleDeclaration:
6631-
case SyntaxKind.ImportDeclaration:
6632-
return true;
6633-
}
6634-
return false;
6635-
}
6636-
66376601
// True if the given identifier is part of a type reference
66386602
function isTypeReferenceIdentifier(entityName: EntityName): boolean {
66396603
var node: Node = entityName;
@@ -6852,6 +6816,17 @@ module ts {
68526816
}
68536817

68546818
function getSymbolInfo(node: Node) {
6819+
if (isDeclarationOrFunctionExpressionOrCatchVariableName(node)) {
6820+
// This is a declaration, call getSymbolOfNode
6821+
return getSymbolOfNode(node.parent);
6822+
}
6823+
6824+
if (node.kind === SyntaxKind.Identifier && isInRightSideOfImportOrExportAssignment(node)) {
6825+
return node.parent.kind === SyntaxKind.ExportAssignment
6826+
? getSymbolOfEntityName(<Identifier>node)
6827+
: getSymbolOfPartOfRightHandSideOfImport(node);
6828+
}
6829+
68556830
switch (node.kind) {
68566831
case SyntaxKind.Identifier:
68576832
case SyntaxKind.PropertyAccess:
@@ -6862,7 +6837,7 @@ module ts {
68626837
case SyntaxKind.SuperKeyword:
68636838
var type = checkExpression(node);
68646839
return type.symbol;
6865-
6840+
68666841
case SyntaxKind.ConstructorKeyword:
68676842
// constructor keyword for an overload, should take us to the definition if it exist
68686843
var constructorDeclaration = node.parent;
@@ -6872,23 +6847,22 @@ module ts {
68726847
return undefined;
68736848

68746849
case SyntaxKind.StringLiteral:
6875-
// Property access
6876-
if (node.parent.kind === SyntaxKind.IndexedAccess && (<IndexedAccess>node.parent).index === node) {
6877-
var objectType = checkExpression((<IndexedAccess>node.parent).object);
6878-
if (objectType === unknownType) return undefined;
6879-
var apparentType = getApparentType(objectType);
6880-
if (<Type>apparentType === unknownType) return undefined;
6881-
return getPropertyOfApparentType(apparentType, (<LiteralExpression>node).text);
6882-
}
68836850
// External module name in an import declaration
6884-
else if (node.parent.kind === SyntaxKind.ImportDeclaration && (<ImportDeclaration>node.parent).externalModuleName === node) {
6851+
if (node.parent.kind === SyntaxKind.ImportDeclaration && (<ImportDeclaration>node.parent).externalModuleName === node) {
68856852
var importSymbol = getSymbolOfNode(node.parent);
68866853
var moduleType = getTypeOfSymbol(importSymbol);
68876854
return moduleType ? moduleType.symbol : undefined;
68886855
}
6889-
// External module name in an ambient declaration
6890-
else if (node.parent.kind === SyntaxKind.ModuleDeclaration) {
6891-
return getSymbolOfNode(node.parent);
6856+
6857+
// Intentinal fallthrough
6858+
case SyntaxKind.NumericLiteral:
6859+
// index access
6860+
if (node.parent.kind == SyntaxKind.IndexedAccess && (<IndexedAccess>node.parent).index === node) {
6861+
var objectType = checkExpression((<IndexedAccess>node.parent).object);
6862+
if (objectType === unknownType) return undefined;
6863+
var apparentType = getApparentType(objectType);
6864+
if (<Type>apparentType === unknownType) return undefined;
6865+
return getPropertyOfApparentType(apparentType, (<LiteralExpression>node).text);
68926866
}
68936867
break;
68946868
}
@@ -6978,6 +6952,10 @@ module ts {
69786952
}
69796953
}
69806954

6955+
function getRootSymbol(symbol: Symbol) {
6956+
return (symbol.flags & SymbolFlags.Transient) ? getSymbolLinks(symbol).target : symbol;
6957+
}
6958+
69816959
// Emitter support
69826960

69836961
function isExternalModuleSymbol(symbol: Symbol): boolean {

src/compiler/parser.ts

Lines changed: 72 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,46 @@ module ts {
362362
return false;
363363
}
364364

365+
export function isDeclaration(node: Node): boolean {
366+
switch (node.kind) {
367+
case SyntaxKind.TypeParameter:
368+
case SyntaxKind.Parameter:
369+
case SyntaxKind.VariableDeclaration:
370+
case SyntaxKind.Property:
371+
case SyntaxKind.PropertyAssignment:
372+
case SyntaxKind.EnumMember:
373+
case SyntaxKind.Method:
374+
case SyntaxKind.FunctionDeclaration:
375+
case SyntaxKind.GetAccessor:
376+
case SyntaxKind.SetAccessor:
377+
case SyntaxKind.ClassDeclaration:
378+
case SyntaxKind.InterfaceDeclaration:
379+
case SyntaxKind.EnumDeclaration:
380+
case SyntaxKind.ModuleDeclaration:
381+
case SyntaxKind.ImportDeclaration:
382+
return true;
383+
}
384+
return false;
385+
}
386+
387+
// True if the given identifier, string literal, or number literal is the name of a declaration node
388+
export function isDeclarationOrFunctionExpressionOrCatchVariableName(name: Node): boolean {
389+
if (name.kind !== SyntaxKind.Identifier && name.kind !== SyntaxKind.StringLiteral && name.kind !== SyntaxKind.NumericLiteral) {
390+
return false;
391+
}
392+
393+
var parent = name.parent;
394+
if (isDeclaration(parent) || parent.kind === SyntaxKind.FunctionExpression) {
395+
return (<Declaration>parent).name === name;
396+
}
397+
398+
if (parent.kind === SyntaxKind.CatchBlock) {
399+
return (<CatchBlock>parent).variable === name;
400+
}
401+
402+
return false;
403+
}
404+
365405
enum ParsingContext {
366406
SourceElements, // Elements in source file
367407
ModuleElements, // Elements in module declaration
@@ -444,6 +484,22 @@ module ts {
444484
nodeIsNestedInLabel(label: Identifier, requireIterationStatement: boolean, stopAtFunctionBoundary: boolean): ControlBlockContext;
445485
}
446486

487+
export function isKeyword(token: SyntaxKind): boolean {
488+
return SyntaxKind.FirstKeyword <= token && token <= SyntaxKind.LastKeyword;
489+
}
490+
491+
export function isModifier(token: SyntaxKind): boolean {
492+
switch (token) {
493+
case SyntaxKind.PublicKeyword:
494+
case SyntaxKind.PrivateKeyword:
495+
case SyntaxKind.StaticKeyword:
496+
case SyntaxKind.ExportKeyword:
497+
case SyntaxKind.DeclareKeyword:
498+
return true;
499+
}
500+
return false;
501+
}
502+
447503
export function createSourceFile(filename: string, sourceText: string, languageVersion: ScriptTarget, version: string, isOpen: boolean = false): SourceFile {
448504
var file: SourceFile;
449505
var scanner: Scanner;
@@ -775,6 +831,10 @@ module ts {
775831
return createNode(SyntaxKind.Missing);
776832
}
777833

834+
function internIdentifier(text: string): string {
835+
return hasProperty(identifiers, text) ? identifiers[text] : (identifiers[text] = text);
836+
}
837+
778838
// An identifier that starts with two underscores has an extra underscore character prepended to it to avoid issues
779839
// with magic property names like '__proto__'. The 'identifiers' object is used to share a single string instance for
780840
// each identifier in order to reduce memory consumption.
@@ -783,7 +843,7 @@ module ts {
783843
if (isIdentifier) {
784844
var node = <Identifier>createNode(SyntaxKind.Identifier);
785845
var text = escapeIdentifier(scanner.getTokenValue());
786-
node.text = hasProperty(identifiers, text) ? identifiers[text] : (identifiers[text] = text);
846+
node.text = internIdentifier(text);
787847
nextToken();
788848
return finishNode(node);
789849
}
@@ -805,28 +865,11 @@ module ts {
805865

806866
function parsePropertyName(): Identifier {
807867
if (token === SyntaxKind.StringLiteral || token === SyntaxKind.NumericLiteral) {
808-
return <LiteralExpression>parsePrimaryExpression();
868+
return parseLiteralNode(/*internName:*/ true);
809869
}
810870
return parseIdentifierName();
811871
}
812872

813-
814-
function isKeyword(token: SyntaxKind): boolean {
815-
return SyntaxKind.FirstKeyword <= token && token <= SyntaxKind.LastKeyword;
816-
}
817-
818-
function isModifier(token: SyntaxKind): boolean {
819-
switch (token) {
820-
case SyntaxKind.PublicKeyword:
821-
case SyntaxKind.PrivateKeyword:
822-
case SyntaxKind.StaticKeyword:
823-
case SyntaxKind.ExportKeyword:
824-
case SyntaxKind.DeclareKeyword:
825-
return true;
826-
}
827-
return false;
828-
}
829-
830873
function parseContextualModifier(t: SyntaxKind): boolean {
831874
return token === t && tryParse(() => {
832875
nextToken();
@@ -1086,9 +1129,11 @@ module ts {
10861129
return finishNode(node);
10871130
}
10881131

1089-
function parseLiteralNode(): LiteralExpression {
1132+
function parseLiteralNode(internName?:boolean): LiteralExpression {
10901133
var node = <LiteralExpression>createNode(token);
1091-
node.text = scanner.getTokenValue();
1134+
var text = scanner.getTokenValue();
1135+
node.text = internName ? internIdentifier(text) : text;
1136+
10921137
var tokenPos = scanner.getTokenPos();
10931138
nextToken();
10941139
finishNode(node);
@@ -1115,7 +1160,7 @@ module ts {
11151160
}
11161161

11171162
function parseStringLiteral(): LiteralExpression {
1118-
if (token === SyntaxKind.StringLiteral) return parseLiteralNode();
1163+
if (token === SyntaxKind.StringLiteral) return parseLiteralNode(/*internName:*/ true);
11191164
error(Diagnostics.String_literal_expected);
11201165
return <LiteralExpression>createMissingNode();
11211166
}
@@ -2019,6 +2064,10 @@ module ts {
20192064
}
20202065
else {
20212066
indexedAccess.index = parseExpression();
2067+
if (indexedAccess.index.kind === SyntaxKind.StringLiteral || indexedAccess.index.kind === SyntaxKind.NumericLiteral) {
2068+
var literal = <LiteralExpression>indexedAccess.index;
2069+
literal.text = internIdentifier(literal.text);
2070+
}
20222071
parseExpected(SyntaxKind.CloseBracketToken);
20232072
}
20242073

@@ -3569,6 +3618,7 @@ module ts {
35693618
file.version = version;
35703619
file.isOpen = isOpen;
35713620
file.languageVersion = languageVersion;
3621+
file.identifiers = identifiers;
35723622
return file;
35733623
}
35743624

src/compiler/scanner.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,18 @@ module ts {
441441
return getCommentRanges(text, pos, /*trailing*/ true);
442442
}
443443

444+
export function isIdentifierStart(ch: number, languageVersion: ScriptTarget): boolean {
445+
return ch >= CharacterCodes.A && ch <= CharacterCodes.Z || ch >= CharacterCodes.a && ch <= CharacterCodes.z ||
446+
ch === CharacterCodes.$ || ch === CharacterCodes._ ||
447+
ch > CharacterCodes.maxAsciiCharacter && isUnicodeIdentifierStart(ch, languageVersion);
448+
}
449+
450+
export function isIdentifierPart(ch: number, languageVersion: ScriptTarget): boolean {
451+
return ch >= CharacterCodes.A && ch <= CharacterCodes.Z || ch >= CharacterCodes.a && ch <= CharacterCodes.z ||
452+
ch >= CharacterCodes._0 && ch <= CharacterCodes._9 || ch === CharacterCodes.$ || ch === CharacterCodes._ ||
453+
ch > CharacterCodes.maxAsciiCharacter && isUnicodeIdentifierPart(ch, languageVersion);
454+
}
455+
444456
export function createScanner(languageVersion: ScriptTarget, text?: string, onError?: ErrorCallback, onComment?: CommentCallback): Scanner {
445457
var pos: number; // Current position (end position of text of current token)
446458
var len: number; // Length of text

src/compiler/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ module ts {
537537
isOpen: boolean;
538538
version: string;
539539
languageVersion: ScriptTarget;
540+
identifiers: Map<string>;
540541
}
541542

542543
export interface Program {
@@ -616,6 +617,8 @@ module ts {
616617
typeToString(type: Type, enclosingDeclaration?: Node, flags?: TypeFormatFlags): string;
617618
symbolToString(symbol: Symbol, enclosingDeclaration?: Node, meaning?: SymbolFlags): string;
618619
getAugmentedPropertiesOfApparentType(type: Type): Symbol[];
620+
getRootSymbol(symbol: Symbol): Symbol;
621+
getContextualType(node: Node): Type;
619622
}
620623

621624
export interface TextWriter {

0 commit comments

Comments
 (0)