Skip to content

Commit

Permalink
Report error instead of exception for getter or setter in export obje…
Browse files Browse the repository at this point in the history
…ct literal.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=157834460
  • Loading branch information
bellashim authored and brad4d committed Jun 2, 2017
1 parent 645f971 commit b37e600
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/com/google/javascript/jscomp/ProcessCommonJSModules.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public final class ProcessCommonJSModules implements CompilerPass {
"Suspicious re-assignment of \"exports\" variable."
+ " Did you actually intend to export something?");

public static final DiagnosticType UNSUPPORTED_GETTER_SETTER_METHOD =
DiagnosticType.warning(
"JSC_UNSUPPORTED_GETTER_SETTER_METHOD",
"Getter and setter methods are unsupported.");

private final Compiler compiler;
private final boolean reportDependencies;

Expand Down Expand Up @@ -863,10 +868,14 @@ private void expandObjectLitAssignment(NodeTraversal t, Node export) {

Node rValue = NodeUtil.getRValueOfLValue(export);
Node key = rValue.getFirstChild();

while (key != null) {
Node lhs;
if (key.isQuotedString()) {
lhs = IR.getelem(export.cloneTree(), IR.string(key.getString()));
} else if (key.isGetterDef() || key.isSetterDef()) {
compiler.report(t.makeError(key, UNSUPPORTED_GETTER_SETTER_METHOD));
return;
} else {
lhs = IR.getprop(export.cloneTree(), IR.string(key.getString()));
}
Expand Down
10 changes: 10 additions & 0 deletions test/com/google/javascript/jscomp/ProcessCommonJSModulesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -829,4 +829,14 @@ public void testAbsoluteImportsWithModuleRoots() {
"var name = module$mod$name;",
"(function() { module$mod$name(); })();"))));
}

public void testIssue2510() {
setFilename("test");
testModules(
"module.exports = {a: function() {return 1}};",
LINE_JOINER.join(
"goog.provide('module$test');",
"/** @const */ var module$test = {};",
"module$test.a = function() {return 1};"));
}
}

0 comments on commit b37e600

Please sign in to comment.