From 6f9824778dc7600e81c6237f9f23a7de82baf04b Mon Sep 17 00:00:00 2001 From: Lucas Azzola Date: Sun, 2 Jul 2017 02:11:13 +1000 Subject: [PATCH] Strip BOM before parsing, fixes #2362 (#2373) --- index.js | 2 ++ package.json | 1 + .../css_bom/__snapshots__/jsfmt.spec.js.snap | 19 +++++++++++++++++++ tests/css_bom/bom.css | 8 ++++++++ tests/css_bom/jsfmt.spec.js | 1 + 5 files changed, 31 insertions(+) create mode 100644 tests/css_bom/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/css_bom/bom.css create mode 100644 tests/css_bom/jsfmt.spec.js diff --git a/index.js b/index.js index 166cb11b6..ab22b25c1 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,6 @@ "use strict"; +const stripBom = require("strip-bom"); const comments = require("./src/comments"); const version = require("./package.json").version; const printAstToDoc = require("./src/printer").printAstToDoc; @@ -54,6 +55,7 @@ function ensureAllCommentsPrinted(astComments) { } function formatWithCursor(text, opts, addAlignmentSize) { + text = stripBom(text); addAlignmentSize = addAlignmentSize || 0; const ast = parser.parse(text, opts); diff --git a/package.json b/package.json index 367536f22..61b5cd5a8 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "rollup-plugin-node-resolve": "2.0.0", "rollup-plugin-replace": "1.1.1", "shelljs": "0.7.8", + "strip-bom": "3.0.0", "sw-toolbox": "3.6.0", "uglify-es": "3.0.15", "webpack": "2.6.1" diff --git a/tests/css_bom/__snapshots__/jsfmt.spec.js.snap b/tests/css_bom/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..ede957ff9 --- /dev/null +++ b/tests/css_bom/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,19 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`bom.css 1`] = ` + + +/* Block comment */ + +html { + content: "#{1}"; + +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +/* Block comment */ + +html { + content: "#{1}"; +} + +`; diff --git a/tests/css_bom/bom.css b/tests/css_bom/bom.css new file mode 100644 index 000000000..54a6e7f65 --- /dev/null +++ b/tests/css_bom/bom.css @@ -0,0 +1,8 @@ + + +/* Block comment */ + +html { + content: "#{1}"; + +} diff --git a/tests/css_bom/jsfmt.spec.js b/tests/css_bom/jsfmt.spec.js new file mode 100644 index 000000000..a7d303b6c --- /dev/null +++ b/tests/css_bom/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, { parser: "postcss" });