From 0465bb579065834e386a1b4f6fdcbbd3439ced49 Mon Sep 17 00:00:00 2001 From: James Long Date: Fri, 30 Dec 2016 21:23:50 -0500 Subject: [PATCH] Add flow parser as an option, default to babylon --- bin/jscodefmt | 4 +++- index.js | 31 ++++++++++++++++++------------- package.json | 6 +++--- tests_config/run_spec.js | 3 +-- 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/bin/jscodefmt b/bin/jscodefmt index 69dd7871..0f171c39 100755 --- a/bin/jscodefmt +++ b/bin/jscodefmt @@ -7,8 +7,10 @@ const jscodefmt = require("../index"); const filename = argv["_"][0]; const printWidth = argv['print-width'] || 80; const tabWidth = argv['tab-width'] || 2; +const useFlowParser = argv['flow-parser']; console.log(jscodefmt.format(fs.readFileSync(filename, "utf8"), { printWidth, - tabWidth + tabWidth, + useFlowParser })); diff --git a/index.js b/index.js index ab81a9f9..52152b4a 100644 --- a/index.js +++ b/index.js @@ -28,21 +28,26 @@ var babylonOptions = { module.exports = { format: function(text, opts={}) { let { tabWidth = 2, printWidth = 80 } = opts; + let ast; - // const ast = recast.parse(text, { - // parser: { - // parse: function(source) { - // return babylon.parse(source, babylonOptions); - // } - // } - // }); - const ast = flowParser.parse(text); - if(ast.errors.length > 0) { - let msg = ast.errors[0].message + " on line " + ast.errors[0].loc.start.line - if(opts.filename) { - msg += " in file " + opts.filename; + if(opts.useFlowParser) { + ast = flowParser.parse(text); + if(ast.errors.length > 0) { + let msg = ast.errors[0].message + " on line " + ast.errors[0].loc.start.line + if(opts.filename) { + msg += " in file " + opts.filename; + } + throw new Error(msg); } - throw new Error(msg); + } + else { + ast = recast.parse(text, { + parser: { + parse: function(source) { + return babylon.parse(source, babylonOptions); + } + } + }); } const printer = new Printer({ tabWidth, wrapColumn: printWidth }); diff --git a/package.json b/package.json index 46c1061d..f495b970 100644 --- a/package.json +++ b/package.json @@ -9,12 +9,12 @@ "ast-types": "git+https://github.com/jlongster/ast-types.git", "babylon": "git+https://github.com/jlongster/babylon.git#published", "minimist": "^1.2.0", - "recast": "^0.11.18" + "recast": "^0.11.18", + "flow-parser": "^0.37.0" }, "devDependencies": { "glob": "^7.1.1", - "jest": "^18.0.0", - "flow-parser": "^0.37.0" + "jest": "^18.0.0" }, "scripts": { "test": "jest" diff --git a/tests_config/run_spec.js b/tests_config/run_spec.js index 11fe6055..2f4f123c 100644 --- a/tests_config/run_spec.js +++ b/tests_config/run_spec.js @@ -1,5 +1,4 @@ const fs = require('fs'); -const child_process = require('child_process'); const jscodefmt = require("../"); const recast = require("recast"); const types = require("ast-types"); @@ -81,7 +80,7 @@ function parse(string) { } function prettyprint(src, filename) { - return jscodefmt.format(src, { filename }); + return jscodefmt.format(src, { filename, useFlowParser: true }); } function read(filename) {