Skip to content

Commit fb499b0

Browse files
authored
Merge pull request #1138 from asger-semmle/ts-import-namespace-as-type
Approved by xiemaisi
2 parents 23d77f3 + 1a6c95c commit fb499b0

File tree

9 files changed

+138
-129
lines changed

9 files changed

+138
-129
lines changed

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import com.semmle.js.ast.Identifier;
4343
import com.semmle.js.ast.IfStatement;
4444
import com.semmle.js.ast.ImportDeclaration;
45-
import com.semmle.js.ast.ImportNamespaceSpecifier;
4645
import com.semmle.js.ast.ImportSpecifier;
4746
import com.semmle.js.ast.InvokeExpression;
4847
import com.semmle.js.ast.JumpStatement;
@@ -1449,13 +1448,7 @@ public Label visit(ImportDeclaration nd, Context c) {
14491448
public Label visit(ImportSpecifier nd, Context c) {
14501449
Label lbl = super.visit(nd, c);
14511450
visit(nd.getImported(), lbl, 0, IdContext.label);
1452-
visit(
1453-
nd.getLocal(),
1454-
lbl,
1455-
1,
1456-
nd instanceof ImportNamespaceSpecifier
1457-
? IdContext.varAndNamespaceDecl
1458-
: IdContext.varAndTypeAndNamespaceDecl);
1451+
visit(nd.getLocal(), lbl, 1, IdContext.varAndTypeAndNamespaceDecl);
14591452
return lbl;
14601453
}
14611454

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class Main {
3737
* A version identifier that should be updated every time the extractor changes in such a way that
3838
* it may produce different tuples for the same file under the same {@link ExtractorConfig}.
3939
*/
40-
public static final String EXTRACTOR_VERSION = "2019-03-13";
40+
public static final String EXTRACTOR_VERSION = "2019-03-20";
4141

4242
public static final Pattern NEWLINE = Pattern.compile("\n");
4343

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import com.semmle.js.ast.Identifier;
1818
import com.semmle.js.ast.IfStatement;
1919
import com.semmle.js.ast.ImportDeclaration;
20-
import com.semmle.js.ast.ImportNamespaceSpecifier;
2120
import com.semmle.js.ast.ImportSpecifier;
2221
import com.semmle.js.ast.LabeledStatement;
2322
import com.semmle.js.ast.LetExpression;
@@ -559,9 +558,7 @@ public Void visit(ImportDeclaration nd, Void c) {
559558

560559
@Override
561560
public Void visit(ImportSpecifier nd, Void c) {
562-
return visit(
563-
nd.getLocal(),
564-
nd instanceof ImportNamespaceSpecifier ? DeclKind.varAndNamespace : DeclKind.all);
561+
return visit(nd.getLocal(), DeclKind.all);
565562
}
566563

567564
@Override

javascript/extractor/tests/es2015/output/trap/import5.js.trap

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -65,45 +65,49 @@ isModule(#20001)
6565
isES2015Module(#20001)
6666
#20021=@"var;{foo};{#20020}"
6767
variables(#20021,"foo",#20020)
68-
#20022=@"local_namespace_name;{foo};{#20020}"
69-
local_namespace_names(#20022,"foo",#20020)
68+
#20022=@"local_type_name;{foo};{#20020}"
69+
local_type_names(#20022,"foo",#20020)
70+
#20023=@"local_namespace_name;{foo};{#20020}"
71+
local_namespace_names(#20023,"foo",#20020)
7072
variables(#20021,"foo",#20020)
71-
local_namespace_names(#20022,"foo",#20020)
72-
#20023=*
73-
stmts(#20023,27,#20001,0,"import ... 'foo';")
74-
hasLocation(#20023,#20003)
75-
stmtContainers(#20023,#20001)
73+
local_type_names(#20022,"foo",#20020)
74+
local_namespace_names(#20023,"foo",#20020)
7675
#20024=*
77-
exprs(#20024,4,#20023,-1,"'foo'")
78-
hasLocation(#20024,#20015)
79-
enclosingStmt(#20024,#20023)
80-
exprContainers(#20024,#20001)
81-
literals("foo","'foo'",#20024)
76+
stmts(#20024,27,#20001,0,"import ... 'foo';")
77+
hasLocation(#20024,#20003)
78+
stmtContainers(#20024,#20001)
8279
#20025=*
83-
exprs(#20025,85,#20023,0,"* as foo")
84-
#20026=@"loc,{#10000},1,8,1,15"
85-
locations_default(#20026,#10000,1,8,1,15)
86-
hasLocation(#20025,#20026)
87-
enclosingStmt(#20025,#20023)
80+
exprs(#20025,4,#20024,-1,"'foo'")
81+
hasLocation(#20025,#20015)
82+
enclosingStmt(#20025,#20024)
8883
exprContainers(#20025,#20001)
89-
#20027=*
90-
exprs(#20027,78,#20025,1,"foo")
91-
hasLocation(#20027,#20011)
92-
enclosingStmt(#20027,#20023)
93-
exprContainers(#20027,#20001)
94-
literals("foo","foo",#20027)
95-
decl(#20027,#20021)
96-
namespacedecl(#20027,#20022)
84+
literals("foo","'foo'",#20025)
85+
#20026=*
86+
exprs(#20026,85,#20024,0,"* as foo")
87+
#20027=@"loc,{#10000},1,8,1,15"
88+
locations_default(#20027,#10000,1,8,1,15)
89+
hasLocation(#20026,#20027)
90+
enclosingStmt(#20026,#20024)
91+
exprContainers(#20026,#20001)
9792
#20028=*
98-
entry_cfg_node(#20028,#20001)
99-
#20029=@"loc,{#10000},1,1,1,0"
100-
locations_default(#20029,#10000,1,1,1,0)
101-
hasLocation(#20028,#20029)
102-
#20030=*
103-
exit_cfg_node(#20030,#20001)
104-
hasLocation(#20030,#20019)
105-
successor(#20023,#20030)
106-
successor(#20025,#20023)
107-
successor(#20028,#20025)
93+
exprs(#20028,78,#20026,1,"foo")
94+
hasLocation(#20028,#20011)
95+
enclosingStmt(#20028,#20024)
96+
exprContainers(#20028,#20001)
97+
literals("foo","foo",#20028)
98+
decl(#20028,#20021)
99+
typedecl(#20028,#20022)
100+
namespacedecl(#20028,#20023)
101+
#20029=*
102+
entry_cfg_node(#20029,#20001)
103+
#20030=@"loc,{#10000},1,1,1,0"
104+
locations_default(#20030,#10000,1,1,1,0)
105+
hasLocation(#20029,#20030)
106+
#20031=*
107+
exit_cfg_node(#20031,#20001)
108+
hasLocation(#20031,#20019)
109+
successor(#20024,#20031)
110+
successor(#20026,#20024)
111+
successor(#20029,#20026)
108112
numlines(#10000,1,1,0)
109113
filetype(#10000,"javascript")

javascript/extractor/tests/ts/output/trap/importExport.ts.trap

Lines changed: 84 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -122,98 +122,102 @@ isES2015Module(#20001)
122122
variables(#20042,"Something",#20041)
123123
#20043=@"var;{importExport};{#20041}"
124124
variables(#20043,"importExport",#20041)
125-
#20044=@"local_type_name;{importExport};{#20041}"
126-
local_type_names(#20044,"importExport",#20041)
127-
#20045=@"local_namespace_name;{Something};{#20041}"
128-
local_namespace_names(#20045,"Something",#20041)
129-
#20046=@"local_namespace_name;{importExport};{#20041}"
130-
local_namespace_names(#20046,"importExport",#20041)
125+
#20044=@"local_type_name;{Something};{#20041}"
126+
local_type_names(#20044,"Something",#20041)
127+
#20045=@"local_type_name;{importExport};{#20041}"
128+
local_type_names(#20045,"importExport",#20041)
129+
#20046=@"local_namespace_name;{Something};{#20041}"
130+
local_namespace_names(#20046,"Something",#20041)
131+
#20047=@"local_namespace_name;{importExport};{#20041}"
132+
local_namespace_names(#20047,"importExport",#20041)
131133
variables(#20042,"Something",#20041)
132134
variables(#20043,"importExport",#20041)
133-
local_type_names(#20044,"importExport",#20041)
134-
local_namespace_names(#20045,"Something",#20041)
135-
local_namespace_names(#20046,"importExport",#20041)
136-
#20047=*
137-
stmts(#20047,27,#20001,0,"import ... where';")
138-
hasLocation(#20047,#20003)
139-
stmtContainers(#20047,#20001)
135+
local_type_names(#20044,"Something",#20041)
136+
local_type_names(#20045,"importExport",#20041)
137+
local_namespace_names(#20046,"Something",#20041)
138+
local_namespace_names(#20047,"importExport",#20041)
140139
#20048=*
141-
exprs(#20048,4,#20047,-1,"'somewhere'")
142-
hasLocation(#20048,#20019)
143-
enclosingStmt(#20048,#20047)
144-
exprContainers(#20048,#20001)
145-
literals("somewhere","'somewhere'",#20048)
140+
stmts(#20048,27,#20001,0,"import ... where';")
141+
hasLocation(#20048,#20003)
142+
stmtContainers(#20048,#20001)
146143
#20049=*
147-
exprs(#20049,85,#20047,0,"* as Something")
148-
#20050=@"loc,{#10000},1,8,1,21"
149-
locations_default(#20050,#10000,1,8,1,21)
150-
hasLocation(#20049,#20050)
151-
enclosingStmt(#20049,#20047)
144+
exprs(#20049,4,#20048,-1,"'somewhere'")
145+
hasLocation(#20049,#20019)
146+
enclosingStmt(#20049,#20048)
152147
exprContainers(#20049,#20001)
153-
#20051=*
154-
exprs(#20051,78,#20049,1,"Something")
155-
hasLocation(#20051,#20015)
156-
enclosingStmt(#20051,#20047)
157-
exprContainers(#20051,#20001)
158-
literals("Something","Something",#20051)
159-
decl(#20051,#20042)
160-
namespacedecl(#20051,#20045)
148+
literals("somewhere","'somewhere'",#20049)
149+
#20050=*
150+
exprs(#20050,85,#20048,0,"* as Something")
151+
#20051=@"loc,{#10000},1,8,1,21"
152+
locations_default(#20051,#10000,1,8,1,21)
153+
hasLocation(#20050,#20051)
154+
enclosingStmt(#20050,#20048)
155+
exprContainers(#20050,#20001)
161156
#20052=*
162-
stmts(#20052,30,#20001,1,"export ... thingy;")
163-
hasLocation(#20052,#20007)
164-
stmtContainers(#20052,#20001)
157+
exprs(#20052,78,#20050,1,"Something")
158+
hasLocation(#20052,#20015)
159+
enclosingStmt(#20052,#20048)
160+
exprContainers(#20052,#20001)
161+
literals("Something","Something",#20052)
162+
decl(#20052,#20042)
163+
typedecl(#20052,#20044)
164+
namespacedecl(#20052,#20046)
165165
#20053=*
166-
stmts(#20053,32,#20052,-1,"import ... thingy;")
167-
#20054=@"loc,{#10000},3,8,3,46"
168-
locations_default(#20054,#10000,3,8,3,46)
169-
hasLocation(#20053,#20054)
166+
stmts(#20053,30,#20001,1,"export ... thingy;")
167+
hasLocation(#20053,#20007)
170168
stmtContainers(#20053,#20001)
171-
#20055=*
172-
exprs(#20055,78,#20053,0,"importExport")
173-
hasLocation(#20055,#20027)
174-
enclosingStmt(#20055,#20053)
175-
exprContainers(#20055,#20001)
176-
literals("importExport","importExport",#20055)
177-
decl(#20055,#20043)
178-
typedecl(#20055,#20044)
179-
namespacedecl(#20055,#20046)
169+
#20054=*
170+
stmts(#20054,32,#20053,-1,"import ... thingy;")
171+
#20055=@"loc,{#10000},3,8,3,46"
172+
locations_default(#20055,#10000,3,8,3,46)
173+
hasLocation(#20054,#20055)
174+
stmtContainers(#20054,#20001)
180175
#20056=*
181-
exprs(#20056,14,#20053,1,"Something.thingy")
182-
#20057=@"loc,{#10000},3,30,3,45"
183-
locations_default(#20057,#10000,3,30,3,45)
184-
hasLocation(#20056,#20057)
185-
enclosingStmt(#20056,#20053)
176+
exprs(#20056,78,#20054,0,"importExport")
177+
hasLocation(#20056,#20027)
178+
enclosingStmt(#20056,#20054)
186179
exprContainers(#20056,#20001)
187-
#20058=*
188-
exprs(#20058,103,#20056,0,"Something")
189-
hasLocation(#20058,#20031)
190-
enclosingStmt(#20058,#20053)
191-
exprContainers(#20058,#20001)
192-
literals("Something","Something",#20058)
193-
namespacebind(#20058,#20045)
194-
bind(#20058,#20042)
180+
literals("importExport","importExport",#20056)
181+
decl(#20056,#20043)
182+
typedecl(#20056,#20045)
183+
namespacedecl(#20056,#20047)
184+
#20057=*
185+
exprs(#20057,14,#20054,1,"Something.thingy")
186+
#20058=@"loc,{#10000},3,30,3,45"
187+
locations_default(#20058,#10000,3,30,3,45)
188+
hasLocation(#20057,#20058)
189+
enclosingStmt(#20057,#20054)
190+
exprContainers(#20057,#20001)
195191
#20059=*
196-
exprs(#20059,0,#20056,1,"thingy")
197-
hasLocation(#20059,#20035)
198-
enclosingStmt(#20059,#20053)
192+
exprs(#20059,103,#20057,0,"Something")
193+
hasLocation(#20059,#20031)
194+
enclosingStmt(#20059,#20054)
199195
exprContainers(#20059,#20001)
200-
literals("thingy","thingy",#20059)
196+
literals("Something","Something",#20059)
197+
namespacebind(#20059,#20046)
198+
bind(#20059,#20042)
201199
#20060=*
202-
entry_cfg_node(#20060,#20001)
203-
#20061=@"loc,{#10000},1,1,1,0"
204-
locations_default(#20061,#10000,1,1,1,0)
205-
hasLocation(#20060,#20061)
206-
#20062=*
207-
exit_cfg_node(#20062,#20001)
208-
hasLocation(#20062,#20039)
209-
successor(#20052,#20055)
210-
successor(#20059,#20056)
211-
successor(#20058,#20059)
212-
successor(#20056,#20053)
213-
successor(#20055,#20058)
214-
successor(#20053,#20062)
215-
successor(#20047,#20052)
216-
successor(#20049,#20047)
217-
successor(#20060,#20049)
200+
exprs(#20060,0,#20057,1,"thingy")
201+
hasLocation(#20060,#20035)
202+
enclosingStmt(#20060,#20054)
203+
exprContainers(#20060,#20001)
204+
literals("thingy","thingy",#20060)
205+
#20061=*
206+
entry_cfg_node(#20061,#20001)
207+
#20062=@"loc,{#10000},1,1,1,0"
208+
locations_default(#20062,#10000,1,1,1,0)
209+
hasLocation(#20061,#20062)
210+
#20063=*
211+
exit_cfg_node(#20063,#20001)
212+
hasLocation(#20063,#20039)
213+
successor(#20053,#20056)
214+
successor(#20060,#20057)
215+
successor(#20059,#20060)
216+
successor(#20057,#20054)
217+
successor(#20056,#20059)
218+
successor(#20054,#20063)
219+
successor(#20048,#20053)
220+
successor(#20050,#20048)
221+
successor(#20061,#20050)
218222
numlines(#10000,3,2,0)
219223
filetype(#10000,"typescript")

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ class TypeDecl extends Identifier, TypeRef, LexicalDecl {
359359
TypeDecl() {
360360
this = any(ClassOrInterface ci).getIdentifier() or
361361
this = any(TypeParameter tp).getIdentifier() or
362-
this = any(ImportSpecifier im | not im instanceof ImportNamespaceSpecifier).getLocal() or
362+
this = any(ImportSpecifier im).getLocal() or
363363
this = any(ImportEqualsDeclaration im).getId() or
364364
this = any(TypeAliasDeclaration td).getIdentifier() or
365365
this = any(EnumDeclaration ed).getIdentifier() or

javascript/ql/test/library-tests/TypeScript/LocalTypeResolution/ResolveTypeNames.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
| exports.ts:16:5:16:8 | Enum | exports.ts:7:6:7:9 | Enum |
55
| namespaceDecls.ts:38:8:38:8 | A | namespaceDecls.ts:1:8:1:8 | A |
66
| namespaceDecls.ts:38:8:38:8 | A | namespaceDecls.ts:6:11:6:11 | A |
7+
| namespaceDecls.ts:39:8:39:8 | E | namespaceDecls.ts:4:13:4:13 | E |
78
| namespaceDecls.ts:39:8:39:8 | E | namespaceDecls.ts:7:11:7:11 | E |
89
| tst.ts:6:9:6:9 | I | tst.ts:4:11:4:11 | I |
910
| tst.ts:8:11:8:11 | I | tst.ts:4:11:4:11 | I |

javascript/ql/test/query-tests/Declarations/UnusedVariable/UnusedVariable.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
| importWithoutPragma.jsx:1:1:1:27 | import ... react'; | Unused import h. |
88
| multi-imports.js:1:1:1:29 | import ... om 'x'; | Unused imports a, b, d. |
99
| multi-imports.js:2:1:2:42 | import ... om 'x'; | Unused imports alphabetically, ordered. |
10+
| namespaceImportAsType.ts:3:1:3:23 | import ... om "z"; | Unused import Z. |
1011
| require-react-in-other-scope.js:2:9:2:13 | React | Unused variable React. |
1112
| typeoftype.ts:9:7:9:7 | y | Unused variable y. |
1213
| underscore.js:6:7:6:7 | e | Unused variable e. |
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import * as X from "x"; // OK
2+
import * as Y from "y"; // OK
3+
import * as Z from "z"; // NOT OK
4+
5+
function f(x: X) {}
6+
function g(x: Y.T) {}
7+
8+
f(null);
9+
g(null);

0 commit comments

Comments
 (0)