From 93315a6d2da10f6c6e5fc8f3a0ea07e8b5d37eaa Mon Sep 17 00:00:00 2001 From: Michael Zhou Date: Tue, 1 Mar 2016 00:52:37 -0500 Subject: [PATCH] Don't rename @define's in ProcessEs6Modules Treat @define's as truly global definitions and don't mangle the names. Fixes #1601. --- .../google/javascript/jscomp/ProcessEs6Modules.java | 5 +++++ .../javascript/jscomp/ProcessEs6ModulesTest.java | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/com/google/javascript/jscomp/ProcessEs6Modules.java b/src/com/google/javascript/jscomp/ProcessEs6Modules.java index e67d307e6b4..6f91db215c7 100644 --- a/src/com/google/javascript/jscomp/ProcessEs6Modules.java +++ b/src/com/google/javascript/jscomp/ProcessEs6Modules.java @@ -499,6 +499,11 @@ public void visit(NodeTraversal t, Node n, Node parent) { Var var = t.getScope().getVar(name); if (var != null && var.isGlobal()) { + JSDocInfo varInfo = NodeUtil.getBestJSDocInfo(var.getNameNode()); + // Assume @define's are truly global and don't mangle them. + if (varInfo != null && varInfo.isDefine()) { + return; + } // Avoid polluting the global namespace. n.setString(name + "$$" + suffix); n.setOriginalName(name); diff --git a/test/com/google/javascript/jscomp/ProcessEs6ModulesTest.java b/test/com/google/javascript/jscomp/ProcessEs6ModulesTest.java index 53854f2f7ef..8c5a9dd720d 100644 --- a/test/com/google/javascript/jscomp/ProcessEs6ModulesTest.java +++ b/test/com/google/javascript/jscomp/ProcessEs6ModulesTest.java @@ -606,4 +606,14 @@ public void testNamespaceImports() { testModules("import * as Foo from 'goog:other.Foo';", ProcessEs6Modules.NAMESPACE_IMPORT_CANNOT_USE_STAR); } + + public void testDefine() { + testModules( + LINE_JOINER.join( + "import name from 'other';", + "/** @define {boolean} */ var FOO = true; use(FOO);"), + LINE_JOINER.join( + "goog.require('module$other');", + "/** @define {boolean} */ var FOO = true; use(FOO);")); + } }