Skip to content

Commit 0ccbaf9

Browse files
author
Max Schaefer
committed
JavaScript: Handle empty package.json files gracefully.
1 parent 4deb43f commit 0ccbaf9

File tree

5 files changed

+48
-1
lines changed

5 files changed

+48
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,11 @@ private String getPackageType(File folder) {
126126
}
127127
try {
128128
BufferedReader reader = new BufferedReader(new FileReader(file));
129-
String result = new Gson().fromJson(reader, PackageJSON.class).type;
129+
PackageJSON pkgjson = new Gson().fromJson(reader, PackageJSON.class);
130+
if (pkgjson == null) {
131+
return null;
132+
}
133+
String result = pkgjson.type;
130134
packageTypeCache.put(folder, Optional.ofNullable(result));
131135
return result;
132136
} catch (IOException | JsonSyntaxException e) {

javascript/extractor/tests/moduleTypes3/input/package.json

Whitespace-only changes.

javascript/extractor/tests/moduleTypes3/input/tst.js

Whitespace-only changes.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#10000=@"/package.json;sourcefile"
2+
files(#10000,"/package.json","package","json",0)
3+
#10001=@"/;folder"
4+
folders(#10001,"/","")
5+
containerparent(#10001,#10000)
6+
#10002=@"loc,{#10000},0,0,0,0"
7+
locations_default(#10002,#10000,0,0,0,0)
8+
hasLocation(#10000,#10002)
9+
#20000=*
10+
json_errors(#20000,"Error: Unexpected token")
11+
#20001=@"loc,{#10000},1,1,1,1"
12+
locations_default(#20001,#10000,1,1,1,1)
13+
hasLocation(#20000,#20001)
14+
numlines(#10000,0,0,0)
15+
filetype(#10000,"json")
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#10000=@"/tst.js;sourcefile"
2+
files(#10000,"/tst.js","tst","js",0)
3+
#10001=@"/;folder"
4+
folders(#10001,"/","")
5+
containerparent(#10001,#10000)
6+
#10002=@"loc,{#10000},0,0,0,0"
7+
locations_default(#10002,#10000,0,0,0,0)
8+
hasLocation(#10000,#10002)
9+
#20000=@"global_scope"
10+
scopes(#20000,0)
11+
#20001=@"script;{#10000},1,1"
12+
numlines(#20001,0,0,0)
13+
#20002=*
14+
tokeninfo(#20002,0,#20001,0,"")
15+
#20003=@"loc,{#10000},1,1,1,0"
16+
locations_default(#20003,#10000,1,1,1,0)
17+
hasLocation(#20002,#20003)
18+
toplevels(#20001,0)
19+
hasLocation(#20001,#20003)
20+
#20004=*
21+
entry_cfg_node(#20004,#20001)
22+
hasLocation(#20004,#20003)
23+
#20005=*
24+
exit_cfg_node(#20005,#20001)
25+
hasLocation(#20005,#20003)
26+
successor(#20004,#20005)
27+
numlines(#10000,0,0,0)
28+
filetype(#10000,"javascript")

0 commit comments

Comments
 (0)