Socket
Socket
Sign inDemoInstall

prettier

Package Overview
Dependencies
9
Maintainers
2
Versions
162
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.9 to 0.0.10

src/doc-builders.js

69

bin/prettier.js

@@ -13,9 +13,18 @@ #!/usr/bin/env node

"stdin",
"flow-parser",
"bracket-spacing",
"single-quote",
"trailing-comma",
"version"
"version",
"debug-print-doc",
// Deprecated in 0.0.10
"flow-parser"
],
default: { "bracket-spacing": true }
string: [
"parser"
],
default: {
"bracket-spacing": true,
parser: "babylon",
}
});

@@ -34,11 +43,12 @@

console.log(
"Usage: prettier [opts] [filename ...]\n\n" + "Available options:\n" +
" --write Edit the file in-place (beware!)\n" +
" --stdin Read input from stdin\n" +
" --print-width <int> Specify the length of line that the printer will wrap on. Defaults to 80.\n" +
" --tab-width <int> Specify the number of spaces per indentation-level. Defaults to 2.\n" +
" --flow-parser Use the flow parser instead of babylon\n" +
" --single-quote Use single quotes instead of double\n" +
" --trailing-comma Print trailing commas wherever possible\n" +
" --bracket-spacing Put spaces between brackets. Defaults to true, set false to turn off"
"Usage: prettier [opts] [filename ...]\n\n" +
"Available options:\n" +
" --write Edit the file in-place (beware!)\n" +
" --stdin Read input from stdin\n" +
" --print-width <int> Specify the length of line that the printer will wrap on. Defaults to 80.\n" +
" --tab-width <int> Specify the number of spaces per indentation-level. Defaults to 2.\n" +
" --parser <flow|babylon> Specify which parse to use. Defaults to babylon\n" +
" --single-quote Use single quotes instead of double\n" +
" --trailing-comma Print trailing commas wherever possible\n" +
" --bracket-spacing Put spaces between brackets. Defaults to true, set false to turn off"
);

@@ -48,11 +58,30 @@ process.exit(1);

function getParser() {
// For backward compatibility. Deprecated in 0.0.10
if (argv["flow-parser"]) {
return "flow";
}
if (argv["parser"] === "flow") {
return "flow";
}
return "babylon";
}
const options = {
printWidth: argv["print-width"],
tabWidth: argv["tab-width"],
bracketSpacing: argv["bracket-spacing"],
parser: getParser(),
singleQuote: argv["single-quote"],
trailingComma: argv["trailing-comma"]
};
function format(input) {
return jscodefmt.format(input, {
printWidth: argv["print-width"],
tabWidth: argv["tab-width"],
bracketSpacing: argv["bracket-spacing"],
useFlowParser: argv["flow-parser"],
singleQuote: argv["single-quote"],
trailingComma: argv["trailing-comma"]
});
if (argv["debug-print-doc"]) {
const doc = jscodefmt.__debug.printToDoc(input, options);
return jscodefmt.__debug.formatDoc(doc);
}
return jscodefmt.format(input, options);
}

@@ -59,0 +88,0 @@

@@ -0,4 +1,30 @@

