Skip to content

Commit 10e77b7

Browse files
rahul-kamatcopybara-github
authored andcommitted
Update JSCompiler stage 2 save/restore flags to match MSS compiler flags
This change deletes the `restore_stage1_from_file` and `save_stage2_to_file` flag from JSCompiler and replaces them with the new `stage2_filename_to_restore_from` and `stage2_filename_to_save_to flags`. PiperOrigin-RevId: 733876143
1 parent 8682069 commit 10e77b7

File tree

2 files changed

+2
-161
lines changed

2 files changed

+2
-161
lines changed

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

+2-24
Original file line numberDiff line numberDiff line change
@@ -265,13 +265,6 @@ private static class Flags {
265265
hidden = true)
266266
private @Nullable String continueSavedCompilationFile = null;
267267

268-
// TODO(b/395966861): deprecate and remove this in favor of --stage2_filename_to_restore_from
269-
@Option(
270-
name = "--restore_stage1_from_file",
271-
usage = "Filename where a stage 1 compilation state was previously saved.",
272-
hidden = true)
273-
private @Nullable String restoreStage1FromFile = null;
274-
275268
@Option(
276269
name = "--stage2_filename_to_restore_from",
277270
usage = "Filename where a stage 2 compilation state was previously saved.",
@@ -297,13 +290,6 @@ private static class Flags {
297290
hidden = true)
298291
private @Nullable String saveStage1ToFile = null;
299292

300-
// TODO(b/395966861): deprecate and remove this in favor of --stage2_filename_to_save_to
301-
@Option(
302-
name = "--save_stage2_to_file",
303-
usage = "Filename to save stage 2 state so that the compilation can be resumed later.",
304-
hidden = true)
305-
private @Nullable String saveStage2ToFile = null;
306-
307293
@Option(
308294
name = "--stage2_filename_to_save_to",
309295
usage = "Filename to save stage 2 state so that the compilation can be resumed later.",
@@ -1802,11 +1788,7 @@ private void initConfigFromFlags(String[] args, PrintStream out, PrintStream err
18021788
.setJsonStreamMode(flags.jsonStreamMode)
18031789
.setErrorFormat(flags.errorFormat);
18041790

1805-
String stage1RestoreFile = flags.restoreStage1FromFile;
1806-
if (stage1RestoreFile == null) {
1807-
// TODO(b/395966861): delete the `flags.restoreStage1FromFile` flag and just use this one
1808-
stage1RestoreFile = flags.stage2FilenameToRestoreFrom;
1809-
}
1791+
String stage1RestoreFile = flags.stage2FilenameToRestoreFrom;
18101792
if (stage1RestoreFile == null) {
18111793
// TODO(bradfordcsmith): deprecate and remove this flag
18121794
stage1RestoreFile = flags.continueSavedCompilationFile;
@@ -1828,11 +1810,7 @@ private void initConfigFromFlags(String[] args, PrintStream out, PrintStream err
18281810
// TODO(bradfordcsmith): deprecate and remove this flag
18291811
stage1SaveFile = flags.saveAfterChecksFile;
18301812
}
1831-
String stage2SaveFile = flags.saveStage2ToFile;
1832-
if (stage2SaveFile == null) {
1833-
// TODO(b/395966861): delete the `flags.saveStage2ToFile` flag and just use this one
1834-
stage2SaveFile = flags.stage2FilenameToSaveTo;
1835-
}
1813+
String stage2SaveFile = flags.stage2FilenameToSaveTo;
18361814
if (stage1SaveFile != null) {
18371815
checkState(stage2SaveFile == null, "cannot save both stage 1 and stage 2");
18381816
checkState(stage1RestoreFile == null, "cannot perform stage 1 on a restored stage 1");

test/com/google/javascript/jscomp/CommandLineRunnerTest.java

-137
Original file line numberDiff line numberDiff line change
@@ -186,143 +186,6 @@ public void testStage1ErrorExitStatus() throws Exception {
186186
assertThat(new String(outReader.toByteArray(), UTF_8)).isEmpty();
187187
}
188188

189-
// TODO(b/395966861): delete this test, `test3StageCompile` is the same test but uses the new
190-
// stage 2 flags.
191-
@Test
192-
public void test3StageCompile_Old() throws Exception {
193-
194-
// Create a message bundle to use
195-
File msgBundle = temporaryFolder.newFile("messages.xtb");
196-
final ImmutableList<String> lines =
197-
ImmutableList.of(
198-
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
199-
"<!DOCTYPE translationbundle SYSTEM \"translationbundle.dtd\">",
200-
"<translationbundle lang=\"es\">",
201-
"<translation id=\"6289482750305328564\">hola</translation>",
202-
"</translationbundle>",
203-
"");
204-
writeLinesToFile(msgBundle, lines);
205-
206-
// Create test externs with a definition for goog.getMsg().
207-
final File externsFile = temporaryFolder.newFile("externs.js");
208-
writeLinesToFile(
209-
externsFile,
210-
"/**",
211-
" * @fileoverview test externs",
212-
" * @externs",
213-
" */",
214-
"var goog = {};",
215-
"/**",
216-
" * @nosideeffects",
217-
" * @param {string} msg",
218-
" * @param {Object=} placeholderReplacements",
219-
" * @param {Object=} options",
220-
" * @return {string}",
221-
" */",
222-
"goog.getMsg = function(msg, placeholderReplacements, options) {};");
223-
224-
// Create an input file
225-
File srcFile = temporaryFolder.newFile("input.js");
226-
writeLinesToFile(
227-
srcFile,
228-
"/** @desc greeting */",
229-
"const MSG_HELLO = goog.getMsg('hello');",
230-
"console.log(MSG_HELLO);");
231-
232-
// Create a path for the stage 1 output
233-
File stage1Save = temporaryFolder.newFile("stage1.save");
234-
235-
ImmutableList<String> commonFlags =
236-
ImmutableList.of(
237-
"--compilation_level=ADVANCED_OPTIMIZATIONS",
238-
"--source_map_include_content",
239-
"--translations_file",
240-
msgBundle.toString(),
241-
"--externs",
242-
externsFile.toString(),
243-
"--js",
244-
srcFile.toString());
245-
246-
// Run the compiler to generate the stage 1 save file
247-
final ImmutableList<String> stage1Flags =
248-
createStringList(
249-
commonFlags, new String[] {"--save_stage1_to_file", stage1Save.toString()});
250-
verifyFlagsAreIncompatibleWithChecksOnly(stage1Flags);
251-
CommandLineRunner runner =
252-
new CommandLineRunner(
253-
stringListToArray(stage1Flags), new PrintStream(outReader), new PrintStream(errReader));
254-
assertThat(runner.doRun()).isEqualTo(0);
255-
assertThat(new String(outReader.toByteArray(), UTF_8)).isEmpty();
256-
257-
assertThat(runner.getCompiler().toSource())
258-
.isEqualTo("const MSG_HELLO=goog.getMsg(\"hello\");console.log(MSG_HELLO);");
259-
260-
// Create a path for the stage 2 output
261-
File stage2Save = temporaryFolder.newFile("stage2.save");
262-
// run the compiler to generate the stage 2 save file
263-
final ImmutableList<String> stage2Flags =
264-
createStringList(
265-
commonFlags,
266-
new String[] {
267-
"--restore_stage1_from_file",
268-
stage1Save.toString(),
269-
"--save_stage2_to_file",
270-
stage2Save.toString()
271-
});
272-
verifyFlagsAreIncompatibleWithChecksOnly(stage2Flags);
273-
runner = new CommandLineRunner(stringListToArray(stage2Flags));
274-
assertThat(runner.doRun()).isEqualTo(0);
275-
276-
// During stage 2 the message is wrapped in a function call to protect it from mangling by
277-
// optimizations.
278-
assertThat(runner.getCompiler().toSource())
279-
.isEqualTo(
280-
concatStrings(
281-
"console.log(",
282-
"__jscomp_define_msg__({\"key\":\"MSG_HELLO\",\"msg_text\":\"hello\"})",
283-
");"));
284-
285-
// Create a path for the final output
286-
File compiledFile = temporaryFolder.newFile("compiled.js");
287-
// Create a path for the output source map
288-
File sourceMapFile = temporaryFolder.newFile("compiled.sourcemap");
289-
290-
// run the compiler to generate the final output
291-
final ImmutableList<String> stage3Flags =
292-
createStringList(
293-
commonFlags,
294-
new String[] {
295-
"--restore_stage2_from_file",
296-
stage2Save.toString(),
297-
"--js_output_file",
298-
compiledFile.toString(),
299-
"--create_source_map",
300-
sourceMapFile.toString()
301-
});
302-
verifyFlagsAreIncompatibleWithChecksOnly(stage3Flags);
303-
runner = new CommandLineRunner(stringListToArray(stage3Flags));
304-
assertThat(runner.doRun()).isEqualTo(0);
305-
306-
// During stage 3 the message is actually replaced and the output written to the compiled
307-
// output file.
308-
final String compiledJs = java.nio.file.Files.readString(compiledFile.toPath());
309-
assertThat(compiledJs).isEqualTo("console.log(\"hola\");\n");
310-
311-
final JsonObject expectedSourceMap = new JsonObject();
312-
expectedSourceMap.addProperty("version", 3);
313-
expectedSourceMap.addProperty("file", compiledFile.getAbsolutePath());
314-
expectedSourceMap.addProperty("lineCount", 1);
315-
expectedSourceMap.addProperty("mappings", "AAEAA,OAAQC,CAAAA,GAAR,CADkBC,MAClB;");
316-
expectedSourceMap.add("sources", newJsonArrayOfStrings(srcFile.getAbsolutePath()));
317-
expectedSourceMap.add(
318-
"sourcesContent", newJsonArrayOfStrings(java.nio.file.Files.readString(srcFile.toPath())));
319-
expectedSourceMap.add("names", newJsonArrayOfStrings("console", "log", "MSG_HELLO"));
320-
321-
final String sourceMapText = java.nio.file.Files.readString(sourceMapFile.toPath());
322-
JsonObject actualSourceMap = getJsonObjectFromJson(sourceMapText);
323-
assertThat(actualSourceMap).isEqualTo(expectedSourceMap);
324-
}
325-
326189
@Test
327190
public void test3StageCompile() throws Exception {
328191

0 commit comments

Comments
 (0)