Files
test-repo/index.js
T

120 lines
3.2 KiB
JavaScript
Raw Normal View History

2017-01-10 12:18:22 -05:00
"use strict";
2017-01-28 07:50:22 -08:00
const codeFrame = require("babel-code-frame");
const comments = require("./src/comments");
2017-01-18 15:31:46 -08:00
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;
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) {
const parseFunction = opts.parser === "flow"
? parser.parseWithFlow
: parser.parseWithBabylon;
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-19 14:18:22 -08:00
}
2017-01-19 14:18:22 -08:00
function attachComments(text, ast, opts) {
const astComments = ast.comments;
if (astComments) {
delete ast.comments;
comments.attach(astComments, ast, text);
}
ast.tokens = [];
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-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);
return str;
}
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);
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-26 14:16:42 -08:00
return shebang + newLine + format(programText, opts);
}
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: {
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
};