[Pkg-javascript-commits] [node-ast-types] 01/06: Imported Upstream version 0.8.16

Julien Puydt julien.puydt at laposte.net
Sun May 15 13:44:53 UTC 2016


This is an automated email from the git hooks/post-receive script.

jpuydt-guest pushed a commit to branch master
in repository node-ast-types.

commit 8236a1078f4b55b0a9f9a5ee3e63a8d7c6e648e2
Author: Julien Puydt <julien.puydt at laposte.net>
Date:   Sun May 15 09:39:25 2016 +0200

    Imported Upstream version 0.8.16
---
 .travis.yml                    |   1 +
 README.md                      |   7 +--
 def/core.js                    |  10 +++-
 def/{fb-harmony.js => flow.js} | 114 +++++++----------------------------------
 def/jsx.js                     | 100 ++++++++++++++++++++++++++++++++++++
 lib/scope.js                   |  59 ++++++++++++++++-----
 main.js                        |   3 +-
 package.json                   |   2 +-
 test/run.js                    |  58 +++++++++++++++++++++
 9 files changed, 240 insertions(+), 114 deletions(-)

diff --git a/.travis.yml b/.travis.yml
index 05f32a1..6875ba4 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,5 +1,6 @@
 language: node_js
 node_js:
+  - "5.0"
   - "4.0"
   - "iojs"
   - "0.12"
diff --git a/README.md b/README.md
index 7b63289..fc8ca80 100644
--- a/README.md
+++ b/README.md
@@ -481,8 +481,9 @@ b.file("lib/types.js", b.thisExpression());
 ```
 The `def` syntax is used to define all the default AST node types found in
 [core.js](def/core.js),
+[e4x.js](def/e4x.js),
 [es6.js](def/es6.js),
-[mozilla.js](def/mozilla.js),
-[e4x.js](def/e4x.js), and
-[fb-harmony.js](def/fb-harmony.js), so you have
+[es7.js](def/es7.js),
+[flow.js](def/flow.js), and
+[jsx.js](def/jsx.js), so you have
 no shortage of examples to learn from.
diff --git a/def/core.js b/def/core.js
index 6441cdc..2942c4b 100644
--- a/def/core.js
+++ b/def/core.js
@@ -305,7 +305,15 @@ def("MemberExpression")
     .build("object", "property", "computed")
     .field("object", def("Expression"))
     .field("property", or(def("Identifier"), def("Expression")))
-    .field("computed", Boolean, defaults["false"]);
+    .field("computed", Boolean, function(){
+        var type = this.property.type;
+        if (type === 'Literal' ||
+            type === 'MemberExpression' ||
+            type === 'BinaryExpression') {
+            return true;
+        }
+        return false;
+    });
 
 def("Pattern").bases("Node");
 
diff --git a/def/fb-harmony.js b/def/flow.js
similarity index 67%
rename from def/fb-harmony.js
rename to def/flow.js
index 654c223..b7774a6 100644
--- a/def/fb-harmony.js
+++ b/def/flow.js
@@ -5,100 +5,6 @@ var def = types.Type.def;
 var or = types.Type.or;
 var defaults = require("../lib/shared").defaults;
 
-def("JSXAttribute")
-    .bases("Node")
-    .build("name", "value")
-    .field("name", or(def("JSXIdentifier"), def("JSXNamespacedName")))
-    .field("value", or(
-        def("Literal"), // attr="value"
-        def("JSXExpressionContainer"), // attr={value}
-        null // attr= or just attr
-    ), defaults["null"]);
-
-def("JSXIdentifier")
-    .bases("Identifier")
-    .build("name")
-    .field("name", String);
-
-def("JSXNamespacedName")
-    .bases("Node")
-    .build("namespace", "name")
-    .field("namespace", def("JSXIdentifier"))
-    .field("name", def("JSXIdentifier"));
-
-def("JSXMemberExpression")
-    .bases("MemberExpression")
-    .build("object", "property")
-    .field("object", or(def("JSXIdentifier"), def("JSXMemberExpression")))
-    .field("property", def("JSXIdentifier"))
-    .field("computed", Boolean, defaults.false);
-
-var JSXElementName = or(
-    def("JSXIdentifier"),
-    def("JSXNamespacedName"),
-    def("JSXMemberExpression")
-);
-
-def("JSXSpreadAttribute")
-    .bases("Node")
-    .build("argument")
-    .field("argument", def("Expression"));
-
-var JSXAttributes = [or(
-    def("JSXAttribute"),
-    def("JSXSpreadAttribute")
-)];
-
-def("JSXExpressionContainer")
-    .bases("Expression")
-    .build("expression")
-    .field("expression", def("Expression"));
-
-def("JSXElement")
-    .bases("Expression")
-    .build("openingElement", "closingElement", "children")
-    .field("openingElement", def("JSXOpeningElement"))
-    .field("closingElement", or(def("JSXClosingElement"), null), defaults["null"])
-    .field("children", [or(
-        def("JSXElement"),
-        def("JSXExpressionContainer"),
-        def("JSXText"),
-        def("Literal") // TODO Esprima should return JSXText instead.
-    )], defaults.emptyArray)
-    .field("name", JSXElementName, function() {
-        // Little-known fact: the `this` object inside a default function
-        // is none other than the partially-built object itself, and any
-        // fields initialized directly from builder function arguments
-        // (like openingElement, closingElement, and children) are
-        // guaranteed to be available.
-        return this.openingElement.name;
-    }, true) // hidden from traversal
-    .field("selfClosing", Boolean, function() {
-        return this.openingElement.selfClosing;
-    }, true) // hidden from traversal
-    .field("attributes", JSXAttributes, function() {
-        return this.openingElement.attributes;
-    }, true); // hidden from traversal
-
-def("JSXOpeningElement")
-    .bases("Node") // TODO Does this make sense? Can't really be an JSXElement.
-    .build("name", "attributes", "selfClosing")
-    .field("name", JSXElementName)
-    .field("attributes", JSXAttributes, defaults.emptyArray)
-    .field("selfClosing", Boolean, defaults["false"]);
-
-def("JSXClosingElement")
-    .bases("Node") // TODO Same concern.
-    .build("name")
-    .field("name", JSXElementName);
-
-def("JSXText")
-    .bases("Literal")
-    .build("value")
-    .field("value", String);
-
-def("JSXEmptyExpression").bases("Expression").build();
-
 // Type Annotations
 def("Type").bases("Node");
 
@@ -154,6 +60,14 @@ def("NullableTypeAnnotation")
   .build("typeAnnotation")
   .field("typeAnnotation", def("Type"));
 
+def("NullLiteralTypeAnnotation")
+  .bases("Type")
+  .build();
+
+def("ThisTypeAnnotation")
+  .bases("Type")
+  .build();
+
 def("FunctionTypeAnnotation")
   .bases("Type")
   .build("params", "returnType", "rest", "typeParameters")
@@ -201,7 +115,7 @@ def("ObjectTypeCallProperty")
   .bases("Node")
   .build("value")
   .field("value", def("FunctionTypeAnnotation"))
-  .field("static", Boolean, false);
+  .field("static", Boolean, defaults["false"]);
 
 def("QualifiedTypeIdentifier")
   .bases("Node")
@@ -273,7 +187,7 @@ def("ClassImplements")
          defaults["null"]);
 
 def("InterfaceDeclaration")
-  .bases("Statement")
+  .bases("Declaration")
   .build("id", "body", "extends")
   .field("id", def("Identifier"))
   .field("typeParameters",
@@ -282,6 +196,10 @@ def("InterfaceDeclaration")
   .field("body", def("ObjectTypeAnnotation"))
   .field("extends", [def("InterfaceExtends")]);
 
+def("DeclareInterface")
+  .bases("InterfaceDeclaration")
+  .build("id", "body", "extends");
+
 def("InterfaceExtends")
   .bases("Node")
   .build("id")
@@ -295,6 +213,10 @@ def("TypeAlias")
   .field("typeParameters", or(def("TypeParameterDeclaration"), null))
   .field("right", def("Type"));
 
+def("DeclareTypeAlias")
+  .bases("TypeAlias")
+  .build("id", "typeParameters", "right");
+
 def("TypeCastExpression")
   .bases("Expression")
   .build("expression", "typeAnnotation")
diff --git a/def/jsx.js b/def/jsx.js
new file mode 100644
index 0000000..18fa4ea
--- /dev/null
+++ b/def/jsx.js
@@ -0,0 +1,100 @@
+require("./es7");
+
+var types = require("../lib/types");
+var def = types.Type.def;
+var or = types.Type.or;
+var defaults = require("../lib/shared").defaults;
+
+def("JSXAttribute")
+    .bases("Node")
+    .build("name", "value")
+    .field("name", or(def("JSXIdentifier"), def("JSXNamespacedName")))
+    .field("value", or(
+        def("Literal"), // attr="value"
+        def("JSXExpressionContainer"), // attr={value}
+        null // attr= or just attr
+    ), defaults["null"]);
+
+def("JSXIdentifier")
+    .bases("Identifier")
+    .build("name")
+    .field("name", String);
+
+def("JSXNamespacedName")
+    .bases("Node")
+    .build("namespace", "name")
+    .field("namespace", def("JSXIdentifier"))
+    .field("name", def("JSXIdentifier"));
+
+def("JSXMemberExpression")
+    .bases("MemberExpression")
+    .build("object", "property")
+    .field("object", or(def("JSXIdentifier"), def("JSXMemberExpression")))
+    .field("property", def("JSXIdentifier"))
+    .field("computed", Boolean, defaults.false);
+
+var JSXElementName = or(
+    def("JSXIdentifier"),
+    def("JSXNamespacedName"),
+    def("JSXMemberExpression")
+);
+
+def("JSXSpreadAttribute")
+    .bases("Node")
+    .build("argument")
+    .field("argument", def("Expression"));
+
+var JSXAttributes = [or(
+    def("JSXAttribute"),
+    def("JSXSpreadAttribute")
+)];
+
+def("JSXExpressionContainer")
+    .bases("Expression")
+    .build("expression")
+    .field("expression", def("Expression"));
+
+def("JSXElement")
+    .bases("Expression")
+    .build("openingElement", "closingElement", "children")
+    .field("openingElement", def("JSXOpeningElement"))
+    .field("closingElement", or(def("JSXClosingElement"), null), defaults["null"])
+    .field("children", [or(
+        def("JSXElement"),
+        def("JSXExpressionContainer"),
+        def("JSXText"),
+        def("Literal") // TODO Esprima should return JSXText instead.
+    )], defaults.emptyArray)
+    .field("name", JSXElementName, function() {
+        // Little-known fact: the `this` object inside a default function
+        // is none other than the partially-built object itself, and any
+        // fields initialized directly from builder function arguments
+        // (like openingElement, closingElement, and children) are
+        // guaranteed to be available.
+        return this.openingElement.name;
+    }, true) // hidden from traversal
+    .field("selfClosing", Boolean, function() {
+        return this.openingElement.selfClosing;
+    }, true) // hidden from traversal
+    .field("attributes", JSXAttributes, function() {
+        return this.openingElement.attributes;
+    }, true); // hidden from traversal
+
+def("JSXOpeningElement")
+    .bases("Node") // TODO Does this make sense? Can't really be an JSXElement.
+    .build("name", "attributes", "selfClosing")
+    .field("name", JSXElementName)
+    .field("attributes", JSXAttributes, defaults.emptyArray)
+    .field("selfClosing", Boolean, defaults["false"]);
+
+def("JSXClosingElement")
+    .bases("Node") // TODO Same concern.
+    .build("name")
+    .field("name", JSXElementName);
+
+def("JSXText")
+    .bases("Literal")
+    .build("value")
+    .field("value", String);
+
+def("JSXEmptyExpression").bases("Expression").build();
diff --git a/lib/scope.js b/lib/scope.js
index 34ae1f1..f4c7b9d 100644
--- a/lib/scope.js
+++ b/lib/scope.js
@@ -34,7 +34,8 @@ function Scope(path, parentScope) {
         isGlobal: { value: !parentScope, enumerable: true },
         depth: { value: depth },
         parent: { value: parentScope },
-        bindings: { value: {} }
+        bindings: { value: {} },
+        types: { value: {} },
     });
 }
 
@@ -67,6 +68,11 @@ Sp.declares = function(name) {
     return hasOwn.call(this.bindings, name);
 };
 
+Sp.declaresType = function(name) {
+    this.scan();
+    return hasOwn.call(this.types, name);
+};
+
 Sp.declareTemporary = function(prefix) {
     if (prefix) {
         if (!/^[a-z$_]/i.test(prefix)) {
@@ -115,7 +121,7 @@ Sp.scan = function(force) {
             // Empty out this.bindings, just in cases.
             delete this.bindings[name];
         }
-        scanScope(this.path, this.bindings);
+        scanScope(this.path, this.bindings, this.types);
         this.didScan = true;
     }
 };
@@ -125,7 +131,12 @@ Sp.getBindings = function () {
     return this.bindings;
 };
 
-function scanScope(path, bindings) {
+Sp.getTypes = function () {
+    this.scan();
+    return this.types;
+};
+
+function scanScope(path, bindings, scopeTypes) {
     var node = path.value;
     ScopeType.assert(node);
 
@@ -136,11 +147,11 @@ function scanScope(path, bindings) {
         addPattern(path.get("param"), bindings);
 
     } else {
-        recursiveScanScope(path, bindings);
+        recursiveScanScope(path, bindings, scopeTypes);
     }
 }
 
-function recursiveScanScope(path, bindings) {
+function recursiveScanScope(path, bindings, scopeTypes) {
     var node = path.value;
 
     if (path.parent &&
@@ -154,7 +165,7 @@ function recursiveScanScope(path, bindings) {
 
     } else if (isArray.check(node)) {
         path.each(function(childPath) {
-            recursiveScanChild(childPath, bindings);
+            recursiveScanChild(childPath, bindings, scopeTypes);
         });
 
     } else if (namedTypes.Function.check(node)) {
@@ -162,11 +173,14 @@ function recursiveScanScope(path, bindings) {
             addPattern(paramPath, bindings);
         });
 
-        recursiveScanChild(path.get("body"), bindings);
+        recursiveScanChild(path.get("body"), bindings, scopeTypes);
+
+    } else if (namedTypes.TypeAlias && namedTypes.TypeAlias.check(node)) {
+        addTypePattern(path.get("id"), scopeTypes);
 
     } else if (namedTypes.VariableDeclarator.check(node)) {
         addPattern(path.get("id"), bindings);
-        recursiveScanChild(path.get("init"), bindings);
+        recursiveScanChild(path.get("init"), bindings, scopeTypes);
 
     } else if (node.type === "ImportSpecifier" ||
                node.type === "ImportNamespaceSpecifier" ||
@@ -187,12 +201,12 @@ function recursiveScanScope(path, bindings) {
             if (childPath.value !== child) {
                 throw new Error("");
             }
-            recursiveScanChild(childPath, bindings);
+            recursiveScanChild(childPath, bindings, scopeTypes);
         });
     }
 }
 
-function recursiveScanChild(path, bindings) {
+function recursiveScanChild(path, bindings, scopeTypes) {
     var node = path.value;
 
     if (!node || Expression.check(node)) {
@@ -213,7 +227,7 @@ function recursiveScanChild(path, bindings) {
             // Any declarations that occur inside the catch body that do
             // not have the same name as the catch parameter should count
             // as bindings in the outer scope.
-            recursiveScanScope(path.get("body"), bindings);
+            recursiveScanScope(path.get("body"), bindings, scopeTypes);
 
             // If a new binding matching the catch parameter name was
             // created while scanning the catch body, ignore it because it
@@ -225,7 +239,7 @@ function recursiveScanChild(path, bindings) {
         }
 
     } else {
-        recursiveScanScope(path, bindings);
+        recursiveScanScope(path, bindings, scopeTypes);
     }
 }
 
@@ -278,6 +292,20 @@ function addPattern(patternPath, bindings) {
     }
 }
 
+function addTypePattern(patternPath, types) {
+    var pattern = patternPath.value;
+    namedTypes.Pattern.assert(pattern);
+
+    if (namedTypes.Identifier.check(pattern)) {
+        if (hasOwn.call(types, pattern.name)) {
+            types[pattern.name].push(patternPath);
+        } else {
+            types[pattern.name] = [patternPath];
+        }
+
+    }
+}
+
 Sp.lookup = function(name) {
     for (var scope = this; scope; scope = scope.parent)
         if (scope.declares(name))
@@ -285,6 +313,13 @@ Sp.lookup = function(name) {
     return scope;
 };
 
+Sp.lookupType = function(name) {
+    for (var scope = this; scope; scope = scope.parent)
+        if (scope.declaresType(name))
+            break;
+    return scope;
+};
+
 Sp.getGlobalScope = function() {
     var scope = this;
     while (!scope.isGlobal)
diff --git a/main.js b/main.js
index 374ae4f..69f2493 100644
--- a/main.js
+++ b/main.js
@@ -10,7 +10,8 @@ require("./def/es6");
 require("./def/es7");
 require("./def/mozilla");
 require("./def/e4x");
-require("./def/fb-harmony");
+require("./def/jsx");
+require("./def/flow");
 require("./def/esprima");
 require("./def/babel");
 
diff --git a/package.json b/package.json
index 5f5bf18..e12bfcb 100644
--- a/package.json
+++ b/package.json
@@ -18,7 +18,7 @@
     "transformation",
     "syntax"
   ],
-  "version": "0.8.14",
+  "version": "0.8.16",
   "homepage": "http://github.com/benjamn/ast-types",
   "repository": {
     "type": "git",
diff --git a/test/run.js b/test/run.js
index 59b2177..a2702ed 100644
--- a/test/run.js
+++ b/test/run.js
@@ -2133,3 +2133,61 @@ describe("RegExpLiteral nodes", function() {
         assert.strictEqual(n.Literal.check(regExpLiteral, true), false);
     });
 });
+
+
+describe("MemberExpression", function() {
+    it("should set computed flag to false by default", function(){
+        var memberExpression = b.memberExpression(
+            b.identifier('foo'),
+            b.identifier('bar')
+        )
+
+        assert.strictEqual(memberExpression.computed, false)
+    });
+
+    it("should not set computed to true if property is a callExpression", function(){
+        var memberExpression = b.memberExpression(
+            b.identifier('foo'),
+            b.callExpression(b.identifier('bar'), [])
+        )
+
+        assert.strictEqual(memberExpression.computed, false)
+    });
+
+    it("should set computed flag to true if property is a literal", function(){
+        var memberExpression = b.memberExpression(
+            b.identifier('foo'),
+            b.literal('bar')
+        )
+
+        assert.strictEqual(memberExpression.computed, true)
+    });
+
+    it("should set computed flag to true if property is a memberExpression", function(){
+        var memberExpression = b.memberExpression(
+            b.identifier('foo'),
+            b.memberExpression(b.identifier('foo'), b.literal('bar'))
+        )
+
+        assert.strictEqual(memberExpression.computed, true)
+    });
+
+    it("should set computed flag to true if property is a binaryExpression", function(){
+        var memberExpression = b.memberExpression(
+            b.identifier('foo'),
+            b.memberExpression(b.identifier('foo'), b.literal('bar'))
+        )
+
+        assert.strictEqual(memberExpression.computed, true)
+    });
+
+    it("should override computed value when passed as a third argument to the builder", function(){
+        var memberExpression = b.memberExpression(
+            b.identifier('foo'),
+            b.callExpression(b.identifier('bar'), []),
+            true
+        )
+
+        assert.strictEqual(memberExpression.computed, true);
+    });
+});

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-ast-types.git



More information about the Pkg-javascript-commits mailing list