prettier/index.js

81 lines
2.3 KiB
JavaScript
Raw Normal View History

2017-01-10 20:18:22 +03:00
"use strict";
const comments = require("./src/comments");
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;
2016-11-29 23:23:00 +03:00
function parse(text, opts) {
2017-01-20 21:12:37 +03:00
if (opts.parser === "flow") {
return parser.parseWithFlow(text, opts.filename);
}
return parser.parseWithBabylon(text);
}
2016-11-29 23:23:00 +03:00
function attachComments(text, ast, opts) {
2017-01-13 23:03:53 +03:00
if (ast.comments) {
comments.attach(ast.comments, ast, text);
ast.comments = [];
}
ast.tokens = [];
opts.originalText = text;
}
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;
}
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);
const addNewline = nextChar == "\n" || nextChar == "\r";
return shebang + (addNewline ? "\n" : "") + format(programText, opts);
}
module.exports = {
format: function(text, opts) {
return formatWithShebang(text, normalizeOptions(opts));
},
version: version,
__debug: {
formatAST: function(ast, opts) {
opts = normalizeOptions(opts);
const doc = printAstToDoc(ast, opts);
const str = printDocToString(doc, opts.printWidth);
return str;
},
// 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 23:23:00 +03:00
};