Skip to content

Commit

Permalink
Don't consider member function defs when rewriting aliases
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sjamesr authored and copybara-github committed Aug 3, 2023
1 parent ac8c8de commit 0461ee6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,12 @@ private void rewriteNestedAliasReference(
continue;
}

if (target.isMemberFunctionDef()) {
// In the case of a method reference, the reference to the alias is not fully qualified,
// so we should not touch it, see b/293184904.
continue;
}

for (int i = 0; i <= depth; i++) {
if (target.isGetProp()) {
target = target.getFirstChild();
Expand Down
39 changes: 39 additions & 0 deletions test/com/google/javascript/jscomp/integration/IntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4797,4 +4797,43 @@ public void testNoSideEffectsPropagationOnStaticMembers_withEs2018Out() {
// to hide side-effects.
"");
}

@Test
public void testDeclareLegacyNamespaceSubModuleCrash() {
CompilerOptions options = createCompilerOptions();
CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
options.setLanguageOut(LanguageMode.ECMASCRIPT_2016);

options.setRemoveDeadCode(false);
options.setRemoveUnusedVariables(Reach.NONE);
options.setRemoveUnusedClassProperties(false);

// Ensures that references to aliases that occur within member function defs do not crash the
// compiler (see b/293184904). In this example, the name 'method' in 'static method() {...}'
// is a reference to a.b.Foo.method, but since the reference is not fully qualified, the
// compiler should not try to touch it.
//
// This test case contains a use of a computed property to ensure that the fix for the above
// problem does not affect computed properties.
test(
options,
new String[] {
lines(
"goog.module('a.b');",
"goog.module.declareLegacyNamespace();",
"var b = {};",
"exports = b;\n"),
lines(
"goog.provide('a.b.Foo');",
"a.b.Foo = class {",
" static method() {}",
" static getVarName() { return 'someVar'; }",
"};",
"var x = {[a.b.Foo.getVarName()]: '4'};",
"alert(x['someVar']);")
},
new String[] {
lines(""), lines("alert(\"4\");"),
});
}
}

0 comments on commit 0461ee6

Please sign in to comment.