Use default parameters where possible and other microrefactoring (#10135)

This commit is contained in:
Georgii Dolzhykov authored and GitHub committed 2021-01-27 12:36:39 +08:00
1 parent a98292e417
commit 1df287acc8
10 files changed
+35 -51

No files matched your search

+1
View File
@@ -105,6 +105,7 @@
"elektronik",
"Eneman",
"ENOENT",
"eqeqeq",
"Ericsburgh",
"errored",
"Esben",
+3 -7
View File
@@ -164,8 +164,7 @@ function skipNewline(text, index, opts) {
* @param {SkipOptions=} opts
* @returns {boolean}
*/
function hasNewline(text, index, opts) {
opts = opts || {};
function hasNewline(text, index, opts = {}) {
const idx = skipSpaces(text, opts.backwards ? index - 1 : index, opts);
const idx2 = skipNewline(text, idx, opts);
return idx !== idx2;
@@ -292,8 +291,7 @@ function getNextNonSpaceNonCommentCharacter(text, node, locEnd) {
* @param {SkipOptions=} opts
* @returns {boolean}
*/
function hasSpaces(text, index, opts) {
opts = opts || {};
function hasSpaces(text, index, opts = {}) {
const idx = skipSpaces(text, opts.backwards ? index - 1 : index, opts);
return idx !== index;
}
@@ -304,9 +302,7 @@ function hasSpaces(text, index, opts) {
* @param {number=} startIndex
* @returns {number}
*/
function getAlignmentSize(value, tabWidth, startIndex) {
startIndex = startIndex || 0;
function getAlignmentSize(value, tabWidth, startIndex = 0) {
let size = 0;
for (let i = startIndex; i < value.length; ++i) {
if (value[i] === "\t") {
+2 -6
View File
@@ -86,9 +86,7 @@ function align(n, contents) {
* @param {object} [opts] - TBD ???
* @returns Doc
*/
function group(contents, opts) {
opts = opts || {};
function group(contents, opts = {}) {
if (process.env.NODE_ENV !== "production") {
assertDoc(contents);
}
@@ -156,9 +154,7 @@ function fill(parts) {
* @param {object} [opts] - TBD ???
* @returns Doc
*/
function ifBreak(breakContents, flatContents, opts) {
opts = opts || {};
function ifBreak(breakContents, flatContents, opts = {}) {
if (process.env.NODE_ENV !== "production") {
if (breakContents) {
assertDoc(breakContents);
+1 -5
View File
@@ -504,8 +504,6 @@ function getNthListSiblingIndex(node, parentNode) {
}
function getNthSiblingIndex(node, parentNode, condition) {
condition = condition || (() => true);
let index = -1;
for (const childNode of parentNode.children) {
@@ -698,9 +696,7 @@ function printRoot(path, options, print) {
});
}
function printChildren(path, options, print, events) {
events = events || {};
function printChildren(path, options, print, events = {}) {
const { postprocessor } = events;
const processor = events.processor || ((childPath) => childPath.call(print));
+1 -3
View File
@@ -204,8 +204,6 @@ function getFencedCodeBlockValue(node, originalText) {
function mapAst(ast, handler) {
return (function preorder(node, index, parentStack) {
parentStack = parentStack || [];
const newNode = { ...handler(node, index, parentStack) };
if (newNode.children) {
newNode.children = newNode.children.map((child, index) =>
@@ -214,7 +212,7 @@ function mapAst(ast, handler) {
}
return newNode;
})(ast, null, null);
})(ast, null, []);
}
function isAutolink(node) {
+24 -20
View File
@@ -36,13 +36,11 @@ function attachComments(text, ast, opts) {
return astComments;
}
function coreFormat(originalText, opts, addAlignmentSize) {
function coreFormat(originalText, opts, addAlignmentSize = 0) {
if (!originalText || originalText.trim().length === 0) {
return { formatted: "", cursorOffset: -1 };
}
addAlignmentSize = addAlignmentSize || 0;
const { ast, text } = parser.parse(originalText, opts);
if (opts.cursorOffset >= 0) {
@@ -181,8 +179,7 @@ function formatRange(originalText, opts) {
let { cursorOffset } = opts;
if (cursorOffset >= rangeEnd) {
// handle the case where the cursor was past the end of the range
cursorOffset =
opts.cursorOffset + (rangeTrimmed.length - rangeString.length);
cursorOffset += rangeTrimmed.length - rangeString.length;
} else if (rangeResult.cursorOffset >= 0) {
// handle the case where the cursor was in the range
cursorOffset = rangeResult.cursorOffset + rangeStart;
@@ -272,22 +269,21 @@ function normalizeInputAndOptions(text, options) {
};
}
function format(originalText, originalOptions) {
function hasPragma(text, options) {
const selectedParser = parser.resolveParser(options);
return !selectedParser.hasPragma || selectedParser.hasPragma(text);
}
function formatWithCursor(originalText, originalOptions) {
let { hasBOM, text, options } = normalizeInputAndOptions(
originalText,
normalizeOptions(originalOptions)
);
if (options.rangeStart >= options.rangeEnd && text !== "") {
return {
formatted: originalText,
cursorOffset: originalOptions.cursorOffset,
};
}
const selectedParser = parser.resolveParser(options);
const hasPragma = !selectedParser.hasPragma || selectedParser.hasPragma(text);
if (options.requirePragma && !hasPragma) {
if (
(options.rangeStart >= options.rangeEnd && text !== "") ||
(options.requirePragma && !hasPragma(text, options))
) {
return {
formatted: originalText,
cursorOffset: originalOptions.cursorOffset,
@@ -299,7 +295,12 @@ function format(originalText, originalOptions) {
if (options.rangeStart > 0 || options.rangeEnd < text.length) {
result = formatRange(text, options);
} else {
if (!hasPragma && options.insertPragma && options.printer.insertPragma) {
if (
!options.requirePragma &&
options.insertPragma &&
options.printer.insertPragma &&
!hasPragma(text, options)
) {
text = options.printer.insertPragma(text);
}
result = coreFormat(text, options);
@@ -317,7 +318,8 @@ function format(originalText, originalOptions) {
}
module.exports = {
formatWithCursor: format,
formatWithCursor,
parse(originalText, originalOptions, massage) {
const { text, options } = normalizeInputAndOptions(
originalText,
@@ -338,8 +340,10 @@ module.exports = {
// Doesn't handle shebang for now
formatDoc(doc, options) {
return format(printDocToDebug(doc), { ...options, parser: "babel" })
.formatted;
return formatWithCursor(printDocToDebug(doc), {
...options,
parser: "babel",
}).formatted;
},
printToDoc(originalText, options) {
+1 -3
View File
@@ -18,9 +18,7 @@ const hiddenDefaults = {
};
// Copy options and fill in default values.
function normalize(options, opts) {
opts = opts || {};
function normalize(options, opts = {}) {
const rawOptions = { ...options };
const supportOptions = getSupportInfo({
+1 -3
View File
@@ -27,9 +27,7 @@ function getParsers(options) {
return parsers;
}
function resolveParser(opts, parsers) {
parsers = parsers || getParsers(opts);
function resolveParser(opts, parsers = getParsers(opts)) {
if (typeof opts.parser === "function") {
// Custom parser API always works with JavaScript.
return {
+1 -2
View File
@@ -2,8 +2,7 @@
const raw = require("jest-snapshot-serializer-raw").wrap;
function printSeparator(width, description) {
description = description || "";
function printSeparator(width, description = "") {
const leftLength = Math.floor((width - description.length) / 2);
const rightLength = width - leftLength - description.length;
return "=".repeat(leftLength) + description + "=".repeat(rightLength);
-2
View File
@@ -116,8 +116,6 @@ function runPrettier(dir, args = [], options = {}) {
const result = { status, stdout, stderr, write };
const testResult = (testOptions) => {
testOptions = testOptions || {};
for (const name of Object.keys(result)) {
test(`(${name})`, () => {
const value =