fix missing requires in global functions and variables

Project: http://git-wip-us.apache.org/repos/asf/flex-falcon/repo
Commit: http://git-wip-us.apache.org/repos/asf/flex-falcon/commit/9ff297fa
Tree: http://git-wip-us.apache.org/repos/asf/flex-falcon/tree/9ff297fa
Diff: http://git-wip-us.apache.org/repos/asf/flex-falcon/diff/9ff297fa

Branch: refs/heads/develop
Commit: 9ff297fa659635bcaed8ab6a71366578d5ec2f4a
Parents: e1c69a7
Author: Alex Harui <[email protected]>
Authored: Mon Jan 25 11:25:58 2016 -0800
Committer: Alex Harui <[email protected]>
Committed: Mon Jan 25 16:19:12 2016 -0800

----------------------------------------------------------------------
 .../codegen/js/flexjs/TestFlexJSPackage.java    |   8 +-
 .../codegen/js/flexjs/TestFlexJSProject.java    |  15 +++
 .../flex/compiler/internal/test/TestBase.java   |   2 +-
 .../flexjs/projects/circular/Super_result.js    |   1 -
 .../flexjs/projects/internal/MainClass.as       |  38 ++++++
 .../projects/internal/MainClass_result.js       | 115 +++++++++++++++++++
 .../flexjs/projects/internal/OtherClass.as      |  25 ++++
 .../projects/internal/OtherClass_result.js      |  63 ++++++++++
 .../codegen/js/jx/PackageHeaderEmitter.java     |  70 ++++++-----
 9 files changed, 301 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/9ff297fa/compiler.jx.tests/src/org/apache/flex/compiler/internal/codegen/js/flexjs/TestFlexJSPackage.java
----------------------------------------------------------------------
diff --git 
a/compiler.jx.tests/src/org/apache/flex/compiler/internal/codegen/js/flexjs/TestFlexJSPackage.java
 
