prettier/index.js

163 lines
4.8 KiB
JavaScript

"use strict";
const comments = require("./src/comments");
const version = require("./package.json").version;
const printAstToDoc = require("./src/printer").printAstToDoc;
const getAlignmentSize = require("./src/util").getAlignmentSize;
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 guessLineEnding(text) {
const index = text.indexOf("\n");
if (index >= 0 && text.charAt(index - 1) === "\r") {
return "\r\n";
}
return "\n";
}
function attachComments(text, ast, opts) {
const astComments = ast.comments;
if (astComments) {
delete ast.comments;
comments.attach(astComments, ast, text, opts);
}
ast.tokens = [];
opts.originalText = text.trimRight();
return astComments;
}
function ensureAllCommentsPrinted(astComments) {
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;
}
}
astComments.forEach(comment => {
if (!comment.printed) {
throw new Error(
'Comment "' +
comment.value.trim() +
'" was not printed. Please report this error!'
);
}
delete comment.printed;
});
}
function format(text, opts, addAlignmentSize) {
addAlignmentSize = addAlignmentSize || 0;
const formattedRangeOnly = formatRange(text, opts);
if (formattedRangeOnly) {
return formattedRangeOnly;
}
const ast = parser.parse(text, opts);
const astComments = attachComments(text, ast, opts);
const doc = printAstToDoc(ast, opts, addAlignmentSize);
opts.newLine = guessLineEnding(text);
const str = printDocToString(doc, opts);
ensureAllCommentsPrinted(astComments);
// Remove extra leading newline as well as the added indentation after last newline
if (addAlignmentSize > 0) {
return str.slice(opts.newLine.length).trimRight() + opts.newLine;
}
return str;
}
function formatRange(text, opts) {
// Use `Math.min` since `lastIndexOf` returns 0 when `rangeStart` is 0
const rangeStart = Math.min(
opts.rangeStart,
text.lastIndexOf("\n", opts.rangeStart) + 1
);
// Use `text.length - 1` as the maximum since `indexOf` returns -1 if `fromIndex >= text.length`
const fromIndex = Math.min(opts.rangeEnd, text.length - 1);
const nextNewLineIndex = text.indexOf("\n", fromIndex);
const rangeEnd = (nextNewLineIndex < 0 ? fromIndex : nextNewLineIndex) + 1; // Add one to make rangeEnd exclusive
if (0 < rangeStart || rangeEnd < text.length) {
const rangeString = text.substring(rangeStart, rangeEnd);
const alignmentSize = getAlignmentSize(
rangeString.slice(0, rangeString.search(/[^ \t]/)),
opts.tabWidth
);
const rangeFormatted = format(
rangeString,
Object.assign({}, opts, {
rangeStart: 0,
rangeEnd: Infinity,
printWidth: opts.printWidth - alignmentSize
}),
alignmentSize
);
return text.slice(0, rangeStart) + rangeFormatted + text.slice(rangeEnd);
}
}
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 newLine = nextChar === "\n" ? "\n" : nextChar === "\r" ? "\r\n" : "";
return shebang + newLine + format(programText, opts);
}
module.exports = {
format: function(text, opts) {
return formatWithShebang(text, normalizeOptions(opts));
},
check: function(text, opts) {
try {
const formatted = formatWithShebang(text, normalizeOptions(opts));
return formatted === text;
} catch (e) {
return false;
}
},
version: version,
__debug: {
parse: function(text, opts) {
return parser.parse(text, opts);
},
formatAST: function(ast, opts) {
opts = normalizeOptions(opts);
const doc = printAstToDoc(ast, opts);
const str = printDocToString(doc, opts);
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 = parser.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);
return str;
}
}
};