This is an automated email from the ASF dual-hosted git repository.

joshtynjala pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/royale-compiler.git


The following commit(s) were added to refs/heads/develop by this push:
     new 9afab16  Definitions containing [JSModule] with a custom module name 
are no longer required to use strict camelCase naming scheme.
9afab16 is described below

commit 9afab16b897f5b4b34c6ed1a18a05d2a06a4f106
Author: Josh Tynjala <[email protected]>
AuthorDate: Tue Oct 8 14:59:04 2019 -0700

    Definitions containing [JSModule] with a custom module name are no longer 
required to use strict camelCase naming scheme.
    
    Previously, if a module was named my-module, the definition was required to 
be named myModule. Now, it can be named anything that is a valid identifier, 
and the compiler will output that identifier for the require() call. Example:
    
    [JSModule(name="my-module")] public class MyCustomModule {}
    
    Becomes:
    
    var MyCustomModule = require('my-module');
    
    Additionally, if the definition is in a package, a custom module name is 
not strictly required anymore. If no custom module name is specified, the first 
part of the package name is used as the module name. For instance, 
com.example.MyClass without a custom module name automatically requires 'com'.
---
 .../apache/royale/compiler/utils/NodeJSUtils.java  | 41 ---------------------
 .../compiler/clients/ExternCConfiguration.java     | 21 ++++++++++-
 .../codegen/js/jx/PackageHeaderEmitter.java        | 27 +++++++++-----
 .../common/JSModuleRequireDescription.java         | 37 +++++++++++++++++++
 .../internal/projects/RoyaleJSProject.java         | 43 ++++++++++++++--------
 5 files changed, 101 insertions(+), 68 deletions(-)

diff --git 
a/compiler-common/src/main/java/org/apache/royale/compiler/utils/NodeJSUtils.java
 
b/compiler-common/src/main/java/org/apache/royale/compiler/utils/NodeJSUtils.java
deleted file mode 100644
index 63fd232..0000000
--- 
a/compiler-common/src/main/java/org/apache/royale/compiler/utils/NodeJSUtils.java
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- *
- *  Licensed to the Apache Software Foundation (ASF) under one or more
- *  contributor license agreements.  See the NOTICE file distributed with
- *  this work for additional information regarding copyright ownership.
- *  The ASF licenses this file to You under the Apache License, Version 2.0
- *  (the "License"); you may not use this file except in compliance with
- *  the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS,
- *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *
- */
-
-package org.apache.royale.compiler.utils;
-
-public class NodeJSUtils
-{
-    /**
-     * Converts the name of a node module with dashes into a version in camel
-     * case so that it can be a valid identifier.
-     */
-    public static String convertFromDashesToCamelCase(String 
moduleNameWithDashes)
-    {
-        String camelCaseModule = moduleNameWithDashes;
-        int moduleIndex = camelCaseModule.indexOf("-");
-        while (moduleIndex != -1 && moduleIndex < camelCaseModule.length() - 1)
-        {
-            camelCaseModule = camelCaseModule.substring(0, moduleIndex)
-                    + camelCaseModule.substring(moduleIndex + 1, moduleIndex + 
2).toUpperCase()
-                    + camelCaseModule.substring(moduleIndex + 2);
-            moduleIndex = camelCaseModule.indexOf("-");
-        }
-        return camelCaseModule;
-    }
-}
diff --git 
a/compiler-externc/src/main/java/org/apache/royale/compiler/clients/ExternCConfiguration.java
 
b/compiler-externc/src/main/java/org/apache/royale/compiler/clients/ExternCConfiguration.java
index 00ed4cb..ca77287 100644
--- 
a/compiler-externc/src/main/java/org/apache/royale/compiler/clients/ExternCConfiguration.java
+++ 
b/compiler-externc/src/main/java/org/apache/royale/compiler/clients/ExternCConfiguration.java
@@ -38,7 +38,6 @@ import 
org.apache.royale.compiler.internal.config.annotations.Arguments;
 import org.apache.royale.compiler.internal.config.annotations.Config;
 import 
org.apache.royale.compiler.internal.config.annotations.InfiniteArguments;
 import org.apache.royale.compiler.internal.config.annotations.Mapping;