b/compiler.jx.tests/src/org/apache/flex/compiler/internal/codegen/js/flexjs/TestFlexJSPackage.java
index eaf24e6..db6d43d 100644
--- 
a/compiler.jx.tests/src/org/apache/flex/compiler/internal/codegen/js/flexjs/TestFlexJSPackage.java
+++ 
b/compiler.jx.tests/src/org/apache/flex/compiler/internal/codegen/js/flexjs/TestFlexJSPackage.java
@@ -1047,7 +1047,7 @@ public class TestFlexJSPackage extends TestGoogPackage
        {
                IFileNode node = compileAS("package {public function A(){}}");
                asBlockWalker.visitFile(node);
-               assertOutWithMetadata("/**\n * A\n *\n * @fileoverview\n *\n * 
@suppress {checkTypes|accessControls}\n */\n\ngoog.provide('A');\n\n/**\n * 
@export\n */\nA = function() {\n}");
+               assertOutWithMetadata("/**\n * A\n *\n * @fileoverview\n *\n * 
@suppress {checkTypes|accessControls}\n */\n\ngoog.provide('A');\n\n\n\n/**\n * 
@export\n */\nA = function() {\n}");
        }
 
        @Test
@@ -1055,7 +1055,7 @@ public class TestFlexJSPackage extends TestGoogPackage
        {
                IFileNode node = compileAS("package foo.bar.baz {public 
function A(){}}");
                asBlockWalker.visitFile(node);
-               assertOutWithMetadata("/**\n * foo.bar.baz.A\n *\n * 
@fileoverview\n *\n * @suppress {checkTypes|accessControls}\n 
*/\n\ngoog.provide('foo.bar.baz.A');\n\n/**\n * @export\n */\nfoo.bar.baz.A = 
function() {\n}");
+               assertOutWithMetadata("/**\n * foo.bar.baz.A\n *\n * 
@fileoverview\n *\n * @suppress {checkTypes|accessControls}\n 
*/\n\ngoog.provide('foo.bar.baz.A');\n\n\n\n/**\n * @export\n */\nfoo.bar.baz.A 
= function() {\n}");
        }
 
        @Test
@@ -1063,7 +1063,7 @@ public class TestFlexJSPackage extends TestGoogPackage
        {
                IFileNode node = compileAS("package {public var A:String = 
\"Hello\";");
                asBlockWalker.visitFile(node);
-               assertOutWithMetadata("/**\n * A\n *\n * @fileoverview\n *\n * 
@suppress {checkTypes|accessControls}\n */\n\ngoog.provide('A');\n\n/**\n * 
@export\n * @type {string}\n */\nA = \"Hello\"");
+               assertOutWithMetadata("/**\n * A\n *\n * @fileoverview\n *\n * 
@suppress {checkTypes|accessControls}\n */\n\ngoog.provide('A');\n\n\n\n/**\n * 
@export\n * @type {string}\n */\nA = \"Hello\"");
        }
 
        @Test
@@ -1071,7 +1071,7 @@ public class TestFlexJSPackage extends TestGoogPackage
        {
                IFileNode node = compileAS("package foo.bar.baz {public var 
A:String = \"Hello\";");
                asBlockWalker.visitFile(node);
-               assertOutWithMetadata("/**\n * foo.bar.baz.A\n *\n * 
@fileoverview\n *\n * @suppress {checkTypes|accessControls}\n 
*/\n\ngoog.provide('foo.bar.baz.A');\n\n/**\n * @export\n * @type {string}\n 
*/\nfoo.bar.baz.A = \"Hello\"");
+               assertOutWithMetadata("/**\n * foo.bar.baz.A\n *\n * 
@fileoverview\n *\n * @suppress {checkTypes|accessControls}\n 
*/\n\ngoog.provide('foo.bar.baz.A');\n\n\n\n/**\n * @export\n * @type 
{string}\n */\nfoo.bar.baz.A = \"Hello\"");
        }
     
     @Override

http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/9ff297fa/compiler.jx.tests/src/org/apache/flex/compiler/internal/codegen/js/flexjs/TestFlexJSProject.java
----------------------------------------------------------------------
diff --git 
a/compiler.jx.tests/src/org/apache/flex/compiler/internal/codegen/js/flexjs/TestFlexJSProject.java
 
b/compiler.jx.tests/src/org/apache/flex/compiler/internal/codegen/js/flexjs/TestFlexJSProject.java
index bba76fe..2b45164 100644
--- 
a/compiler.jx.tests/src/org/apache/flex/compiler/internal/codegen/js/flexjs/TestFlexJSProject.java
+++ 
b/compiler.jx.tests/src/org/apache/flex/compiler/internal/codegen/js/flexjs/TestFlexJSProject.java
@@ -95,6 +95,21 @@ public class TestFlexJSProject extends TestGoogProject
     }
 
     @Test
+    public void test_InternalAndSamePackageRequires()
+    {
+        String testDirPath = projectDirPath + "/internal";
+
+        String fileName = "MainClass";
+
+        sourcePath = "test-files"
+            + File.separator + projectDirPath + "/internal";
+        
+        List<String> compiledFileNames = compileProject(fileName, testDirPath);
+
+        assertProjectOut(compiledFileNames, testDirPath);
+    }
+
+    @Test
     public void test_IsItCircular()
     {
         String testDirPath = projectDirPath + "/circular";

http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/9ff297fa/compiler.jx.tests/src/org/apache/flex/compiler/internal/test/TestBase.java
----------------------------------------------------------------------
diff --git 
a/compiler.jx.tests/src/org/apache/flex/compiler/internal/test/TestBase.java 
b/compiler.jx.tests/src/org/apache/flex/compiler/internal/test/TestBase.java
index d09322e..7b83e79 100644
--- a/compiler.jx.tests/src/org/apache/flex/compiler/internal/test/TestBase.java
+++ b/compiler.jx.tests/src/org/apache/flex/compiler/internal/test/TestBase.java
@@ -324,7 +324,7 @@ public class TestBase implements ITestBase
                     BufferedOutputStream out = new BufferedOutputStream(
                             new FileOutputStream(outputClassFile));
 
-                    out.write(writer.toString().getBytes());
+                    
out.write(emitter.postProcess(writer.toString()).getBytes());
                     out.flush();
                     out.close();
                 }

http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/9ff297fa/compiler.jx.tests/test-files/flexjs/projects/circular/Super_result.js
----------------------------------------------------------------------
diff --git 
a/compiler.jx.tests/test-files/flexjs/projects/circular/Super_result.js 
b/compiler.jx.tests/test-files/flexjs/projects/circular/Super_result.js
index 83acc09..a8e15e1 100644
--- a/compiler.jx.tests/test-files/flexjs/projects/circular/Super_result.js
+++ b/compiler.jx.tests/test-files/flexjs/projects/circular/Super_result.js
@@ -21,7 +21,6 @@
 
 goog.provide('Super');
 
-goog.require('Base');
 
 
 

http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/9ff297fa/compiler.jx.tests/test-files/flexjs/projects/internal/MainClass.as
----------------------------------------------------------------------
diff --git a/compiler.jx.tests/test-files/flexjs/projects/internal/MainClass.as 
b/compiler.jx.tests/test-files/flexjs/projects/internal/MainClass.as
new file mode 100644
index 0000000..fe7c8a5
--- /dev/null
+++ b/compiler.jx.tests/test-files/flexjs/projects/internal/MainClass.as
@@ -0,0 +1,38 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  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
+{
+
+  public class MainClass
+  {
+    public function MainClass() 
+    {
+    }
+  }
+}
+
+class InternalClass
+{
+       public function InternalClass()
+       {
+               foo = new OtherClass();
+       }
+       
+       public var foo:OtherClass;
+}

http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/9ff297fa/compiler.jx.tests/test-files/flexjs/projects/internal/MainClass_result.js
----------------------------------------------------------------------
diff --git 
a/compiler.jx.tests/test-files/flexjs/projects/internal/MainClass_result.js 
b/compiler.jx.tests/test-files/flexjs/projects/internal/MainClass_result.js
new file mode 100644
index 0000000..2f5ea28
--- /dev/null
+++ b/compiler.jx.tests/test-files/flexjs/projects/internal/MainClass_result.js
@@ -0,0 +1,115 @@
+/**
+ * Licensed 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.
+ */
+/**
+ * MainClass
+ *
+ * @fileoverview
+ *
+ * @suppress {checkTypes|accessControls}
+ */
+
+goog.provide('MainClass');
+
+goog.require('OtherClass');
+
+
+
+/**
+ * @constructor
+ */
+MainClass = function() {
+};
+
+
+/**
+ * Metadata
+ *
+ * @type {Object.<string, Array.<Object>>}
+ */
+MainClass.prototype.FLEXJS_CLASS_INFO = { names: [{ name: 'MainClass', qName: 
'MainClass'}] };
+
+
+
+/**
+ * Reflection
+ *
+ * @return {Object.<string, Function>}
+ */
+MainClass.prototype.FLEXJS_REFLECTION_INFO = function () {
+  return {
+    variables: function () {
+      return {
+      };
+    },
+    accessors: function () {
+      return {
+      };
+    },
+    methods: function () {
+      return {
+        'MainClass': { type: '', declaredBy: 'MainClass'}
+      };
+    }
+  };
+};
+
+
+
+/**
+ * @constructor
+ */
+MainClass.InternalClass = function() {
+  this.foo = new OtherClass();
+};
+
+
+/**
+ * @export
+ * @type {OtherClass}
+ */
+MainClass.InternalClass.prototype.foo;
+
+
+/**
+ * Metadata
+ *
+ * @type {Object.<string, Array.<Object>>}
+ */
+MainClass.InternalClass.prototype.FLEXJS_CLASS_INFO = { names: [{ name: 
'InternalClass', qName: 'MainClass.InternalClass'}] };
+
+
+
+/**
+ * Reflection
+ *
+ * @return {Object.<string, Function>}
+ */
+MainClass.InternalClass.prototype.FLEXJS_REFLECTION_INFO = function () {
+  return {
+    variables: function () {
+      return {
+        'foo': { type: 'OtherClass'}
+      };
+    },
+    accessors: function () {
+      return {
+      };
+    },
+    methods: function () {
+      return {
+        'InternalClass': { type: '', declaredBy: 'MainClass.InternalClass'}
+      };
+    }
+  };
+};

http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/9ff297fa/compiler.jx.tests/test-files/flexjs/projects/internal/OtherClass.as
----------------------------------------------------------------------
diff --git 
a/compiler.jx.tests/test-files/flexjs/projects/internal/OtherClass.as 
b/compiler.jx.tests/test-files/flexjs/projects/internal/OtherClass.as
new file mode 100644
index 0000000..2d01b44
--- /dev/null
+++ b/compiler.jx.tests/test-files/flexjs/projects/internal/OtherClass.as
@@ -0,0 +1,25 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  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
+{
+  public class OtherClass
+  {
+    public function OtherClass() {}
+  }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/9ff297fa/compiler.jx.tests/test-files/flexjs/projects/internal/OtherClass_result.js
----------------------------------------------------------------------
diff --git 
a/compiler.jx.tests/test-files/flexjs/projects/internal/OtherClass_result.js 
b/compiler.jx.tests/test-files/flexjs/projects/internal/OtherClass_result.js
new file mode 100644
index 0000000..732d6fb
--- /dev/null
+++ b/compiler.jx.tests/test-files/flexjs/projects/internal/OtherClass_result.js
@@ -0,0 +1,63 @@
+/**
+ * Licensed 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.
+ */
+/**
+ * OtherClass
+ *
+ * @fileoverview
+ *
+ * @suppress {checkTypes|accessControls}
+ */
+
+goog.provide('OtherClass');
+
+
+
+/**
+ * @constructor
+ */
+OtherClass = function() {
+};
+
+
+/**
+ * Metadata
+ *
+ * @type {Object.<string, Array.<Object>>}
+ */
+OtherClass.prototype.FLEXJS_CLASS_INFO = { names: [{ name: 'OtherClass', 
qName: 'OtherClass'}] };
+
+
+
+/**
+ * Reflection
+ *
+ * @return {Object.<string, Function>}
+ */
+OtherClass.prototype.FLEXJS_REFLECTION_INFO = function () {
+  return {
+    variables: function () {
+      return {
+      };
+    },
+    accessors: function () {
+      return {
+      };
+    },
+    methods: function () {
+      return {
+        'OtherClass': { type: '', declaredBy: 'OtherClass'}
+      };
+    }
+  };
+};
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/flex-falcon/blob/9ff297fa/compiler.jx/src/org/apache/flex/compiler/internal/codegen/js/jx/PackageHeaderEmitter.java
----------------------------------------------------------------------
diff --git 
a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/js/jx/PackageHeaderEmitter.java
 
b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/js/jx/PackageHeaderEmitter.java
index 21d3c01..d026e9c 100644
--- 
a/compiler.jx/src/org/apache/flex/compiler/internal/codegen/js/jx/PackageHeaderEmitter.java
+++ 
b/compiler.jx/src/org/apache/flex/compiler/internal/codegen/js/jx/PackageHeaderEmitter.java
@@ -21,12 +21,14 @@ package org.apache.flex.compiler.internal.codegen.js.jx;
 
 import java.io.File;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
 
 import org.apache.flex.compiler.asdoc.flexjs.ASDocComment;
 import org.apache.flex.compiler.codegen.ISubEmitter;
 import org.apache.flex.compiler.codegen.js.IJSEmitter;
+import org.apache.flex.compiler.definitions.IDefinition;
 import org.apache.flex.compiler.definitions.IFunctionDefinition;
 import org.apache.flex.compiler.definitions.IPackageDefinition;
 import org.apache.flex.compiler.definitions.ITypeDefinition;
@@ -135,48 +137,56 @@ public class PackageHeaderEmitter extends JSSubEmitter 
implements
 
         ArrayList<String> writtenRequires = new ArrayList<String>();
 
-        ITypeDefinition type = EmitterUtils.findType(containedScope
-                .getAllLocalDefinitions());
+        Collection<IDefinition> localDefinitions = 
containedScope.getAllLocalDefinitions();
+        ITypeDefinition type = EmitterUtils.findType(localDefinitions);
+        IDefinition otherMainDefinition = null;
         if (type == null)
-            return;
-
-        ITypeNode typeNode = type.getNode();
-        if (typeNode instanceof ClassNode)
         {
-            ClassNode classNode = (ClassNode) typeNode;
-            ASDocComment asDoc = (ASDocComment) classNode.getASDocComment();
-            if (asDoc != null)
-            {
-                String asDocString = asDoc.commentNoEnd();
-                String ignoreToken = JSFlexJSEmitterTokens.IGNORE_IMPORT
-                        .getToken();
-                int ignoreIndex = asDocString.indexOf(ignoreToken);
-                while (ignoreIndex != -1)
-                {
-                    String ignorable = asDocString.substring(ignoreIndex
-                            + ignoreToken.length());
-                    int endIndex = ignorable.indexOf("\n");
-                    ignorable = ignorable.substring(0, endIndex);
-                    ignorable = ignorable.trim();
-                    // pretend we've already written the goog.requires for this
-                    writtenRequires.add(ignorable);
-                    ignoreIndex = asDocString.indexOf(ignoreToken,
-                            ignoreIndex + ignoreToken.length());
-                }
-            }
+               if (localDefinitions.isEmpty())
+                       return;
+               // function or variable definition
+               otherMainDefinition = localDefinitions.iterator().next();
         }
-
+        else
+        {
+               ITypeNode typeNode = type.getNode();
+               if (typeNode instanceof ClassNode)
+               {
+                   ClassNode classNode = (ClassNode) typeNode;
+                   ASDocComment asDoc = (ASDocComment) 
classNode.getASDocComment();
+                   if (asDoc != null)
+                   {
+                       String asDocString = asDoc.commentNoEnd();
+                       String ignoreToken = JSFlexJSEmitterTokens.IGNORE_IMPORT
+                               .getToken();
+                       int ignoreIndex = asDocString.indexOf(ignoreToken);
+                       while (ignoreIndex != -1)
+                       {
+                           String ignorable = asDocString.substring(ignoreIndex
+                                   + ignoreToken.length());
+                           int endIndex = ignorable.indexOf("\n");
+                           ignorable = ignorable.substring(0, endIndex);
+                           ignorable = ignorable.trim();
+                           // pretend we've already written the goog.requires 
for this
+                           writtenRequires.add(ignorable);
+                           ignoreIndex = asDocString.indexOf(ignoreToken,
+                                   ignoreIndex + ignoreToken.length());
+                       }
+                   }
+               }
+        }
+        
         //        if (project == null)
         //            project = getWalker().getProject();
 
         FlexJSProject flexProject = (FlexJSProject) getProject();
         ASProjectScope projectScope = (ASProjectScope) flexProject.getScope();
         ICompilationUnit cu = projectScope
-                .getCompilationUnitForDefinition(type);
+                .getCompilationUnitForDefinition(type != null ? type : 
otherMainDefinition);
         ArrayList<String> requiresList = flexProject.getRequires(cu);
         ArrayList<String> interfacesList = flexProject.getInterfaces(cu);
         
-        String cname = type.getQualifiedName();
+        String cname = (type != null) ? type.getQualifiedName() : 
otherMainDefinition.getQualifiedName();
         writtenRequires.add(cname); // make sure we don't add ourselves
 
         boolean emitsRequires = false;

Reply via email to