Improve requirePragma on files with BOM (#9003)

This commit is contained in:
fisker Cheung authored and GitHub committed 2020-08-21 10:30:42 +08:00
1 parent 86ad299768
commit 1d2ff824cc
6 files changed
+85 -36

No files matched your search

+41 -35
View File
@@ -36,7 +36,7 @@ function attachComments(text, ast, opts) {
function coreFormat(text, opts, addAlignmentSize) {
if (!text || !text.trim().length) {
return { formatted: "", cursorOffset: 0 };
return { formatted: "", cursorOffset: -1 };
}
addAlignmentSize = addAlignmentSize || 0;
@@ -138,7 +138,7 @@ function coreFormat(text, opts, addAlignmentSize) {
return { formatted: result.formatted, cursorOffset };
}
return { formatted: result.formatted };
return { formatted: result.formatted, cursorOffset: -1 };
}
function formatRange(text, opts) {
@@ -186,7 +186,7 @@ function formatRange(text, opts) {
// handle the case where the cursor was past the end of the range
cursorOffset =
opts.cursorOffset + (rangeTrimmed.length - rangeString.length);
} else if (rangeResult.cursorOffset !== undefined) {
} else if (rangeResult.cursorOffset >= 0) {
// handle the case where the cursor was in the range
cursorOffset = rangeResult.cursorOffset + rangeStart;
}
@@ -215,21 +215,41 @@ function countCrlf(text) {
return match ? match.length : 0;
}
function format(text, opts) {
function format(originalText, opts) {
const selectedParser = parser.resolveParser(opts);
const hasBOM = originalText.charAt(0) === BOM;
let text = hasBOM ? originalText.slice(1) : originalText;
const hasCursor = opts.cursorOffset >= 0;
if (!hasCursor) {
opts.cursorOffset = -1;
}
const hasPragma = !selectedParser.hasPragma || selectedParser.hasPragma(text);
if (opts.requirePragma && !hasPragma) {
return { formatted: text };
return { formatted: originalText, cursorOffset: opts.cursorOffset };
}
if (opts.endOfLine === "auto") {
opts.endOfLine = guessEndOfLine(text);
}
const hasCursor = opts.cursorOffset >= 0;
const hasRangeStart = opts.rangeStart > 0;
const hasRangeEnd = opts.rangeEnd < text.length;
if (hasBOM) {
if (hasCursor) {
opts.cursorOffset--;
}
if (hasRangeStart) {
opts.rangeStart--;
}
if (hasRangeEnd) {
opts.rangeEnd--;
}
}
// get rid of CR/CRLF parsing
if (text.includes("\r")) {
if (hasCursor) {
@@ -245,24 +265,6 @@ function format(text, opts) {
text = text.replace(/\r\n?/g, "\n");
}
const hasUnicodeBOM = text.charAt(0) === BOM;
if (hasUnicodeBOM) {
text = text.slice(1);
if (hasCursor) {
opts.cursorOffset--;
}
if (hasRangeStart) {
opts.rangeStart--;
}
if (hasRangeEnd) {
opts.rangeEnd--;
}
}
if (!hasCursor) {
opts.cursorOffset = -1;
}
/* istanbul ignore next */
if (opts.rangeStart < 0) {
opts.rangeStart = 0;
}
@@ -270,20 +272,21 @@ function format(text, opts) {
opts.rangeEnd = text.length;
}
const result =
hasRangeStart || hasRangeEnd
? formatRange(text, opts)
: coreFormat(
opts.insertPragma && opts.printer.insertPragma && !hasPragma
? opts.printer.insertPragma(text)
: text,
opts
);
let result;
if (hasUnicodeBOM) {
if (hasRangeStart || hasRangeEnd) {
result = formatRange(text, opts);
} else {
if (!hasPragma && opts.insertPragma && opts.printer.insertPragma) {
text = opts.printer.insertPragma(text);
}
result = coreFormat(text, opts);
}
if (hasBOM) {
result.formatted = BOM + result.formatted;
if (hasCursor) {
if (hasCursor && result.cursorOffset >= 0) {
result.cursorOffset++;
}
}
@@ -299,6 +302,9 @@ module.exports = {
parse(text, opts, massage) {
opts = normalizeOptions(opts);
if (text.charAt(0) === BOM) {
text = text.slice(1);
}
if (text.includes("\r")) {
text = text.replace(/\r\n?/g, "\n");
}
@@ -0,0 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`cursor-without-pragma.js - {"requirePragma":true} format 1`] = `
====================================options=====================================
parsers: ["babel", "typescript", "flow"]
printWidth: 80
requirePragma: true
| printWidth
=====================================input======================================
(function() {return <|> 15})()
=====================================output=====================================
(function() {return <|> 15})()
================================================================================
`;
@@ -0,0 +1 @@
(function() {return <|> 15})()
@@ -0,0 +1 @@
run_spec(__dirname, ["babel", "typescript", "flow"], { requirePragma: true });
@@ -154,3 +154,21 @@ function test() { const answer = 42;console.log("%s is the answer to life, the u
================================================================================
`;
exports[`snippet: #0 - {"requirePragma":true} format 1`] = `
====================================options=====================================
parsers: ["flow", "babel", "typescript"]
printWidth: 80
requirePragma: true
| printWidth
=====================================input======================================
#!/usr/bin/env node
/** @format */
prettier
=====================================output=====================================
#!/usr/bin/env node
/** @format */
prettier;
================================================================================
`;
+8 -1
View File
@@ -1 +1,8 @@
run_spec(__dirname, ["flow", "babel", "typescript"], { requirePragma: true });
run_spec(
{
dirname: __dirname,
snippets: ["\uFEFF#!/usr/bin/env node\n/** @format */\nprettier"],
},
["flow", "babel", "typescript"],
{ requirePragma: true }
);