UTF8-BOM - if input string has BOM that mean output string also should have it (#3283)

The issue that prettier lost BOM and mean convert UTF-8 with BOM to just UTF-8
master
Anton 2017-11-22 00:38:21 +03:00 committed by suchipi
parent 759953ef6d
commit a0c95cf862
7 changed files with 23 additions and 18 deletions

View File

@ -1,6 +1,5 @@
"use strict";
const stripBom = require("strip-bom");
const comments = require("./src/comments");
const version = require("./package.json").version;
const printAstToDoc = require("./src/printer").printAstToDoc;
@ -68,7 +67,11 @@ function formatWithCursor(text, opts, addAlignmentSize) {
return { formatted: text };
}
text = stripBom(text);
const UTF8BOM = 0xfeff;
const hasUnicodeBOM = text.charCodeAt(0) === UTF8BOM;
if (hasUnicodeBOM) {
text = text.substring(1);
}
if (
opts.insertPragma &&
@ -110,7 +113,10 @@ function formatWithCursor(text, opts, addAlignmentSize) {
const doc = printAstToDoc(ast, opts, addAlignmentSize);
opts.newLine = guessLineEnding(text);
const toStringResult = printDocToString(doc, opts);
const str = toStringResult.formatted;
let str = toStringResult.formatted;
if (hasUnicodeBOM) {
str = String.fromCharCode(UTF8BOM) + str;
}
const cursorOffsetResult = toStringResult.cursor;
ensureAllCommentsPrinted(astComments);
// Remove extra leading indentation as well as the added indentation after last newline

View File

@ -46,7 +46,6 @@
"remark-parse": "4.0.0",
"semver": "5.4.1",
"string-width": "2.1.1",
"strip-bom": "3.0.0",
"typescript": "2.5.3",
"typescript-eslint-parser": "git://github.com/eslint/typescript-eslint-parser.git#9c71a627da36e97da52ed2731d58509c952b67ae",
"unicode-regex": "1.0.1",

View File

@ -10,7 +10,7 @@ html {
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/* Block comment */
/* Block comment */
html {
content: "#{1}";

View File

@ -74,7 +74,7 @@ var b = (<any>new a);
var b = (<any>new a.b);
var b = (<any>new a).b
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class a {
class a {
static b: any;
}
@ -200,7 +200,7 @@ exports[`commentInNamespaceDeclarationWithIdentifierPathName.ts 1`] = `
function foo() {}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
namespace hello.hi.world {
namespace hello.hi.world {
function foo() {
}
}

View File

@ -4,7 +4,7 @@ exports[`templateStringWithEmbeddedTypeAssertionOnAdditionES6.ts 1`] = `
// @target: ES6
var x = \`abc\${ <any>(10 + 10) }def\`;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// @target: ES6
// @target: ES6
var x = \`abc\${<any>(10 + 10)}def\`;
`;

View File

@ -27,7 +27,7 @@ unionTuple = unionTuple2;
unionTuple2 = unionTuple;
numStrTuple = unionTuple3;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// no error
// no error
var numStrTuple: [number, string] = [5, "hello"];
var numStrTuple2: [number, string] = [5, "foo", true];
var numStrBoolTuple: [number, string, boolean] = [5, "foo", true];
@ -89,7 +89,7 @@ var eleUnion24 = unionTuple2[idx1]; // string | number | boolean
var eleUnion25 = unionTuple2["0"]; // boolean
var eleUnion26 = unionTuple2["1"]; // string | number
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var strNumTuple: [string, number] = ["foo", 10];
var strNumTuple: [string, number] = ["foo", 10];
var numTupleTuple: [number, [string, number]] = [10, ["bar", 20]];
var unionTuple1: [number, string | number] = [10, "foo"];
var unionTuple2: [boolean, string | number] = [true, "foo"];
@ -178,7 +178,7 @@ var zipResultEle = zipResult[0]; // [string, number]
var zipResultEleEle = zipResult[0][0]; // string
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function combine<T, U>(x: T, y: U): [T, U] {
function combine<T, U>(x: T, y: U): [T, U] {
return [x, y];
}

View File

@ -76,7 +76,7 @@ var unionWithRestParameter4: { (...a: string[]): string; } | { (a: string, b: st
strOrNum = unionWithRestParameter4("hello"); // error supplied parameters do not match any call signature
strOrNum = unionWithRestParameter4("hello", "world");
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var numOrDate: number | Date;
var numOrDate: number | Date;
var strOrBoolean: string | boolean;
var strOrNum: string | number;
@ -188,7 +188,7 @@ var fUnion: typeof f1 | typeof f2 | typeof f3 | typeof f4 | typeof f5 | typeof f
fUnion(""); // All constituents can be called by passing a single string.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function f1(s: string) {}
function f1(s: string) {}
function f2(s?: string) {}
function f3(...s: string[]) {}
function f4(s: string, s2?: string) {}
@ -236,7 +236,7 @@ f12345("a"); // error
f12345("a", "b");
f12345("a", "b", "c"); // error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
type F1 = (a: string, b?: string) => void;
type F1 = (a: string, b?: string) => void;
type F2 = (a: string, b?: string, c?: string) => void;
type F3 = (a: string, ...rest: string[]) => void;
type F4 = (a: string, b?: string, ...rest: string[]) => void;
@ -336,7 +336,7 @@ strOrNum = new unionWithRestParameter3('hello', 10, 11); // error no call signat
strOrNum = new unionWithRestParameter3('hello', "hello"); // error no call signature
strOrNum = new unionWithRestParameter3(); // error no call signature
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var numOrDate: number | Date;
var numOrDate: number | Date;
var strOrBoolean: string | boolean;
var strOrNum: string | number;
@ -451,7 +451,7 @@ var BC : number | boolean;
var z1: typeof AB | boolean;
var z1: string | typeof BC;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// A | B is equivalent to A if B is a subtype of A
// A | B is equivalent to A if B is a subtype of A
class C {}
class D extends C {
foo() {}
@ -499,7 +499,7 @@ var arr7 = [c, d, e]; // (C | D)[]
var arr8 = [c, e]; // C[]
var arr9 = [e, f]; // (E|F)[]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// The resulting type an array literal expression is determined as follows:
// The resulting type an array literal expression is determined as follows:
// If the array literal is empty, the resulting type is an array type with the element type Undefined.
// Otherwise, if the array literal is contextually typed by a type that has a property with the numeric name 0, the resulting type is a tuple type constructed from the types of the element expressions.
// Otherwise, the resulting type is an array type with an element type that is the union of the types of the element expressions.
@ -558,7 +558,7 @@ var unionOfTypesWithAndWithoutStringSignature1: { [a: number]: number; } | boole
anyVar = unionOfTypesWithAndWithoutStringSignature1["hello"]; // any
anyVar = unionOfTypesWithAndWithoutStringSignature1[10]; // any
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var numOrDate: number | Date;
var numOrDate: number | Date;
var anyVar: number;
// If each type in U has a string index signature,