Fix wrong range format on files with BOM (#8936)

This commit is contained in:
fisker Cheung authored and GitHub committed 2020-08-10 22:18:55 +08:00
1 parent 69d26582e7
commit 6833310f87
4 files changed
+125 -3

No files matched your search

+28
View File
@@ -0,0 +1,28 @@
#### Fix wrong range format on files with BOM ([#8936](https://github.com/prettier/prettier/pull/8936) by [@fisker](https://github.com/fisker))
When files have BOM, in previous version, the real range is wrongly calculated.
<!-- prettier-ignore -->
```js
const text = "\uFEFF" + "foo = 1.0000;bar = 1.0000;";
// ^^^^^^^^^^^^^ Range
const result = require("prettier")
.format(text, {
parser: "babel",
rangeStart: 1,
rangeEnd: 13,
})
// Visualize BOM
.replace("\uFEFF", "<<BOM>>")
// Visualize EOL
.replace("\n", "<<EOL>>");
console.log(result);
// Prettier stable
// -> <<BOM>>foo = 1.0;<<EOL>>bar = 1.0;
// ^^^^^^^^^ This part should not be formatted
// Prettier master
// -> <<BOM>>foo = 1.0;bar = 1.0000;
```
+3 -3
View File
@@ -284,13 +284,13 @@ function format(text, opts) {
if (hasUnicodeBOM) {
text = text.slice(1);
if (hasCursor) {
opts.cursorOffset++;
opts.cursorOffset--;
}
if (hasRangeStart) {
opts.rangeStart++;
opts.rangeStart--;
}
if (hasRangeEnd) {
opts.rangeEnd++;
opts.rangeEnd--;
}
}
@@ -0,0 +1,75 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`snippet: cursor-1.js format 1`] = `
====================================options=====================================
parsers: ["babel"]
printWidth: 80
| printWidth
=====================================input======================================
(function(){return <|>15})()
=====================================output=====================================
(function () {
return <|>15;
})();
================================================================================
`;
exports[`snippet: range-1.js format 1`] = `
====================================options=====================================
parsers: ["babel"]
printWidth: 80
| | printWidth
=====================================input======================================
1 | 
2 |
> 3 | class a {
| ^^^^
> 4 | b( ) {}
| ^^^^^^^^^^^
5 | }
6 |
7 | let x
8 |
=====================================output=====================================

class a {
b() {}
}
let x
================================================================================
`;
exports[`snippet: range-and-cursor-1.js format 1`] = `
====================================options=====================================
parsers: ["babel"]
printWidth: 80
| | printWidth
=====================================input======================================
1 | 
2 |
3 |
> 4 | class a {
| ^^^^
> 5 | b( <|> ) {}
| ^^^^^^^^^^^^^^
6 | }
7 |
8 | let x
9 |
=====================================output=====================================

class a {
b(<|>) {}
}
let x
================================================================================
`;
+19
View File
@@ -0,0 +1,19 @@
const fs = require("fs");
const path = require("path");
const fixtureDirectory = path.join(__dirname, "../eol");
const snippets = fs
.readdirSync(fixtureDirectory)
.filter(
(fileName) => fileName !== "__snapshots__" && fileName !== "jsfmt.spec.js"
)
.map((fileName) => {
const file = path.join(fixtureDirectory, fileName);
const code = "\uFEFF" + fs.readFileSync(file, "utf8");
return {
name: fileName,
code,
};
});
run_spec({ dirname: __dirname, snippets }, ["babel"]);