Skip to content

Commit 204b2a3

Browse files
committed
TypeScript: support syntax for optional tuple type elements
1 parent 7e7e30c commit 204b2a3

File tree

5 files changed

+19
-1
lines changed

5 files changed

+19
-1
lines changed

javascript/ql/src/semmle/javascript/TypeScript.qll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,14 @@ class IsTypeExpr extends @istypeexpr, TypeExpr {
10731073
TypeExpr getPredicateType() { result = this.getChildTypeExpr(1) }
10741074
}
10751075

1076+
/**
1077+
* An optional type element in a tuple type, such as `number?` in `[string, number?]`.
1078+
*/
1079+
class OptionalTypeExpr extends @optionaltypeexpr, TypeExpr {
1080+
/** Gets the type `T` in `T?` */
1081+
TypeExpr getElementType() { result = getChildTypeExpr(0) }
1082+
}
1083+
10761084
/**
10771085
* A possibly qualified name that refers to a variable from inside a type.
10781086
*

javascript/ql/src/semmlecode.javascript.dbscheme

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,9 @@ case @typeexpr.kind of
556556
| 29 = @infertypeexpr
557557
| 30 = @importtypeaccess
558558
| 31 = @importnamespaceaccess
559-
| 32 = @importvartypeaccess;
559+
| 32 = @importvartypeaccess
560+
| 33 = @optionaltypeexpr
561+
;
560562

561563
@typeref = @typeaccess | @typedecl;
562564
@typeidentifier = @typedecl | @localtypeaccess | @typelabel | @localvartypeaccess | @localnamespaceaccess;

javascript/ql/test/library-tests/TypeScript/TypeAnnotations/TupleTypeExpr.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
| tst.ts:48:16:48:40 | [number ... oolean] | 2 | 3 | tst.ts:48:33:48:39 | boolean |
44
| tst.ts:95:31:95:35 | [S,T] | 0 | 2 | tst.ts:95:32:95:32 | S |
55
| tst.ts:95:31:95:35 | [S,T] | 1 | 2 | tst.ts:95:34:95:34 | T |
6+
| tst.ts:133:31:133:55 | [number ... umber?] | 0 | 3 | tst.ts:133:32:133:37 | number |
7+
| tst.ts:133:31:133:55 | [number ... umber?] | 1 | 3 | tst.ts:133:40:133:45 | string |
8+
| tst.ts:133:31:133:55 | [number ... umber?] | 2 | 3 | tst.ts:133:48:133:54 | number? |

javascript/ql/test/library-tests/TypeScript/TypeAnnotations/VariableTypes.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,5 @@
6969
| tst.ts:126:5:126:18 | importedTypeof | importedTypeof | tst.ts:126:21:126:42 | typeof ... value") |
7070
| tst.ts:127:5:127:27 | importe ... dTypeof | importedQualifiedTypeof | tst.ts:127:30:127:53 | typeof ... lue").x |
7171
| tst.ts:128:5:128:35 | importe ... tespace | importedQualifiedTypeWhitespace | tst.ts:131:4:131:6 | bar |
72+
| tst.ts:133:5:133:28 | tupleWi ... Element | tupleWithOptionalElement | tst.ts:133:31:133:55 | [number ... umber?] |
73+
| tst.ts:134:5:134:14 | emptyTuple | emptyTuple | tst.ts:134:17:134:18 | [] |

javascript/ql/test/library-tests/TypeScript/TypeAnnotations/tst.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,6 @@ var importedQualifiedTypeWhitespace: import(
129129
'awkard-namespace'
130130
)
131131
.bar;
132+
133+
let tupleWithOptionalElement: [number, string, number?];
134+
let emptyTuple: [];

0 commit comments

Comments
 (0)