[Pkg-javascript-commits] [node-recast] 01/03: New upstream version 0.11.16

Julien Puydt julien.puydt at laposte.net
Mon Nov 7 22:39:59 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-recast.

commit 3a50e9982e7a3d697126976daacad452e4ee479c
Author: Julien Puydt <julien.puydt at laposte.net>
Date:   Mon Nov 7 08:23:57 2016 +0100

    New upstream version 0.11.16
---
 lib/options.js      |  16 ++++-
 lib/printer.js      | 175 +++++++++++++++++++++++++++++++++++++++++++++-------
 lib/util.js         |   8 +++
 package.json        |  10 +--
 test/babylon.js     | 120 ++++++++++++++++++++++++++++++++++-
 test/printer.js     |  43 ++++++++++++-
 test/type-syntax.js |  12 ++--
 7 files changed, 346 insertions(+), 38 deletions(-)

diff --git a/lib/options.js b/lib/options.js
index 2e71d08..afbb527 100644
--- a/lib/options.js
+++ b/lib/options.js
@@ -60,9 +60,19 @@ var defaults = {
     // Otherwise, double quotes are used.
     quote: null,
 
-    // If you want to print trailing commas in object literals,
-    // array expressions, functions calls and function definitions pass true
-    // for this option.
+    // Controls the printing of trailing commas in object literals,
+    // array expressions and function parameters.
+    //
+    // This option could either be:
+    // * Boolean - enable/disable in all contexts (objects, arrays and function params).
+    // * Object - enable/disable per context.
+    //
+    // Example:
+    // trailingComma: {
+    //   objects: true,
+    //   arrays: true,
+    //   parameters: false,
+    // }
     trailingComma: false,
 
     // If you want parenthesis to wrap single-argument arrow function parameter
diff --git a/lib/printer.js b/lib/printer.js
index 161ff96..88abaf2 100644
--- a/lib/printer.js
+++ b/lib/printer.js
@@ -225,9 +225,18 @@ function genericPrintNoParens(path, options, print) {
         return path.call(print, "program");
 
     case "Program":
-        return path.call(function(bodyPath) {
+        // Babel 6
+        if (n.directives) {
+            path.each(function(childPath) {
+                parts.push(print(childPath), ";\n");
+            }, "directives");
+        }
+
+        parts.push(path.call(function(bodyPath) {
             return printStatementSequence(bodyPath, options, print);
-        }, "body");
+        }, "body"));
+
+        return concat(parts);
 
     case "Noop": // Babel extension.
     case "EmptyStatement":
@@ -333,6 +342,10 @@ function genericPrintNoParens(path, options, print) {
         if (n.async)
             parts.push("async ");
 
+        if (n.typeParameters) {
+            parts.push(path.call(print, "typeParameters"));
+        }
+
         if (
             !options.arrowParensAlways &&
             n.params.length === 1 &&
@@ -355,7 +368,6 @@ function genericPrintNoParens(path, options, print) {
 
         return concat(parts);
 
-    case "ClassMethod": // Babel 6
     case "MethodDefinition":
         if (n.static) {
             parts.push("static ");
@@ -524,15 +536,28 @@ function genericPrintNoParens(path, options, print) {
             return printStatementSequence(bodyPath, options, print);
         }, "body");
 
+
         if (naked.isEmpty()) {
-            return fromString("{}");
+            if (!n.directives || n.directives.length === 0) {
+                return fromString("{}");
+            }
         }
 
-        return concat([
-            "{\n",
-            naked.indent(options.tabWidth),
-            "\n}"
-        ]);
+        parts.push("{\n");
+        // Babel 6
+        if (n.directives) {
+            path.each(function(childPath) {
+                parts.push(
+                    print(childPath).indent(options.tabWidth),
+                    ";",
+                    n.directives.length > 1 || !naked.isEmpty() ? "\n" : ""
+                );
+            }, "directives");
+        }
+        parts.push(naked.indent(options.tabWidth));
+        parts.push("\n}");
+
+        return concat(parts);
 
     case "ReturnStatement":
         parts.push("return");
@@ -582,7 +607,10 @@ function genericPrintNoParens(path, options, print) {
         });
 
         var oneLine = (isTypeAnnotation && len === 1) || len === 0;
-        parts.push(oneLine ? "{" : "{\n");
+        var leftBrace = n.exact ? "{|" : "{";
+        var rightBrace = n.exact ? "|}" : "}";
+        parts.push(oneLine ? leftBrace : leftBrace + "\n");
+        var leftBraceIndex = parts.length - 1;
 
         var i = 0;
         fields.forEach(function(field) {
@@ -608,14 +636,19 @@ function genericPrintNoParens(path, options, print) {
                     allowBreak = !multiLine;
                 } else if (len !== 1 && isTypeAnnotation) {
                     parts.push(separator);
-                } else if (!oneLine && options.trailingComma) {
+                } else if (!oneLine && util.isTrailingCommaEnabled(options, "objects")) {
                     parts.push(separator);
                 }
                 i++;
             }, field);
         });
 
-        parts.push(oneLine ? "}" : "\n}");
+        parts.push(oneLine ? rightBrace : "\n" + rightBrace);
+
+        if (i !== 0 && oneLine) {
+            parts[leftBraceIndex] = leftBrace + " ";
+            parts[parts.length - 1] = " " + rightBrace;
+        }
 
         return concat(parts);
 
@@ -627,7 +660,6 @@ function genericPrintNoParens(path, options, print) {
         ]);
 
     case "ObjectProperty": // Babel 6
-    case "ObjectMethod": // Babel 6
     case "Property": // Non-standard AST node type.
         if (n.method || n.kind === "get" || n.kind === "set") {
             return printMethod(path, options, print);
@@ -646,6 +678,16 @@ function genericPrintNoParens(path, options, print) {
 
         return concat(parts);
 
+    case "ClassMethod": // Babel 6
+        if (n.static) {
+            parts.push("static ");
+        }
+
+        return concat([parts, printObjectMethod(path, options, print)]);
+
+    case "ObjectMethod": // Babel 6
+        return printObjectMethod(path, options, print);
+
     case "Decorator":
         return concat(["@", path.call(print, "expression")]);
 
@@ -678,7 +720,7 @@ function genericPrintNoParens(path, options, print) {
                     lines = lines.indent(options.tabWidth);
                 }
                 parts.push(lines);
-                if (i < len - 1 || (!oneLine && options.trailingComma))
+                if (i < len - 1 || (!oneLine && util.isTrailingCommaEnabled(options, "arrays")))
                     parts.push(",");
                 if (!oneLine)
                     parts.push("\n");
@@ -698,17 +740,27 @@ function genericPrintNoParens(path, options, print) {
     case "Super":
         return fromString("super");
 
-    case "BooleanLiteral": // Babel 6 Literal split
+    case "NullLiteral": // Babel 6 Literal split
+        return fromString("null");
+
     case "RegExpLiteral": // Babel 6 Literal split
+        return fromString(n.extra.raw);
+
+    case "BooleanLiteral": // Babel 6 Literal split
     case "NumericLiteral": // Babel 6 Literal split
     case "StringLiteral": // Babel 6 Literal split
-    case "NullLiteral": // Babel 6 Literal split
     case "Literal":
         if (typeof n.value !== "string")
             return fromString(n.value, options);
 
         return fromString(nodeStr(n.value, options), options);
 
+    case "Directive": // Babel 6
+        return path.call(print, "value");
+
+    case "DirectiveLiteral": // Babel 6
+        return fromString(nodeStr(n.value, options));
+
     case "ModuleSpecifier":
         if (n.local) {
             throw new Error(
@@ -780,7 +832,9 @@ function genericPrintNoParens(path, options, print) {
         if (!namedTypes.ForStatement.check(parentNode) &&
             !namedTypes.ForInStatement.check(parentNode) &&
             !(namedTypes.ForOfStatement &&
-              namedTypes.ForOfStatement.check(parentNode))) {
+              namedTypes.ForOfStatement.check(parentNode)) &&
+            !(namedTypes.ForAwaitStatement &&
+              namedTypes.ForAwaitStatement.check(parentNode))) {
             parts.push(";");
         }
 
@@ -863,6 +917,16 @@ function genericPrintNoParens(path, options, print) {
             adjustClause(path.call(print, "body"), options)
         ]);
 
+    case "ForAwaitStatement":
+        return concat([
+            "for await (",
+            path.call(print, "left"),
+            " of ",
+            path.call(print, "right"),
+            ")",
+            adjustClause(path.call(print, "body"), options)
+        ]);
+
     case "DoWhileStatement":
         var doBody = concat([
             "do",
@@ -1103,7 +1167,16 @@ function genericPrintNoParens(path, options, print) {
         if (n.static)
             parts.push("static ");
 
-        parts.push(path.call(print, "key"));
+        var key = path.call(print, "key");
+        if (n.computed) {
+            key = concat(["[", key, "]"]);
+        } else if (n.variance === "plus") {
+            key = concat(["+", key]);
+        } else if (n.variance === "minus") {
+            key = concat(["-", key]);
+        }
+        parts.push(key);
+
         if (n.typeAnnotation)
             parts.push(path.call(print, "typeAnnotation"));
 
@@ -1209,9 +1282,13 @@ function genericPrintNoParens(path, options, print) {
 
         return fromString("");
 
+    case "ExistentialTypeParam":
     case "ExistsTypeAnnotation":
         return fromString("*", options);
 
+    case "EmptyTypeAnnotation":
+        return fromString("empty", options);
+
     case "AnyTypeAnnotation":
         return fromString("any", options);
 
@@ -1254,6 +1331,12 @@ function genericPrintNoParens(path, options, print) {
             path.call(print, "body"),
         ]);
 
+    case "DeclareModuleExports":
+        return printFlowDeclaration(path, [
+            "module.exports",
+            path.call(print, "typeAnnotation"),
+        ]);
+
     case "DeclareVariable":
         return printFlowDeclaration(path, [
             "var ",
@@ -1367,7 +1450,12 @@ function genericPrintNoParens(path, options, print) {
         return path.call(print, "value");
 
     case "ObjectTypeIndexer":
+        var variance =
+            n.variance === "plus" ? "+" :
+            n.variance === "minus" ? "-" : "";
+
         return concat([
+            variance,
             "[",
             path.call(print, "id"),
             ": ",
@@ -1377,7 +1465,12 @@ function genericPrintNoParens(path, options, print) {
         ]);
 
     case "ObjectTypeProperty":
+        var variance =
+            n.variance === "plus" ? "+" :
+            n.variance === "minus" ? "-" : "";
+
         return concat([
+            variance,
             path.call(print, "key"),
             n.optional ? "?" : "",
             ": ",
@@ -1408,6 +1501,7 @@ function genericPrintNoParens(path, options, print) {
         return concat([
             "type ",
             path.call(print, "id"),
+            path.call(print, "typeParameters"),
             " = ",
             path.call(print, "right"),
             ";"
@@ -1649,7 +1743,11 @@ function printMethod(path, options, print) {
     var kind = node.kind;
     var parts = [];
 
-    namedTypes.FunctionExpression.assert(node.value);
+    if (node.type === "ObjectMethod" || node.type === "ClassMethod") {
+        node.value = node;
+    } else {
+        namedTypes.FunctionExpression.assert(node.value);
+    }
 
     if (node.value.async) {
         parts.push("async ");
@@ -1687,6 +1785,7 @@ function printMethod(path, options, print) {
 
 function printArgumentsList(path, options, print) {
     var printed = path.map(print, "arguments");
+    var trailingComma = util.isTrailingCommaEnabled(options, "parameters");
 
     var joined = fromString(", ").join(printed);
     if (joined.getLineLength(1) > options.wrapColumn) {
@@ -1694,7 +1793,7 @@ function printArgumentsList(path, options, print) {
         return concat([
             "(\n",
             joined.indent(options.tabWidth),
-            options.trailingComma ? ",\n)" : "\n)"
+            trailingComma ? ",\n)" : "\n)"
         ]);
     }
 
@@ -1703,6 +1802,7 @@ function printArgumentsList(path, options, print) {
 
 function printFunctionParams(path, options, print) {
     var fun = path.getValue();
+
     namedTypes.Function.assert(fun);
 
     var printed = path.map(print, "params");
@@ -1725,7 +1825,7 @@ function printFunctionParams(path, options, print) {
     if (joined.length > 1 ||
         joined.getLineLength(1) > options.wrapColumn) {
         joined = fromString(",\n").join(printed);
-        if (options.trailingComma && !fun.rest) {
+        if (util.isTrailingCommaEnabled(options, "parameters") && !fun.rest) {
             joined = concat([joined, ",\n"]);
         } else {
             joined = concat([joined, "\n"]);
@@ -1736,6 +1836,39 @@ function printFunctionParams(path, options, print) {
     return joined;
 }
 
+function printObjectMethod(path, options, print) {
+    var objMethod = path.getValue();
+    var parts = [];
+
+    if (objMethod.async)
+        parts.push("async ");
+
+    if (objMethod.generator)
+        parts.push("*");
+
+    if (objMethod.method || objMethod.kind === "get" || objMethod.kind === "set") {
+        return printMethod(path, options, print);
+    }
+
+    var key = path.call(print, "key");
+    if (objMethod.computed) {
+        parts.push("[", key, "]");
+    } else {
+        parts.push(key);
+    }
+
+    parts.push(
+        "(",
+        printFunctionParams(path, options, print),
+        ")",
+        path.call(print, "returnType"),
+        " ",
+        path.call(print, "body")
+    );
+
+    return concat(parts);
+}
+
 function printExportDeclaration(path, options, print) {
     var decl = path.getValue();
     var parts = ["export "];
diff --git a/lib/util.js b/lib/util.js
index ae6e69d..9b22280 100644
--- a/lib/util.js
+++ b/lib/util.js
@@ -296,3 +296,11 @@ util.getParentExportDeclaration = function (path) {
 
   return null;
 };
+
+util.isTrailingCommaEnabled = function(options, context) {
+  var trailingComma = options.trailingComma;
+  if (typeof trailingComma === "object") {
+    return !!trailingComma[context];
+  }
+  return !!trailingComma;
+};
diff --git a/package.json b/package.json
index 8cc40cc..da8d079 100644
--- a/package.json
+++ b/package.json
@@ -12,7 +12,7 @@
     "parsing",
     "pretty-printing"
   ],
-  "version": "0.11.14",
+  "version": "0.11.16",
   "homepage": "http://github.com/benjamn/recast",
   "repository": {
     "type": "git",
@@ -28,15 +28,15 @@
     "fs": false
   },
   "dependencies": {
-    "ast-types": "0.9.0",
-    "esprima": "~3.0.0",
+    "ast-types": "0.9.1",
+    "esprima": "~3.1.0",
     "private": "~0.1.5",
     "source-map": "~0.5.0"
   },
   "devDependencies": {
-    "babylon": "~6.9.1",
+    "babylon": "~6.13.1",
     "esprima-fb": "^15001.1001.0-dev-harmony-fb",
-    "mocha": "~3.0.1"
+    "mocha": "~3.1.2"
   },
   "engines": {
     "node": ">= 0.8"
diff --git a/test/babylon.js b/test/babylon.js
index 2fa96db..8a03142 100644
--- a/test/babylon.js
+++ b/test/babylon.js
@@ -36,6 +36,124 @@ describe("decorators", function () {
     }
   };
 
+  it("babel 6 printing", function () {
+    var code = [
+      '"use strict";', // Directive, DirectiveLiteral in Program
+      '"use strict";', // Directive, DirectiveLiteral in BlockStatement
+      'function a() {',
+      '  "use strict";',
+      '}',
+      '',
+      'function a() {',
+      '  "use strict";',
+      '  b;',
+      '}',
+      '',
+      '() => {',
+      '  "use strict";',
+      '};',
+      '',
+      '() => {',
+      '  "use strict";',
+      '  b;',
+      '};',
+      '',
+      'var a = function a() {',
+      '  "use strict";',
+      '};',
+      '',
+      'var a = function a() {',
+      '  "use strict";',
+      '  b;',
+      '};',
+      '',
+      'null;', // NullLiteral
+      '"asdf";', // StringLiteral
+      '/a/;', // RegExpLiteral
+      'false;', // BooleanLiteral
+      '1;', // NumericLiteral
+      'const find2 = <X>() => {};', // typeParameters
+      '',
+      'class A<T> {',
+      '  a;',
+      '  a = 1;',
+      '  [a] = 1;', // computed in ClassProperty
+      '}',
+      '',
+      'function f<T>(x: empty): T {', // EmptyTypeAnnotation
+      '  return x;',
+      '}',
+      '',
+      'var a: {| numVal: number |};', // exact
+      'const bar1 = (x: number): string => {};',
+      'declare module.exports: { foo: string }', // DeclareModuleExports
+      'type Maybe<T> = _Maybe<T, *>;', // ExistentialTypeParam
+      // 'declare class B { foo: () => number }', // interesting failure ref https://github.com/babel/babel/pull/3663
+      'declare function foo(): number;',
+      'var A: (a: B) => void;',
+      '',
+      'async function* a() {', // async in Function
+      '  for await (let x of y) {', // ForAwaitStatement
+      '    x;',
+      '  }',
+      '}',
+      '',
+      'class C2<+T, -U> {', // variance
+      '  +p: T = e;',
+      '}',
+      '',
+      'type T = { -p: T };',
+      'type T = { +[k: K]: V };',
+      '',
+      'class A {',
+      '  static async *z(a, b): number {', // ClassMethod
+      '    b;',
+      '  }',
+      '',
+      '  static get y(): number {',
+      '    return 1;',
+      '  }',
+      '',
+      '  static set x(a): void {',
+      '    return 1;',
+      '  }',
+      '',
+      '  static async *[d](a, b): number {',
+      '    return 1;',
+      '  }',
+      '}',
+      '',
+      '({',
+      '  async *a() {', // ObjectMethod
+      '    b;',
+      '  },',
+      '',
+      '  get a() {',
+      '    return 1;',
+      '  },',
+      '',
+      '  set a(b) {',
+      '    return 1;',
+      '  },',
+      '',
+      '  async *[d](c) {',
+      '    return 1;',
+      '  },',
+      '',
+      '  a: 3,',
+      '  [a]: 3,',
+      '  1: 3,',
+      '  "1": 3,',
+      '  1() {},',
+      '  "1"() {}',
+      '});',
+    ].join(eol);
+
+    var ast = recast.parse(code, parseOptions);
+    var output = recast.prettyPrint(ast, { tabWidth: 2 }).code;
+    assert.strictEqual(output, code);
+  });
+
   it("should not disappear when surrounding code changes", function () {
     var code = [
       'import foo from "foo";',
@@ -185,7 +303,7 @@ describe("decorators", function () {
 
     assert.strictEqual(
       recast.print(ast).code,
-      "type X = {b: number};"
+      "type X = { b: number };"
     );
   });
 
diff --git a/test/printer.js b/test/printer.js
index ea7f95b..dc2f8f2 100644
--- a/test/printer.js
+++ b/test/printer.js
@@ -815,6 +815,15 @@ describe("printer", function() {
 
         var pretty = printer.printGenerically(ast).code;
         assert.strictEqual(pretty, code);
+
+        // It should also work when using the `trailingComma` option as an object.
+        printer = new Printer({
+          tabWidth: 2,
+          trailingComma: { objects: true },
+        });
+
+        pretty = printer.printGenerically(ast).code;
+        assert.strictEqual(pretty, code);
     });
 
     it("prints trailing commas in function calls", function() {
@@ -838,6 +847,16 @@ describe("printer", function() {
 
         var pretty = printer.printGenerically(ast).code;
         assert.strictEqual(pretty, code);
+
+        // It should also work when using the `trailingComma` option as an object.
+        printer = new Printer({
+          tabWidth: 2,
+          wrapColumn: 1,
+          trailingComma: { parameters: true },
+        });
+
+        pretty = printer.printGenerically(ast).code;
+        assert.strictEqual(pretty, code);
     });
 
     it("prints trailing commas in array expressions", function() {
@@ -861,6 +880,16 @@ describe("printer", function() {
 
         var pretty = printer.printGenerically(ast).code;
         assert.strictEqual(pretty, code);
+
+        // It should also work when using the `trailingComma` option as an object.
+        printer = new Printer({
+          tabWidth: 2,
+          wrapColumn: 1,
+          trailingComma: { arrays: true },
+        });
+
+        pretty = printer.printGenerically(ast).code;
+        assert.strictEqual(pretty, code);
     });
 
     it("prints trailing commas in function definitions", function() {
@@ -884,6 +913,16 @@ describe("printer", function() {
 
         var pretty = printer.printGenerically(ast).code;
         assert.strictEqual(pretty, code);
+
+        // It should also work when using the `trailingComma` option as an object.
+        printer = new Printer({
+          tabWidth: 2,
+          wrapColumn: 1,
+          trailingComma: { parameters: true },
+        });
+
+        pretty = printer.printGenerically(ast).code;
+        assert.strictEqual(pretty, code);
     });
 
     it("should support AssignmentPattern and RestElement", function() {
@@ -1386,8 +1425,8 @@ describe("printer", function() {
     });
 
     it("shouldn't print a trailing comma for single-line flow object types", function() {
-        var code1 = "type MyType = {message: string};";
-        var code2 = "type MyType = {[key: string]: string};";
+        var code1 = "type MyType = { message: string };";
+        var code2 = "type MyType = { [key: string]: string };";
 
         var ast1 = b.typeAlias(
             b.identifier("MyType"),
diff --git a/test/type-syntax.js b/test/type-syntax.js
index cf2fc2f..723b215 100644
--- a/test/type-syntax.js
+++ b/test/type-syntax.js
@@ -50,7 +50,7 @@ describe("type syntax", function() {
     // Type aliases
     check("type A = B;");
     check("type A = B.C;");
-    check("type A = {optionalNumber?: number};")
+    check("type A = { optionalNumber?: number };")
 
     // Generic
     check("var a: Array<Foo>;");
@@ -61,9 +61,9 @@ describe("type syntax", function() {
     check("var a: () => X = fn;");
 
     // Object
-    check("var a: {" + eol + "  b: number;" + eol + "  x: {y: A};" + eol + "};");
-    check("var b: {[key: string]: number};")
-    check("var c: {(): number};")
+    check("var a: {" + eol + "  b: number;" + eol + "  x: { y: A };" + eol + "};");
+    check("var b: { [key: string]: number };")
+    check("var c: { (): number };")
     check("var d: {" + eol + "  [key: string]: A;" + eol + "  [key: number]: B;" + eol + "  (): C;" + eol + "  a: D;" + eol + "};")
 
     // Casts
@@ -76,7 +76,7 @@ describe("type syntax", function() {
     check("declare function foo(c: C, b: B): void;");
     check("declare function foo(c: (e: Event) => void, b: B): void;");
     check("declare function foo(c: C, d?: Array<D>): void;");
-    check("declare class C {x: string}");
+    check("declare class C { x: string }");
     check("declare module M {" + eol + "  declare function foo(c: C): void;" + eol + "}");
 
     // Classes
@@ -92,7 +92,7 @@ describe("type syntax", function() {
     check("class A {" + eol + "  foo<T>(a: number): string {}" + eol + "}");
 
     // Interfaces
-    check("interface A<X> extends B<A>, C {a: number}");
+    check("interface A<X> extends B<A>, C { a: number }");
     check("class A extends B implements C<T>, Y {}");
 
     // Bounded polymorphism

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



More information about the Pkg-javascript-commits mailing list