Skip to content

Commit 824a50d

Browse files
committed
TS: fix extraction of symbols with unusual names
1 parent 242f8f2 commit 824a50d

File tree

11 files changed

+108
-12
lines changed

11 files changed

+108
-12
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,8 @@ export class TypeTable {
251251
* A symbol string is a `;`-separated string consisting of:
252252
* - a tag string, `root`, `member`, or `other`,
253253
* - an empty string or a `file:pos` string to distinguish this from symbols with other lexical roots,
254-
* - the unqualified name of the symbol,
255-
* - for non-root symbols, the ID of the parent symbol.
254+
* - the ID of the parent symbol, or an empty string if this is a root symbol,
255+
* - the unqualified name of the symbol.
256256
*
257257
* Symbol strings serve the same dual purpose as type strings (see `typeIds`).
258258
*/
@@ -667,11 +667,11 @@ export class TypeTable {
667667
private getSymbolString(symbol: AugmentedSymbol): string {
668668
let parent = symbol.parent;
669669
if (parent == null || parent.escapedName === ts.InternalSymbolName.Global) {
670-
return "root;" + this.getSymbolDeclarationString(symbol) + ";" + symbol.name;
670+
return "root;" + this.getSymbolDeclarationString(symbol) + ";;" + symbol.name;
671671
} else if (parent.exports != null && parent.exports.get(symbol.escapedName) === symbol) {
672-
return "member;;" + symbol.name + ";" + this.getSymbolId(parent);
672+
return "member;;" + this.getSymbolId(parent) + ";" + symbol.name;
673673
} else {
674-
return "other;" + this.getSymbolDeclarationString(symbol) + ";" + symbol.name + ";" + this.getSymbolId(parent);
674+
return "other;" + this.getSymbolDeclarationString(symbol) + ";" + this.getSymbolId(parent) + ";" + symbol.name;
675675
}
676676
}
677677

javascript/extractor/src/com/semmle/ts/extractor/TypeExtractor.java

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
import com.google.gson.JsonObject;
55
import com.semmle.util.trap.TrapWriter;
66
import com.semmle.util.trap.TrapWriter.Label;
7+
8+
import java.util.ArrayList;
9+
import java.util.Arrays;
710
import java.util.LinkedHashMap;
11+
import java.util.List;
812
import java.util.Map;
913

1014
/**
@@ -97,7 +101,7 @@ public void extract() {
97101
private void extractType(int id) {
98102
Label lbl = trapWriter.globalID("type;" + id);
99103
String contents = table.getTypeString(id);
100-
String[] parts = contents.split(";");
104+
String[] parts = split(contents);
101105
int kind = tagToKind.get(parts[0]);
102106
trapWriter.addTuple("types", lbl, kind, table.getTypeToStringValue(id));
103107
int firstChild = 1;
@@ -160,14 +164,15 @@ private void extractPropertyLookups(JsonObject lookups) {
160164
}
161165

162166
private void extractSymbol(int index) {
163-
// Format is: kind;decl;name[;parent]
164-
String[] parts = table.getSymbolString(index).split(";");
167+
// Format is: kind;decl;parent;name
168+
String[] parts = split(table.getSymbolString(index), 4);
165169
int kind = symbolKind.get(parts[0]);
166-
String name = parts[2];
170+
String name = parts[3];
167171
Label label = trapWriter.globalID("symbol;" + index);
168172
trapWriter.addTuple("symbols", label, kind, name);
169-
if (parts.length == 4) {
170-
Label parentLabel = trapWriter.globalID("symbol;" + parts[3]);
173+
String parentStr = parts[2];
174+
if (parentStr.length() > 0) {
175+
Label parentLabel = trapWriter.globalID("symbol;" + parentStr);
171176
trapWriter.addTuple("symbol_parent", label, parentLabel);
172177
}
173178
}
@@ -185,7 +190,7 @@ private void extractSymbolNameMapping(String relationName, JsonObject mappings)
185190
private void extractSignature(int index) {
186191
// Format is:
187192
// kind;numTypeParams;requiredParams;returnType(;paramName;paramType)*
188-
String[] parts = table.getSignatureString(index).split(";");
193+
String[] parts = split(table.getSignatureString(index));
189194
Label label = trapWriter.globalID("signature;" + index);
190195
int kind = Integer.parseInt(parts[0]);
191196
int numberOfTypeParameters = Integer.parseInt(parts[1]);
@@ -269,4 +274,35 @@ private void extractSelfTypes(JsonObject table) {
269274
trapWriter.globalID("type;" + typeId));
270275
}
271276
}
277+
278+
/** Like {@link #split(String)} without a limit. */
279+
private static String[] split(String input) {
280+
return split(input, -1);
281+
}
282+
283+
/**
284+
* Splits the input around the semicolon (<code>;</code>) character, preserving all empty
285+
* substrings.
286+
*
287+
* <p>At most <code>limit</code> substrings will be extracted. If the limit is reached, the last
288+
* substring will extend to the end of the string, possibly itself containing semicolons.
289+
*
290+
* <p>Note that the {@link String#split(String)} method does not preserve empty substrings at the
291+
* end of the string in case the string ends with a semicolon.
292+
*/
293+
private static String[] split(String input, int limit) {
294+
List<String> result = new ArrayList<String>();
295+
int lastPos = 0;
296+
for (int i = 0; i < input.length(); ++i) {
297+
if (input.charAt(i) == ';') {
298+
result.add(input.substring(lastPos, i));
299+
lastPos = i + 1;
300+
if (result.size() == limit - 1) break;
301+
}
302+
}
303+
result.add(input.substring(lastPos));
304+
return result.toArray(EMPTY_STRING_ARRAY);
305+
}
306+
307+
private static final String[] EMPTY_STRING_ARRAY = new String[0];
272308
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
| in unknown scope |
2+
| Array in global scope |
3+
| Intl in global scope |
4+
| Intl.CollatorOptions in global scope |
5+
| Intl.NumberFormatOptions in global scope |
6+
| MK in unknown scope |
7+
| Mapped in test.ts |
8+
| RegExp in global scope |
9+
| RegExpMatchArray in global scope |
10+
| fn in test.ts |
11+
| test.ts |
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import javascript
2+
3+
from CanonicalName name
4+
select name
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
type Mapped<MK extends string = ''> = {
2+
    [mk in MK]: string
3+
};
4+
5+
export function fn(ev: Mapped) {
6+
    const props: Mapped = {
7+
        ...ev
8+
    };
9+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"include": ["."]
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| Bar.Foo in global scope | Bar in global scope |
2+
| Intl.CollatorOptions in global scope | Intl in global scope |
3+
| Intl.NumberFormatOptions in global scope | Intl in global scope |
4+
| fn in test.ts | test.ts |
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import javascript
2+
3+
from CanonicalName typename
4+
select typename, typename.getParent()
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
type Mapped<MK extends string = ';'> = {
2+
    [mk in MK]: string
3+
};
4+
5+
export function fn(ev: Mapped) {
6+
    const props: Mapped = {
7+
        ...ev
8+
    };
9+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"include": ["."]
3+
}

0 commit comments

Comments
 (0)