diff --git a/bin/prettier.js b/bin/prettier.js
index c3b76ca16..c6854634f 100755
--- a/bin/prettier.js
+++ b/bin/prettier.js
@@ -14,6 +14,7 @@ const argv = minimist(process.argv.slice(2), {
"single-quote",
"trailing-comma",
"version",
+ "debug-print-doc",
// Deprecated in 0.0.10
"flow-parser"
@@ -65,15 +66,21 @@ function getParser() {
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"],
- parser: getParser(),
- 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);
}
if (stdin) {
diff --git a/docs/index.html b/docs/index.html
index 747381374..90fb6e6e6 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -58,8 +58,11 @@
font-size: 11.05px;
line-height: 17.68px;
}
- .editor.input {
- margin-right: 5px;
+ .editor {
+ margin-left: 5px;
+ }
+ .editor:first-child {
+ margin-left: 0;
}
.options {
display: flex;
@@ -88,20 +91,25 @@
+
+
diff --git a/index.js b/index.js
index 19c4b183b..aec70cef6 100644
--- a/index.js
+++ b/index.js
@@ -6,6 +6,7 @@ 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;
function parse(text, opts) {
if (opts.parser === 'flow') {
@@ -25,9 +26,9 @@ function attachComments(text, ast, opts) {
function format(text, opts) {
const ast = parse(text, opts);
- attachComments(text, ast, opts)
- const doc = printAstToDoc(ast, opts)
- const str = printDocToString(doc, opts.printWidth)
+ attachComments(text, ast, opts);
+ const doc = printAstToDoc(ast, opts);
+ const str = printDocToString(doc, opts.printWidth);
return str;
}
@@ -49,5 +50,30 @@ module.exports = {
format: function(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;
+ }
+ }
};
diff --git a/src/doc-debug.js b/src/doc-debug.js
new file mode 100644
index 000000000..99e7aeb6d
--- /dev/null
+++ b/src/doc-debug.js
@@ -0,0 +1,102 @@
+"use strict";
+
+var jsesc = require("jsesc");
+
+function flattenDoc(doc) {
+ if (!doc ||
+ typeof doc === "string" ||
+ doc.type === "line") {
+ return doc;
+ }
+
+ if (doc.type === "concat") {
+ var res = [];
+
+ for (var i = 0; i < doc.parts.length; ++i) {
+ const doc2 = doc.parts[i];
+ if (typeof doc2 !== "string" && doc2.type === "concat") {
+ [].push.apply(res, flattenDoc(doc2).parts);
+ } else {
+ const flattened = flattenDoc(doc2);
+ if (flattened !== "") {
+ res.push(flattened);
+ }
+ }
+ }
+
+ return Object.assign({}, doc, {
+ parts: res
+ });
+ }
+
+ if (doc.type === "indent") {
+ return Object.assign({}, doc, {
+ contents: flattenDoc(doc.contents)
+ });
+ }
+
+ if (doc.type === "if-break") {
+ return Object.assign({}, doc, {
+ breakContents: flattenDoc(doc.breakContents),
+ flatContents: flattenDoc(doc.flatContents),
+ });
+ }
+
+ if (doc.type === "group") {
+ return Object.assign({}, doc, {
+ contents: flattenDoc(doc.contents),
+ expandedStates: doc.expandedStates ?
+ doc.expandedStates.map(flattenDoc) :
+ doc.expandedStates,
+ });
+ }
+}
+
+function printDoc(doc) {
+ if (typeof doc === "string") {
+ return jsesc(doc, {wrap: true});
+ }
+
+ if (doc.type === "line") {
+ if (doc.literalline) {
+ return "literalline";
+ }
+ if (doc.hard) {
+ return "hardline";
+ }
+ if (doc.soft) {
+ return "softline";
+ }
+ return "line";
+ }
+
+ if (doc.type === "concat") {
+ return "[" + doc.parts.map(printDoc).join(", ") + "]";
+ }
+
+ if (doc.type === "indent") {
+ return "indent(" + doc.n + ", " + printDoc(doc.contents) + ")";
+ }
+
+ if (doc.type === "if-break") {
+ return "ifBreak(" +
+ printDoc(doc.breakContents) +
+ (doc.flatContents ? ", " + printDoc(doc.flatContents) : "") +
+ ")";
+ }
+
+ if (doc.type === "group") {
+ return (doc.break ? "multilineGroup" : (doc.expandedStates ? "conditionalGroup" : "group")) +
+ "(" + printDoc(doc.contents) +
+ (doc.expandedStates ?
+ ", [" + doc.expandedStates.map(printDoc).join(",") + "]" :
+ "") +
+ ")";
+ }
+}
+
+module.exports = {
+ printDocToDebug: function(doc) {
+ return printDoc(flattenDoc(doc));
+ }
+}