2017-01-10 12:18:22 -05:00
|
|
|
"use strict";
|
2017-01-28 07:50:22 -08:00
|
|
|
|
2017-01-23 18:10:51 +01:00
|
|
|
const codeFrame = require("babel-code-frame");
|
2017-01-09 17:37:45 -05:00
|
|
|
const comments = require("./src/comments");
|
2017-01-18 15:31:46 -08:00
|
|
|
const version = require("./package.json").version;
|
2017-01-19 12:46:57 -08:00
|
|
|
const printAstToDoc = require("./src/printer").printAstToDoc;
|
|
|
|
|
const printDocToString = require("./src/doc-printer").printDocToString;
|
2017-01-19 12:43:10 -08:00
|
|
|
const normalizeOptions = require("./src/options").normalize;
|
2017-01-19 13:23:04 -08:00
|
|
|
const parser = require("./src/parser");
|
2017-01-20 09:54:32 -08:00
|
|
|
const printDocToDebug = require("./src/doc-debug").printDocToDebug;
|
2016-11-29 15:23:00 -05:00
|
|
|
|
2017-01-26 14:16:42 -08:00
|
|
|
function guessLineEnding(text) {
|
|
|
|
|
const index = text.indexOf("\n");
|
|
|
|
|
if (index >= 0 && text.charAt(index - 1) === "\r") {
|
|
|
|
|
return "\r\n";
|
|
|
|
|
}
|
|
|
|
|
return "\n";
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-19 14:18:22 -08:00
|
|
|
function parse(text, opts) {
|
2017-03-08 21:18:13 +00:00
|
|
|
let parseFunction;
|
|
|
|
|
|
|
|
|
|
if (opts.parser === 'flow') {
|
|
|
|
|
parseFunction = parser.parseWithFlow;
|
|
|
|
|
} else if (opts.parser === 'typescript') {
|
|
|
|
|
parseFunction = parser.parseWithTypeScript;
|
|
|
|
|
} else {
|
|
|
|
|
parseFunction = parser.parseWithBabylon;
|
|
|
|
|
}
|
2017-01-23 18:10:51 +01: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 10:39:07 -08:00
|
|
|
}
|
2017-01-19 14:18:22 -08:00
|
|
|
}
|
2017-01-13 10:39:07 -08:00
|
|
|
|
2017-01-19 14:18:22 -08:00
|
|
|
function attachComments(text, ast, opts) {
|
2017-02-04 18:36:29 -08:00
|
|
|
const astComments = ast.comments;
|
|
|
|
|
if (astComments) {
|
|
|
|
|
delete ast.comments;
|
2017-02-23 18:23:56 +01:00
|
|
|
comments.attach(astComments, ast, text, opts);
|
2017-01-13 10:39:07 -08:00
|
|
|
}
|
|
|
|
|
ast.tokens = [];
|
2017-02-07 07:39:04 -08:00
|
|
|
opts.originalText = text.trimRight();
|
2017-02-13 09:04:21 -08:00
|
|
|
return astComments;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function ensureAllCommentsPrinted(astComments) {
|
|
|
|
|
astComments.forEach(comment => {
|
|
|
|
|
if (!comment.printed) {
|
|
|
|
|
throw new Error(
|
2017-02-15 19:56:11 -08:00
|
|
|
'Comment "' +
|
|
|
|
|
comment.value.trim() +
|
|
|
|
|
'" was not printed. Please report this error!'
|
2017-02-13 09:04:21 -08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
delete comment.printed;
|
|
|
|
|
});
|
2017-01-19 14:18:22 -08:00
|
|
|
}
|
2017-01-13 10:39:07 -08:00
|
|
|
|
2017-01-19 14:18:22 -08:00
|
|
|
function format(text, opts) {
|
|
|
|
|
const ast = parse(text, opts);
|
2017-02-13 09:04:21 -08:00
|
|
|
const astComments = attachComments(text, ast, opts);
|
2017-01-20 09:54:32 -08:00
|
|
|
const doc = printAstToDoc(ast, opts);
|
2017-01-26 14:16:42 -08:00
|
|
|
const str = printDocToString(doc, opts.printWidth, guessLineEnding(text));
|
2017-02-13 09:04:21 -08:00
|
|
|
ensureAllCommentsPrinted(astComments);
|
2017-01-19 12:46:57 -08:00
|
|
|
return str;
|
2017-01-13 10:39:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function formatWithShebang(text, opts) {
|
|
|
|
|
if (!text.startsWith("#!")) {
|
|
|
|
|
return format(text, opts);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const index = text.indexOf("\n");
|
|
|
|
|
const shebang = text.slice(0, index + 1);
|
|
|
|
|
const programText = text.slice(index + 1);
|
2017-01-18 06:16:40 +08:00
|
|
|
const nextChar = text.charAt(index + 1);
|
2017-01-28 07:50:22 -08:00
|
|
|
const newLine = nextChar === "\n" ? "\n" : nextChar === "\r" ? "\r\n" : "";
|
2017-01-18 06:16:40 +08:00
|
|
|
|
2017-01-26 14:16:42 -08:00
|
|
|
return shebang + newLine + format(programText, opts);
|
2017-01-13 10:39:07 -08:00
|
|
|
}
|
|
|
|
|
|
2016-11-29 15:23:00 -05:00
|
|
|
module.exports = {
|
2017-01-10 12:18:22 -05:00
|
|
|
format: function(text, opts) {
|
2017-01-19 14:18:22 -08:00
|
|
|
return formatWithShebang(text, normalizeOptions(opts));
|
2017-01-18 12:45:44 -08:00
|
|
|
},
|
2017-01-20 09:54:32 -08:00
|
|
|
version: version,
|
|
|
|
|
__debug: {
|
2017-01-22 22:00:23 +00:00
|
|
|
formatAST: function(ast, opts) {
|
|
|
|
|
opts = normalizeOptions(opts);
|
|
|
|
|
const doc = printAstToDoc(ast, opts);
|
|
|
|
|
const str = printDocToString(doc, opts.printWidth);
|
|
|
|
|
return str;
|
|
|
|
|
},
|
2017-01-20 09:54:32 -08: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);
|
|
|
|
|
const str = printDocToString(doc, opts.printWidth);
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-29 15:23:00 -05:00
|
|
|
};
|