diff --git a/src/common/parser-include-shebang.js b/src/common/parser-include-shebang.js index b1423113a..0f15c2937 100644 --- a/src/common/parser-include-shebang.js +++ b/src/common/parser-include-shebang.js @@ -1,15 +1,18 @@ "use strict"; +const { getShebang } = require("./util"); + function includeShebang(text, ast) { - if (!text.startsWith("#!")) { + const shebang = getShebang(text); + + if (!shebang) { return; } + const index = shebang.length; - const index = text.indexOf("\n"); - const shebang = text.slice(2, index); const comment = { type: "Line", - value: shebang, + value: shebang.slice(2), range: [0, index], loc: { start: { diff --git a/src/common/util.js b/src/common/util.js index e1de6aa85..bca353a4d 100644 --- a/src/common/util.js +++ b/src/common/util.js @@ -844,6 +844,17 @@ function isFrontMatterNode(node) { return node && node.type === "front-matter"; } +function getShebang(text) { + if (!text.startsWith("#!")) { + return ""; + } + const index = text.indexOf("\n"); + if (index === -1) { + return text; + } + return text.slice(0, index); +} + module.exports = { replaceEndOfLineWith, getStringWidth, @@ -889,4 +900,5 @@ module.exports = { addTrailingComment, isWithinParentArrayProperty, isFrontMatterNode, + getShebang, }; diff --git a/src/language-js/pragma.js b/src/language-js/pragma.js index d5baf188f..bd1215872 100644 --- a/src/language-js/pragma.js +++ b/src/language-js/pragma.js @@ -1,18 +1,7 @@ "use strict"; const docblock = require("jest-docblock"); - -// Logic from `/src/common/parser-include-shebang.js` -function getShebang(text) { - if (!text.startsWith("#!")) { - return ""; - } - const index = text.indexOf("\n"); - if (index === -1) { - return ""; - } - return text.slice(0, index); -} +const { getShebang } = require("../common/util"); function hasPragma(text) { const shebang = getShebang(text); diff --git a/tests/js/shebang/__snapshots__/jsfmt.spec.js.snap b/tests/js/shebang/__snapshots__/jsfmt.spec.js.snap index 8221c950a..15f0edfe2 100644 --- a/tests/js/shebang/__snapshots__/jsfmt.spec.js.snap +++ b/tests/js/shebang/__snapshots__/jsfmt.spec.js.snap @@ -33,3 +33,30 @@ function a() {} ================================================================================ `; + +exports[`snippet: Empty file with shebang format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +#!/usr/bin/env node +=====================================output===================================== +#!/usr/bin/env node + +================================================================================ +`; + +exports[`snippet: Empty file with shebang format 2`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +#!/usr/bin/env node + +=====================================output===================================== +#!/usr/bin/env node + +================================================================================ +`; diff --git a/tests/js/shebang/jsfmt.spec.js b/tests/js/shebang/jsfmt.spec.js index eb85eda6b..20d7a86e3 100644 --- a/tests/js/shebang/jsfmt.spec.js +++ b/tests/js/shebang/jsfmt.spec.js @@ -1 +1,16 @@ -run_spec(__dirname, ["babel", "flow", "typescript"]); +run_spec( + { + dirname: __dirname, + snippets: [ + { + name: "Empty file with shebang", + code: "#!/usr/bin/env node", + }, + { + name: "Empty file with shebang", + code: "#!/usr/bin/env node\n", + }, + ], + }, + ["babel", "flow", "typescript"] +);