# 0.0.10
[link](https://github.com/jlongster/prettier/compare/0.0.9...0.0.10)
* Add description to package.json (#320)
* Fix crash for single rest on class declaration (#315)
* Add canonical link to Prettier SublimeText package. (#318)
* Properly escape JSXText (#329)
* Hug objects and arrays inside of JSXExpressionContainer (#213)
* Add quotes around unicode keys in flow parser (#328)
* Add tests for comments (#330)
* Print dangling comments in blocks (#331)
* Remove Printer module in favor of single function (#333)
* Split pp.js into doc-{printer,builders,utils}.js (#334)
* Fix node 4 (#336)
* Remove unused functions from recast (#337)
* Kill fromString (#335)
* Extract parser.js (#338)
* Normalize exports (#339)
* Refactor index.js (#340)
* Add semicolon to more default exports (#343)
* Introduce --parser/parser option and deprecate --flow-parser/useFlowParser (#342)
* Remove parens around AwaitExpression in ternary (#346)
* Indent while test the same way as if test (#352)
* Add debugging support for doc IR (#347)
# 0.0.9
[link](https://github.com/jlongster/prettier/compare/0192d58...a7405257)
[link](https://github.com/jlongster/prettier/compare/0.0.8...0.0.9)

@@ -5,0 +31,0 @@ * Workaround flow bug parsing astral unicode characters (#277)

@@ -5,2 +5,3 @@ import resolve from 'rollup-plugin-node-resolve';

import babili from 'rollup-plugin-real-babili';
import json from 'rollup-plugin-json';

@@ -12,2 +13,3 @@ export default {

plugins: [
json(),
resolve(),

@@ -14,0 +16,0 @@ commonjs(),

"use strict";
const babylon = require("babylon");
const Printer = require("./src/printer").Printer;
const flowParser = require("flow-parser");
const comments = require("./src/comments");
const version = require('./package.json').version;
const version = require("./package.json").version;
const printAstToDoc = require("./src/printer").printAstToDoc;
const printDocToString = require("./src/doc-printer").printDocToString;
const normalizeOptions = require("./src/options").normalize;
const parser = require("./src/parser");
const printDocToDebug = require("./src/doc-debug").printDocToDebug;
var babylonOptions = {
sourceType: "module",
allowImportExportEverywhere: false,
allowReturnOutsideFunction: false,
plugins: [
"jsx",
"flow",
"doExpressions",
"objectRestSpread",
"decorators",
"classProperties",
"exportExtensions",
"asyncGenerators",
"functionBind",
"functionSent",
"dynamicImport"
]
};
function format(text, opts) {
opts = opts || {};
let ast;
if (opts.useFlowParser) {
ast = flowParser.parse(text, {
esproposal_class_instance_fields: true,
esproposal_class_static_fields: true,
esproposal_export_star_as: true,
});
if (ast.errors.length > 0) {
let msg = ast.errors[0].message + " on line " +
ast.errors[0].loc.start.line;
if (opts.filename) {
msg += " in file " + opts.filename;
}
throw new Error(msg);
}
} else {
ast = babylon.parse(text, babylonOptions);
function parse(text, opts) {
if (opts.parser === 'flow') {
return parser.parseWithFlow(text, opts.filename);
}
return parser.parseWithBabylon(text);
}
// Interleave comment nodes
function attachComments(text, ast, opts) {
if (ast.comments) {

@@ -56,5 +25,10 @@ comments.attach(ast.comments, ast, text);

opts.originalText = text;
}
const printer = new Printer(opts);
return printer.print(ast);
function format(text, opts) {
const ast = parse(text, opts);
attachComments(text, ast, opts);
const doc = printAstToDoc(ast, opts);
const str = printDocToString(doc, opts.printWidth);
return str;
}

@@ -78,5 +52,30 @@

format: function(text, opts) {
return formatWithShebang(text, opts);
return formatWithShebang(text, normalizeOptions(opts));
},
version: version
version: version,
__debug: {
// Doesn't handle shebang for now
formatDoc: function(doc, opts) {
opts = normalizeOptions(opts);
const debug = printDocToDebug(doc);
const str = format(debug, opts);
return str;
},
printToDoc: function(text, opts) {
opts = normalizeOptions(opts);
const ast = parse(text, opts);
attachComments(text, ast, opts);
const doc = printAstToDoc(ast, opts);
return doc;
},
printDocToString: function(doc, opts) {
opts = normalizeOptions(opts);
const str = printDocToString(doc, opts.printWidth);
return str;
}
}
};
{
"name": "prettier",
"version": "0.0.9",
"version": "0.0.10",
"description": "Prettier is an opinionated JavaScript formatter",
"bin": {

@@ -31,2 +32,3 @@ "prettier": "./bin/prettier.js"

"rollup-plugin-commonjs": "7.0.0",
"rollup-plugin-json": "^2.1.0",
"rollup-plugin-node-builtins": "2.0.0",

@@ -33,0 +35,0 @@ "rollup-plugin-node-resolve": "2.0.0",

@@ -142,5 +142,2 @@ # Prettier

// Use the flow parser instead of babylon
useFlowParser: false,
// If true, will use single instead of double quotes

@@ -153,3 +150,6 @@ singleQuote: false,

// Controls the printing of spaces inside array and objects
bracketSpacing: true
bracketSpacing: true,
// Which parser to use. Valid options are 'flow' and 'babylon'
parser: 'babylon'
});

@@ -190,3 +190,4 @@ ```

Please see [this issue](https://github.com/jlongster/prettier/issues/17) for those interested in working on Sublime Text integration.
Sublime Text support is available through Package Control and
the [JsPrettier](https://packagecontrol.io/packages/JsPrettier) plug-in.

@@ -202,3 +203,3 @@ More editors are coming soon.

[flow](https://github.com/facebook/flow) parser with the
`useFlowParser` API or `--flow-parser` CLI option.
`parser` API or `--parser` CLI option.

@@ -205,0 +206,0 @@ All of JSX and Flow syntax is supported. In fact, the test suite in

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

var isObject = types.builtInTypes.object;
var pp = require("./pp");
var fromString = pp.fromString;
var concat = pp.concat;
var hardline = pp.hardline;
var docBuilders = require("./doc-builders");
var fromString = docBuilders.fromString;
var concat = docBuilders.concat;
var hardline = docBuilders.hardline;
var util = require("./util");

@@ -122,3 +122,3 @@ var comparePos = util.comparePos;

exports.attach = function(comments, ast, text) {
function attach(comments, ast, text) {
if (!isArray.check(comments)) {

@@ -166,3 +166,4 @@ return;

addDanglingComment(en, comment);
} else {}
} else {
}
});

@@ -188,4 +189,4 @@

var pn = tiesToBreak[(0)].precedingNode;
var fn = tiesToBreak[(0)].followingNode;
var pn = tiesToBreak[0].precedingNode;
var fn = tiesToBreak[0].followingNode;
var gapEndPos = locStart(fn);

@@ -274,3 +275,17 @@

exports.printComments = function(path, print, options) {
function printDanglingComments(path, print, options) {
const text = options.originalText;
const parts = [];
path.each(commentPath => {
const comment = commentPath.getValue();
if(!comment.leading && !comment.trailing) {
parts.push(util.newlineExistsBefore(text, locStart(comment)) ? hardline : " ");
parts.push(commentPath.call(print));
}
}, "comments");
return concat(parts);
}
function printComments(path, print, options) {
var value = path.getValue();

@@ -280,3 +295,3 @@ var parent = path.getParentNode();

var comments = n.Node.check(value) && types.getFieldValue(value, "comments");
var isFirstInProgram = n.Program.check(parent) && parent.body[(0)] === value;
var isFirstInProgram = n.Program.check(parent) && parent.body[0] === value;

@@ -299,3 +314,4 @@ if (!comments || comments.length === 0) {

trailing &&
!(n.Statement.check(value) || comment.type === "Block" ||
!(n.Statement.check(value) ||
comment.type === "Block" ||
comment.type === "CommentBlock")

@@ -324,1 +340,7 @@ ) {

};
module.exports = {
attach,
printComments,
printDanglingComments,
}

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

var FPp = FastPath.prototype;
module.exports = FastPath;

@@ -30,4 +29,3 @@ // Static convenience function for coercing a value to a FastPath.

var stack = [ obj.value ];
for (var pp; pp = obj.parentPath; obj = pp)
stack.push(obj.name, pp.value);
for (var pp; pp = obj.parentPath; obj = pp) stack.push(obj.name, pp.value);
copy.stack = stack.reverse();

@@ -88,15 +86,2 @@ return copy;

// The length of the stack can be either even or odd, depending on whether
// or not we have a name for the root value. The difference between the
// index of the root value and the index of the final value is always
// even, though, which allows us to return the root value in constant time
// (i.e. without iterating backwards through the stack).
FPp.getRootValue = function getRootValue() {
var s = this.stack;
if (s.length % 2 === 0) {
return s[(1)];
}
return s[(0)];
};
// Temporarily push properties named by string arguments given after the

@@ -216,3 +201,4 @@ // callback function onto this.stack, then call the callback with a

if (
parent.type === "ClassDeclaration" && parent.superClass === node &&
parent.type === "ClassDeclaration" &&
parent.superClass === node &&
node.type === "AwaitExpression"

@@ -226,3 +212,4 @@ ) {

if (
parent.type === "BinaryExpression" && parent.operator === "**" &&
parent.type === "BinaryExpression" &&
parent.operator === "**" &&
parent.left === node &&

@@ -240,3 +227,4 @@ node.type !== "Identifier" &&

case "SpreadProperty":
return parent.type === "MemberExpression" && name === "object" &&
return parent.type === "MemberExpression" &&
name === "object" &&
parent.object === node;

@@ -319,3 +307,2 @@

case "NewExpression":
case "ConditionalExpression":
case "MemberExpression":

@@ -349,3 +336,4 @@ return true;

case "Literal":
return parent.type === "MemberExpression" && isNumber.check(node.value) &&
return parent.type === "MemberExpression" &&
isNumber.check(node.value) &&
name === "object" &&

@@ -408,3 +396,4 @@ parent.object === node;

if (
parent.type === "NewExpression" && name === "callee" &&
parent.type === "NewExpression" &&
name === "callee" &&
parent.callee === node

@@ -417,3 +406,4 @@ ) {

if (
assumeExpressionContext !== true && !this.canBeFirstInStatement() &&
assumeExpressionContext !== true &&
!this.canBeFirstInStatement() &&
this.firstInStatement()

@@ -430,10 +420,2 @@ )

function isUnaryLike(node) {
return // I considered making SpreadElement and SpreadProperty subtypes
// of UnaryExpression, but they're not really Expression nodes.
n.UnaryExpression.check(
node
) || n.SpreadElement && n.SpreadElement.check(node) || n.SpreadProperty && n.SpreadProperty.check(node);
}
function containsCallExpression(node) {

@@ -459,3 +441,4 @@ if (n.CallExpression.check(node)) {

var node = this.getNode();
return !n.FunctionExpression.check(node) && !n.ObjectExpression.check(node) &&
return !n.FunctionExpression.check(node) &&
!n.ObjectExpression.check(node) &&
!n.ClassExpression.check(node) &&

@@ -485,3 +468,3 @@ !(n.AssignmentExpression.check(node) && n.ObjectPattern.check(node.left));

) {
assert.strictEqual(parent.body[(0)], child);
assert.strictEqual(parent.body[0], child);
return true;

@@ -496,6 +479,7 @@ }

if (
n.SequenceExpression.check(parent) && parentName === "expressions" &&
n.SequenceExpression.check(parent) &&
parentName === "expressions" &&
childName === 0
) {
assert.strictEqual(parent.expressions[(0)], child);
assert.strictEqual(parent.expressions[0], child);
continue;

@@ -525,3 +509,4 @@ }

if (
n.UnaryExpression.check(parent) && !parent.prefix &&
n.UnaryExpression.check(parent) &&
!parent.prefix &&
childName === "argument"

@@ -538,1 +523,3 @@ ) {

};
module.exports = FastPath;

@@ -0,1 +1,3 @@

"use strict";
var defaults = {

@@ -11,8 +13,17 @@ // Number of spaces the pretty-printer should use per tab

// Controls the printing of spaces inside array and objects
bracketSpacing: true
bracketSpacing: true,
// Which parser to use. Valid options are 'flow' and 'babylon'
parser: 'babylon'
};
// Copy options and fill in default values.
exports.normalize = function(options) {
const normalized = Object.assign({}, options);
function normalize(options) {
const normalized = Object.assign({}, options || {});
// For backward compatibility. Deprecated in 0.0.10
if ('useFlowParser' in normalized) {
normalized.parser = normalized.useFlowParser ? 'flow' : 'babylon';
delete normalized.useFlowParser;
}
Object.keys(defaults).forEach(k => {

@@ -23,3 +34,8 @@ if (normalized[k] == null) {

});
return normalized;
};
module.exports = {
normalize
};
"use strict";
var assert = require("assert");
var types = require("ast-types");
var getFieldValue = types.getFieldValue;
var n = types.namedTypes;
var hasOwn = Object.prototype.hasOwnProperty;
var util = exports;
function getUnionOfKeys() {
var result = {};
var argc = arguments.length;
for (var i = 0; i < argc; ++i) {
var keys = Object.keys(arguments[i]);
var keyCount = keys.length;
for (var j = 0; j < keyCount; ++j) {
result[keys[j]] = true;
}
}
return result;
}
util.getUnionOfKeys = getUnionOfKeys;
function comparePos(pos1, pos2) {
return pos1.line - pos2.line || pos1.column - pos2.column;
}
util.comparePos = comparePos;
function copyPos(pos) {
return { line: pos.line, column: pos.column };
}
util.copyPos = copyPos;
function expandLoc(parentNode, childNode) {

@@ -43,3 +20,3 @@ if (locStart(childNode) - locStart(parentNode) < 0) {

util.fixFaultyLocations = function(node, text) {
function fixFaultyLocations(node, text) {
if (node.decorators) {

@@ -51,3 +28,3 @@ // Expand the loc of the node responsible for printing the decorators

});
} else if (node.declaration && util.isExportDeclaration(node)) {
} else if (node.declaration && isExportDeclaration(node)) {
// Expand the loc of the node responsible for printing the decorators

@@ -82,5 +59,4 @@ // (here, the export declaration) so that it includes node.decorators.

util.isExportDeclaration = function(node) {
if (node)
switch (node.type) {
function isExportDeclaration(node) {
if (node) switch (node.type) {
case "ExportDeclaration":

@@ -98,6 +74,6 @@ case "ExportDefaultDeclaration":

util.getParentExportDeclaration = function(path) {
function getParentExportDeclaration(path) {
var parentNode = path.getParentNode();
if (
path.getName() === "declaration" && util.isExportDeclaration(parentNode)
path.getName() === "declaration" && isExportDeclaration(parentNode)
) {

@@ -110,11 +86,3 @@ return parentNode;

util.isTrailingCommaEnabled = function(options, context) {
var trailingComma = options.trailingComma;
if (typeof trailingComma === "object") {
return !!trailingComma[context];
}
return !!trailingComma;
};
util.getLast = function(arr) {
function getLast(arr) {
if (arr.length > 0) {

@@ -131,3 +99,3 @@ return arr[arr.length - 1];

// scan until we hit a newline in order to skip it.
while(index < text.length) {
while (index < text.length) {
if (text.charAt(index) === "\n") {

@@ -144,3 +112,3 @@ return index + 1;

function _findNewline(text, index, backwards) {
function findNewline(text, index, backwards) {
const length = text.length;

@@ -163,8 +131,8 @@ let cursor = backwards ? index - 1 : skipNewLineForward(text, index);

util.newlineExistsBefore = function(text, index) {
return _findNewline(text, index, true);
function newlineExistsBefore(text, index) {
return findNewline(text, index, true);
};
util.newlineExistsAfter = function(text, index) {
return _findNewline(text, index);
function newlineExistsAfter(text, index) {
return findNewline(text, index);
};

@@ -187,23 +155,20 @@

}
util.skipSpaces = skipSpaces;
function locStart(node) {
if (node.range) {
return node.range[(0)];
return node.range[0];
}
return node.start;
}
util.locStart = locStart;
function locEnd(node) {
if (node.range) {
return node.range[(1)];
return node.range[1];
}
return node.end;
}
util.locEnd = locEnd;
function setLocStart(node, index) {
if (node.range) {
node.range[(0)] = index;
node.range[0] = index;
} else {

@@ -213,7 +178,6 @@ node.start = index;

}
util.setLocStart = setLocStart;
function setLocEnd(node, index) {
if (node.range) {
node.range[(1)] = index;
node.range[1] = index;
} else {

@@ -223,3 +187,2 @@ node.end = index;

}
util.setLocEnd = setLocEnd;

@@ -235,4 +198,13 @@ // http://stackoverflow.com/a/7124052

}
util.htmlEscapeInsideDoubleQuote = htmlEscapeInsideDoubleQuote;
// http://stackoverflow.com/a/7124052
function htmlEscapeInsideAngleBracket(str) {
return str.replace(/</g, "&lt;").replace(/>/g, "&gt;");
// Intentionally disable the following since it is safe inside of a
// angle bracket context
// .replace(/&/g, '&amp;')
// .replace(/"/g, '&quot;')
// .replace(/'/g, '&#39;')
}
var PRECEDENCE = {};

@@ -256,4 +228,22 @@ [

util.getPrecedence = function(op) {
function getPrecedence(op) {
return PRECEDENCE[op];
}
};
module.exports = {
comparePos,
getPrecedence,
fixFaultyLocations,
isExportDeclaration,
getParentExportDeclaration,
getLast,
newlineExistsBefore,
newlineExistsAfter,
skipSpaces,
locStart,
locEnd,
setLocStart,
setLocEnd,
htmlEscapeInsideDoubleQuote,
htmlEscapeInsideAngleBracket
};

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc