From 8ec543276838f66f0a84824a39c215a3039e9071 Mon Sep 17 00:00:00 2001 From: Aakansha Doshi Date: Wed, 6 Jun 2018 19:11:12 +0530 Subject: [PATCH] Fix empty file error in json and graphql (#4553) * Fix empty file error in json and graphql * fix AST_COMPARE=1 issue * use tests/empty * revert * fix lint --- src/language-js/printer-estree-json.js | 3 +++ src/main/core.js | 4 ++++ tests/empty/jsfmt.spec.js | 4 ---- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/language-js/printer-estree-json.js b/src/language-js/printer-estree-json.js index da349295..55f58a41 100644 --- a/src/language-js/printer-estree-json.js +++ b/src/language-js/printer-estree-json.js @@ -49,6 +49,9 @@ function genericPrint(path, options, print) { return JSON.stringify(node.value); case "Identifier": return JSON.stringify(node.name); + default: + /* istanbul ignore next */ + throw new Error("unknown type: " + JSON.stringify(node.type)); } } diff --git a/src/main/core.js b/src/main/core.js index 4c938182..040bd56d 100644 --- a/src/main/core.js +++ b/src/main/core.js @@ -63,6 +63,10 @@ function attachComments(text, ast, opts) { } function coreFormat(text, opts, addAlignmentSize) { + if (!text || !text.trim().length) { + return { formatted: "", cursorOffset: 0 }; + } + addAlignmentSize = addAlignmentSize || 0; const parsed = parser.parse(text, opts); diff --git a/tests/empty/jsfmt.spec.js b/tests/empty/jsfmt.spec.js index 07cfc489..1277371c 100644 --- a/tests/empty/jsfmt.spec.js +++ b/tests/empty/jsfmt.spec.js @@ -8,8 +8,4 @@ run_spec( parsers.choices .filter(choice => !choice.deprecated) .map(choice => choice.value) - .filter( - parser => - ["json", "json5", "json-stringify", "graphql"].indexOf(parser) === -1 - ) );