Add flow parser as an option, default to babylon
parent
7ea2348b03
commit
0465bb5790
|
@ -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
|
||||
}));
|
||||
|
|
31
index.js
31
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 });
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue