From ecb26b34322e21b0cc3240abcfb91bd434448da6 Mon Sep 17 00:00:00 2001 From: Jamie Webb Date: Fri, 13 Jan 2017 17:57:27 +0000 Subject: [PATCH] treat shebang outside of parsing (#137) * treat shebang outside of parsing * use less costly indexOf and reuse format function * avoid reprinting new line and potential double space at start * move options back to format function --- bin/prettier.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/bin/prettier.js b/bin/prettier.js index a5b6229f..f034c7e3 100755 --- a/bin/prettier.js +++ b/bin/prettier.js @@ -33,6 +33,13 @@ if (!filenames.length && !stdin) { process.exit(1); } +function formatWithShebang(input) { + const index = input.indexOf("\n"); + const shebang = input.slice(0, index + 1); + const programInput = input.slice(index + 1); + return shebang + format(programInput); +} + function format(input) { return jscodefmt.format(input, { printWidth: argv["print-width"], @@ -70,7 +77,7 @@ if (stdin) { let output; try { - output = format(input); + output = input.startsWith("#!") ? formatWithShebang(input) : format(input); } catch (e) { process.exitCode = 2; console.error(e);