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

Julien Puydt julien.puydt at laposte.net
Tue Nov 29 07:25:56 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 adec0dedc0797598959cee305d2a3ff64a9bf7d1
Author: Julien Puydt <julien.puydt at laposte.net>
Date:   Tue Nov 29 07:42:36 2016 +0100

    New upstream version 0.11.18
---
 lib/options.js  | 11 +++++++
 lib/printer.js  | 35 ++++++++++++++++-----
 package.json    |  4 +--
 test/printer.js | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 138 insertions(+), 10 deletions(-)

diff --git a/lib/options.js b/lib/options.js
index afbb527..82e46b2 100644
--- a/lib/options.js
+++ b/lib/options.js
@@ -75,6 +75,15 @@ var defaults = {
     // }
     trailingComma: false,
 
+    // Controls the printing of spaces inside array brackets.
+    // See: http://eslint.org/docs/rules/array-bracket-spacing
+    arrayBracketSpacing: false,
+
+    // Controls the printing of spaces inside object literals,
+    // destructuring assignments, and import/export specifiers.
+    // See: http://eslint.org/docs/rules/object-curly-spacing
+    objectCurlySpacing: true,
+
     // If you want parenthesis to wrap single-argument arrow function parameter
     // lists, pass true for this option.
     arrowParensAlways: false,
@@ -110,6 +119,8 @@ exports.normalize = function(options) {
         tolerant: get("tolerant"),
         quote: get("quote"),
         trailingComma: get("trailingComma"),
+        arrayBracketSpacing: get("arrayBracketSpacing"),
+        objectCurlySpacing: get("objectCurlySpacing"),
         arrowParensAlways: get("arrowParensAlways"),
         flowObjectCommas: get("flowObjectCommas"),
     };
diff --git a/lib/printer.js b/lib/printer.js
index 88abaf2..6a23cb2 100644
--- a/lib/printer.js
+++ b/lib/printer.js
@@ -513,7 +513,9 @@ function genericPrintNoParens(path, options, print) {
                     namedTypes.ImportSpecifier.assert(value);
                     if (!foundImportSpecifier) {
                         foundImportSpecifier = true;
-                        parts.push("{");
+                        parts.push(
+                          options.objectCurlySpacing ? "{ " : "{"
+                        );
                     }
                 }
 
@@ -521,7 +523,9 @@ function genericPrintNoParens(path, options, print) {
             }, "specifiers");
 
             if (foundImportSpecifier) {
-                parts.push("}");
+                parts.push(
+                  options.objectCurlySpacing ? " }" : "}"
+                );
             }
 
             parts.push(" from ");
