Skip to content

Commit 0461ee6

Browse files
sjamesrcopybara-github
authored andcommitted
Don't consider member function defs when rewriting aliases
Previously, the compiler would crash when emitting member function names that refer to collapsed properties. This is a problem when the output language is ES6 and above. PiperOrigin-RevId: 553475247
1 parent ac8c8de commit 0461ee6

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,12 @@ private void rewriteNestedAliasReference(
975975
continue;
976976
}
977977

978+
if (target.isMemberFunctionDef()) {
979+
// In the case of a method reference, the reference to the alias is not fully qualified,
980+
// so we should not touch it, see b/293184904.
981+
continue;
982+
}
983+
978984
for (int i = 0; i <= depth; i++) {
979985
if (target.isGetProp()) {
980986
target = target.getFirstChild();

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4797,4 +4797,43 @@ public void testNoSideEffectsPropagationOnStaticMembers_withEs2018Out() {
47974797
// to hide side-effects.
47984798
"");
47994799
}
4800+
4801+
@Test
4802+
public void testDeclareLegacyNamespaceSubModuleCrash() {
4803+
CompilerOptions options = createCompilerOptions();
4804+
CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
4805+
options.setLanguageOut(LanguageMode.ECMASCRIPT_2016);
4806+
4807+
options.setRemoveDeadCode(false);
4808+
options.setRemoveUnusedVariables(Reach.NONE);
4809+
options.setRemoveUnusedClassProperties(false);
4810+
4811+
// Ensures that references to aliases that occur within member function defs do not crash the
4812+
// compiler (see b/293184904). In this example, the name 'method' in 'static method() {...}'
4813+
// is a reference to a.b.Foo.method, but since the reference is not fully qualified, the
4814+
// compiler should not try to touch it.
4815+
//
4816+
// This test case contains a use of a computed property to ensure that the fix for the above
4817+
// problem does not affect computed properties.
4818+
test(
4819+
options,
4820+
new String[] {
4821+
lines(
4822+
"goog.module('a.b');",
4823+
"goog.module.declareLegacyNamespace();",
4824+
"var b = {};",
4825+
"exports = b;\n"),
4826+
lines(
4827+
"goog.provide('a.b.Foo');",
4828+
"a.b.Foo = class {",
4829+
" static method() {}",
4830+
" static getVarName() { return 'someVar'; }",
4831+
"};",
4832+
"var x = {[a.b.Foo.getVarName()]: '4'};",
4833+
"alert(x['someVar']);")
4834+
},
4835+
new String[] {
4836+
lines(""), lines("alert(\"4\");"),
4837+
});
4838+
}
48004839
}

0 commit comments

Comments
 (0)