Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e46e2b2

Browse files
authored
Revert "JS: Add support for Closure modules"
1 parent b8be66e commit e46e2b2

105 files changed

Lines changed: 124 additions & 3517 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

change-notes/1.20/analysis-javascript.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
* Type inference for function calls has been improved. This may give additional results for queries that rely on type inference.
1515

16-
* The [Closure-Library](https://github.com/google/closure-library/wiki/goog.module:-an-ES6-module-like-alternative-to-goog.provide) module system is now supported.
17-
1816
## New queries
1917

2018
| **Query** | **Tags** | **Purpose** |

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

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ private class V extends DefaultVisitor<Context, Label> {
306306
public V(Platform platform, SourceType sourceType) {
307307
this.platform = platform;
308308
this.sourceType = sourceType;
309-
this.isStrict = sourceType.isStrictMode();
309+
this.isStrict = sourceType == SourceType.MODULE;
310310
}
311311

312312
private Label visit(INode child, Label parent, int childIndex) {
@@ -546,27 +546,21 @@ public Label visit(Program nd, Context c) {
546546

547547
isStrict = hasUseStrict(nd.getBody());
548548

549-
// Add platform-specific globals.
550-
scopeManager.addVariables(platform.getPredefinedGlobals());
549+
// if we're extracting a Node.js/ES2015 module, introduce module scope
550+
if (platform == Platform.NODE) {
551+
// add node.js-specific globals
552+
scopeManager.addVariables("global", "process", "console", "Buffer");
551553

552-
// Introduce local scope if there is one.
553-
if (sourceType.hasLocalScope()) {
554554
Label moduleScopeKey = trapwriter.globalID("module;{" + locationManager.getFileLabel() + "}," + locationManager.getStartLine() + "," + locationManager.getStartColumn());
555555
scopeManager.enterScope(3, moduleScopeKey, toplevelLabel);
556-
scopeManager.addVariables(sourceType.getPredefinedLocals(platform, locationManager.getSourceFileExtension()));
556+
// special variables aren't available in `.mjs` modules
557+
if (!".mjs".equals(locationManager.getSourceFileExtension()))
558+
scopeManager.addVariables("require", "module", "exports", "__filename", "__dirname", "arguments");
559+
trapwriter.addTuple("isModule", toplevelLabel);
560+
} else if (sourceType == SourceType.MODULE) {
561+
Label moduleScopeKey = trapwriter.globalID("module;{" + locationManager.getFileLabel() + "}," + locationManager.getStartLine() + "," + locationManager.getStartColumn());
562+
scopeManager.enterScope(3, moduleScopeKey, toplevelLabel);
557563
trapwriter.addTuple("isModule", toplevelLabel);
558-
}
559-
560-
// Emit the specific source type.
561-
switch (sourceType) {
562-
case CLOSURE_MODULE:
563-
trapwriter.addTuple("isClosureModule", toplevelLabel);
564-
break;
565-
case MODULE:
566-
trapwriter.addTuple("isES2015Module", toplevelLabel);
567-
break;
568-
default:
569-
break;
570564
}
571565

572566
// add all declared global (or module-scoped) names, both non-lexical and lexical
@@ -575,8 +569,8 @@ public Label visit(Program nd, Context c) {
575569

576570
visitAll(nd.getBody(), toplevelLabel);
577571

578-
// Leave the local scope again.
579-
if (sourceType.hasLocalScope())
572+
// if we're extracting a Node.js/ES2015 module, leave its scope
573+
if (platform == Platform.NODE || sourceType == SourceType.MODULE)
580574
scopeManager.leaveScope();
581575

582576
contextManager.leaveContainer();

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

Lines changed: 1 addition & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@
44
import java.nio.charset.IllegalCharsetNameException;
55
import java.nio.charset.StandardCharsets;
66
import java.nio.charset.UnsupportedCharsetException;
7-
import java.util.Arrays;
8-
import java.util.Collections;
9-
import java.util.LinkedHashSet;
10-
import java.util.Set;
117

12-
import com.semmle.js.parser.JcornWrapper;
138
import com.semmle.util.data.StringUtil;
149
import com.semmle.util.exception.UserError;
1510

@@ -55,75 +50,13 @@ public static ECMAVersion of(int version) {
5550
}
5651
};
5752

58-
/**
59-
* The type of a source file, which together with the {@link Platform}
60-
* determines how the top-level scope of the file behaves, and whether ES2015
61-
* module syntax should be allowed.
62-
* <p>
63-
* Note that the names of these enum members are depended on by {@link Main},
64-
* {@link AutoBuild}, and {@link JcornWrapper}.
65-
*/
6653
public static enum SourceType {
67-
/** A script executed in the global scope. */
68-
SCRIPT,
69-
70-
/** An ES2015 module. */
71-
MODULE,
72-
73-
/** A Closure-Library module, defined using `goog.module()`. */
74-
CLOSURE_MODULE,
75-
76-
/** A CommonJS module that is not also an ES2015 module. */
77-
COMMONJS_MODULE,
78-
79-
/** Automatically determined source type. */
80-
AUTO;
54+
SCRIPT, MODULE, AUTO;
8155

8256
@Override
8357
public String toString() {
8458
return StringUtil.lc(name());
8559
}
86-
87-
/**
88-
* Returns true if this source is executed directly in the global scope,
89-
* or false if it has its own local scope.
90-
*/
91-
public boolean hasLocalScope() {
92-
return this != SCRIPT;
93-
}
94-
95-
/**
96-
* Returns true if this source is implicitly in strict mode.
97-
*/
98-
public boolean isStrictMode() {
99-
return this == MODULE;
100-
}
101-
102-
private static final Set<String> closureLocals = Collections.singleton("exports");
103-
private static final Set<String> commonJsLocals = new LinkedHashSet<>(Arrays.asList("require", "module", "exports", "__filename", "__dirname", "arguments"));
104-
105-
/**
106-
* Returns the set of local variables in scope at the top-level of this module.
107-
* <p/>
108-
* If this source type has no local scope, the empty set is returned.
109-
*/
110-
public Set<String> getPredefinedLocals(Platform platform, String extension) {
111-
switch (this) {
112-
case CLOSURE_MODULE:
113-
return closureLocals;
114-
case COMMONJS_MODULE:
115-
return commonJsLocals;
116-
case MODULE:
117-
if (platform == Platform.NODE && !extension.equals(".mjs")) {
118-
// An ES2015 module that is compiled to a Node.js module effectively has the locals
119-
// from Node.js even if they are not part of the ES2015 standard.
120-
return commonJsLocals;
121-
}
122-
return Collections.emptySet();
123-
default:
124-
return Collections.emptySet();
125-
}
126-
}
12760
};
12861

12962
public static enum Platform {
@@ -133,15 +66,6 @@ public static enum Platform {
13366
public String toString() {
13467
return StringUtil.lc(name());
13568
}
136-
137-
private static final Set<String> nodejsGlobals = new LinkedHashSet<>(Arrays.asList("global", "process", "console", "Buffer"));
138-
139-
/**
140-
* Gets the set of predefined globals for this platform.
141-
*/
142-
public Set<String> getPredefinedGlobals() {
143-
return this == NODE ? nodejsGlobals : Collections.emptySet();
144-
}
14569
}
14670

14771
/**

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public JSExtractor(ExtractorConfig config) {
2929
this.config = config;
3030
}
3131

32-
// heuristic: if `import`, `export`, or `goog.module` appears at the beginning of a line, it's probably a module
33-
private static final Pattern containsModuleIndicator = Pattern.compile("(?m)^([ \t]*)(import|export|goog\\.module)\\b");
32+
// heuristic: if `import` or `export` appears at the beginning of a line, it's probably a module
33+
private static final Pattern containsImportOrExport = Pattern.compile("(?m)^([ \t]*)(import|export)\\b");
3434

3535
public Pair<Label, LoCInfo> extract(TextualExtractor textualExtractor, String source, int toplevelKind, ScopeManager scopeManager) throws ParseError {
3636
// if the file starts with `{ "<string>":` it won't parse as JavaScript; try parsing as JSON instead
@@ -69,10 +69,9 @@ public SourceType establishSourceType(String source, boolean allowLeadingWS) {
6969
if (sourceType != SourceType.AUTO)
7070
return sourceType;
7171
if (config.getEcmaVersion().compareTo(ECMAVersion.ECMA2015) >= 0) {
72-
Matcher m = containsModuleIndicator.matcher(source);
73-
if (m.find() && (allowLeadingWS || m.group(1).isEmpty())) {
74-
return m.group(2).startsWith("goog") ? SourceType.CLOSURE_MODULE : SourceType.MODULE;
75-
}
72+
Matcher m = containsImportOrExport.matcher(source);
73+
if (m.find() && (allowLeadingWS || m.group(1).isEmpty()))
74+
return SourceType.MODULE;
7675
}
7776
return SourceType.SCRIPT;
7877
}
@@ -90,9 +89,6 @@ public Pair<Label, LoCInfo> extract(TextualExtractor textualExtractor,
9089
LoCInfo loc;
9190
if (ast != null) {
9291
platform = getPlatform(platform, ast);
93-
if (sourceType == SourceType.SCRIPT && platform == Platform.NODE) {
94-
sourceType = SourceType.COMMONJS_MODULE;
95-
}
9692

9793
lexicalExtractor = new LexicalExtractor(textualExtractor, parserRes.getTokens(), parserRes.getComments());
9894
ASTExtractor scriptExtractor = new ASTExtractor(lexicalExtractor, scopeManager);
@@ -129,7 +125,7 @@ public Pair<Label, LoCInfo> extract(TextualExtractor textualExtractor,
129125

130126
if (config.isExterns())
131127
textualExtractor.getTrapwriter().addTuple("isExterns", toplevelLabel);
132-
if (platform == Platform.NODE && sourceType == SourceType.COMMONJS_MODULE)
128+
if (platform == Platform.NODE && sourceType != SourceType.MODULE)
133129
textualExtractor.getTrapwriter().addTuple("isNodejs", toplevelLabel);
134130

135131
return Pair.make(toplevelLabel, loc);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class Main {
4141
* such a way that it may produce different tuples for the same file under the same
4242
* {@link ExtractorConfig}.
4343
*/
44-
public static final String EXTRACTOR_VERSION = "2019-02-04";
44+
public static final String EXTRACTOR_VERSION = "2019-01-29";
4545

4646
public static final Pattern NEWLINE = Pattern.compile("\n");
4747

javascript/extractor/tests/closure/input/googDotDeclareModuleId.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

javascript/extractor/tests/closure/input/googDotModule.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

javascript/extractor/tests/closure/input/googDotProvide.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)