Add flow parser as an option, default to babylon

master
James Long 2016-12-30 21:23:50 -05:00
parent 7ea2348b03
commit 0465bb5790
4 changed files with 25 additions and 19 deletions

View File

@ -7,8 +7,10 @@ const jscodefmt = require("../index");
const filename = argv["_"][0]; const filename = argv["_"][0];
const printWidth = argv['print-width'] || 80; const printWidth = argv['print-width'] || 80;
const tabWidth = argv['tab-width'] || 2; const tabWidth = argv['tab-width'] || 2;
const useFlowParser = argv['flow-parser'];
console.log(jscodefmt.format(fs.readFileSync(filename, "utf8"), { console.log(jscodefmt.format(fs.readFileSync(filename, "utf8"), {
printWidth, printWidth,
tabWidth tabWidth,
useFlowParser
})); }));

View File

@ -28,21 +28,26 @@ var babylonOptions = {
module.exports = { module.exports = {
format: function(text, opts={}) { format: function(text, opts={}) {
let { tabWidth = 2, printWidth = 80 } = opts; let { tabWidth = 2, printWidth = 80 } = opts;
let ast;
// const ast = recast.parse(text, { if(opts.useFlowParser) {
// parser: { ast = flowParser.parse(text);
// parse: function(source) { if(ast.errors.length > 0) {
// return babylon.parse(source, babylonOptions); let msg = ast.errors[0].message + " on line " + ast.errors[0].loc.start.line
// } if(opts.filename) {
// } msg += " in file " + opts.filename;
// }); }
const ast = flowParser.parse(text); throw new Error(msg);
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); }
else {
ast = recast.parse(text, {
parser: {
parse: function(source) {
return babylon.parse(source, babylonOptions);
}
}
});
} }
const printer = new Printer({ tabWidth, wrapColumn: printWidth }); const printer = new Printer({ tabWidth, wrapColumn: printWidth });

View File

@ -9,12 +9,12 @@
"ast-types": "git+https://github.com/jlongster/ast-types.git", "ast-types": "git+https://github.com/jlongster/ast-types.git",
"babylon": "git+https://github.com/jlongster/babylon.git#published", "babylon": "git+https://github.com/jlongster/babylon.git#published",
"minimist": "^1.2.0", "minimist": "^1.2.0",
"recast": "^0.11.18" "recast": "^0.11.18",
"flow-parser": "^0.37.0"
}, },
"devDependencies": { "devDependencies": {
"glob": "^7.1.1", "glob": "^7.1.1",
"jest": "^18.0.0", "jest": "^18.0.0"
"flow-parser": "^0.37.0"
}, },
"scripts": { "scripts": {
"test": "jest" "test": "jest"

View File

@ -1,5 +1,4 @@
const fs = require('fs'); const fs = require('fs');
const child_process = require('child_process');
const jscodefmt = require("../"); const jscodefmt = require("../");
const recast = require("recast"); const recast = require("recast");
const types = require("ast-types"); const types = require("ast-types");
@ -81,7 +80,7 @@ function parse(string) {
} }
function prettyprint(src, filename) { function prettyprint(src, filename) {
return jscodefmt.format(src, { filename }); return jscodefmt.format(src, { filename, useFlowParser: true });
} }
function read(filename) { function read(filename) {