diff --git a/src/common/parser-include-shebang.js b/src/common/parser-include-shebang.js deleted file mode 100644 index c9ba9f1dd..000000000 --- a/src/common/parser-include-shebang.js +++ /dev/null @@ -1,22 +0,0 @@ -"use strict"; - -const { getShebang } = require("./util"); - -function includeShebang(text, ast) { - const shebang = getShebang(text); - - if (!shebang) { - return; - } - const index = shebang.length; - - const comment = { - type: "Line", - value: shebang.slice(2), - range: [0, index], - }; - - ast.comments = [comment].concat(ast.comments); -} - -module.exports = includeShebang; diff --git a/src/language-js/parser-flow.js b/src/language-js/parser-flow.js index 1cafd8c4d..84d94363a 100644 --- a/src/language-js/parser-flow.js +++ b/src/language-js/parser-flow.js @@ -1,7 +1,6 @@ "use strict"; const createError = require("../common/parser-create-error"); -const includeShebang = require("../common/parser-include-shebang"); const { hasPragma } = require("./pragma"); const locFns = require("./loc"); const postprocess = require("./postprocess"); @@ -28,7 +27,6 @@ function parse(text, parsers, opts) { }); } - includeShebang(text, ast); return postprocess(ast, { ...opts, originalText: text }); } diff --git a/src/language-js/parser-typescript.js b/src/language-js/parser-typescript.js index c8726a23e..16e73384e 100644 --- a/src/language-js/parser-typescript.js +++ b/src/language-js/parser-typescript.js @@ -1,7 +1,6 @@ "use strict"; const createError = require("../common/parser-create-error"); -const includeShebang = require("../common/parser-include-shebang"); const { hasPragma } = require("./pragma"); const locFns = require("./loc"); const postprocess = require("./postprocess"); @@ -30,7 +29,6 @@ function parse(text, parsers, opts) { } } - includeShebang(text, ast); return postprocess(ast, { ...opts, originalText: text }); } diff --git a/src/language-js/postprocess.js b/src/language-js/postprocess.js index 2a1530539..8fa88084b 100644 --- a/src/language-js/postprocess.js +++ b/src/language-js/postprocess.js @@ -3,11 +3,16 @@ const { getLast, getNextNonSpaceNonCommentCharacter, + getShebang, } = require("../common/util"); const { composeLoc, locEnd } = require("./loc"); const { isTypeCastComment } = require("./comments"); function postprocess(ast, options) { + if (options.parser === "typescript" || options.parser === "flow") { + includeShebang(ast, options); + } + // Keep Babel's non-standard ParenthesizedExpression nodes only if they have Closure-style type cast comments. if (options.parser !== "typescript" && options.parser !== "flow") { const startOffsetsOfTypeCastedNodes = new Set(); @@ -178,4 +183,16 @@ function rebalanceLogicalTree(node) { }); } +function includeShebang(ast, options) { + const shebang = getShebang(options.originalText); + + if (shebang) { + ast.comments.unshift({ + type: "Line", + value: shebang.slice(2), + range: [0, shebang.length], + }); + } +} + module.exports = postprocess;