Skip to content

Commit 3d5c156

Browse files
committed
basic support for .cjs files
1 parent 103f739 commit 3d5c156

File tree

10 files changed

+168
-11
lines changed

10 files changed

+168
-11
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@
144144
*
145145
* <ul>
146146
* <li>All JavaScript files, that is, files with one of the extensions supported by {@link
147-
* FileType#JS} (currently ".js", ".jsx", ".mjs", ".es6", ".es").
147+
* FileType#JS} (currently ".js", ".jsx", ".mjs", ".cjs", ".es6", ".es").
148148
* <li>All HTML files, that is, files with with one of the extensions supported by {@link
149149
* FileType#HTML} (currently ".htm", ".html", ".xhtm", ".xhtml", ".vue").
150150
* <li>All YAML files, that is, files with one of the extensions supported by {@link

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public String toString() {
5858
}
5959
},
6060

61-
JS(".js", ".jsx", ".mjs", ".es6", ".es") {
61+
JS(".js", ".jsx", ".mjs", ".cjs", ".es6", ".es") {
6262
@Override
6363
public IExtractor mkExtractor(ExtractorConfig config, ExtractorState state) {
6464
return new ScriptExtractor(config);

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

Lines changed: 1 addition & 1 deletion
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 = "2020-08-18";
46+
public static final String EXTRACTOR_VERSION = "2020-08-19";
4747

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

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ private boolean isAlwaysModule(String extension) {
1919
return extension.equals(".mjs") || extension.equals(".es6") || extension.equals(".es");
2020
}
2121

22+
/** True if files with the given extension should always be treated as CommonJS modules. */
23+
private boolean isAlwaysCommonJSModule(String extension) {
24+
return extension.equals(".cjs");
25+
}
26+
2227
@Override
2328
public LoCInfo extract(TextualExtractor textualExtractor) {
2429
LocationManager locationManager = textualExtractor.getLocationManager();
@@ -45,9 +50,13 @@ public LoCInfo extract(TextualExtractor textualExtractor) {
4550
}
4651

4752
// Some file extensions are interpreted as modules by default.
48-
if (isAlwaysModule(locationManager.getSourceFileExtension())) {
49-
if (config.getSourceType() == SourceType.AUTO)
53+
if (config.getSourceType() == SourceType.AUTO) {
54+
if (isAlwaysModule(locationManager.getSourceFileExtension())) {
5055
config = config.withSourceType(SourceType.MODULE);
56+
}
57+
if (isAlwaysCommonJSModule(locationManager.getSourceFileExtension())) {
58+
config = config.withSourceType(SourceType.COMMONJS_MODULE);
59+
}
5160
}
5261

5362
ScopeManager scopeManager =
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("Hello CommonJS");
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#10000=@"/tst4.cjs;sourcefile"
2+
files(#10000,"/tst4.cjs","tst4","cjs",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+
#20002=*
13+
lines(#20002,#20001,"console.log(""Hello CommonJS"");","
14+
")
15+
#20003=@"loc,{#10000},1,1,1,30"
16+
locations_default(#20003,#10000,1,1,1,30)
17+
hasLocation(#20002,#20003)
18+
numlines(#20001,1,1,0)
19+
#20004=*
20+
tokeninfo(#20004,6,#20001,0,"console")
21+
#20005=@"loc,{#10000},1,1,1,7"
22+
locations_default(#20005,#10000,1,1,1,7)
23+
hasLocation(#20004,#20005)
24+
#20006=*
25+
tokeninfo(#20006,8,#20001,1,".")
26+
#20007=@"loc,{#10000},1,8,1,8"
27+
locations_default(#20007,#10000,1,8,1,8)
28+
hasLocation(#20006,#20007)
29+
#20008=*
30+
tokeninfo(#20008,6,#20001,2,"log")
31+
#20009=@"loc,{#10000},1,9,1,11"
32+
locations_default(#20009,#10000,1,9,1,11)
33+
hasLocation(#20008,#20009)
34+
#20010=*
35+
tokeninfo(#20010,8,#20001,3,"(")
36+
#20011=@"loc,{#10000},1,12,1,12"
37+
locations_default(#20011,#10000,1,12,1,12)
38+
hasLocation(#20010,#20011)
39+
#20012=*
40+
tokeninfo(#20012,4,#20001,4,"""Hello CommonJS""")
41+
#20013=@"loc,{#10000},1,13,1,28"
42+
locations_default(#20013,#10000,1,13,1,28)
43+
hasLocation(#20012,#20013)
44+
#20014=*
45+
tokeninfo(#20014,8,#20001,5,")")
46+
#20015=@"loc,{#10000},1,29,1,29"
47+
locations_default(#20015,#10000,1,29,1,29)
48+
hasLocation(#20014,#20015)
49+
#20016=*
50+
tokeninfo(#20016,8,#20001,6,";")
51+
#20017=@"loc,{#10000},1,30,1,30"
52+
locations_default(#20017,#10000,1,30,1,30)
53+
hasLocation(#20016,#20017)
54+
#20018=*
55+
tokeninfo(#20018,0,#20001,7,"")
56+
#20019=@"loc,{#10000},2,1,2,0"
57+
locations_default(#20019,#10000,2,1,2,0)
58+
hasLocation(#20018,#20019)
59+
toplevels(#20001,0)
60+
#20020=@"loc,{#10000},1,1,2,0"
61+
locations_default(#20020,#10000,1,1,2,0)
62+
hasLocation(#20001,#20020)
63+
#20021=@"module;{#10000},1,1"
64+
scopes(#20021,3)
65+
scopenodes(#20001,#20021)
66+
scopenesting(#20021,#20000)
67+
#20022=@"var;{require};{#20021}"
68+
variables(#20022,"require",#20021)
69+
#20023=@"var;{module};{#20021}"
70+
variables(#20023,"module",#20021)
71+
#20024=@"var;{exports};{#20021}"
72+
variables(#20024,"exports",#20021)
73+
#20025=@"var;{__filename};{#20021}"
74+
variables(#20025,"__filename",#20021)
75+
#20026=@"var;{__dirname};{#20021}"
76+
variables(#20026,"__dirname",#20021)
77+
#20027=@"var;{arguments};{#20021}"
78+
variables(#20027,"arguments",#20021)
79+
isModule(#20001)
80+
#20028=*
81+
stmts(#20028,2,#20001,0,"console ... onJS"");")
82+
hasLocation(#20028,#20003)
83+
stmtContainers(#20028,#20001)
84+
#20029=*
85+
exprs(#20029,13,#20028,0,"console ... monJS"")")
86+
#20030=@"loc,{#10000},1,1,1,29"
87+
locations_default(#20030,#10000,1,1,1,29)
88+
hasLocation(#20029,#20030)
89+
enclosingStmt(#20029,#20028)
90+
exprContainers(#20029,#20001)
91+
#20031=*
92+
exprs(#20031,14,#20029,-1,"console.log")
93+
#20032=@"loc,{#10000},1,1,1,11"
94+
locations_default(#20032,#10000,1,1,1,11)
95+
hasLocation(#20031,#20032)
96+
enclosingStmt(#20031,#20028)
97+
exprContainers(#20031,#20001)
98+
#20033=*
99+
exprs(#20033,79,#20031,0,"console")
100+
hasLocation(#20033,#20005)
101+
enclosingStmt(#20033,#20028)
102+
exprContainers(#20033,#20001)
103+
literals("console","console",#20033)
104+
#20034=@"var;{console};{#20000}"
105+
variables(#20034,"console",#20000)
106+
bind(#20033,#20034)
107+
#20035=*
108+
exprs(#20035,0,#20031,1,"log")
109+
hasLocation(#20035,#20009)
110+
enclosingStmt(#20035,#20028)
111+
exprContainers(#20035,#20001)
112+
literals("log","log",#20035)
113+
#20036=*
114+
exprs(#20036,4,#20029,0,"""Hello CommonJS""")
115+
hasLocation(#20036,#20013)
116+
enclosingStmt(#20036,#20028)
117+
exprContainers(#20036,#20001)
118+
literals("Hello CommonJS","""Hello CommonJS""",#20036)
119+
#20037=*
120+
regexpterm(#20037,14,#20036,0,"Hello CommonJS")
121+
#20038=@"loc,{#10000},1,14,1,27"
122+
locations_default(#20038,#10000,1,14,1,27)
123+
hasLocation(#20037,#20038)
124+
regexpConstValue(#20037,"Hello CommonJS")
125+
#20039=*
126+
entry_cfg_node(#20039,#20001)
127+
#20040=@"loc,{#10000},1,1,1,0"
128+
locations_default(#20040,#10000,1,1,1,0)
129+
hasLocation(#20039,#20040)
130+
#20041=*
131+
exit_cfg_node(#20041,#20001)
132+
hasLocation(#20041,#20019)
133+
successor(#20028,#20033)
134+
successor(#20036,#20029)
135+
successor(#20035,#20031)
136+
successor(#20033,#20035)
137+
successor(#20031,#20036)
138+
successor(#20029,#20041)
139+
successor(#20039,#20028)
140+
numlines(#10000,1,1,0)
141+
filetype(#10000,"javascript")

javascript/ql/src/semmle/javascript/NodeJS.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ class Require extends CallExpr, Import {
221221
*
222222
* <ul>
223223
* <li> the file `c/p`;
224-
* <li> the file `c/p.{tsx,ts,jsx,es6,es,mjs}`;
224+
* <li> the file `c/p.{tsx,ts,jsx,es6,es,mjs,cjs}`;
225225
* <li> the file `c/p.js`;
226226
* <li> the file `c/p.json`;
227227
* <li> the file `c/p.node`;
@@ -230,12 +230,12 @@ class Require extends CallExpr, Import {
230230
* <li> if `c/p/package.json` exists and specifies a `main` module `m`:
231231
* <ul>
232232
* <li> the file `c/p/m`;
233-
* <li> the file `c/p/m.{tsx,ts,jsx,es6,es,mjs}`;
233+
* <li> the file `c/p/m.{tsx,ts,jsx,es6,es,mjs,cjs}`;
234234
* <li> the file `c/p/m.js`;
235235
* <li> the file `c/p/m.json`;
236236
* <li> the file `c/p/m.node`;
237237
* </ul>
238-
* <li> the file `c/p/index.{tsx,ts,jsx,es6,es,mjs}`;
238+
* <li> the file `c/p/index.{tsx,ts,jsx,es6,es,mjs,cjs}`;
239239
* <li> the file `c/p/index.js`;
240240
* <li> the file `c/p/index.json`;
241241
* <li> the file `c/p/index.node`.

javascript/ql/src/semmle/javascript/NodeModuleResolutionImpl.qll

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ int getFileExtensionPriority(string ext) {
2626
or
2727
ext = "mjs" and result = 5
2828
or
29-
ext = "js" and result = 6
29+
ext = "cjs" and result = 6
3030
or
31-
ext = "json" and result = 7
31+
ext = "js" and result = 7
3232
or
33-
ext = "node" and result = 8
33+
ext = "json" and result = 8
34+
or
35+
ext = "node" and result = 9
3436
}
3537

3638
int prioritiesPerCandidate() { result = 3 * (numberOfExtensions() + 1) }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var fs = require("fs");
2+
console.log("I'm a .cjs file!");
3+
console.log(fs);

javascript/ql/test/library-tests/ModuleTypes/tests.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
| commonjs.cjs:1:1:3:16 | <toplevel> | node |
12
| import.js:1:1:5:2 | <toplevel> | es2015 |
23
| mjs.mjs:1:1:1:32 | <toplevel> | es2015 |
34
| require.js:1:1:7:1 | <toplevel> | node |

0 commit comments

Comments
 (0)