Socket
Socket
Sign inDemoInstall

recast

Package Overview
Dependencies
Maintainers
1
Versions
266
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

recast - npm Package Compare versions

Comparing version 0.3.5 to 0.4.0

lib/path.js

23

lib/lines.js

@@ -290,5 +290,11 @@ var assert = require("assert");

Lp.firstNonSpacePos = function() {
var lines = this,
pos = lines.firstPos();
// Returns the position of the first non-whitespace character, starting
// from and including startPos (or lines.firstPos() if startPos is not
// specified).
Lp.firstNonSpacePos = function(startPos) {
var lines = this;
var pos = startPos ? {
line: startPos.line,
column: startPos.column
} : lines.firstPos();

@@ -309,5 +315,10 @@ while (isOnlyWhitespace(lines.charAt(pos)))

Lp.lastNonSpacePos = function(lines) {
var lines = this,
pos = lines.lastPos();
// Returns the position of the last non-whitespace character before endPos
// (or lines.lastPos() if endPos is not specified).
Lp.lastNonSpacePos = function(endPos) {
var lines = this;
var pos = endPos ? {
line: endPos.line,
column: endPos.column
} : lines.lastPos();

@@ -314,0 +325,0 @@ while (lines.prevPos(pos))

@@ -5,2 +5,3 @@ var assert = require("assert");

var util = require("./util");
var Path = require("./path").Path;

@@ -65,4 +66,6 @@ function Patcher(lines) {

exports.getReprinter = function(node) {
var orig = node.original;
exports.getReprinter = function(path) {
assert.ok(path instanceof Path);
var orig = path.node.original;
var origLoc = orig && orig.loc;

@@ -72,3 +75,3 @@ var lines = origLoc && origLoc.lines;

if (!lines || !findReprints(node, reprints))
if (!lines || !findReprints(path, reprints))
return;

@@ -88,3 +91,3 @@

old.loc,
print(reprint.newNode).indentTail(
print(reprint.newPath).indentTail(
getIndent(old)));

@@ -97,7 +100,7 @@ });

function findReprints(node, reprints) {
function findReprints(path, reprints) {
var node = path.node;
assert.ok(node.original);
assert.deepEqual(reprints, []);
var path = null; // Just a placeholder for now.
var canReprint = findChildReprints(path, node, node.original, reprints);

@@ -147,2 +150,3 @@

path = path.cons(newNode);
var childReprints = [];

@@ -156,5 +160,8 @@ var canReprintChildren = findChildReprints(path, newNode, oldNode, childReprints);

if (!canReprintChildren) {
if (!canReprintChildren ||
// Require reprinting if additional parentheses are needed.
(path.needsParens() && !hasParens(oldNode)))
{
reprints.push({
newNode: newNode,
newPath: path,
oldNode: oldNode

@@ -171,3 +178,24 @@ });

function hasParens(oldNode) {
var loc = oldNode.loc;
var lines = loc && loc.lines;
if (lines) {
// This logic can technically be fooled if the node has
// parentheses but there are comments intervening between the
// parentheses and the node. In such cases the node will be
// harmlessly wrapped in an additional layer of parentheses.
var pos = lines.lastNonSpacePos(loc.start);
if (pos && lines.charAt(pos) === "(") {
pos = lines.firstNonSpacePos(loc.end);
if (pos && lines.charAt(pos) === ")")
return true;
}
}
return false;
}
function findChildReprints(path, newNode, oldNode, reprints) {
assert.strictEqual(path.node, newNode);
assert.strictEqual(typeof newNode, "object");

@@ -174,0 +202,0 @@ assert.strictEqual(typeof oldNode, "object");

@@ -1,9 +0,11 @@

var assert = require("assert"),
Syntax = require("./types").Syntax,
printComment = require("./comments").print,
linesModule = require("./lines"),
fromString = linesModule.fromString,
concat = linesModule.concat,
normalizeOptions = require("./options").normalize,
getReprinter = require("./patcher").getReprinter;
var assert = require("assert");
var Syntax = require("./types").Syntax;
var printComment = require("./comments").print;
var linesModule = require("./lines");
var fromString = linesModule.fromString;
var concat = linesModule.concat;
var normalizeOptions = require("./options").normalize;
var getReprinter = require("./patcher").getReprinter;
var types = require("./types").namedTypes;
var Path = require("./path").Path;

@@ -15,8 +17,7 @@ function Printer(options) {

function printWithComments(node) {
if (!node)
return fromString("");
function printWithComments(path) {
assert.ok(path instanceof Path);
var without = print(node),
orig = node.original;
var without = print(path);
var orig = path.node.original;

@@ -33,26 +34,34 @@ if (orig && orig.comments) {

function print(node, includeComments) {
if (!node)
return fromString("");
function print(path, includeComments) {
if (includeComments)
return printWithComments(node);
return printWithComments(path);
var reprinter = getReprinter(node);
assert.ok(path instanceof Path);
var reprinter = getReprinter(path);
if (reprinter)
return reprinter(print);
return reprinter(printRootGenerically);
return genericPrint(node, options, printWithComments);
return printRootGenerically(path);
}
function printGenerically(node) {
return genericPrint(node, options, printGenerically);
// Print the root node generically, but then resume reprinting its
// children non-generically.
function printRootGenerically(path) {
return genericPrint(path, options, printWithComments);
}
// Print the entire AST generically.
function printGenerically(path) {
return genericPrint(path, options, printGenerically);
}
this.print = function(ast) {
return print(ast, true).toString(options);
if (!ast) return "";
return print(new Path(ast), true).toString(options);
};
this.printGenerically = function(ast) {
return printGenerically(ast).toString(options);
if (!ast) return "";
return printGenerically(new Path(ast)).toString(options);
};

@@ -63,13 +72,20 @@ }

var types = require("./types").namedTypes;
function genericPrint(path, options, printPath) {
var lines = genericPrintNoParens(path.node, options, function(node) {
if (!node)
return fromString("");
function genericPrint(n, options, print) {
if (!n)
return fromString("");
if (typeof node === "string")
return fromString(node, options.tabWidth);
if (typeof n === "string")
return fromString(n, options.tabWidth);
return printPath(path.cons(node));
});
types.Node.assert(n);
if (path.needsParens())
return concat(["(", lines, ")"]);
return lines;
}
function genericPrintNoParens(n, options, print) {
switch (n.type) {

@@ -325,7 +341,3 @@ case Syntax.File:

case Syntax.SequenceExpression:
return concat([
"(",
fromString(", ").join(n.expressions.map(print)),
")"
]);
return fromString(", ").join(n.expressions.map(print));

@@ -366,17 +378,12 @@ case Syntax.ThisExpression:

case Syntax.NewExpression:
var parts = [];
var parts = ["new ", print(n.callee)];
var args = n.arguments;
if (types.Identifier.check(n.callee)) {
parts.push("new ", print(n.callee));
} else {
// Parenthesize the callee expression in case it's a function call.
parts.push("new (", print(n.callee), ")");
}
var args = n.arguments;
if (args)
if (args) {
parts.push(
"(",
fromString(", ").join(args.map(print)),
")");
")"
);
}

@@ -383,0 +390,0 @@ return concat(parts);

@@ -15,3 +15,3 @@ {

],
"version": "0.3.5",
"version": "0.4.0",
"homepage": "http://github.com/benjamn/recast",

@@ -18,0 +18,0 @@ "repository": {

@@ -1,9 +0,10 @@

var assert = require("assert"),
parse = require("../lib/parser").parse,
getReprinter = require("../lib/patcher").getReprinter,
Printer = require("../lib/printer").Printer,
Visitor = require("../lib/visitor").Visitor,
Syntax = require("../lib/types").Syntax,
printComment = require("../lib/comments").print,
fromString = require("../lib/lines").fromString;
var assert = require("assert");
var parse = require("../lib/parser").parse;
var getReprinter = require("../lib/patcher").getReprinter;
var Printer = require("../lib/printer").Printer;
var Visitor = require("../lib/visitor").Visitor;
var Syntax = require("../lib/types").Syntax;
var printComment = require("../lib/comments").print;
var fromString = require("../lib/lines").fromString;
var Path = require("../lib/path").Path;

@@ -18,3 +19,3 @@ // Esprima seems unable to handle unnamed top-level functions, so declare

assert.strictEqual(ast.type, Syntax.File);
assert.ok(getReprinter(ast));
assert.ok(getReprinter(new Path(ast)));

@@ -26,3 +27,3 @@ var funDecl = ast.program.body[0],

assert.strictEqual(funBody.type, Syntax.BlockStatement);
assert.ok(getReprinter(funBody));
assert.ok(getReprinter(new Path(funBody)));

@@ -32,7 +33,7 @@ var lastStatement = funBody.body.pop(),

assert.ok(!getReprinter(funBody));
assert.ok(getReprinter(ast));
assert.ok(!getReprinter(new Path(funBody)));
assert.ok(getReprinter(new Path(ast)));
funBody.body.push(lastStatement);
assert.ok(getReprinter(funBody));
assert.ok(getReprinter(new Path(funBody)));

@@ -39,0 +40,0 @@ assert.strictEqual(tFinish.callee.object.name, "t");

@@ -21,1 +21,2 @@ function submodule(id) {

submodule("identity");
submodule("parens");

@@ -42,3 +42,3 @@ var assert = require("assert");

decl.id.type === Syntax.Identifier &&
decl.id.name === "genericPrint")
decl.id.name === "genericPrintNoParens")
{

@@ -45,0 +45,0 @@ new CaseVisitor(this.types).visit(decl);

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc