Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f11dc11

Browse files
committed
TS: Fix type of RHS of TypeAliasDeclaration
1 parent a3aef1e commit f11dc11

3 files changed

Lines changed: 29 additions & 27 deletions

File tree

javascript/extractor/lib/typescript/src/ast_extractor.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,9 @@ export function augmentAst(ast: AugmentedSourceFile, code: string, project: Proj
198198
: null;
199199
let type = contextualType || typeChecker.getTypeAtLocation(node);
200200
if (type != null) {
201-
let id = typeTable.buildType(type);
201+
let parent = node.parent;
202+
let unfoldAlias = ts.isTypeAliasDeclaration(parent) && node === parent.type;
203+
let id = typeTable.buildType(type, unfoldAlias);
202204
if (id != null) {
203205
node.$type = id;
204206
}

javascript/extractor/lib/typescript/src/type_table.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -372,9 +372,9 @@ export class TypeTable {
372372
/**
373373
* Gets the canonical ID for the given type, generating a fresh ID if necessary.
374374
*/
375-
public buildType(type: ts.Type): number | null {
375+
public buildType(type: ts.Type, unfoldAlias: boolean): number | null {
376376
this.isInShallowTypeContext = false;
377-
let id = this.getId(type);
377+
let id = this.getId(type, unfoldAlias);
378378
this.iterateBuildTypeWorklist();
379379
if (id == null) return null;
380380
return id;
@@ -385,7 +385,7 @@ export class TypeTable {
385385
*
386386
* Returns `null` if we do not support extraction of this type.
387387
*/
388-
public getId(type: ts.Type): number | null {
388+
public getId(type: ts.Type, unfoldAlias: boolean): number | null {
389389
if (this.typeRecursionDepth > 100) {
390390
// Ignore infinitely nested anonymous types, such as `{x: {x: {x: ... }}}`.
391391
// Such a type can't be written directly with TypeScript syntax (as it would need to be named),
@@ -397,12 +397,12 @@ export class TypeTable {
397397
type = this.typeChecker.getBaseTypeOfLiteralType(type);
398398
}
399399
++this.typeRecursionDepth;
400-
let content = this.getTypeString(type);
400+
let content = this.getTypeString(type, unfoldAlias);
401401
--this.typeRecursionDepth;
402402
if (content == null) return null; // Type not supported.
403403
let id = this.typeIds.get(content);
404404
if (id == null) {
405-
let stringValue = this.stringifyType(type);
405+
let stringValue = this.stringifyType(type, unfoldAlias);
406406
if (stringValue == null) {
407407
return null; // Type not supported.
408408
}
@@ -432,14 +432,14 @@ export class TypeTable {
432432
return id;
433433
}
434434

435-
private stringifyType(type: ts.Type): string {
435+
private stringifyType(type: ts.Type, unfoldAlias: boolean): string {
436+
let formatFlags = unfoldAlias
437+
? ts.TypeFormatFlags.InTypeAlias
438+
: ts.TypeFormatFlags.UseAliasDefinedOutsideCurrentScope;
436439
let toStringValue: string;
437440
// Some types can't be stringified. Just discard the type if we can't stringify it.
438441
try {
439-
toStringValue = this.typeChecker.typeToString(
440-
type,
441-
undefined,
442-
ts.TypeFormatFlags.UseAliasDefinedOutsideCurrentScope);
442+
toStringValue = this.typeChecker.typeToString(type, undefined, formatFlags);
443443
} catch (e) {
444444
console.warn("Recovered from a compiler crash while stringifying a type. Discarding the type.");
445445
console.warn(e.stack);
@@ -477,9 +477,9 @@ export class TypeTable {
477477
/**
478478
* Gets a string representing the kind and contents of the given type.
479479
*/
480-
private getTypeString(type: AugmentedType): string | null {
480+
private getTypeString(type: AugmentedType, unfoldAlias: boolean): string | null {
481481
// Reference to a type alias.
482-
if (type.aliasSymbol != null) {
482+
if (!unfoldAlias && type.aliasSymbol != null) {
483483
let tag = "reference;" + this.getSymbolId(type.aliasSymbol);
484484
return type.aliasTypeArguments == null
485485
? tag
@@ -499,7 +499,7 @@ export class TypeTable {
499499
if (flags & ts.TypeFlags.TypeVariable) {
500500
let enclosingType = getEnclosingTypeOfThisType(type);
501501
if (enclosingType != null) {
502-
return "this;" + this.getId(enclosingType);
502+
return "this;" + this.getId(enclosingType, false);
503503
} else if (symbol.parent == null) {
504504
// The type variable is bound on a call signature. Only extract it by name.
505505
return "lextypevar;" + symbol.name;
@@ -730,7 +730,7 @@ export class TypeTable {
730730
private makeTypeStringVector(tag: string, types: ReadonlyArray<ts.Type>, length = types.length): string | null {
731731
let hash = tag;
732732
for (let i = 0; i < length; ++i) {
733-
let id = this.getId(types[i]);
733+
let id = this.getId(types[i], false);
734734
if (id == null) return null;
735735
hash += ";" + id;
736736
}
@@ -748,7 +748,7 @@ export class TypeTable {
748748
for (let property of type.getProperties()) {
749749
let propertyType = this.typeChecker.getTypeOfSymbolAtLocation(property, this.arbitraryAstNode);
750750
if (propertyType == null) return null;
751-
let propertyTypeId = this.getId(propertyType);
751+
let propertyTypeId = this.getId(propertyType, false);
752752
if (propertyTypeId == null) return null;
753753
hash += ";p" + this.getSymbolId(property) + ';' + propertyTypeId;
754754
}
@@ -761,13 +761,13 @@ export class TypeTable {
761761
}
762762
let indexType = type.getStringIndexType();
763763
if (indexType != null) {
764-
let indexTypeId = this.getId(indexType);
764+
let indexTypeId = this.getId(indexType, false);
765765
if (indexTypeId == null) return null;
766766
hash += ";s" + indexTypeId;
767767
}
768768
indexType = type.getNumberIndexType();
769769
if (indexType != null) {
770-
let indexTypeId = this.getId(indexType);
770+
let indexTypeId = this.getId(indexType, false);
771771
if (indexTypeId == null) return null;
772772
hash += ";i" + indexTypeId;
773773
}
@@ -847,7 +847,7 @@ export class TypeTable {
847847
for (let symbol of props) {
848848
let propertyType = this.typeChecker.getTypeOfSymbolAtLocation(symbol, this.arbitraryAstNode);
849849
if (propertyType == null) continue;
850-
let propertyTypeId = this.getId(propertyType);
850+
let propertyTypeId = this.getId(propertyType, false);
851851
if (propertyTypeId == null) continue;
852852
this.propertyLookups.baseTypes.push(id);
853853
this.propertyLookups.names.push(symbol.name);
@@ -892,7 +892,7 @@ export class TypeTable {
892892
break;
893893
}
894894
}
895-
let returnTypeId = this.getId(signature.getReturnType());
895+
let returnTypeId = this.getId(signature.getReturnType(), false);
896896
if (returnTypeId == null) {
897897
return null;
898898
}
@@ -901,7 +901,7 @@ export class TypeTable {
901901
tag += ";" + typeParameter.symbol.name;
902902
let constraint = typeParameter.getConstraint();
903903
let constraintId: number;
904-
if (constraint == null || (constraintId = this.getId(constraint)) == null) {
904+
if (constraint == null || (constraintId = this.getId(constraint, false)) == null) {
905905
tag += ";";
906906
} else {
907907
tag += ";" + constraintId;
@@ -912,7 +912,7 @@ export class TypeTable {
912912
if (parameterType == null) {
913913
return null;
914914
}
915-
let parameterTypeId = this.getId(parameterType);
915+
let parameterTypeId = this.getId(parameterType, false);
916916
if (parameterTypeId == null) {
917917
return null;
918918
}
@@ -946,7 +946,7 @@ export class TypeTable {
946946

947947
private extractIndexer(baseType: number, indexType: ts.Type, table: IndexerTable) {
948948
if (indexType == null) return;
949-
let indexTypeId = this.getId(indexType);
949+
let indexTypeId = this.getId(indexType, false);
950950
if (indexTypeId == null) return;
951951
table.baseTypes.push(baseType);
952952
table.propertyTypes.push(indexTypeId);
@@ -1017,7 +1017,7 @@ export class TypeTable {
10171017
let selfType = this.getSelfType(type);
10181018
if (selfType != null) {
10191019
this.checkExpansiveness(selfType);
1020-
let id = this.getId(selfType);
1020+
let id = this.getId(selfType, false);
10211021
return this.expansiveTypes.get(id);
10221022
}
10231023
return false;
@@ -1086,7 +1086,7 @@ export class TypeTable {
10861086
search(type, 0);
10871087

10881088
function search(type: ts.TypeReference, expansionDepth: number): number | null {
1089-
let id = typeTable.getId(type);
1089+
let id = typeTable.getId(type, false);
10901090
if (id == null) return null;
10911091

10921092
let index = indexTable.get(id);

javascript/ql/test/library-tests/TypeScript/TypeAliases/TypeAliases.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
rightHandSide
22
| tst.ts:1:1:1:16 | type A = number; | number |
3-
| tst.ts:2:1:2:16 | type B<T> = T[]; | B<T> |
3+
| tst.ts:2:1:2:16 | type B<T> = T[]; | T[] |
44
| tst.ts:8:10:8:20 | type C = A; | number |
5-
| tst.ts:15:1:15:23 | type Un ... \| Two; | Union |
5+
| tst.ts:15:1:15:23 | type Un ... \| Two; | One \| Two |
66
#select
77
| tst.ts:1:1:1:16 | type A = number; | tst.ts:1:6:1:6 | A | 0 | tst.ts:1:10:1:15 | number |
88
| tst.ts:2:1:2:16 | type B<T> = T[]; | tst.ts:2:6:2:6 | B | 1 | tst.ts:2:13:2:15 | T[] |

0 commit comments

Comments
 (0)