From a0c95cf862b5a31aadd5837dbe867dfd143f35c2 Mon Sep 17 00:00:00 2001 From: Anton Date: Wed, 22 Nov 2017 00:38:21 +0300 Subject: [PATCH] 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 --- index.js | 12 +++++++++--- package.json | 1 - tests/css_bom/__snapshots__/jsfmt.spec.js.snap | 2 +- .../compiler/__snapshots__/jsfmt.spec.js.snap | 4 ++-- .../es6/templates/__snapshots__/jsfmt.spec.js.snap | 2 +- .../types/tuple/__snapshots__/jsfmt.spec.js.snap | 6 +++--- .../types/union/__snapshots__/jsfmt.spec.js.snap | 14 +++++++------- 7 files changed, 23 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index 87086d3d8..4a612e360 100644 --- a/index.js +++ b/index.js @@ -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 diff --git a/package.json b/package.json index 78cf2b09d..c51b71aa9 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/tests/css_bom/__snapshots__/jsfmt.spec.js.snap b/tests/css_bom/__snapshots__/jsfmt.spec.js.snap index ede957ff9..8b6d00c05 100644 --- a/tests/css_bom/__snapshots__/jsfmt.spec.js.snap +++ b/tests/css_bom/__snapshots__/jsfmt.spec.js.snap @@ -10,7 +10,7 @@ html { } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -/* Block comment */ +/* Block comment */ html { content: "#{1}"; diff --git a/tests/typescript/compiler/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/compiler/__snapshots__/jsfmt.spec.js.snap index 2bbbee092..897b49b07 100644 --- a/tests/typescript/compiler/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/compiler/__snapshots__/jsfmt.spec.js.snap @@ -74,7 +74,7 @@ var b = (new a); var b = (new a.b); var b = (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() { } } diff --git a/tests/typescript/conformance/es6/templates/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/conformance/es6/templates/__snapshots__/jsfmt.spec.js.snap index 20414ca7a..73f046cd2 100644 --- a/tests/typescript/conformance/es6/templates/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/conformance/es6/templates/__snapshots__/jsfmt.spec.js.snap @@ -4,7 +4,7 @@ exports[`templateStringWithEmbeddedTypeAssertionOnAdditionES6.ts 1`] = ` // @target: ES6 var x = \`abc\${ (10 + 10) }def\`; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -// @target: ES6 +// @target: ES6 var x = \`abc\${(10 + 10)}def\`; `; diff --git a/tests/typescript/conformance/types/tuple/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/conformance/types/tuple/__snapshots__/jsfmt.spec.js.snap index 6f0b9e39b..226e8de1d 100644 --- a/tests/typescript/conformance/types/tuple/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/conformance/types/tuple/__snapshots__/jsfmt.spec.js.snap @@ -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(x: T, y: U): [T, U] { +function combine(x: T, y: U): [T, U] { return [x, y]; } diff --git a/tests/typescript/conformance/types/union/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/conformance/types/union/__snapshots__/jsfmt.spec.js.snap index 5d80e2e35..851ef589e 100644 --- a/tests/typescript/conformance/types/union/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/conformance/types/union/__snapshots__/jsfmt.spec.js.snap @@ -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,