Skip to content

Commit a435a24

Browse files
concavelenzcopybara-github
authored andcommitted
Renaming MoveFunctionDeclarations and associated flags and options to better reflect what it actually does and distinguish it from RescopeGlobalSymbols which does a completely different kind of rewriting.
PiperOrigin-RevId: 323101509
1 parent 7d3c361 commit a435a24

File tree

8 files changed

+45
-34
lines changed

8 files changed

+45
-34
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ public void setReplaceMessagesWithChromeI18n(
834834
private Map<String, Object> tweakReplacements;
835835

836836
/** Move top-level function declarations to the top */
837-
public boolean moveFunctionDeclarations;
837+
public boolean rewriteGlobalDeclarationsForTryCatchWrapping;
838838

839839
boolean checksOnly;
840840

@@ -1423,7 +1423,7 @@ public CompilerOptions() {
14231423
defineReplacements = new HashMap<>();
14241424
tweakProcessing = TweakProcessing.OFF;
14251425
tweakReplacements = new HashMap<>();
1426-
moveFunctionDeclarations = false;
1426+
rewriteGlobalDeclarationsForTryCatchWrapping = false;
14271427
checksOnly = false;
14281428
outputJs = OutputJs.NORMAL;
14291429
generateExports = false;
@@ -2549,8 +2549,13 @@ public void setTweakReplacements(Map<String, Object> tweakReplacements) {
25492549
this.tweakReplacements = tweakReplacements;
25502550
}
25512551

2552+
@Deprecated
25522553
public void setMoveFunctionDeclarations(boolean moveFunctionDeclarations) {
2553-
this.moveFunctionDeclarations = moveFunctionDeclarations;
2554+
setRewriteGlobalDeclarationsForTryCatchWrapping(moveFunctionDeclarations);
2555+
}
2556+
2557+
public void setRewriteGlobalDeclarationsForTryCatchWrapping(boolean rewrite) {
2558+
this.rewriteGlobalDeclarationsForTryCatchWrapping = rewrite;
25542559
}
25552560

25562561
public void setCssRenamingMap(CssRenamingMap cssRenamingMap) {
@@ -3040,7 +3045,9 @@ public String toString() {
30403045
.add("messageBundle", messageBundle)
30413046
.add("moduleRoots", moduleRoots)
30423047
.add("chunksToPrintAfterEachPassRegexList", chunksToPrintAfterEachPassRegexList)
3043-
.add("moveFunctionDeclarations", moveFunctionDeclarations)
3048+
.add(
3049+
"rewriteGlobalDeclarationsForTryCatchWrapping",
3050+
rewriteGlobalDeclarationsForTryCatchWrapping)
30443051
.add("nameGenerator", nameGenerator)
30453052
.add("optimizeArgumentsArray", optimizeArgumentsArray)
30463053
.add("optimizeCalls", optimizeCalls)

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -777,11 +777,11 @@ protected List<PassFactory> getOptimizations() {
777777
}
778778

779779
// Move functions before extracting prototype member declarations.
780-
if (options.moveFunctionDeclarations
781-
// renamePrefixNamescape relies on moveFunctionDeclarations
780+
if (options.rewriteGlobalDeclarationsForTryCatchWrapping
781+
// renamePrefixNamescape relies on rewriteGlobalDeclarationsForTryCatchWrapping
782782
// to preserve semantics.
783783
|| options.renamePrefixNamespace != null) {
784-
passes.add(moveFunctionDeclarations);
784+
passes.add(rewriteGlobalDeclarationsForTryCatchWrapping);
785785
}
786786

787787
if (options.anonymousFunctionNaming == AnonymousFunctionNamingPolicy.MAPPED) {
@@ -2543,11 +2543,14 @@ compiler, PassNames.EXPLOIT_ASSIGN, new ExploitAssigns()))
25432543
.setFeatureSetForOptimizations()
25442544
.build();
25452545

2546-
/** Moves function declarations to the top, to simulate actual hoisting. */
2547-
private final PassFactory moveFunctionDeclarations =
2546+
/**
2547+
* Moves function declarations to the top to simulate actual hoisting and rewrites block scope
2548+
* declarations to 'var'.
2549+
*/
2550+
private final PassFactory rewriteGlobalDeclarationsForTryCatchWrapping =
25482551
PassFactory.builder()
2549-
.setName(PassNames.MOVE_FUNCTION_DECLARATIONS)
2550-
.setInternalFactory(MoveFunctionDeclarations::new)
2552+
.setName("rewriteGlobalDeclarationsForTryCatchWrapping")
2553+
.setInternalFactory(RewriteGlobalDeclarationsForTryCatchWrapping::new)
25512554
.setFeatureSetForOptimizations()
25522555
.build();
25532556

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ public final class PassNames {
7171
public static final String INLINE_TYPE_ALIASES = "inlineTypeAliases";
7272
public static final String INLINE_VARIABLES = "inlineVariables";
7373
public static final String LINT_CHECKS = "lintChecks";
74-
public static final String MOVE_FUNCTION_DECLARATIONS = "moveFunctionDeclarations";
7574
public static final String NAME_ANONYMOUS_FUNCTIONS = "nameAnonymousFunctions";
7675
public static final String NORMALIZE = "normalize";
7776
public static final String OPTIMIZE_ARGUMENTS_ARRAY = "optimizeArgumentsArray";

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,29 @@
2828
import java.util.Set;
2929

3030
/**
31-
* Finds all references to global symbols and rewrites them to be property
32-
* accesses to a special object with the same name as the global symbol.
31+
* Finds all references to global symbols and rewrites them to be property accesses to a special
32+
* object with the same name as the global symbol.
33+
*
34+
* <p>Given the name of the global object is NS
3335
*
34-
* Given the name of the global object is NS
3536
* <pre> var a = 1; function b() { return a }</pre>
37+
*
3638
* becomes
39+
*
3740
* <pre> NS.a = 1; NS.b = function b() { return NS.a }</pre>
3841
*
39-
* This allows splitting code into modules that depend on each other's
40-
* global symbols, without using polluting JavaScript's global scope with those
41-
* symbols. You typically define just a single global symbol, wrap each module
42-
* in a function wrapper, and pass the global symbol around, eg,
42+
* This allows splitting code into modules that depend on each other's global symbols, without using
43+
* polluting JavaScript's global scope with those symbols. You typically define just a single global
44+
* symbol, wrap each module in a function wrapper, and pass the global symbol around, eg,
45+
*
4346
* <pre> var uniqueNs = uniqueNs || {}; </pre>
44-
* <pre> (function (NS) { ...your module code here... })(uniqueNs); </pre>
4547
*
48+
* <pre> (function (NS) { ...your module code here... })(uniqueNs); </pre>
4649
*
47-
* <p>This compile step requires moveFunctionDeclarations to be turned on
48-
* to guarantee semantics.
50+
* <p>This compile step requires rewriteGlobalDeclarationsForTryCatchWrapping to be turned on to
51+
* guarantee semantics.
4952
*
5053
* <p>For lots of examples, see the unit test.
51-
*
5254
*/
5355
final class RescopeGlobalSymbols implements CompilerPass {
5456

src/com/google/javascript/jscomp/MoveFunctionDeclarations.java renamed to src/com/google/javascript/jscomp/RewriteGlobalDeclarationsForTryCatchWrapping.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@
5757
* <p>NOTE: now that this pass also moves class declarations it should be renamed along with the
5858
* associated options.
5959
*/
60-
class MoveFunctionDeclarations implements Callback, CompilerPass {
60+
class RewriteGlobalDeclarationsForTryCatchWrapping implements Callback, CompilerPass {
6161
private final AbstractCompiler compiler;
6262
private final ListMultimap<JSModule, Node> functions = ArrayListMultimap.create();
6363
private final ArrayList<Node> classes = new ArrayList<>();
6464

65-
MoveFunctionDeclarations(AbstractCompiler compiler) {
65+
RewriteGlobalDeclarationsForTryCatchWrapping(AbstractCompiler compiler) {
6666
this.compiler = compiler;
6767
}
6868

src/com/google/javascript/jscomp/debugger/common/CompilationParam.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -808,12 +808,12 @@ public String getJavaInfo() {
808808
MOVE_FUNCTION_DECLARATIONS(ParamGroup.OPTIMIZATION) {
809809
@Override
810810
public void apply(CompilerOptions options, boolean value) {
811-
options.setMoveFunctionDeclarations(value);
811+
options.setRewriteGlobalDeclarationsForTryCatchWrapping(value);
812812
}
813813

814814
@Override
815815
public boolean isApplied(CompilerOptions options) {
816-
return options.moveFunctionDeclarations;
816+
return options.rewriteGlobalDeclarationsForTryCatchWrapping;
817817
}
818818
},
819819

test/com/google/javascript/jscomp/MoveFunctionDeclarationsTest.java renamed to test/com/google/javascript/jscomp/RewriteGlobalDeclarationsForTryCatchWrappingTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
import org.junit.runners.JUnit4;
2323

2424
/**
25-
* Tests for {@link MoveFunctionDeclarations}
25+
* Tests for {@link RewriteGlobalDeclarationsForTryCatchWrapping}
2626
*/
2727
@RunWith(JUnit4.class)
28-
public final class MoveFunctionDeclarationsTest extends CompilerTestCase {
28+
public final class RewriteGlobalDeclarationsForTryCatchWrappingTest extends CompilerTestCase {
2929

3030
@Override
3131
protected CompilerPass getProcessor(Compiler compiler) {
32-
return new MoveFunctionDeclarations(compiler);
32+
return new RewriteGlobalDeclarationsForTryCatchWrapping(compiler);
3333
}
3434

3535
@Test

test/com/google/javascript/jscomp/integration/IntegrationTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2062,12 +2062,12 @@ public void testCollapseAnonymousFunctions() {
20622062
}
20632063

20642064
@Test
2065-
public void testMoveFunctionDeclarations() {
2065+
public void testRewriteGlobalDeclarationsForTryCatchWrapping() {
20662066
CompilerOptions options = createCompilerOptions();
20672067
String code = "var x = f(); function f() { return 3; }";
20682068
testSame(options, code);
20692069

2070-
options.moveFunctionDeclarations = true;
2070+
options.rewriteGlobalDeclarationsForTryCatchWrapping = true;
20712071
test(options, code, "var f = function() { return 3; }; var x = f();");
20722072
}
20732073

@@ -3284,11 +3284,11 @@ public void testRenameCollision() {
32843284
}
32853285

32863286
@Test
3287-
public void testRenamePrefixNamespaceActivatesMoveFunctionDeclarations() {
3287+
public void testRenamePrefixNamespaceActivatesRewriteGlobalDeclarationsForTryCatchWrapping() {
32883288
CompilerOptions options = createCompilerOptions();
32893289
String code = "var x = f; function f() { return 3; }";
32903290
testSame(options, code);
3291-
assertThat(options.moveFunctionDeclarations).isFalse();
3291+
assertThat(options.rewriteGlobalDeclarationsForTryCatchWrapping).isFalse();
32923292
options.setRenamePrefixNamespace("_");
32933293
test(options, code, "_.f = function() { return 3; }; _.x = _.f;");
32943294
}

0 commit comments

Comments
 (0)