Skip to content

Commit 6bb9dd8

Browse files
Merge pull request #4007 from Vertispan:4006-dont-parse-ast-for-bundle
PiperOrigin-RevId: 504106625
2 parents a432931 + 3bb1cb8 commit 6bb9dd8

File tree

11 files changed

+69
-45
lines changed

11 files changed

+69
-45
lines changed

src/com/google/javascript/jscomp/AbstractCommandLineRunner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2403,7 +2403,7 @@ void printBundleTo(JSChunk module, Appendable out) throws IOException {
24032403
// ES6 modules will need a runtime in a bundle. Skip appending this runtime if there are no
24042404
// ES6 modules to cut down on size.
24052405
for (CompilerInput input : inputs) {
2406-
if ("es6".equals(input.getLoadFlags().get("module"))) {
2406+
if (input.isEs6Module()) {
24072407
appendRuntimeTo(out);
24082408
break;
24092409
}

src/com/google/javascript/jscomp/Compiler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2249,7 +2249,7 @@ private void parsePotentialModules(Iterable<CompilerInput> inputsToProcess) {
22492249
// Only process files that are detected as ES6 modules
22502250
if (!options.getDependencyOptions().shouldPrune()
22512251
|| !JsFileRegexParser.isSupported()
2252-
|| "es6".equals(input.getLoadFlags().get("module"))) {
2252+
|| input.isEs6Module()) {
22532253
filteredInputs.add(input);
22542254
}
22552255
}

src/com/google/javascript/jscomp/CompilerInput.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
* and maintain state such as module for the input and whether the input is an extern. Also
4848
* calculates provided and required types.
4949
*/
50-
public class CompilerInput extends DependencyInfo.Base {
50+
public class CompilerInput implements DependencyInfo {
5151

5252
private static final long serialVersionUID = 2L;
5353

@@ -587,6 +587,20 @@ public String toString() {
587587
return getName();
588588
}
589589

590+
@Override
591+
public boolean isEs6Module() {
592+
// Instead of doing a full parse to read all load flags, just ask the delegate, which at least
593+
// has this much info
594+
return getDependencyInfo().isEs6Module();
595+
}
596+
597+
@Override
598+
public boolean isGoogModule() {
599+
// Instead of doing a full parse to read all load flags, just ask the delegate, which at least
600+
// has this much info
601+
return getDependencyInfo().isGoogModule();
602+
}
603+
590604
@Override
591605
public ImmutableMap<String, String> getLoadFlags() {
592606
return getDependencyInfo().getLoadFlags();

src/com/google/javascript/jscomp/JSChunk.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
* A JavaScript chunk has a unique name, consists of a list of compiler inputs, and can depend on
4242
* other chunks.
4343
*/
44-
public final class JSChunk extends DependencyInfo.Base implements Serializable {
44+
public final class JSChunk implements Serializable, DependencyInfo {
4545
// The name of the artificial chunk containing all strong sources when there is no chunk spec.
4646
// If there is a chunk spec, strong sources go in their respective chunks, and this chunk does
4747
// not exist.
@@ -59,7 +59,7 @@ public final class JSChunk extends DependencyInfo.Base implements Serializable {
5959
private final String name;
6060

6161
/** Source code inputs */
62-
// non-final for deserilaization
62+
// non-final for deserialization
6363
// CompilerInputs must be explicitly added to the JSChunk again after deserialization
6464
// A map keyed by the {@code CompilerInput.getName()} to speed up getByName and removeByName.
6565
private transient Map<String, CompilerInput> inputs = new LinkedHashMap<>();
@@ -132,11 +132,12 @@ public ImmutableMap<String, String> getLoadFlags() {
132132
}
133133

134134
@Override
135-
public boolean isModule() {
136-
// NOTE: The meaning of "module" has changed over time. A "JsChunk" is
137-
// a collection of inputs that are loaded together. A "module" file,
138-
// is a CommonJs module, ES6 module, goog.module or other file whose
139-
// top level symbols are not in global scope.
135+
public boolean isGoogModule() {
136+
throw new UnsupportedOperationException();
137+
}
138+
139+
@Override
140+
public boolean isEs6Module() {
140141
throw new UnsupportedOperationException();
141142
}
142143

src/com/google/javascript/jscomp/LazyParsedDependencyInfo.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import org.jspecify.nullness.Nullable;
3030

3131
/** A DependencyInfo class that determines load flags by parsing the AST just-in-time. */
32-
public class LazyParsedDependencyInfo extends DependencyInfo.Base {
32+
public class LazyParsedDependencyInfo implements DependencyInfo {
3333

3434
private final DependencyInfo delegate;
3535
private @Nullable JsAst ast;
@@ -43,6 +43,20 @@ public LazyParsedDependencyInfo(DependencyInfo delegate, JsAst ast, AbstractComp
4343
this.compiler = checkNotNull(compiler);
4444
}
4545

46+
@Override
47+
public boolean isEs6Module() {
48+
// Instead of doing a full parse to read all load flags, just ask the delegate, which at least
49+
// has this much info
50+
return delegate.isEs6Module();
51+
}
52+
53+
@Override
54+
public boolean isGoogModule() {
55+
// Instead of doing a full parse to read all load flags, just ask the delegate, which at least
56+
// has this much info
57+
return delegate.isGoogModule();
58+
}
59+
4660
@Override
4761
public ImmutableMap<String, String> getLoadFlags() {
4862
if (loadFlags == null) {

src/com/google/javascript/jscomp/deps/ClosureBundler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,9 @@ public void appendTo(Appendable out, DependencyInfo info, File content, Charset
189189
/** Append the contents of the CharSource to the supplied appendable. */
190190
public void appendTo(Appendable out, DependencyInfo info, CharSource content) throws IOException {
191191
String code = content.read();
192-
if (info.isModule()) {
192+
if (info.isGoogModule()) {
193193
mode.appendGoogModule(transpile(code), out, sourceUrl);
194-
} else if ("es6".equals(info.getLoadFlags().get("module")) && transpiler == Transpiler.NULL) {
194+
} else if (info.isEs6Module() && transpiler == Transpiler.NULL) {
195195
// TODO(johnplaisted): Make the default transpiler the ES_MODULE_TO_CJS_TRANSPILER. Currently
196196
// some code is passing in unicode identifiers in non-ES6 modules the compiler fails to parse.
197197
// Once this compiler bug is fixed we can always transpile.

src/com/google/javascript/jscomp/deps/DependencyInfo.java

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.Collection;
3131
import java.util.List;
3232
import java.util.Map;
33+
import java.util.Objects;
3334

3435
/** A data structure for JS dependency information for a single .js file. */
3536
public interface DependencyInfo {
@@ -129,38 +130,32 @@ abstract static class Builder {
129130
/** Gets the symbols required by this file. */
130131
ImmutableList<Require> getRequires();
131132

132-
ImmutableList<String> getRequiredSymbols();
133+
default ImmutableList<String> getRequiredSymbols() {
134+
return Require.asSymbolList(getRequires());
135+
}
133136

134137
/** Gets the symbols type-required by this file (i.e. for typechecking only). */
135138
ImmutableList<String> getTypeRequires();
136139

137140
/** Gets the loading information for this file. */
138141
ImmutableMap<String, String> getLoadFlags();
139142

140-
/** Whether the symbol is provided by a module */
141-
boolean isModule();
143+
/** Whether the symbol is provided by an ES6 module */
144+
default boolean isEs6Module() {
145+
return Objects.equals(getLoadFlags().get("module"), "es6");
146+
}
147+
148+
/** Whether the symbol is provided by a goog module */
149+
default boolean isGoogModule() {
150+
return Objects.equals(getLoadFlags().get("module"), "goog");
151+
}
142152

143153
/** Whether the file '@externs' annotation. */
144154
boolean getHasExternsAnnotation();
145155

146156
/** Whether the file has the '@nocompile' annotation. */
147157
boolean getHasNoCompileAnnotation();
148158

149-
/**
150-
* Abstract base implementation that defines derived accessors such
151-
* as {@link #isModule}.
152-
*/
153-
abstract class Base implements DependencyInfo {
154-
@Override public boolean isModule() {
155-
return "goog".equals(getLoadFlags().get("module"));
156-
}
157-
158-
@Override
159-
public ImmutableList<String> getRequiredSymbols() {
160-
return Require.asSymbolList(getRequires());
161-
}
162-
}
163-
164159
/** Utility methods. */
165160
class Util {
166161
private Util() {}

src/com/google/javascript/jscomp/deps/DepsGenerator.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ private Map<String, DependencyInfo> removeMungedSymbols(
219219

220220
ImmutableList<String> provides = dependencyInfo.getProvides();
221221

222-
if ("es6".equals(dependencyInfo.getLoadFlags().get("module"))) {
222+
if (dependencyInfo.isEs6Module()) {
223223
String mungedProvide = loader.resolve(dependencyInfo.getName()).toModuleName();
224224
// Filter out the munged symbol.
225225
// Note that at the moment ES6 modules should not have any other provides! In the future
@@ -277,8 +277,7 @@ private void validateDependencies(Iterable<DependencyInfo> preparsedFileDependen
277277
} else if (provider == depInfo) {
278278
reportSameFile(namespace, depInfo);
279279
} else {
280-
depInfo.isModule();
281-
boolean providerIsEs6Module = "es6".equals(provider.getLoadFlags().get("module"));
280+
boolean providerIsEs6Module = provider.isEs6Module();
282281

283282
switch (require.getType()) {
284283
case ES6_IMPORT:
@@ -363,7 +362,7 @@ private void addToProvideMap(
363362
.toModuleName());
364363
} else {
365364
// ES6 modules already provide these munged symbols.
366-
if (!"es6".equals(depInfo.getLoadFlags().get("module"))) {
365+
if (!depInfo.isEs6Module()) {
367366
provides.add(loader.resolve(depInfo.getName()).toModuleName());
368367
}
369368
}

src/com/google/javascript/jscomp/deps/SimpleDependencyInfo.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
import java.util.Collection;
2424
import java.util.Map;
2525

26-
/**
27-
* A class to hold JS dependency information for a single .js file.
28-
*/
29-
@Immutable @AutoValue @AutoValue.CopyAnnotations
30-
public abstract class SimpleDependencyInfo extends DependencyInfo.Base {
26+
/** A class to hold JS dependency information for a single .js file. */
27+
@Immutable
28+
@AutoValue
29+
@AutoValue.CopyAnnotations
30+
public abstract class SimpleDependencyInfo implements DependencyInfo {
3131

3232
public static Builder builder(String srcPathRelativeToClosure, String pathOfDefiningFile) {
3333
return new AutoValue_SimpleDependencyInfo.Builder()

src/com/google/javascript/jscomp/deps/SortedDependencies.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ private void processInputs() {
207207
|| (provides.size() == 1
208208
&& firstProvide.startsWith("module$")
209209
// ES6 modules should always be considered as exporting something.
210-
&& !"es6".equals(userOrderedInput.getLoadFlags().get("module")))) {
210+
&& !userOrderedInput.isEs6Module())) {
211211
nonExportingInputs.put(
212212
ModuleNames.fileToModuleName(userOrderedInput.getName()), userOrderedInput);
213213
}

0 commit comments

Comments
 (0)