Skip to content

Commit 44823ca

Browse files
authored
Merge pull request #1522 from asger-semmle/ts-stringify-recursive-type-alias
Approved by xiemaisi
2 parents e079406 + 52a5bce commit 44823ca

File tree

9 files changed

+29
-6
lines changed

9 files changed

+29
-6
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ function parseSingleFile(filename: string): {ast: ts.SourceFile, code: string} {
244244

245245
function handleOpenProjectCommand(command: OpenProjectCommand) {
246246
let tsConfigFilename = String(command.tsConfig);
247-
let tsConfig = ts.readConfigFile(tsConfigFilename, ts.sys.readFile)
247+
let tsConfig = ts.readConfigFile(tsConfigFilename, ts.sys.readFile);
248248
let basePath = pathlib.dirname(tsConfigFilename);
249249

250250
let parseConfigHost: ts.ParseConfigHost = {

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,10 @@ export class TypeTable {
436436
let toStringValue: string;
437437
// Some types can't be stringified. Just discard the type if we can't stringify it.
438438
try {
439-
toStringValue = this.typeChecker.typeToString(type);
439+
toStringValue = this.typeChecker.typeToString(
440+
type,
441+
undefined,
442+
ts.TypeFormatFlags.UseAliasDefinedOutsideCurrentScope);
440443
} catch (e) {
441444
console.warn("Recovered from a compiler crash while stringifying a type. Discarding the type.");
442445
console.warn(e.stack);
@@ -454,7 +457,11 @@ export class TypeTable {
454457
// Some types can't be stringified. Just discard the type if we can't stringify it.
455458
try {
456459
toStringValue =
457-
this.typeChecker.signatureToString(signature, signature.declaration, ts.TypeFormatFlags.None, kind);
460+
this.typeChecker.signatureToString(
461+
signature,
462+
signature.declaration,
463+
ts.TypeFormatFlags.UseAliasDefinedOutsideCurrentScope,
464+
kind);
458465
} catch (e) {
459466
console.warn("Recovered from a compiler crash while stringifying a signature. Discarding the signature.");
460467
console.warn(e.stack);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
| unique symbol | esmodule | externalSymbol |
1+
| typeof externalSymbol | esmodule | externalSymbol |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.ts:3:1:7:6 | type Di ... \\n T; | Disjunction<T> |
2+
| test.ts:9:8:9:65 | type Tr ... n<T>>>; | Disjunction<Disjunction<Disjunction<T>>> |
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import javascript
2+
3+
from TypeAliasDeclaration decl
4+
select decl, decl.getDefinition().getType()
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { ArrayBox, Box } from 'somewhere';
2+
3+
type Disjunction<T> =
4+
T extends ArrayBox<infer U1> ? U1[] :
5+
T extends Box<infer U2> ? U2 :
6+
T extends ({[P in keyof T]: T[P]} & {p: any}) ? {[P in Exclude<keyof T, 'p'>]: Disjunction<T[P]>} :
7+
T;
8+
9+
export type Triple<T> = Disjunction<Disjunction<Disjunction<T>>>;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

javascript/ql/test/library-tests/TypeScript/Types/GetExprType.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
| tst.ts:28:22:28:25 | null | null |
7070
| tst.ts:29:5:29:13 | neverType | () => never |
7171
| tst.ts:30:5:30:14 | symbolType | symbol |
72-
| tst.ts:31:7:31:22 | uniqueSymbolType | unique symbol |
72+
| tst.ts:31:7:31:22 | uniqueSymbolType | typeof uniqueSymbolType |
7373
| tst.ts:31:41:31:44 | null | null |
7474
| tst.ts:32:5:32:14 | objectType | object |
7575
| tst.ts:33:5:33:16 | intersection | string & { x: string; } |

javascript/ql/test/library-tests/TypeScript/Types/GetTypeExprType.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
| tst.ts:29:16:29:26 | () => never | () => never |
4141
| tst.ts:29:22:29:26 | never | never |
4242
| tst.ts:30:17:30:22 | symbol | symbol |
43-
| tst.ts:31:25:31:37 | unique symbol | unique symbol |
43+
| tst.ts:31:25:31:37 | unique symbol | typeof uniqueSymbolType |
4444
| tst.ts:32:17:32:22 | object | object |
4545
| tst.ts:33:19:33:24 | string | string |
4646
| tst.ts:33:19:33:38 | string & {x: string} | string & { x: string; } |

0 commit comments

Comments
 (0)