Skip to content

Commit 2936e1a

Browse files
authored
Merge pull request #9457 from asgerf/js/madman-prep2
JS: Some more improvements to d.ts file analysis
2 parents b16fcb7 + 15278fe commit 2936e1a

File tree

12 files changed

+2542
-33
lines changed

12 files changed

+2542
-33
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,6 @@ go/main
5858
# node_modules folders except in the JS test suite
5959
node_modules/
6060
!/javascript/ql/test/**/node_modules/
61+
62+
# Temporary folders for working with generated models
63+
.model-temp

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,7 @@ export class TypeTable {
10681068
let superType = this.typeChecker.getTypeFromTypeNode(typeExpr);
10691069
if (superType == null) continue;
10701070
let baseTypeSymbol = superType.symbol;
1071+
baseTypeSymbol = (baseTypeSymbol as any)?.type?.symbol ?? baseTypeSymbol;
10711072
if (baseTypeSymbol == null) continue;
10721073
let baseId = this.getSymbolId(baseTypeSymbol);
10731074
// Note: take care not to perform a recursive call between the two `push` calls.

javascript/extractor/src/com/semmle/js/extractor/ASTExtractor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2191,6 +2191,7 @@ public Label visit(ExternalModuleDeclaration nd, Context c) {
21912191
visitAll(nd.getBody(), key);
21922192
contextManager.leaveContainer();
21932193
scopeManager.leaveScope();
2194+
emitNodeSymbol(nd, key);
21942195
return key;
21952196
}
21962197

javascript/extractor/src/com/semmle/js/extractor/Main.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class Main {
4343
* A version identifier that should be updated every time the extractor changes in such a way that
4444
* it may produce different tuples for the same file under the same {@link ExtractorConfig}.
4545
*/
46-
public static final String EXTRACTOR_VERSION = "2022-05-24";
46+
public static final String EXTRACTOR_VERSION = "2022-06-08";
4747

4848
public static final Pattern NEWLINE = Pattern.compile("\n");
4949

@@ -153,7 +153,7 @@ public void run(String[] args) {
153153
ensureFileIsExtracted(file, ap);
154154
}
155155
}
156-
156+
157157
TypeScriptParser tsParser = extractorState.getTypeScriptParser();
158158
tsParser.setTypescriptRam(extractorConfig.getTypeScriptRam());
159159
if (containsTypeScriptFiles()) {
@@ -460,7 +460,7 @@ private static TypeScriptMode getTypeScriptMode(ArgsParser ap) {
460460
if (ap.has(P_TYPESCRIPT)) return TypeScriptMode.BASIC;
461461
return TypeScriptMode.NONE;
462462
}
463-
463+
464464
private Path inferSourceRoot(ArgsParser ap) {
465465
List<File> files = getFilesArg(ap);
466466
Path sourceRoot = files.iterator().next().toPath().toAbsolutePath().getParent();

javascript/extractor/src/com/semmle/ts/ast/ExternalModuleDeclaration.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
import java.util.List;
88

99
/** A statement of form <code>declare module "X" {...}</code>. */
10-
public class ExternalModuleDeclaration extends Statement {
10+
public class ExternalModuleDeclaration extends Statement implements INodeWithSymbol {
1111
private final Literal name;
1212
private final List<Statement> body;
13+
private int symbol = -1;
1314

1415
public ExternalModuleDeclaration(SourceLocation loc, Literal name, List<Statement> body) {
1516
super("ExternalModuleDeclaration", loc);
@@ -29,4 +30,14 @@ public Literal getName() {
2930
public List<Statement> getBody() {
3031
return body;
3132
}
33+
34+
@Override
35+
public int getSymbol() {
36+
return this.symbol;
37+
}
38+
39+
@Override
40+
public void setSymbol(int symbol) {
41+
this.symbol = symbol;
42+
}
3243
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ private Node convertNodeUntyped(JsonObject node, String defaultKind) throws Pars
591591
return convertTryStatement(node, loc);
592592
case "TupleType":
593593
return convertTupleType(node, loc);
594-
case "NamedTupleMember":
594+
case "NamedTupleMember":
595595
return convertNamedTupleMember(node, loc);
596596
case "TypeAliasDeclaration":
597597
return convertTypeAliasDeclaration(node, loc);
@@ -1710,7 +1710,9 @@ private Node convertNamespaceDeclaration(JsonObject node, SourceLocation loc) th
17101710
}
17111711
if (nameNode instanceof Literal) {
17121712
// Declaration of form: declare module "X" {...}
1713-
return new ExternalModuleDeclaration(loc, (Literal) nameNode, body);
1713+
ExternalModuleDeclaration decl = new ExternalModuleDeclaration(loc, (Literal) nameNode, body);
1714+
attachSymbolInformation(decl, node);
1715+
return decl;
17141716
}
17151717
if (hasFlag(node, "GlobalAugmentation")) {
17161718
// Declaration of form: declare global {...}

javascript/ql/lib/semmle/javascript/NPM.qll

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,35 @@ class PackageJson extends JsonObject {
180180
Module getMainModule() {
181181
result = min(Module m, int prio | m.getFile() = resolveMainModule(this, prio) | m order by prio)
182182
}
183+
184+
/**
185+
* Gets the `types` or `typings` field of this package.
186+
*/
187+
string getTypings() { result = this.getPropStringValue(["types", "typings"]) }
188+
189+
/**
190+
* Gets the file containing the typings of this package, which can either be from the `types` or
191+
* `typings` field, or derived from the `main` or `module` fields.
192+
*/
193+
File getTypingsFile() {
194+
result =
195+
TypingsModulePathString::of(this).resolve(this.getFile().getParentContainer()).getContainer()
196+
or
197+
not exists(TypingsModulePathString::of(this)) and
198+
exists(File mainFile |
199+
mainFile = this.getMainModule().getFile() and
200+
result =
201+
mainFile
202+
.getParentContainer()
203+
.getFile(mainFile.getStem().regexpReplaceAll("\\.d$", "") + ".d.ts")
204+
)
205+
}
206+
207+
/**
208+
* Gets the module containing the typings of this package, which can either be from the `types` or
209+
* `typings` field, or derived from the `main` or `module` fields.
210+
*/
211+
Module getTypingsModule() { result.getFile() = this.getTypingsFile() }
183212
}
184213

185214
/** DEPRECATED: Alias for PackageJson */

javascript/ql/lib/semmle/javascript/NodeModuleResolutionImpl.qll

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,29 @@ private class FilesPath extends PathExpr, @json_string {
198198
private module FilesPath {
199199
FilesPath of(PackageJson pkg) { result.getPackageJson() = pkg }
200200
}
201+
202+
/**
203+
* A JSON string in a `package.json` file specifying the path of the
204+
* TypeScript typings entry point.
205+
*/
206+
class TypingsModulePathString extends PathString {
207+
PackageJson pkg;
208+
209+
TypingsModulePathString() {
210+
this = pkg.getTypings()
211+
or
212+
not exists(pkg.getTypings()) and
213+
this = pkg.getMain().regexpReplaceAll("\\.[mc]?js$", ".d.ts")
214+
}
215+
216+
/** Gets the `package.json` file containing this path. */
217+
PackageJson getPackageJson() { result = pkg }
218+
219+
override Folder getARootFolder() { result = pkg.getFile().getParentContainer() }
220+
}
221+
222+
/** Companion module to the `TypingsModulePathString` class. */
223+
module TypingsModulePathString {
224+
/** Get the typings path for the given `package.json` file. */
225+
TypingsModulePathString of(PackageJson pkg) { result.getPackageJson() = pkg }
226+
}

javascript/ql/lib/semmlecode.javascript.dbscheme

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,33 @@
33
/** Files and folders **/
44

55
@location = @location_default;
6-
6+
77
locations_default(unique int id: @location_default,
88
int file: @file ref,
99
int beginLine: int ref,
1010
int beginColumn: int ref,
1111
int endLine: int ref,
1212
int endColumn: int ref
1313
);
14-
14+
1515
@sourceline = @locatable;
16-
16+
1717
numlines(int element_id: @sourceline ref,
1818
int num_lines: int ref,
1919
int num_code: int ref,
2020
int num_comment: int ref
2121
);
22-
22+
2323
files(unique int id: @file,
2424
varchar(900) name: string ref);
25-
25+
2626
folders(unique int id: @folder,
2727
varchar(900) name: string ref);
28-
29-
28+
29+
3030
@container = @folder | @file ;
31-
32-
31+
32+
3333
containerparent(int parent: @container ref,
3434
unique int child: @container ref);
3535

@@ -39,14 +39,14 @@ duplicateCode(
3939
unique int id : @duplication,
4040
varchar(900) relativePath : string ref,
4141
int equivClass : int ref);
42-
42+
4343
similarCode(
4444
unique int id : @similarity,
4545
varchar(900) relativePath : string ref,
4646
int equivClass : int ref);
47-
47+
4848
@duplication_or_similarity = @duplication | @similarity;
49-
49+
5050
tokens(
5151
int id : @duplication_or_similarity ref,
5252
int offset : int ref,
@@ -63,9 +63,9 @@ externalData(
6363
int column: int ref,
6464
varchar(900) value : string ref
6565
);
66-
66+
6767
snapshotDate(unique date snapshotDate : date ref);
68-
68+
6969
sourceLocationPrefix(varchar(900) prefix : string ref);
7070

7171
/** Version control data **/
@@ -77,18 +77,18 @@ svnentries(
7777
date revisionDate : date ref,
7878
int changeSize : int ref
7979
);
80-
80+
8181
svnaffectedfiles(
8282
int id : @svnentry ref,
8383
int file : @file ref,
8484
varchar(500) action : string ref
8585
);
86-
86+
8787
svnentrymsg(
8888
int id : @svnentry ref,
8989
varchar(500) message : string ref
9090
);
91-
91+
9292
svnchurn(
9393
int commit : @svnentry ref,
9494
int file : @file ref,
@@ -134,15 +134,15 @@ xml_element_parent_expression(
134134

135135
// statements
136136
#keyset[parent, idx]
137-
stmts (unique int id: @stmt,
137+
stmts (unique int id: @stmt,
138138
int kind: int ref,
139-
int parent: @stmt_parent ref,
139+
int parent: @stmt_parent ref,
140140
int idx: int ref,
141141
varchar(900) tostring: string ref);
142142

143143
stmt_containers (unique int stmt: @stmt ref,
144144
int container: @stmt_container ref);
145-
145+
146146
jump_targets (unique int jump: @stmt ref,
147147
int target: @stmt ref);
148148

@@ -217,7 +217,7 @@ exprs (unique int id: @expr,
217217
literals (varchar(900) value: string ref,
218218
varchar(900) raw: string ref,
219219
unique int expr: @expr_or_type ref);
220-
220+
221221
enclosing_stmt (unique int expr: @expr_or_type ref,
222222
int stmt: @stmt ref);
223223

@@ -445,7 +445,7 @@ case @scope.kind of
445445

446446
scopenodes (unique int node: @ast_node ref,
447447
int scope: @scope ref);
448-
448+
449449
scopenesting (unique int inner: @scope ref,
450450
int outer: @scope ref);
451451

@@ -667,7 +667,7 @@ has_asserts_keyword(int node: @predicate_typeexpr ref);
667667

668668
@typed_ast_node = @expr | @typeexpr | @function;
669669
ast_node_type(
670-
unique int node: @typed_ast_node ref,
670+
unique int node: @typed_ast_node ref,
671671
int typ: @type ref);
672672

673673
declared_function_signature(
@@ -713,7 +713,7 @@ case @symbol.kind of
713713
;
714714

715715
@type_with_symbol = @type_reference | @typevariable_type | @typeof_type | @unique_symbol_type;
716-
@ast_node_with_symbol = @type_definition | @namespace_definition | @toplevel | @typeaccess | @namespace_access | @var_decl | @function | @invokeexpr | @import_declaration | @external_module_reference;
716+
@ast_node_with_symbol = @type_definition | @namespace_definition | @toplevel | @typeaccess | @namespace_access | @var_decl | @function | @invokeexpr | @import_declaration | @external_module_reference | @external_module_declaration;
717717

718718
ast_node_symbol(
719719
unique int node: @ast_node_with_symbol ref,
@@ -846,15 +846,15 @@ js_parse_errors (unique int id: @js_parse_error,
846846
int toplevel: @toplevel ref,
847847
varchar(900) message: string ref,
848848
varchar(900) line: string ref);
849-
849+
850850
// regular expressions
851851
#keyset[parent, idx]
852852
regexpterm (unique int id: @regexpterm,
853853
int kind: int ref,
854854
int parent: @regexpparent ref,
855855
int idx: int ref,
856856
varchar(900) tostring: string ref);
857-
857+
858858
@regexpparent = @regexpterm | @regexp_literal | @string_literal | @add_expr;
859859

860860
case @regexpterm.kind of
@@ -972,7 +972,7 @@ case @json_value.kind of
972972
// locations
973973
@ast_node = @toplevel | @stmt | @expr | @property | @typeexpr;
974974

975-
@locatable = @file
975+
@locatable = @file
976976
| @ast_node
977977
| @comment
978978
| @line

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy