[Pkg-javascript-commits] [node-ast-types] 01/03: New upstream version 0.11.3

Julien Puydt julien.puydt at laposte.net
Tue Mar 13 21:45:45 UTC 2018


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 8916cefdc28973723fbb150eadfd16e9f3f291b2
Author: Julien Puydt <julien.puydt at laposte.net>
Date:   Tue Mar 13 22:41:04 2018 +0100

    New upstream version 0.11.3
---
 lib/types.js       | 160 +++++++++++++++++++++++++++++++++--------------------
 package-lock.json  |  46 +++++++--------
 package.json       |   2 +-
 test/ecmascript.js |  50 +++++++++++++++++
 test/run.sh        |  11 ++--
 test/typescript.js |   8 ++-
 6 files changed, 186 insertions(+), 91 deletions(-)

diff --git a/lib/types.js b/lib/types.js
index 8d5ff3b..26f9753 100644
--- a/lib/types.js
+++ b/lib/types.js
@@ -533,77 +533,115 @@ module.exports = function () {
         // Override Dp.buildable for this Def instance.
         Object.defineProperty(self, "buildable", {value: true});
 
-        Object.defineProperty(builders, getBuilderName(self.typeName), {
-            enumerable: true,
+        function addParam(built, param, arg, isArgAvailable) {
+            if (hasOwn.call(built, param))
+                return;
 
-            value: function () {
-                var args = arguments;
-                var argc = args.length;
-                var built = Object.create(nodePrototype);
+            var all = self.allFields;
+            if (!hasOwn.call(all, param)) {
+                throw new Error("" + param);
+            }
 
-                if (!self.finalized) {
-                    throw new Error(
-                      "attempting to instantiate unfinalized type " +
-                      self.typeName
-                    );
-                }
+            var field = all[param];
+            var type = field.type;
+            var value;
+
+            if (isArgAvailable) {
+                value = arg;
+            } else if (field.defaultFn) {
+                // Expose the partially-built object to the default
+                // function as its `this` object.
+                value = field.defaultFn.call(built);
+            } else {
+                var message = "no value or default function given for field " +
+                  JSON.stringify(param) + " of " + self.typeName + "(" +
+                  self.buildParams.map(function (name) {
+                      return all[name];
+                  }).join(", ") + ")";
+                throw new Error(message);
+            }
+
+            if (!type.check(value)) {
+                throw new Error(
+                  shallowStringify(value) +
+                  " does not match field " + field +
+                  " of type " + self.typeName
+                );
+            }
 
-                function add(param, i) {
-                    if (hasOwn.call(built, param))
-                        return;
-
-                    var all = self.allFields;
-                    if (!hasOwn.call(all, param)) {
-                        throw new Error("" + param);
-                    }
-
-                    var field = all[param];
-                    var type = field.type;
-                    var value;
-
-                    if (isNumber.check(i) && i < argc) {
-                        value = args[i];
-                    } else if (field.defaultFn) {
-                        // Expose the partially-built object to the default
-                        // function as its `this` object.
-                        value = field.defaultFn.call(built);
-                    } else {
-                        var message = "no value or default function given for field " +
-                          JSON.stringify(param) + " of " + self.typeName + "(" +
-                          self.buildParams.map(function (name) {
-                              return all[name];
-                          }).join(", ") + ")";
-                        throw new Error(message);
-                    }
-
-                    if (!type.check(value)) {
-                        throw new Error(
-                          shallowStringify(value) +
-                          " does not match field " + field +
-                          " of type " + self.typeName
-                        );
-                    }
-
-                    // TODO Could attach getters and setters here to enforce
-                    // dynamic type safety.
-                    built[param] = value;
+            built[param] = value;
+        }
+
+        // Calling the builder function will construct an instance of the Def,
+        // with positional arguments mapped to the fields original passed to .build.
+        // If not enough arguments are provided, the default value for the remaining fields
+        // will be used.
+        function builder() {
+            var args = arguments;
+            var argc = args.length;
+            
+            if (!self.finalized) {
+                throw new Error(
+                    "attempting to instantiate unfinalized type " +
+                    self.typeName
+                );
+            }
+
+            var built = Object.create(nodePrototype);
+
+            self.buildParams.forEach(function (param, i) {
+                if (i < argc) {
+                    addParam(built, param, args[i], true)
+                } else {
+                    addParam(built, param, null, false);
                 }
+            });
 
-                self.buildParams.forEach(function (param, i) {
-                    add(param, i);
-                });
+            Object.keys(self.allFields).forEach(function (param) {
+                // Use the default value.
+                addParam(built, param, null, false);
+            });
 
-                Object.keys(self.allFields).forEach(function (param) {
-                    add(param); // Use the default value.
-                });
+            // Make sure that the "type" field was filled automatically.
+            if (built.type !== self.typeName) {
+                throw new Error("");
+            }
 
-                // Make sure that the "type" field was filled automatically.
-                if (built.type !== self.typeName) {
-                    throw new Error("");
+            return built;
+        }
+
+        // Calling .from on the builder function will construct an instance of the Def,
+        // using field values from the passed object. For fields missing from the passed object,
+        // their default value will be used.
+        builder.from = function (obj) {
+            if (!self.finalized) {
+                throw new Error(
+                    "attempting to instantiate unfinalized type " +
+                    self.typeName
+                );
+            }
+            
+            var built = Object.create(nodePrototype);
+
+            Object.keys(self.allFields).forEach(function (param) {
+                if (hasOwn.call(obj, param)) {
+                    addParam(built, param, obj[param], true);
+                } else {
+                    addParam(built, param, null, false);
                 }
+            });
 
-                return built;
+            // Make sure that the "type" field was filled automatically.
+            if (built.type !== self.typeName) {
+                throw new Error("");
             }
+
+            return built;
+        }
+
+        Object.defineProperty(builders, getBuilderName(self.typeName), {
+            enumerable: true,
+            value: builder
         });
 
         return self; // For chaining.
diff --git a/package-lock.json b/package-lock.json
index 15fe836..54d289a 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,13 +1,13 @@
 {
   "name": "ast-types",
-  "version": "0.11.2",
+  "version": "0.11.3",
   "lockfileVersion": 1,
   "requires": true,
   "dependencies": {
     "acorn": {
-      "version": "5.4.1",
-      "resolved": "https://registry.npmjs.org/acorn/-/acorn-5.4.1.tgz",
-      "integrity": "sha512-XLmq3H/BVvW6/GbxKryGxWORz1ebilSsUDlyC27bXhWGWAZWkGwS6FLHjOlwFXNFoWFQEO/Df4u0YYd0K3BQgQ==",
+      "version": "5.5.3",
+      "resolved": "https://registry.npmjs.org/acorn/-/acorn-5.5.3.tgz",
+      "integrity": "sha512-jd5MkIUlbbmb07nXH0DT3y7rDVtkzDi4XZOUVWAer8ajmF/DTSSbl5oNFyDOl/OXA33Bl79+ypHhl2pN20VeOQ==",
       "dev": true
     },
     "acorn-jsx": {
@@ -72,9 +72,9 @@
       }
     },
     "browser-stdout": {
-      "version": "1.3.0",
-      "resolved": "https://registry.npmjs.org/browser-stdout/-/browser-stdout-1.3.0.tgz",
-      "integrity": "sha1-81HTKWnTL6XXpVZxVCY9korjvR8=",
+      "version": "1.3.1",
+      "resolved": "https://registry.npmjs.org/browser-stdout/-/browser-stdout-1.3.1.tgz",
+      "integrity": "sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw==",
       "dev": true
     },
     "commander": {
@@ -105,9 +105,9 @@
       }
     },
     "diff": {
-      "version": "3.3.1",
-      "resolved": "https://registry.npmjs.org/diff/-/diff-3.3.1.tgz",
-      "integrity": "sha512-MKPHZDMB0o6yHyDryUOScqZibp914ksXwAMYMTHj6KO8UeKsRYNJD3oNCKjTqZon+V488P7N/HzXF8t7ZR95ww==",
+      "version": "3.5.0",
+      "resolved": "https://registry.npmjs.org/diff/-/diff-3.5.0.tgz",
+      "integrity": "sha512-A46qtFgd+g7pDZinpnwiRJtxbC1hpgf0uzP3iG89scHk0AUC7A1TGxf5OiiOUv/JMZR8GOt8hL900hV0bOy5xA==",
       "dev": true
     },
     "escape-string-regexp": {
@@ -117,12 +117,12 @@
       "dev": true
     },
     "espree": {
-      "version": "3.5.3",
-      "resolved": "https://registry.npmjs.org/espree/-/espree-3.5.3.tgz",
-      "integrity": "sha512-Zy3tAJDORxQZLl2baguiRU1syPERAIg0L+JB2MWorORgTu/CplzvxS9WWA7Xh4+Q+eOQihNs/1o1Xep8cvCxWQ==",
+      "version": "3.5.4",
+      "resolved": "https://registry.npmjs.org/espree/-/espree-3.5.4.tgz",
+      "integrity": "sha512-yAcIQxtmMiB/jL32dzEp2enBeidsB7xWPLNiw3IIkpVds1P+h7qF9YwJq1yUNzp2OKXgAprs4F61ih66UsoD1A==",
       "dev": true,
       "requires": {
-        "acorn": "5.4.1",
+        "acorn": "5.5.3",
         "acorn-jsx": "3.0.1"
       }
     },
@@ -253,15 +253,15 @@
       }
     },
     "mocha": {
-      "version": "5.0.1",
-      "resolved": "https://registry.npmjs.org/mocha/-/mocha-5.0.1.tgz",
-      "integrity": "sha512-SpwyojlnE/WRBNGtvJSNfllfm5PqEDFxcWluSIgLeSBJtXG4DmoX2NNAeEA7rP5kK+79VgtVq8nG6HskaL1ykg==",
+      "version": "5.0.4",
+      "resolved": "https://registry.npmjs.org/mocha/-/mocha-5.0.4.tgz",
+      "integrity": "sha512-nMOpAPFosU1B4Ix1jdhx5e3q7XO55ic5a8cgYvW27CequcEY+BabS0kUVL1Cw1V5PuVHZWeNRWFLmEPexo79VA==",
       "dev": true,
       "requires": {
-        "browser-stdout": "1.3.0",
+        "browser-stdout": "1.3.1",
         "commander": "2.11.0",
         "debug": "3.1.0",
-        "diff": "3.3.1",
+        "diff": "3.5.0",
         "escape-string-regexp": "1.0.5",
         "glob": "7.1.2",
         "growl": "1.10.3",
@@ -298,12 +298,12 @@
       "dev": true
     },
     "reify": {
-      "version": "0.14.1",
-      "resolved": "https://registry.npmjs.org/reify/-/reify-0.14.1.tgz",
-      "integrity": "sha512-HAKYzqlCCWsfAcOTm8gsJZ6FE5I06kvjzHRQTCDD6xqUJaY8Mh4ve9oiCDe8yiYXOqZqpt03gn8yS4+bokRmhQ==",
+      "version": "0.14.2",
+      "resolved": "https://registry.npmjs.org/reify/-/reify-0.14.2.tgz",
+      "integrity": "sha512-gJA67pCidAROSmr4/8SQuMQXtZwtSvoVIDR6KjoWxnO4LSD8iYNjMcRrHuQQ/c8cXYaoCpEQh0rO9CNNu1z5lA==",
       "dev": true,
       "requires": {
-        "acorn": "5.4.1",
+        "acorn": "5.5.3",
         "minizlib": "1.1.0",
         "semver": "5.5.0"
       }
diff --git a/package.json b/package.json
index 9b7d6ba..4feecb4 100644
--- a/package.json
+++ b/package.json
@@ -18,7 +18,7 @@
     "transformation",
     "syntax"
   ],
-  "version": "0.11.2",
+  "version": "0.11.3",
   "homepage": "http://github.com/benjamn/ast-types",
   "repository": {
     "type": "git",
diff --git a/test/ecmascript.js b/test/ecmascript.js
index 83692a7..2dc63f5 100644
--- a/test/ecmascript.js
+++ b/test/ecmascript.js
@@ -61,6 +61,56 @@ describe("basic type checking", function() {
   });
 });
 
+describe("builders", function() {
+  it("should build types using positional arguments", function() {
+    var fooId = b.identifier("foo");
+    var consequent = b.blockStatement([
+      b.expressionStatement(b.callExpression(fooId, []))
+    ]);
+    var ifFoo = b.ifStatement(fooId, consequent);
+
+    assert.ok(n.Identifier.check(fooId));
+    assert.ok(n.IfStatement.check(ifFoo));
+    assert.ok(n.Statement.check(ifFoo));
+
+    assert.strictEqual(fooId.name, "foo");
+    assert.strictEqual(fooId.optional, false);
+
+    assert.strictEqual(ifFoo.test, fooId);
+    assert.strictEqual(ifFoo.consequent, consequent);
+    assert.strictEqual(ifFoo.alternate, null);
+  });
+
+  it("should build types using `.from`", function() {
+    var fooId = b.identifier.from({
+      name: "foo",
+      optional: true
+    });
+    var consequent = b.blockStatement.from({
+      body: [
+        b.expressionStatement.from({
+          expression: b.callExpression.from({ callee: fooId, arguments: [] })
+        })
+      ]
+    });
+    var ifFoo = b.ifStatement.from({
+      test: fooId,
+      consequent: consequent
+    });
+
+    assert.ok(n.Identifier.check(fooId));
+    assert.ok(n.IfStatement.check(ifFoo));
+    assert.ok(n.Statement.check(ifFoo));
+    
+    assert.strictEqual(fooId.name, "foo");
+    assert.strictEqual(fooId.optional, true);
+
+    assert.strictEqual(ifFoo.test, fooId);
+    assert.strictEqual(ifFoo.consequent, consequent);
+    assert.strictEqual(ifFoo.alternate, null);
+  });
+});
+
 describe("isSupertypeOf", function() {
   it("should report correct supertype relationships", function() {
     var def = types.Type.def;
diff --git a/test/run.sh b/test/run.sh
index 83e7c4c..70968e0 100755
--- a/test/run.sh
+++ b/test/run.sh
@@ -6,18 +6,21 @@ cd $(dirname $0)/data
 
 BAB_TAG=v$(node -p 'require("babylon/package.json").version')
 
-if [ ! -d babylon-typescript-fixtures ]
+if [ ! -d babylon ]
 then
     git clone --branch "$BAB_TAG" --depth 1 \
         https://github.com/babel/babel.git
-    mv babel/packages/babylon/test/fixtures/typescript \
-       babylon-typescript-fixtures
+    mv babel/packages/babylon .
     rm -rf babel
 fi
 
+# Hard-code this for now.
+TS_TAG=v2.7.2
+
 if [ ! -d typescript-compiler ]
 then
-    git clone --depth 1 https://github.com/Microsoft/TypeScript.git
+    git clone --branch "$TS_TAG" --depth 1 \
+        https://github.com/Microsoft/TypeScript.git
     mv TypeScript/src/compiler typescript-compiler
     rm -rf TypeScript
 fi
diff --git a/test/typescript.js b/test/typescript.js
index 0029c30..fbb9d0d 100644
--- a/test/typescript.js
+++ b/test/typescript.js
@@ -1,3 +1,5 @@
+"use strict";
+
 var assert = require("assert");
 var fs = require("fs");
 var path = require("path");
@@ -8,8 +10,10 @@ var tsTypes = require("../fork.js")([
   require("../def/jsx"),
 ]);
 
-var babylonTSFixturesDir =
-  path.resolve(__dirname, "data", "babylon-typescript-fixtures");
+const babylonDir = path.resolve(__dirname, "data", "babylon");
+
+const babylonTSFixturesDir =
+  path.join(babylonDir, "test", "fixtures", "typescript");
 
 require("glob")("**/input.js", {
   cwd: babylonTSFixturesDir,

-- 
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