-import org.apache.royale.compiler.utils.NodeJSUtils;
 import org.apache.royale.utils.FilenameNormalization;
 
 public class ExternCConfiguration extends Configuration
@@ -327,7 +326,7 @@ public class ExternCConfiguration extends Configuration
         for (String module : namedModules)
         {
             //convert to camel case
-            String camelCaseModule = 
NodeJSUtils.convertFromDashesToCamelCase(module);
+            String camelCaseModule = convertFromDashesToCamelCase(module);
             if(basePackageName.length() == 0)
             {
                 if (classReference.getBaseName().equals(camelCaseModule))
@@ -344,6 +343,24 @@ public class ExternCConfiguration extends Configuration
         return null;
     }
 
+    /**
+     * Converts the name of a node module with dashes into a version in camel
+     * case so that it can be a valid identifier.
+     */
+    private String convertFromDashesToCamelCase(String moduleNameWithDashes)
+    {
+        String camelCaseModule = moduleNameWithDashes;
+        int moduleIndex = camelCaseModule.indexOf("-");
+        while (moduleIndex != -1 && moduleIndex < camelCaseModule.length() - 1)
+        {
+            camelCaseModule = camelCaseModule.substring(0, moduleIndex)
+                    + camelCaseModule.substring(moduleIndex + 1, moduleIndex + 
2).toUpperCase()
+                    + camelCaseModule.substring(moduleIndex + 2);
+            moduleIndex = camelCaseModule.indexOf("-");
+        }
+        return camelCaseModule;
+    }
+
     public File getJsRoot()
     {
         return jsRoot;
diff --git 
a/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/js/jx/PackageHeaderEmitter.java
 
b/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/js/jx/PackageHeaderEmitter.java
index d5ef151..154cd57 100644
--- 
a/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/js/jx/PackageHeaderEmitter.java
+++ 
b/compiler-jx/src/main/java/org/apache/royale/compiler/internal/codegen/js/jx/PackageHeaderEmitter.java
@@ -39,6 +39,7 @@ import 
org.apache.royale.compiler.internal.codegen.js.royale.JSRoyaleEmitterToke
 import org.apache.royale.compiler.internal.codegen.js.goog.JSGoogEmitterTokens;
 import org.apache.royale.compiler.internal.codegen.js.node.NodeEmitterTokens;
 import org.apache.royale.compiler.internal.codegen.js.utils.EmitterUtils;
+import org.apache.royale.compiler.internal.common.JSModuleRequireDescription;
 import org.apache.royale.compiler.internal.definitions.ClassDefinition;
 import 
org.apache.royale.compiler.internal.definitions.NamespaceDefinition.INamepaceDeclarationDirective;
 import org.apache.royale.compiler.internal.projects.RoyaleJSProject;
@@ -52,7 +53,6 @@ import org.apache.royale.compiler.targets.ITarget.TargetType;
 import org.apache.royale.compiler.tree.as.ITypeNode;
 import org.apache.royale.compiler.units.ICompilationUnit;
 import org.apache.royale.compiler.utils.NativeUtils;
-import org.apache.royale.compiler.utils.NodeJSUtils;
 
 public class PackageHeaderEmitter extends JSSubEmitter implements
         ISubEmitter<IPackageDefinition>
@@ -273,7 +273,7 @@ public class PackageHeaderEmitter extends JSSubEmitter 
implements
                 .getCompilationUnitForDefinition(type != null ? type : 
otherMainDefinition);
         ArrayList<String> requiresList = royaleProject.getRequires(cu);
         ArrayList<String> interfacesList = royaleProject.getInterfaces(cu);
-        ArrayList<String> externalRequiresList = 
royaleProject.getExternalRequires(cu);
+        ArrayList<JSModuleRequireDescription> externalRequiresList = 
royaleProject.getExternalRequires(cu);
 
         String cname = (type != null) ? type.getQualifiedName() : 
otherMainDefinition.getQualifiedName();
         writtenRequires.add(cname); // make sure we don't add ourselves
@@ -442,34 +442,41 @@ public class PackageHeaderEmitter extends JSSubEmitter 
implements
         return emitsInterfaces;
     }
 
-    private boolean emitExternalRequires(List<String> externalRequiresList, 
List<String> writtenRequires)
+    private boolean emitExternalRequires(List<JSModuleRequireDescription> 
externalRequiresList, List<String> writtenRequires)
     {
         boolean emitsExternalRequires = false;
         if (externalRequiresList != null)
         {
             Collections.sort(externalRequiresList);
-            for (String nodeJSModuleName : externalRequiresList)
+            for (JSModuleRequireDescription m : externalRequiresList)
             {
-                if (writtenRequires.indexOf(nodeJSModuleName) == -1)
+                // use the qname, if the definition is not in a package
+                String variableName = m.qname;
+                int firstDot = variableName.indexOf('.');
+                if(firstDot != -1)
                 {
-                    String moduleVariableName = 
NodeJSUtils.convertFromDashesToCamelCase(nodeJSModuleName);
-                    /* var x = require('x');\n */
+                    // otherwise, use the first part of the package name
+                    variableName = variableName.substring(0, firstDot);
+                }
+                if (writtenRequires.indexOf(m.moduleName) == -1)
+                {
+                    /* var xyz = require('xyz');\n */
                     /* var someModule = require('some-module');\n */
                     write(ASEmitterTokens.VAR);
                     write(ASEmitterTokens.SPACE);
-                    write(moduleVariableName);
+                    write(variableName);
                     write(ASEmitterTokens.SPACE);
                     write(ASEmitterTokens.EQUAL);
                     write(ASEmitterTokens.SPACE);
                     write(NodeEmitterTokens.REQUIRE);
                     write(ASEmitterTokens.PAREN_OPEN);
                     write(ASEmitterTokens.SINGLE_QUOTE);
-                    write(nodeJSModuleName);
+                    write(m.moduleName);
                     write(ASEmitterTokens.SINGLE_QUOTE);
                     write(ASEmitterTokens.PAREN_CLOSE);
                     writeNewline(ASEmitterTokens.SEMICOLON);
 
-                    writtenRequires.add(nodeJSModuleName);
+                    writtenRequires.add(m.moduleName);
 
                     emitsExternalRequires = true;
                 }
diff --git 
a/compiler-jx/src/main/java/org/apache/royale/compiler/internal/common/JSModuleRequireDescription.java
 
b/compiler-jx/src/main/java/org/apache/royale/compiler/internal/common/JSModuleRequireDescription.java
new file mode 100644
index 0000000..587cde5
--- /dev/null
+++ 
b/compiler-jx/src/main/java/org/apache/royale/compiler/internal/common/JSModuleRequireDescription.java
@@ -0,0 +1,37 @@
+/*
+ *
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+
+package org.apache.royale.compiler.internal.common;
+
+public class JSModuleRequireDescription implements 
Comparable<JSModuleRequireDescription>
+{
+       public JSModuleRequireDescription(String moduleName, String qname)
+       {
+               this.moduleName = moduleName;
+               this.qname = qname;
+       }
+
+       public String moduleName;
+       public String qname;
+
+       public int compareTo(JSModuleRequireDescription o)
+       {
+               return moduleName.compareTo(o.moduleName);
+       }
+}
\ No newline at end of file
diff --git 
a/compiler-jx/src/main/java/org/apache/royale/compiler/internal/projects/RoyaleJSProject.java
 
b/compiler-jx/src/main/java/org/apache/royale/compiler/internal/projects/RoyaleJSProject.java
index 8bf9044..14a5b99 100644
--- 
a/compiler-jx/src/main/java/org/apache/royale/compiler/internal/projects/RoyaleJSProject.java
+++ 
b/compiler-jx/src/main/java/org/apache/royale/compiler/internal/projects/RoyaleJSProject.java
@@ -49,6 +49,7 @@ import 
org.apache.royale.compiler.definitions.references.ReferenceFactory;
 import org.apache.royale.compiler.driver.IBackend;
 import 
org.apache.royale.compiler.internal.codegen.js.royale.JSRoyaleEmitterTokens;
 import 
org.apache.royale.compiler.internal.codegen.mxml.royale.MXMLRoyaleEmitterTokens;
+import org.apache.royale.compiler.internal.common.JSModuleRequireDescription;
 import org.apache.royale.compiler.internal.css.codegen.CSSCompilationSession;
 import org.apache.royale.compiler.internal.definitions.InterfaceDefinition;
 import 
org.apache.royale.compiler.internal.driver.js.royale.JSCSSCompilationSession;
@@ -93,7 +94,7 @@ public class RoyaleJSProject extends RoyaleProject
 
     private HashMap<ICompilationUnit, HashMap<String, String>> interfaces = 
new HashMap<ICompilationUnit, HashMap<String, String>>();
     private HashMap<ICompilationUnit, HashMap<String, DependencyType>> 
requires = new HashMap<ICompilationUnit, HashMap<String, DependencyType>>();
-    private HashMap<ICompilationUnit, HashMap<String, DependencyType>> 
jsModules = new HashMap<ICompilationUnit, HashMap<String, DependencyType>>();
+    private HashMap<ICompilationUnit, HashMap<JSModuleRequireDescription, 
DependencyType>> jsModules = new HashMap<ICompilationUnit, 
HashMap<JSModuleRequireDescription, DependencyType>>();
     public TreeSet<String> mixinClassNames;
     public HashMap<String, String> remoteClassAliasMap;
     public JSGoogConfiguration config;
@@ -227,28 +228,39 @@ public class RoyaleJSProject extends RoyaleProject
     private synchronized void updateJSModulesMap(ICompilationUnit from, 
ICompilationUnit to,
                        DependencyType dt, String qname)
     {
-        HashMap<String, DependencyType> reqs;
+        HashMap<JSModuleRequireDescription, DependencyType> reqs;
         if (jsModules.containsKey(from))
         {
             reqs = jsModules.get(from);
         }
         else
         {
-            reqs = new HashMap<String, DependencyType>();
+            reqs = new HashMap<JSModuleRequireDescription, DependencyType>();
             jsModules.put(from, reqs);
         }
-        IMetaTag tag = getJSModuleMetadata(to);
+        IMetaTag tag = getJSModuleMetadata(to, qname);
         if (tag != null)
         {
             IMetaTagAttribute nameAttribute = tag.getAttribute("name");
+            String moduleName = null;
             if (nameAttribute != null)
             {
-                reqs.put(nameAttribute.getValue(), dt);
+                moduleName = nameAttribute.getValue();
             }
-            else
+            if(moduleName == null)
             {
-                reqs.put(qname, dt);
+                int idx = qname.indexOf('.');
+                if(idx == -1)
+                {
+                    moduleName = qname;
+                }
+                else
+                {
+                    moduleName = qname.substring(0, idx);
+                }
             }
+            JSModuleRequireDescription module = new 
JSModuleRequireDescription(moduleName, qname);
+            reqs.put(module, dt);
         }
     }
     
@@ -291,7 +303,7 @@ public class RoyaleJSProject extends RoyaleProject
     // definitions that should be considered external linkage
     public Collection<String> unitTestExterns;
 
-    private IMetaTag getJSModuleMetadata(ICompilationUnit cu)
+    private IMetaTag getJSModuleMetadata(ICompilationUnit cu, String qname)
     {
         try
         {
@@ -299,7 +311,7 @@ public class RoyaleJSProject extends RoyaleProject
             while(iterator.hasNext())
             {
                 IDefinition def = iterator.next();
-                if (def.hasMetaTagByName("JSModule"))
+                if (def.getQualifiedName().equals(qname) && 
def.hasMetaTagByName("JSModule"))
                 {
                     return def.getMetaTagByName("JSModule");
                 }
@@ -449,15 +461,16 @@ public class RoyaleJSProject extends RoyaleProject
         return null;
     }
 
-    public ArrayList<String> getExternalRequires(ICompilationUnit from)
+    public ArrayList<JSModuleRequireDescription> 
getExternalRequires(ICompilationUnit from)
     {
         if (jsModules.containsKey(from))
         {
-            HashMap<String, DependencyType> map = jsModules.get(from);
-            ArrayList<String> arr = new ArrayList<String>();
-            Set<String> cus = map.keySet();
-            for (String s : cus)
-                arr.add(s);
+            HashMap<JSModuleRequireDescription, DependencyType> map = 
jsModules.get(from);
+            ArrayList<JSModuleRequireDescription> arr = new 
ArrayList<JSModuleRequireDescription>();
+            for (JSModuleRequireDescription m : map.keySet())
+            {
+                arr.add(m);
+            }
             return arr;
         }
         return null;

Reply via email to