2017-01-10 20:18:22 +03:00
|
|
|
"use strict";
|
2017-01-28 18:50:22 +03:00
|
|
|
|
2017-01-23 20:10:51 +03:00
|
|
|
const codeFrame = require("babel-code-frame");
|
2017-01-10 01:37:45 +03:00
|
|
|
const comments = require("./src/comments");
|
2017-01-19 02:31:46 +03:00
|
|
|
const version = require("./package.json").version;
|
2017-01-19 23:46:57 +03:00
|
|
|
const printAstToDoc = require("./src/printer").printAstToDoc;
|
|
|
|
const printDocToString = require("./src/doc-printer").printDocToString;
|
2017-01-19 23:43:10 +03:00
|
|
|
const normalizeOptions = require("./src/options").normalize;
|
2017-01-20 00:23:04 +03:00
|
|
|
const parser = require("./src/parser");
|
Add debugging support for doc IR (#347)
This PR adds two things:
`--debug-print-doc` command that prints the formatted doc
```js
echo "<div><</div>" | ./bin/prettier.js --stdin --debug-print-doc
[
groupConditional(
group([
group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
"<",
"</",
"div",
">"
]),
[
group([
group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
"<",
"</",
"div",
">"
]),
group([
group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
indent(2, groupBreak([ hardline(), "<" ])),
hardline(),
"</",
"div",
">"
])
]
),
";",
hardline()
];
```
The ability to view the IR in real time on the browser display:
![image](https://cloud.githubusercontent.com/assets/197597/22134741/4f172f20-de7e-11e6-84bc-5f813976dc19.png)
The way it works is pretty cool, we take the doc IR and print a valid JavaScript string out of it, that we then send to prettier in order to make it look good :)
2017-01-20 20:54:32 +03:00
|
|
|
const printDocToDebug = require("./src/doc-debug").printDocToDebug;
|
2016-11-29 23:23:00 +03:00
|
|
|
|
2017-01-27 01:16:42 +03:00
|
|
|
function guessLineEnding(text) {
|
|
|
|
const index = text.indexOf("\n");
|
|
|
|
if (index >= 0 && text.charAt(index - 1) === "\r") {
|
|
|
|
return "\r\n";
|
|
|
|
}
|
|
|
|
return "\n";
|
|
|
|
}
|
|
|
|
|
2017-01-20 01:18:22 +03:00
|
|
|
function parse(text, opts) {
|
2017-03-09 00:18:13 +03:00
|
|
|
let parseFunction;
|
|
|
|
|
2017-04-12 20:16:11 +03:00
|
|
|
if (opts.parser === "flow") {
|
2017-03-09 00:18:13 +03:00
|
|
|
parseFunction = parser.parseWithFlow;
|
2017-04-12 20:16:11 +03:00
|
|
|
} else if (opts.parser === "typescript") {
|
2017-03-09 00:18:13 +03:00
|
|
|
parseFunction = parser.parseWithTypeScript;
|
|
|
|
} else {
|
|
|
|
parseFunction = parser.parseWithBabylon;
|
|
|
|
}
|
2017-01-23 20:10:51 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
return parseFunction(text);
|
|
|
|
} catch (error) {
|
|
|
|
const loc = error.loc;
|
|
|
|
|
|
|
|
if (loc) {
|
|
|
|
error.codeFrame = codeFrame(text, loc.line, loc.column + 1, {
|
|
|
|
highlightCode: true
|
|
|
|
});
|
|
|
|
error.message += "\n" + error.codeFrame;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
2017-01-13 21:39:07 +03:00
|
|
|
}
|
2017-01-20 01:18:22 +03:00
|
|
|
}
|
2016-11-29 23:23:00 +03:00
|
|
|
|
2017-01-20 01:18:22 +03:00
|
|
|
function attachComments(text, ast, opts) {
|
2017-02-05 05:36:29 +03:00
|
|
|
const astComments = ast.comments;
|
|
|
|
if (astComments) {
|
|
|
|
delete ast.comments;
|
2017-02-23 20:23:56 +03:00
|
|
|
comments.attach(astComments, ast, text, opts);
|
2017-01-13 21:39:07 +03:00
|
|
|
}
|
|
|
|
ast.tokens = [];
|
2017-02-07 18:39:04 +03:00
|
|
|
opts.originalText = text.trimRight();
|
2017-02-13 20:04:21 +03:00
|
|
|
return astComments;
|
|
|
|
}
|
|
|
|
|
|
|
|
function ensureAllCommentsPrinted(astComments) {
|
2017-04-18 17:51:50 +03:00
|
|
|
for (let i = 0; i < astComments.length; ++i) {
|
|
|
|
if (astComments[i].value.trim() === "prettier-ignore") {
|
|
|
|
// If there's a prettier-ignore, we're not printing that sub-tree so we
|
|
|
|
// don't know if the comments was printed or not.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-13 20:04:21 +03:00
|
|
|
astComments.forEach(comment => {
|
|
|
|
if (!comment.printed) {
|
|
|
|
throw new Error(
|
2017-02-16 06:56:11 +03:00
|
|
|
'Comment "' +
|
|
|
|
comment.value.trim() +
|
|
|
|
'" was not printed. Please report this error!'
|
2017-02-13 20:04:21 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
delete comment.printed;
|
|
|
|
});
|
2017-01-20 01:18:22 +03:00
|
|
|
}
|
2017-01-13 21:39:07 +03:00
|
|
|
|
2017-01-20 01:18:22 +03:00
|
|
|
function format(text, opts) {
|
|
|
|
const ast = parse(text, opts);
|
2017-02-13 20:04:21 +03:00
|
|
|
const astComments = attachComments(text, ast, opts);
|
Add debugging support for doc IR (#347)
This PR adds two things:
`--debug-print-doc` command that prints the formatted doc
```js
echo "<div><</div>" | ./bin/prettier.js --stdin --debug-print-doc
[
groupConditional(
group([
group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
"<",
"</",
"div",
">"
]),
[
group([
group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
"<",
"</",
"div",
">"
]),
group([
group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
indent(2, groupBreak([ hardline(), "<" ])),
hardline(),
"</",
"div",
">"
])
]
),
";",
hardline()
];
```
The ability to view the IR in real time on the browser display:
![image](https://cloud.githubusercontent.com/assets/197597/22134741/4f172f20-de7e-11e6-84bc-5f813976dc19.png)
The way it works is pretty cool, we take the doc IR and print a valid JavaScript string out of it, that we then send to prettier in order to make it look good :)
2017-01-20 20:54:32 +03:00
|
|
|
const doc = printAstToDoc(ast, opts);
|
2017-04-07 05:49:37 +03:00
|
|
|
opts.newLine = guessLineEnding(text);
|
|
|
|
const str = printDocToString(doc, opts);
|
2017-02-13 20:04:21 +03:00
|
|
|
ensureAllCommentsPrinted(astComments);
|
2017-01-19 23:46:57 +03:00
|
|
|
return str;
|
2017-01-13 21:39:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function formatWithShebang(text, opts) {
|
|
|
|
if (!text.startsWith("#!")) {
|
|
|
|
return format(text, opts);
|
|
|
|
}
|
2017-01-05 20:32:56 +03:00
|
|
|
|
2017-01-13 21:39:07 +03:00
|
|
|
const index = text.indexOf("\n");
|
|
|
|
const shebang = text.slice(0, index + 1);
|
|
|
|
const programText = text.slice(index + 1);
|
2017-01-18 01:16:40 +03:00
|
|
|
const nextChar = text.charAt(index + 1);
|
2017-01-28 18:50:22 +03:00
|
|
|
const newLine = nextChar === "\n" ? "\n" : nextChar === "\r" ? "\r\n" : "";
|
2017-01-18 01:16:40 +03:00
|
|
|
|
2017-01-27 01:16:42 +03:00
|
|
|
return shebang + newLine + format(programText, opts);
|
2017-01-13 21:39:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
format: function(text, opts) {
|
2017-01-20 01:18:22 +03:00
|
|
|
return formatWithShebang(text, normalizeOptions(opts));
|
2017-01-18 23:45:44 +03:00
|
|
|
},
|
2017-04-03 19:54:10 +03:00
|
|
|
check: function(text, opts) {
|
|
|
|
try {
|
|
|
|
const formatted = this.format(text, opts);
|
|
|
|
return formatted === text;
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
Add debugging support for doc IR (#347)
This PR adds two things:
`--debug-print-doc` command that prints the formatted doc
```js
echo "<div><</div>" | ./bin/prettier.js --stdin --debug-print-doc
[
groupConditional(
group([
group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
"<",
"</",
"div",
">"
]),
[
group([
group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
"<",
"</",
"div",
">"
]),
group([
group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
indent(2, groupBreak([ hardline(), "<" ])),
hardline(),
"</",
"div",
">"
])
]
),
";",
hardline()
];
```
The ability to view the IR in real time on the browser display:
![image](https://cloud.githubusercontent.com/assets/197597/22134741/4f172f20-de7e-11e6-84bc-5f813976dc19.png)
The way it works is pretty cool, we take the doc IR and print a valid JavaScript string out of it, that we then send to prettier in order to make it look good :)
2017-01-20 20:54:32 +03:00
|
|
|
version: version,
|
|
|
|
__debug: {
|
2017-01-23 01:00:23 +03:00
|
|
|
formatAST: function(ast, opts) {
|
|
|
|
opts = normalizeOptions(opts);
|
|
|
|
const doc = printAstToDoc(ast, opts);
|
2017-04-07 05:49:37 +03:00
|
|
|
const str = printDocToString(doc, opts);
|
2017-01-23 01:00:23 +03:00
|
|
|
return str;
|
|
|
|
},
|
Add debugging support for doc IR (#347)
This PR adds two things:
`--debug-print-doc` command that prints the formatted doc
```js
echo "<div><</div>" | ./bin/prettier.js --stdin --debug-print-doc
[
groupConditional(
group([
group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
"<",
"</",
"div",
">"
]),
[
group([
group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
"<",
"</",
"div",
">"
]),
group([
group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
indent(2, groupBreak([ hardline(), "<" ])),
hardline(),
"</",
"div",
">"
])
]
),
";",
hardline()
];
```
The ability to view the IR in real time on the browser display:
![image](https://cloud.githubusercontent.com/assets/197597/22134741/4f172f20-de7e-11e6-84bc-5f813976dc19.png)
The way it works is pretty cool, we take the doc IR and print a valid JavaScript string out of it, that we then send to prettier in order to make it look good :)
2017-01-20 20:54:32 +03:00
|
|
|
// 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);
|
2017-04-07 05:49:37 +03:00
|
|
|
const str = printDocToString(doc, opts);
|
Add debugging support for doc IR (#347)
This PR adds two things:
`--debug-print-doc` command that prints the formatted doc
```js
echo "<div><</div>" | ./bin/prettier.js --stdin --debug-print-doc
[
groupConditional(
group([
group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
"<",
"</",
"div",
">"
]),
[
group([
group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
"<",
"</",
"div",
">"
]),
group([
group([ "<", "div", group([ indent(2, []), softline() ]), ">" ]),
indent(2, groupBreak([ hardline(), "<" ])),
hardline(),
"</",
"div",
">"
])
]
),
";",
hardline()
];
```
The ability to view the IR in real time on the browser display:
![image](https://cloud.githubusercontent.com/assets/197597/22134741/4f172f20-de7e-11e6-84bc-5f813976dc19.png)
The way it works is pretty cool, we take the doc IR and print a valid JavaScript string out of it, that we then send to prettier in order to make it look good :)
2017-01-20 20:54:32 +03:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
}
|
2016-11-29 23:23:00 +03:00
|
|
|
};
|