Skip to content

Commit c39dce4

Browse files
authored
Merge pull request #3781 from asger-semmle/js/deprecate-type-member-lookup
Approved by erik-krogh
2 parents 3ce4cff + e2a300e commit c39dce4

File tree

23 files changed

+106
-208
lines changed

23 files changed

+106
-208
lines changed

change-notes/1.25/analysis-javascript.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,4 @@ The following low-precision queries are no longer run by default on LGTM (their
9898
- `ParameterNode.asExpr()` and `.getAstNode()` now gets the parameter's AST node, whereas previously it had no result.
9999
- `Expr.flow()` now has a more meaningful result for destructuring patterns. Previously this node was disconnected from the data flow graph. Now it represents the values being destructured by the pattern.
100100
* The global data-flow and taint-tracking libraries now model indirect parameter accesses through the `arguments` object in some cases, which may lead to additional results from some of the security queries, particularly "Prototype pollution in utility function".
101+
* The predicates `Type.getProperty()` and variants of `Type.getMethod()` have been deprecated due to lack of use-cases. Looking up a named property of a static type is no longer supported, favoring faster extraction times instead.

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -875,21 +875,23 @@ export class TypeTable {
875875
}
876876

877877
/**
878-
* Returns the properties of the given type, or `null` if the properties of this
879-
* type could not be computed.
878+
* Returns the properties to extract for the given type or `null` if nothing should be extracted.
879+
*
880+
* For performance reasons we only extract properties needed to recognize promise types at the QL
881+
* level.
880882
*/
881-
private tryGetProperties(type: ts.Type) {
882-
// Workaround for https://github.com/Microsoft/TypeScript/issues/30845
883-
// Should be safe to remove once that has been fixed.
884-
try {
885-
return type.getProperties();
886-
} catch (e) {
887-
return null;
883+
private getPropertiesToExtract(type: ts.Type) {
884+
if (this.getSelfType(type) === type) {
885+
let thenSymbol = this.typeChecker.getPropertyOfType(type, "then");
886+
if (thenSymbol != null) {
887+
return [thenSymbol];
888+
}
888889
}
890+
return null;
889891
}
890892

891893
private extractProperties(type: ts.Type, id: number) {
892-
let props = this.tryGetProperties(type);
894+
let props = this.getPropertiesToExtract(type);
893895
if (props == null) return;
894896
for (let symbol of props) {
895897
let propertyType = this.tryGetTypeOfSymbol(symbol);

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

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,11 +1649,9 @@ class Type extends @type {
16491649
Type getChild(int i) { type_child(result, this, i) }
16501650

16511651
/**
1652-
* Gets the type of the given property of this type.
1653-
*
1654-
* Note that this does not account for properties implied by index signatures.
1652+
* DEPRECATED. Property lookup on types is no longer supported.
16551653
*/
1656-
Type getProperty(string name) { type_property(this, name, result) }
1654+
deprecated Type getProperty(string name) { none() }
16571655

16581656
/**
16591657
* Gets the type of the string index signature on this type,
@@ -1758,33 +1756,19 @@ class Type extends @type {
17581756
int getNumConstructorSignature() { result = count(getAConstructorSignature()) }
17591757

17601758
/**
1761-
* Gets the last signature of the method of the given name.
1762-
*
1763-
* For overloaded methods, this is the most general version of the its
1764-
* signature, which covers all cases, but with less precision than the
1765-
* overload signatures.
1766-
*
1767-
* Use `getAMethodOverload` to get any of its overload signatures.
1759+
* DEPRECATED. Method lookup on types is no longer supported.
17681760
*/
1769-
FunctionCallSignatureType getMethod(string name) {
1770-
result = getProperty(name).getLastFunctionSignature()
1771-
}
1761+
deprecated FunctionCallSignatureType getMethod(string name) { none() }
17721762

17731763
/**
1774-
* Gets the `n`th overload signature of the given method.
1764+
* DEPRECATED. Method lookup on types is no longer supported.
17751765
*/
1776-
FunctionCallSignatureType getMethodOverload(string name, int n) {
1777-
result = getProperty(name).getFunctionSignature(n)
1778-
}
1766+
deprecated FunctionCallSignatureType getMethodOverload(string name, int n) { none() }
17791767

17801768
/**
1781-
* Gets a signature of the method of the given name.
1782-
*
1783-
* Overloaded methods have multiple signatures.
1769+
* DEPRECATED. Method lookup on types is no longer supported.
17841770
*/
1785-
FunctionCallSignatureType getAMethodOverload(string name) {
1786-
result = getProperty(name).getAFunctionSignature()
1787-
}
1771+
deprecated FunctionCallSignatureType getAMethodOverload(string name) { none() }
17881772

17891773
/**
17901774
* Repeatedly unfolds union and intersection types and gets any of the underlying types,
@@ -2638,10 +2622,11 @@ private class PromiseTypeName extends TypeName {
26382622
name.matches("%Deferred")
26392623
) and
26402624
// The `then` method should take a callback, taking an argument of type `T`.
2641-
exists(TypeReference self | self = getType() |
2625+
exists(TypeReference self, Type thenMethod | self = getType() |
26422626
self.getNumTypeArgument() = 1 and
2643-
self
2644-
.getAMethodOverload("then")
2627+
type_property(self, "then", thenMethod) and
2628+
thenMethod
2629+
.getAFunctionSignature()
26452630
.getParameter(0)
26462631
.unfold()
26472632
.getAFunctionSignature()
Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,5 @@
1-
| (T \| ConcatArray<T>)[] | `T \| ConcatArray<T>` |
2-
| (number \| ConcatArray<number>)[] | `number \| ConcatArray<number>` |
3-
| (number[] \| ConcatArray<number[]>)[] | `number[] \| ConcatArray<number[]>` |
4-
| (string \| number \| ConcatArray<string \| number>)[] | `string \| number \| ConcatArray<string \| number>` |
5-
| (string \| number)[] | `string \| number` |
6-
| ConcatArray<T>[] | `ConcatArray<T>` |
7-
| ConcatArray<number>[] | `ConcatArray<number>` |
8-
| ConcatArray<number[]>[] | `ConcatArray<number[]>` |
9-
| ConcatArray<string \| number>[] | `ConcatArray<string \| number>` |
10-
| S[] | `S` |
11-
| T[] | `T` |
12-
| U[] | `U` |
131
| [number, string] | `string \| number` |
14-
| any[] | `any` |
152
| number[] | `number` |
16-
| number[][] | `number[]` |
173
| readonly T[] | `T` |
184
| readonly number[] | `number` |
195
| readonly number[][] | `number[]` |
20-
| string[] | `string` |
Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,7 @@
1-
| (T \| ConcatArray<T>)[] | T \| ConcatArray<T> |
2-
| (number \| ConcatArray<number>)[] | number \| ConcatArray<number> |
3-
| (number[] \| ConcatArray<number[]>)[] | number[] \| ConcatArray<number[]> |
4-
| (string \| number \| ConcatArray<string \| number>)[] | string \| number \| ConcatArray<string \| number> |
5-
| (string \| number)[] | string \| number |
6-
| ConcatArray<T>[] | ConcatArray<T> |
7-
| ConcatArray<number>[] | ConcatArray<number> |
8-
| ConcatArray<number[]>[] | ConcatArray<number[]> |
9-
| ConcatArray<string \| number>[] | ConcatArray<string \| number> |
101
| NumberIndexable | object |
11-
| S[] | S |
12-
| T[] | T |
13-
| U[] | U |
142
| [number, string] | string \| number |
15-
| any[] | any |
163
| number[] | number |
17-
| number[][] | number[] |
184
| readonly T[] | T |
195
| readonly number[] | number |
206
| readonly number[][] | number[] |
217
| string | string |
22-
| string[] | string |
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
| [number, string] | (string \| number)[] |

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,3 @@
1313
| IMulti | IGenericBase |
1414
| IStringSub | IGenericBase |
1515
| ISub | IBase |
16-
| RegExpMatchArray | Array |

javascript/ql/test/library-tests/TypeScript/BaseTypes/SelfTypes.expected

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
| CImplementsString | CImplementsString |
88
| CStringSub | CStringSub |
99
| CSub | CSub |
10-
| CollatorOptions | CollatorOptions |
1110
| IBase | IBase |
1211
| IEmpty | IEmpty |
1312
| IEmptySub | IEmptySub |
@@ -16,6 +15,3 @@
1615
| IMulti | IMulti<T> |
1716
| IStringSub | IStringSub |
1817
| ISub | ISub |
19-
| NumberFormatOptions | NumberFormatOptions |
20-
| RegExp | RegExp |
21-
| RegExpMatchArray | RegExpMatchArray |

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

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -108,51 +108,24 @@ test_FunctionCallSig
108108
| tst.ts:63:3:63:23 | method2 ... ing[]); | (y: string[]): any |
109109
| tst.ts:64:3:64:21 | method3(y: string); | (y: string): any |
110110
test_getRestParameterType
111-
| (...items: (string \| ConcatArray<string>)[]): T[] | string \| ConcatArray<string> |
112-
| (...items: ConcatArray<string>[]): T[] | ConcatArray<string> |
113-
| (...items: string[]): number | string |
114-
| (...strings: string[]): string | string |
115111
| (...y: string[]): any | string |
116-
| (start: number, deleteCount: number, ...items: string[]): T[] | string |
117-
| (substring: string, ...args: any[]): string | any |
118112
| (x: number, ...y: string[]): any | string |
119113
| new (...y: string[]): any | string |
120114
| new (x: number, ...y: string[]): any | string |
121115
test_getRestParameterArray
122-
| (...items: (string \| ConcatArray<string>)[]): T[] | (string \| ConcatArray<string>)[] |
123-
| (...items: ConcatArray<string>[]): T[] | ConcatArray<string>[] |
124-
| (...items: string[]): number | string[] |
125-
| (...strings: string[]): string | string[] |
126116
| (...y: string[]): any | string[] |
127-
| (start: number, deleteCount: number, ...items: string[]): T[] | string[] |
128-
| (substring: string, ...args: any[]): string | any[] |
129117
| (x: number, ...y: string[]): any | string[] |
130118
| new (...y: string[]): any | string[] |
131119
| new (x: number, ...y: string[]): any | string[] |
132120
test_RestSig_getParameter
133-
| (...items: (string \| ConcatArray<string>)[]): T[] | 0 | items | string \| ConcatArray<string> |
134-
| (...items: ConcatArray<string>[]): T[] | 0 | items | ConcatArray<string> |
135-
| (...items: string[]): number | 0 | items | string |
136-
| (...strings: string[]): string | 0 | strings | string |
137121
| (...y: string[]): any | 0 | y | string |
138-
| (start: number, deleteCount: number, ...items: string[]): T[] | 0 | start | number |
139-
| (start: number, deleteCount: number, ...items: string[]): T[] | 1 | deleteCount | number |
140-
| (start: number, deleteCount: number, ...items: string[]): T[] | 2 | items | string |
141-
| (substring: string, ...args: any[]): string | 0 | substring | string |
142-
| (substring: string, ...args: any[]): string | 1 | args | any |
143122
| (x: number, ...y: string[]): any | 0 | x | number |
144123
| (x: number, ...y: string[]): any | 1 | y | string |
145124
| new (...y: string[]): any | 0 | y | string |
146125
| new (x: number, ...y: string[]): any | 0 | x | number |
147126
| new (x: number, ...y: string[]): any | 1 | y | string |
148127
test_RestSig_numRequiredParams
149-
| (...items: (string \| ConcatArray<string>)[]): T[] | 0 |
150-
| (...items: ConcatArray<string>[]): T[] | 0 |
151-
| (...items: string[]): number | 0 |
152-
| (...strings: string[]): string | 0 |
153128
| (...y: string[]): any | 0 |
154-
| (start: number, deleteCount: number, ...items: string[]): T[] | 2 |
155-
| (substring: string, ...args: any[]): string | 1 |
156129
| (x: number, ...y: string[]): any | 1 |
157130
| new (...y: string[]): any | 0 |
158131
| new (x: number, ...y: string[]): any | 1 |

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

Lines changed: 0 additions & 23 deletions
This file was deleted.

javascript/ql/test/library-tests/TypeScript/ExpansiveTypes/ExpansiveTypes.ql

Lines changed: 0 additions & 11 deletions
This file was deleted.

javascript/ql/test/library-tests/TypeScript/ExpansiveTypes/NonExpansiveTypes.expected

Lines changed: 0 additions & 31 deletions
This file was deleted.

javascript/ql/test/library-tests/TypeScript/ExpansiveTypes/NonExpansiveTypes.ql

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
| After |
2+
| AfterX |
3+
| Before |
4+
| BeforeX |
5+
| Box<Expand<T[]>> |
6+
| Box<S> |
7+
| Box<S> |
8+
| Box<T[]> |
9+
| Box<number> |
10+
| C<T> |
11+
| C<T[]> |
12+
| Expand<T> |
13+
| Expand<T[]> |
14+
| ExpandUsingObjectLiteral<T> |
15+
| ExpandUsingObjectLiteral<T[]> |
16+
| Expansive<T> |
17+
| Expansive<T> |
18+
| Expansive<T[]> |
19+
| Expansive<T[]> |
20+
| Expansive<number> |
21+
| Expansive<string> |
22+
| ExpansiveA<S> |
23+
| ExpansiveA<S> |
24+
| ExpansiveA<T> |
25+
| ExpansiveA<T> |
26+
| ExpansiveB<S> |
27+
| ExpansiveB<S> |
28+
| ExpansiveB<T> |
29+
| ExpansiveB<T[]> |
30+
| ExpansiveB<T[]> |
31+
| ExpansiveB<number> |
32+
| ExpansiveByInference<T> |
33+
| ExpansiveByInference<T[]> |
34+
| ExpansiveC<T> |
35+
| ExpansiveC<T> |
36+
| ExpansiveC<T> |
37+
| ExpansiveC<T[]> |
38+
| ExpansiveC<T[]> |
39+
| ExpansiveC<number> |
40+
| ExpansiveConstructSignature<T> |
41+
| ExpansiveConstructSignature<T[]> |
42+
| ExpansiveD<T> |
43+
| ExpansiveD<T> |
44+
| ExpansiveD<T> |
45+
| ExpansiveD<T> |
46+
| ExpansiveFunctionType<T> |
47+
| ExpansiveFunctionType<T[]> |
48+
| ExpansiveMethod<T> |
49+
| ExpansiveMethod<T[]> |
50+
| ExpansiveParameter<T> |
51+
| ExpansiveParameter<T[]> |
52+
| ExpansiveSignature<T> |
53+
| ExpansiveSignature<T[]> |
54+
| ExpansiveSignatureTypeBound<T> |
55+
| ExpansiveSignatureTypeBound<T[]> |
56+
| ExpansiveX<T> |
57+
| ExpansiveX<T[]> |
58+
| NonExpansive<Box<number>> |
59+
| NonExpansive<T> |
60+
| T[] |
61+
| T[] |
62+
| T[] |
63+
| T[] |
64+
| T[] |
65+
| T[] |
66+
| T[] |
67+
| T[] |
68+
| T[] |
69+
| T[] |
70+
| T[] |
71+
| T[] |
72+
| T[] |
73+
| T[] |
74+
| T[] |
75+
| T[] |
76+
| T[] |
77+
| 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 TypeReference type
4+
select type
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
| Intl.CollatorOptions | CollatorOptions |
2-
| Intl.NumberFormatOptions | NumberFormatOptions |
31
| LegacyGlobals.LegacySubclass | LegacySubclass |
42
| Modern.ModernClass | ModernClass |
53
| ModernGlobals.ModernSubclass | ModernSubclass |
6-
| RegExp | RegExp |
7-
| RegExpMatchArray | RegExpMatchArray |
84
| __Legacy.LegacyClass | LegacyClass |

0 commit comments

Comments
 (0)