[Pkg-javascript-commits] [node-recast] 01/03: New upstream version 0.11.22
Julien Puydt
julien.puydt at laposte.net
Fri Feb 17 09:23:25 UTC 2017
This is an automated email from the git hooks/post-receive script.
jpuydt-guest pushed a commit to annotated tag debian/0.11.22-1
in repository node-recast.
commit c4fd030a9959c14dddd0c996f24da2aa699f49fb
Author: Julien Puydt <julien.puydt at laposte.net>
Date: Fri Feb 17 09:58:26 2017 +0100
New upstream version 0.11.22
---
.travis.yml | 1 +
lib/lines.js | 17 +++++++++++
lib/patcher.js | 11 +++++++
lib/printer.js | 8 +++--
package.json | 2 +-
test/comments.js | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
test/mapping.js | 2 --
test/patcher.js | 2 +-
8 files changed, 126 insertions(+), 7 deletions(-)
diff --git a/.travis.yml b/.travis.yml
index dc44815..7b3fa17 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,5 +1,6 @@
language: node_js
node_js:
+ - "7.0"
- "6.0"
- "5.0"
- "4.0"
diff --git a/lib/lines.js b/lib/lines.js
index b83f8a9..3b0bbcf 100644
--- a/lib/lines.js
+++ b/lib/lines.js
@@ -448,6 +448,23 @@ Lp.guessTabWidth = function() {
return secret.cachedTabWidth = result;
};
+// Determine if the list of lines has a first line that starts with a //
+// or /* comment. If this is the case, the code may need to be wrapped in
+// parens to avoid ASI issues.
+Lp.startsWithComment = function () {
+ var secret = getSecret(this);
+ if (secret.infos.length === 0) {
+ return false;
+ }
+ var firstLineInfo = secret.infos[0],
+ sliceStart = firstLineInfo.sliceStart,
+ sliceEnd = firstLineInfo.sliceEnd,
+ firstLine = firstLineInfo.line.slice(sliceStart, sliceEnd).trim();
+ return firstLine.length === 0 ||
+ firstLine.slice(0, 2) === "//" ||
+ firstLine.slice(0, 2) === "/*";
+};
+
Lp.isOnlyWhitespace = function() {
return isOnlyWhitespace(this.toString());
};
diff --git a/lib/patcher.js b/lib/patcher.js
index 7f2a3a1..bddfe68 100644
--- a/lib/patcher.js
+++ b/lib/patcher.js
@@ -4,6 +4,7 @@ var types = require("./types");
var getFieldValue = types.getFieldValue;
var Printable = types.namedTypes.Printable;
var Expression = types.namedTypes.Expression;
+var ReturnStatement = types.namedTypes.ReturnStatement;
var SourceLocation = types.namedTypes.SourceLocation;
var util = require("./util");
var comparePos = util.comparePos;
@@ -508,6 +509,8 @@ function findChildReprints(newPath, oldPath, reprints) {
// Don't bother traversing .loc objects looking for reprintable nodes.
delete keys.loc;
+ var originalReprintCount = reprints.length;
+
for (var k in keys) {
newPath.stack.push(k, types.getFieldValue(newNode, k));
oldPath.stack.push(k, types.getFieldValue(oldNode, k));
@@ -520,5 +523,13 @@ function findChildReprints(newPath, oldPath, reprints) {
}
}
+ // Return statements might end up running into ASI issues due to comments
+ // inserted deep within the tree, so reprint them if anything changed
+ // within them.
+ if (ReturnStatement.check(newPath.getNode()) &&
+ reprints.length > originalReprintCount) {
+ return false;
+ }
+
return true;
}
diff --git a/lib/printer.js b/lib/printer.js
index ca255b4..a89ad90 100644
--- a/lib/printer.js
+++ b/lib/printer.js
@@ -571,9 +571,11 @@ function genericPrintNoParens(path, options, print) {
if (n.argument) {
var argLines = path.call(print, "argument");
- if (argLines.length > 1 &&
- namedTypes.JSXElement &&
- namedTypes.JSXElement.check(n.argument)) {
+ if (argLines.startsWithComment() ||
+ (argLines.length > 1 &&
+ namedTypes.JSXElement &&
+ namedTypes.JSXElement.check(n.argument)
+ )) {
parts.push(
" (\n",
argLines.indent(options.tabWidth),
diff --git a/package.json b/package.json
index 97d86e6..225a3ca 100644
--- a/package.json
+++ b/package.json
@@ -12,7 +12,7 @@
"parsing",
"pretty-printing"
],
- "version": "0.11.21",
+ "version": "0.11.22",
"homepage": "http://github.com/benjamn/recast",
"repository": {
"type": "git",
diff --git a/test/comments.js b/test/comments.js
index d7abfe3..7952866 100644
--- a/test/comments.js
+++ b/test/comments.js
@@ -661,4 +661,94 @@ describe("comments", function() {
code
);
});
+
+ it("should preserve correctness when a return expression has a comment", function () {
+ var code = [
+ "function f() {",
+ " return 3;",
+ "}"
+ ].join(eol);
+
+ var ast = recast.parse(code);
+ ast.program.body[0].body.body[0].argument.comments = [b.line('Foo')];
+
+ assert.strictEqual(recast.print(ast).code, [
+ "function f() {",
+ " return (",
+ " //Foo",
+ " 3",
+ " );",
+ "}"
+ ].join(eol));
+ });
+
+ it("should wrap in parens when the return expression has nested leftmost comment", function () {
+ var code = [
+ "function f() {",
+ " return 1 + 2;",
+ "}"
+ ].join(eol);
+
+ var ast = recast.parse(code);
+ ast.program.body[0].body.body[0].argument.left.comments = [b.line('Foo')];
+
+ assert.strictEqual(recast.print(ast).code, [
+ "function f() {",
+ " return (",
+ " //Foo",
+ " 1 + 2",
+ " );",
+ "}"
+ ].join(eol));
+ });
+
+ it("should not wrap in parens when the return expression has an interior comment", function () {
+ var code = [
+ "function f() {",
+ " return 1 + 2;",
+ "}"
+ ].join(eol);
+
+ var ast = recast.parse(code);
+ ast.program.body[0].body.body[0].argument.right.comments = [b.line('Foo')];
+
+ assert.strictEqual(recast.print(ast).code, [
+ "function f() {",
+ " return 1 + //Foo",
+ " 2;",
+ "}"
+ ].join(eol));
+ });
+
+ it("should not reformat a return statement that is not modified", function () {
+ var code = [
+ "function f() {",
+ " return {",
+ " a: 1,",
+ " b: 2,",
+ " };",
+ "}"
+ ].join(eol);
+
+ var ast = recast.parse(code);
+
+ assert.strictEqual(recast.print(ast).code, code);
+ });
+
+ it("should correctly handle a removing the argument from a return", function () {
+ var code = [
+ "function f() {",
+ " return 'foo';",
+ "}"
+ ].join(eol);
+
+ var ast = recast.parse(code);
+ ast.program.body[0].body.body[0].argument = null;
+
+ assert.strictEqual(recast.print(ast).code, [
+ "function f() {",
+ " return;",
+ "}"
+ ].join(eol));
+ });
});
diff --git a/test/mapping.js b/test/mapping.js
index 1d85de3..58b97f9 100644
--- a/test/mapping.js
+++ b/test/mapping.js
@@ -79,10 +79,8 @@ describe("source maps", function() {
check(1, 0, 1, 0, null); // function
check(1, 18, 1, 18, null); // {
- check(2, 2, 2, 2, null); // return
check(2, 13, 2, 9, null); // bar
check(2, 9, 2, 15, null); // 1
- check(2, 16, 2, 16, null); // ;
check(3, 0, 3, 0, null); // }
});
diff --git a/test/patcher.js b/test/patcher.js
index 3fc023f..4a45f31 100644
--- a/test/patcher.js
+++ b/test/patcher.js
@@ -116,7 +116,7 @@ describe("patcher", function() {
returnStmt.argument = b.literal(null);
assert.strictEqual(
recast.print(strAST).code,
- "return null" // Instead of returnnull.
+ "return null;" // Instead of returnnull.
);
var arrAST = parse("throw[1,2,3]");
--
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