Skip to content

Commit 60f9af0

Browse files
lauraharkercopybara-github
authored andcommitted
Refactor CompilerOptions usages to use getter/setters, not fields
PiperOrigin-RevId: 858648457
1 parent da82459 commit 60f9af0

19 files changed

+323
-298
lines changed

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

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ protected void setRunOptions(CompilerOptions options) throws IOException {
320320
DiagnosticGroups diagnosticGroups = getDiagnosticGroups();
321321

322322
if (config.shouldSaveAfterStage1() || config.shouldContinueCompilation()) {
323-
if (options.checksOnly) {
323+
if (options.isChecksOnly()) {
324324
throw new FlagUsageException(
325325
"checks_only mode is incompatible with multi-stage compilation");
326326
}
@@ -361,12 +361,13 @@ protected void setRunOptions(CompilerOptions options) throws IOException {
361361
options.setDependencyOptions(config.dependencyOptions);
362362
}
363363

364-
options.devMode = config.jscompDevMode;
364+
options.setDevMode(config.jscompDevMode);
365365
options.setCodingConvention(config.codingConvention);
366366
options.setSummaryDetailLevel(config.summaryDetailLevel);
367367
options.setTrustedStrings(true);
368368

369-
legacyOutputCharset = options.outputCharset = getLegacyOutputCharset();
369+
legacyOutputCharset = getLegacyOutputCharset();
370+
options.setOutputCharset(legacyOutputCharset);
370371
outputCharset2 = getOutputCharset2();
371372
inputCharset = getInputCharset();
372373

@@ -395,26 +396,26 @@ protected void setRunOptions(CompilerOptions options) throws IOException {
395396
} else if (isOutputInJson()) {
396397
options.setSourceMapOutputPath("%outname%");
397398
}
398-
options.sourceMapDetailLevel = config.sourceMapDetailLevel;
399-
options.sourceMapFormat = config.sourceMapFormat;
400-
options.sourceMapLocationMappings = config.sourceMapLocationMappings;
401-
options.parseInlineSourceMaps = config.parseInlineSourceMaps;
402-
options.applyInputSourceMaps = config.applyInputSourceMaps;
399+
options.setSourceMapDetailLevel(config.sourceMapDetailLevel);
400+
options.setSourceMapFormat(config.sourceMapFormat);
401+
options.setSourceMapLocationMappings(config.sourceMapLocationMappings);
402+
options.setParseInlineSourceMaps(config.parseInlineSourceMaps);
403+
options.setApplyInputSourceMaps(config.applyInputSourceMaps);
403404

404405
ImmutableMap.Builder<String, SourceMapInput> inputSourceMaps = new ImmutableMap.Builder<>();
405406
for (Map.Entry<String, String> files : config.sourceMapInputFiles.entrySet()) {
406407
SourceFile sourceMap =
407408
SourceFile.builder().withKind(SourceKind.NON_CODE).withPath(files.getValue()).build();
408409
inputSourceMaps.put(files.getKey(), new SourceMapInput(sourceMap));
409410
}
410-
options.inputSourceMaps = inputSourceMaps.buildOrThrow();
411+
options.setInputSourceMaps(inputSourceMaps.buildOrThrow());
411412

412413
if (!config.variableMapInputFile.isEmpty()) {
413-
options.inputVariableMap = VariableMap.load(config.variableMapInputFile);
414+
options.setInputVariableMap(VariableMap.load(config.variableMapInputFile));
414415
}
415416

416417
if (!config.propertyMapInputFile.isEmpty()) {
417-
options.inputPropertyMap = VariableMap.load(config.propertyMapInputFile);
418+
options.setInputPropertyMap(VariableMap.load(config.propertyMapInputFile));
418419
}
419420

420421
if (!config.outputManifests.isEmpty()) {
@@ -439,7 +440,7 @@ protected void setRunOptions(CompilerOptions options) throws IOException {
439440

440441
options.setProcessCommonJSModules(config.processCommonJSModules);
441442
options.setModuleRoots(config.moduleRoots);
442-
options.angularPass = config.angularPass;
443+
options.setAngularPass(config.angularPass);
443444

444445
if (!config.jsonWarningsFile.isEmpty()) {
445446
options.addReportGenerator(
@@ -1045,11 +1046,11 @@ void writeOutput(
10451046
@Nullable Function<String, String> escaper,
10461047
String filename)
10471048
throws IOException {
1048-
if (compiler.getOptions().outputJs == OutputJs.SENTINEL) {
1049+
if (compiler.getOptions().getOutputJs() == OutputJs.SENTINEL) {
10491050
out.append("// No JS output because the compiler was run in checks-only mode.\n");
10501051
return;
10511052
}
1052-
checkState(compiler.getOptions().outputJs == OutputJs.NORMAL);
1053+
checkState(compiler.getOptions().getOutputJs() == OutputJs.NORMAL);
10531054

10541055
String code = chunk == null ? compiler.toSource() : compiler.toSource(licenseTracker, chunk);
10551056
writeOutput(out, compiler, code, wrapper, codePlaceholder, escaper, filename);
@@ -1197,8 +1198,8 @@ protected int doRun() throws IOException {
11971198
}
11981199

11991200
if (foundJsonInputSourceMap) {
1200-
inputSourceMaps.putAll(options.inputSourceMaps);
1201-
options.inputSourceMaps = inputSourceMaps.buildOrThrow();
1201+
inputSourceMaps.putAll(options.getInputSourceMaps());
1202+
options.setInputSourceMaps(inputSourceMaps.buildOrThrow());
12021203
}
12031204

12041205
compiler.initWebpackMap(inputPathByWebpackId.buildOrThrow());
@@ -1363,7 +1364,7 @@ private void runCompilerPasses(CompileMetricsRecorderInterface metricsRecorder)
13631364
} else {
13641365
// This is the code path taken when "building" a library by just checking it for errors
13651366
// and generating an .ijs file and also when doing a full compilation.
1366-
actionMetricsName = compiler.getOptions().checksOnly ? "checks-only" : "full compile";
1367+
actionMetricsName = compiler.getOptions().isChecksOnly() ? "checks-only" : "full compile";
13671368
runStage1 = true;
13681369
runStage2 = true;
13691370
runStage3 = true;
@@ -1516,7 +1517,7 @@ int processResults(Result result, List<JSChunk> chunks, B options) throws IOExce
15161517
outputBundle();
15171518
outputChunkGraphJson();
15181519
return 0;
1519-
} else if (options.outputJs != OutputJs.NONE && result.success) {
1520+
} else if (options.getOutputJs() != OutputJs.NONE && result.success) {
15201521
outputChunkGraphJson();
15211522
if (chunks == null) {
15221523
outputSingleBinary(options);
@@ -2230,8 +2231,8 @@ void printBundleTo(JSChunk chunk, Appendable out) throws IOException {
22302231
CompilerOptions options = compiler.getOptions();
22312232
// Prebuild ASTs before they're needed in getLoadFlags, for performance and because
22322233
// StackOverflowErrors can be hit if not prebuilt.
2233-
if (options.numParallelThreads > 1) {
2234-
new PrebuildAst(compiler, compiler.getOptions().numParallelThreads).prebuild(inputs);
2234+
if (options.getNumParallelThreads() > 1) {
2235+
new PrebuildAst(compiler, compiler.getOptions().getNumParallelThreads()).prebuild(inputs);
22352236
}
22362237
if (options.getRuntimeLibraryMode() == RuntimeJsLibManager.RuntimeLibraryMode.INJECT) {
22372238
// ES6 modules will need a runtime in a bundle. Skip appending this runtime if there are no

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,18 +259,18 @@ public void validateStatement(Node n, boolean isAmbient) {
259259
// setWrapGoogModulesForWhitespaceOnly=false
260260
// TODO: b/294420383 Ideally the LateEs6ToEs3Rewriter pass should not inject code above the
261261
// module body node
262-
if (compiler.getOptions().skipNonTranspilationPasses) {
263-
if (!compiler.getOptions().wrapGoogModulesForWhitespaceOnly) {
262+
if (compiler.getOptions().getSkipNonTranspilationPasses()) {
263+
if (!compiler.getOptions().shouldWrapGoogModulesForWhitespaceOnly()) {
264264
validateModuleContents(n);
265265
return;
266266
}
267267
}
268268
violation("Expected statement but was " + n.getToken() + ".", n);
269269
}
270270
default -> {
271-
if (n.isModuleBody() && compiler.getOptions().skipNonTranspilationPasses) {
271+
if (n.isModuleBody() && compiler.getOptions().getSkipNonTranspilationPasses()) {
272272
checkState(
273-
!compiler.getOptions().wrapGoogModulesForWhitespaceOnly,
273+
!compiler.getOptions().shouldWrapGoogModulesForWhitespaceOnly(),
274274
"Modules can exist in transpiler only if setWrapGoogModulesForWhitespaceOnly is"
275275
+ " false");
276276
validateModuleContents(n);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ private void checkGoogModuleGet(NodeTraversal t, Node call, ModuleMetadata curre
324324

325325
if (!currentModule.isEs6Module()
326326
&& !currentModule.isGoogModule()
327-
&& compiler.getOptions().moduleResolutionMode != ResolutionMode.WEBPACK) {
327+
&& compiler.getOptions().getModuleResolutionMode() != ResolutionMode.WEBPACK) {
328328
// Here we are making a heuristic check of the use of goog.module.get. There are many
329329
// different ways to deliberately subvert these checks. What we are trying to avoid
330330
// is accidential use of a `goog.provide` file as module scoped. So we want to avoid:

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1382,7 +1382,7 @@ private void updateGoogRequireDynamicCallInAwait(Node call) {
13821382
}
13831383

13841384
private String namespaceIdToXid(String namespaceId) {
1385-
Xid.HashFunction hashFunction = this.compiler.getOptions().chunkIdHashFunction;
1385+
Xid.HashFunction hashFunction = this.compiler.getOptions().getChunkIdHashFunction();
13861386
Xid xid = hashFunction == null ? new Xid() : new Xid(hashFunction);
13871387
return xid.get(namespaceId);
13881388
}

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,21 @@ private void setSafeOptimizationAssumptions() {
8181

8282
private void copyOutputOptions() {
8383
shadowOptions.setPrettyPrint(original.isPrettyPrint());
84-
shadowOptions.setGeneratePseudoNames(original.generatePseudoNames);
85-
shadowOptions.setErrorHandler(original.errorHandler);
86-
shadowOptions.setErrorFormat(original.errorFormat);
84+
shadowOptions.setGeneratePseudoNames(original.shouldGeneratePseudoNames());
85+
shadowOptions.setErrorHandler(original.getErrorHandler());
86+
shadowOptions.setErrorFormat(original.getErrorFormat());
8787
}
8888

8989
private void copyDebugOptions() {
9090
shadowOptions.setTracerMode(original.getTracerMode());
91-
shadowOptions.setDevMode(original.devMode);
91+
shadowOptions.setDevMode(original.getDevMode());
9292

93-
shadowOptions.setPrintSourceAfterEachPass(original.printSourceAfterEachPass);
93+
shadowOptions.setPrintSourceAfterEachPass(original.shouldPrintSourceAfterEachPass());
9494
shadowOptions.setFilesToPrintAfterEachPassRegexList(
95-
original.filesToPrintAfterEachPassRegexList);
96-
shadowOptions.setPrintInputDelimiter(original.printInputDelimiter);
97-
shadowOptions.setInputDelimiter(original.inputDelimiter);
95+
original.getFilesToPrintAfterEachPassRegexList());
96+
shadowOptions.setPrintInputDelimiter(original.shouldPrintInputDelimiter());
97+
98+
shadowOptions.setInputDelimiter(original.getInputDelimiter());
9899

99100
shadowOptions.setDebugLogFilter(original.getDebugLogFilter());
100101
Path debugLogDirectory = original.getDebugLogDirectory();

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ protected CodeGenerator(CodeConsumer consumer, CompilerOptions options) {
7878
cc = consumer;
7979

8080
this.outputCharsetEncoder = new OutputCharsetEncoder(options.getOutputCharset());
81-
this.preferSingleQuotes = options.preferSingleQuotes;
82-
this.trustedStrings = options.trustedStrings;
83-
this.preserveTypeAnnotations = options.preserveTypeAnnotations;
81+
this.preferSingleQuotes = options.shouldPreferSingleQuotes();
82+
this.trustedStrings = options.assumeTrustedStrings();
83+
this.preserveTypeAnnotations = options.shouldPreserveTypeAnnotations();
8484
this.printNonJSDocComments = options.getPreserveNonJSDocComments();
85-
this.gentsMode = options.gentsMode;
85+
this.gentsMode = options.getGentsMode();
8686
this.quoteKeywordProperties = options.shouldQuoteKeywordProperties();
8787
this.useOriginalName = options.getUseOriginalNamesInOutput();
8888
this.outputFeatureSet = options.getOutputFeatureSet();

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ public Builder(Node node) {
716716
public Builder setCompilerOptions(CompilerOptions options) {
717717
this.options = options;
718718
this.prettyPrint = options.isPrettyPrint();
719-
this.lineBreak = options.lineBreak;
719+
this.lineBreak = options.shouldAddLineBreak();
720720
return this;
721721
}
722722

@@ -856,20 +856,20 @@ private static SourceAndMappings toSource(
856856
boolean tagAsStrict,
857857
boolean lineBreak,
858858
CodeGeneratorFactory codeGeneratorFactory) {
859-
checkState(options.sourceMapDetailLevel != null);
859+
checkState(options.getSourceMapDetailLevel() != null);
860860

861861
MappedCodePrinter mcp =
862862
outputFormat == Format.COMPACT
863863
? new CompactCodePrinter(
864864
lineBreak,
865-
options.lineLengthThreshold,
865+
options.getLineLengthThreshold(),
866866
options.shouldGatherSourceMapInfo(),
867-
options.sourceMapDetailLevel,
867+
options.getSourceMapDetailLevel(),
868868
licenseTracker)
869869
: new PrettyCodePrinter(
870-
options.lineLengthThreshold,
870+
options.getLineLengthThreshold(),
871871
options.shouldGatherSourceMapInfo(),
872-
options.sourceMapDetailLevel,
872+
options.getSourceMapDetailLevel(),
873873
licenseTracker);
874874
CodeGenerator cg = codeGeneratorFactory.getCodeGenerator(outputFormat, mcp);
875875

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,7 +1448,7 @@ public static enum FormattingOption {
14481448
private void applyToOptions(CompilerOptions options) {
14491449
switch (this) {
14501450
case PRETTY_PRINT -> options.setPrettyPrint(true);
1451-
case PRINT_INPUT_DELIMITER -> options.printInputDelimiter = true;
1451+
case PRINT_INPUT_DELIMITER -> options.setPrintInputDelimiter(true);
14521452
case SINGLE_QUOTES -> options.setPreferSingleQuotes(true);
14531453
}
14541454
}
@@ -1892,9 +1892,9 @@ protected CompilerOptions createOptions() {
18921892
formattingOption.applyToOptions(options);
18931893
}
18941894

1895-
options.closurePass = flags.processClosurePrimitives;
1895+
options.setClosurePass(flags.processClosurePrimitives);
18961896

1897-
options.angularPass = flags.angularPass;
1897+
options.setAngularPass(flags.angularPass);
18981898

18991899
options.setPolymerVersion(flags.polymerVersion);
19001900

@@ -1911,11 +1911,11 @@ protected CompilerOptions createOptions() {
19111911
}
19121912
}
19131913

1914-
options.removeJ2clAsserts = flags.removeJ2cLAsserts;
1914+
options.setRemoveJ2clAsserts(flags.removeJ2cLAsserts);
19151915

1916-
options.renamePrefix = flags.renamePrefix;
1916+
options.setRenamePrefix(flags.renamePrefix);
19171917

1918-
options.renamePrefixNamespace = flags.renamePrefixNamespace;
1918+
options.setRenamePrefixNamespace(flags.renamePrefixNamespace);
19191919

19201920
options.setPreserveTypeAnnotations(flags.preserveTypeAnnotations);
19211921

@@ -1925,16 +1925,16 @@ protected CompilerOptions createOptions() {
19251925
options.setForceLibraryInjection(flags.forceInjectLibraries);
19261926
}
19271927

1928-
options.rewritePolyfills =
1928+
options.setRewritePolyfills(
19291929
flags.rewritePolyfills
1930-
&& options.getLanguageIn().toFeatureSet().contains(FeatureSet.ES2015);
1930+
&& options.getLanguageIn().toFeatureSet().contains(FeatureSet.ES2015));
19311931
options.setIsolatePolyfills(flags.isolatePolyfills);
19321932

19331933
if (!flags.translationsFile.isEmpty()) {
19341934
try {
1935-
options.messageBundle =
1935+
options.setMessageBundle(
19361936
new XtbMessageBundle(
1937-
new FileInputStream(flags.translationsFile), flags.translationsProject);
1937+
new FileInputStream(flags.translationsFile), flags.translationsProject));
19381938
} catch (IOException e) {
19391939
throw new RuntimeException("Reading XTB file", e);
19401940
}
@@ -1947,7 +1947,7 @@ protected CompilerOptions createOptions() {
19471947
// In ADVANCED mode, goog.getMsg is going to be renamed anyway,
19481948
// so we might as well inline it. But shut off the i18n warnings,
19491949
// because the user didn't really ask for i18n.
1950-
options.messageBundle = new EmptyMessageBundle();
1950+
options.setMessageBundle(new EmptyMessageBundle());
19511951
options.setWarningLevel(DiagnosticGroups.MSG_CONVENTIONS, CheckLevel.OFF);
19521952
}
19531953

@@ -2016,7 +2016,7 @@ protected CompilerOptions createOptions() {
20162016
+ "--chunk_output_type is set to ES_MODULES.");
20172017
}
20182018

2019-
options.chunkOutputType = flags.chunkOutputType;
2019+
options.setChunkOutputType(flags.chunkOutputType);
20202020
options.setEmitUseStrict(false);
20212021

20222022
if (level == CompilationLevel.ADVANCED_OPTIMIZATIONS) {

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ public void setOptionsForCompilationLevel(CompilerOptions options) {
8585
}
8686

8787
public void setDebugOptionsForCompilationLevel(CompilerOptions options) {
88-
options.generatePseudoNames = true;
89-
options.removeClosureAsserts = false;
90-
options.removeJ2clAsserts = false;
88+
options.setGeneratePseudoNames(true);
89+
options.setRemoveClosureAsserts(false);
90+
options.setRemoveJ2clAsserts(true);
9191
}
9292

9393
/**
@@ -105,7 +105,7 @@ private static void applyBasicCompilationOptions(CompilerOptions options) {
105105
*/
106106
private static void applyTranspileOnlyOptions(CompilerOptions options) {
107107
// ReplaceIdGenerators is on by default, but should not run in TRANSPILE_ONLY mode.
108-
options.replaceIdGenerators = false;
108+
options.setReplaceIdGenerators(false);
109109

110110
options.setClosurePass(true);
111111
options.setRenamingPolicy(VariableRenamingPolicy.OFF, PropertyRenamingPolicy.OFF);
@@ -115,10 +115,10 @@ private static void applyTranspileOnlyOptions(CompilerOptions options) {
115115
options.setCoalesceVariableNames(false);
116116
options.setDeadAssignmentElimination(false);
117117
options.setCollapseVariableDeclarations(false);
118-
options.convertToDottedProperties = false;
119-
options.labelRenaming = false;
118+
options.setConvertToDottedProperties(false);
119+
options.setLabelRenaming(false);
120120
options.setRemoveUnusedVariables(Reach.NONE);
121-
options.collapseObjectLiterals = false;
121+
options.setCollapseObjectLiterals(false);
122122
/*
123123
* Turn off protecting side-effect free nodes by making them parameters to a extern function
124124
* call.
@@ -137,7 +137,7 @@ private static void applySafeCompilationOptions(CompilerOptions options) {
137137
options.setDependencyOptions(DependencyOptions.sortOnly());
138138

139139
// ReplaceIdGenerators is on by default, but should not run in simple mode.
140-
options.replaceIdGenerators = false;
140+
options.setReplaceIdGenerators(false);
141141

142142
// Does not call applyBasicCompilationOptions(options) because the call to
143143
// skipAllCompilerPasses() cannot be easily undone.
@@ -152,10 +152,10 @@ private static void applySafeCompilationOptions(CompilerOptions options) {
152152
options.setDeadAssignmentElimination(true);
153153
options.setDeadPropertyAssignmentElimination(false);
154154
options.setCollapseVariableDeclarations(true);
155-
options.convertToDottedProperties = true;
156-
options.labelRenaming = true;
155+
options.setConvertToDottedProperties(true);
156+
options.setLabelRenaming(true);
157157
options.setRemoveUnusedVariables(Reach.LOCAL_ONLY);
158-
options.collapseObjectLiterals = true;
158+
options.setCollapseObjectLiterals(true);
159159
options.setProtectHiddenSideEffects(true);
160160
}
161161

@@ -244,7 +244,7 @@ public void setTypeBasedOptimizationOptions(CompilerOptions options) {
244244
*/
245245
public void setWrappedOutputOptimizations(CompilerOptions options) {
246246
// Global variables and properties names can't conflict.
247-
options.reserveRawExports = false;
247+
options.setReserveRawExports(false);
248248
switch (this) {
249249
case SIMPLE_OPTIMIZATIONS -> {
250250
// Enable global variable optimizations (but not property optimizations)

0 commit comments

Comments
 (0)