@@ -645,7 +649,7 @@ function genericPrintNoParens(path, options, print) {
 
         parts.push(oneLine ? rightBrace : "\n" + rightBrace);
 
-        if (i !== 0 && oneLine) {
+        if (i !== 0 && oneLine && options.objectCurlySpacing) {
             parts[leftBraceIndex] = leftBrace + " ";
             parts[parts.length - 1] = " " + rightBrace;
         }
@@ -699,7 +703,15 @@ function genericPrintNoParens(path, options, print) {
         var printed = path.map(print, "elements");
         var joined = fromString(", ").join(printed);
         var oneLine = joined.getLineLength(1) <= options.wrapColumn;
-        parts.push(oneLine ? "[" : "[\n");
+        if (oneLine) {
+          if (options.arrayBracketSpacing) {
+            parts.push("[ ");
+          } else {
+            parts.push("[");
+          }
+        } else {
+          parts.push("[\n");
+        }
 
         path.each(function(elemPath) {
             var i = elemPath.getName();
@@ -727,7 +739,11 @@ function genericPrintNoParens(path, options, print) {
             }
         }, "elements");
 
-        parts.push("]");
+        if (oneLine && options.arrayBracketSpacing) {
+          parts.push(" ]");
+        } else {
+          parts.push("]");
+        }
 
         return concat(parts);
 
@@ -1825,7 +1841,9 @@ function printFunctionParams(path, options, print) {
     if (joined.length > 1 ||
         joined.getLineLength(1) > options.wrapColumn) {
         joined = fromString(",\n").join(printed);
-        if (util.isTrailingCommaEnabled(options, "parameters") && !fun.rest) {
+        if (util.isTrailingCommaEnabled(options, "parameters") &&
+            !fun.rest &&
+            fun.params[fun.params.length - 1].type !== 'RestElement') {
             joined = concat([joined, ",\n"]);
         } else {
             joined = concat([joined, "\n"]);
@@ -1872,6 +1890,7 @@ function printObjectMethod(path, options, print) {
 function printExportDeclaration(path, options, print) {
     var decl = path.getValue();
     var parts = ["export "];
+    var shouldPrintSpaces = options.objectCurlySpacing;
 
     namedTypes.Declaration.assert(decl);
 
@@ -1891,9 +1910,9 @@ function printExportDeclaration(path, options, print) {
             parts.push("*");
         } else {
             parts.push(
-                "{",
+                shouldPrintSpaces ? "{ " : "{",
                 fromString(", ").join(path.map(print, "specifiers")),
-                "}"
+                shouldPrintSpaces ? " }" : "}"
             );
         }
 
diff --git a/package.json b/package.json
index 94eba88..e055939 100644
--- a/package.json
+++ b/package.json
@@ -12,7 +12,7 @@
     "parsing",
     "pretty-printing"
   ],
-  "version": "0.11.17",
+  "version": "0.11.18",
   "homepage": "http://github.com/benjamn/recast",
   "repository": {
     "type": "git",
@@ -34,7 +34,7 @@
     "source-map": "~0.5.0"
   },
   "devDependencies": {
-    "babylon": "~6.13.1",
+    "babylon": "~6.14.1",
     "esprima-fb": "^15001.1001.0-dev-harmony-fb",
     "mocha": "~3.1.2"
   },
diff --git a/test/printer.js b/test/printer.js
index dc2f8f2..6418c09 100644
--- a/test/printer.js
+++ b/test/printer.js
@@ -925,6 +925,30 @@ describe("printer", function() {
         assert.strictEqual(pretty, code);
     });
 
+    it("shouldn't print a trailing comma for a RestElement", function() {
+        var code = [
+            "function foo(",
+            "  a,",
+            "  b,",
+            "  ...rest",
+            ") {}"
+        ].join(eol);
+
+        var ast = parse(code, {
+            // The flow parser and Babylon recognize `...rest` as a `RestElement`
+            parser: require("babylon")
+        });
+
+        var printer = new Printer({
+            tabWidth: 2,
+            wrapColumn: 1,
+            trailingComma: true,
+        });
+
+        var pretty = printer.printGenerically(ast).code;
+        assert.strictEqual(pretty, code);
+    });
+
     it("should support AssignmentPattern and RestElement", function() {
         var code = [
             "function foo(a, [b, c] = d(a), ...[e, f, ...rest]) {",
@@ -1507,4 +1531,78 @@ describe("printer", function() {
         var pretty = printer.printGenerically(ast).code;
         assert.strictEqual(pretty, code);
     });
+
+    it("uses the `arrayBracketSpacing` and the `objectCurlySpacing` option", function() {
+      var babylon = require("babylon");
+      var parseOptions = {
+        parser: {
+          parse: function (source) {
+            return babylon.parse(source, {
+              sourceType: 'module',
+              plugins: ['flow'],
+            });
+          }
+        }
+      };
+
+      var testCaseList = [{
+        printerConfig: {arrayBracketSpacing: false, objectCurlySpacing: false},
+        code: [
+          'import {java, script} from "javascript";',
+          '',
+          'function foo(a) {',
+          '    type MyType = {message: string};',
+          '    return [1, 2, 3];',
+          '}',
+          '',
+          'export {foo};'
+        ].join(eol)
+      }, {
+        printerConfig: {arrayBracketSpacing: true, objectCurlySpacing: false},
+        code: [
+          'import {java, script} from "javascript";',
+          '',
+          'function foo(a) {',
+          '    type MyType = {message: string};',
+          '    return [ 1, 2, 3 ];',
+          '}',
+          '',
+          'export {foo};'
+        ].join(eol)
+      }, {
+        printerConfig: {arrayBracketSpacing: false, objectCurlySpacing: true},
+        code: [
+          'import { java, script } from "javascript";',
+          '',
+          'function foo(a) {',
+          '    type MyType = { message: string };',
+          '    return [1, 2, 3];',
+          '}',
+          '',
+          'export { foo };'
+        ].join(eol)
+      }, {
+        printerConfig: {arrayBracketSpacing: true, objectCurlySpacing: true},
+        code: [
+          'import { java, script } from "javascript";',
+          '',
+          'function foo(a) {',
+          '    type MyType = { message: string };',
+          '    return [ 1, 2, 3 ];',
+          '}',
+          '',
+          'export { foo };'
+        ].join(eol)
+      }];
+
+      testCaseList.forEach(function(testCase) {
+        var code = testCase.code;
+        var printer = new Printer(testCase.printerConfig);
+
+        var ast = parse(code, parseOptions);
+        var pretty = printer.printGenerically(ast).code;
+
+        assert.strictEqual(pretty, code);
+      });
+    });
 });

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