From 29627ee520307494c45073701b20ef64effd3a8f Mon Sep 17 00:00:00 2001 From: James Long Date: Tue, 29 Nov 2016 12:14:10 -0500 Subject: [PATCH] initial --- .gitignore | 1 + bin/jscodefmt | 42 ++++++++++++++++++++++++++++++++++++++++++ package.json | 9 +++++++++ 3 files changed, 52 insertions(+) create mode 100644 .gitignore create mode 100755 bin/jscodefmt create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..3c3629e64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/bin/jscodefmt b/bin/jscodefmt new file mode 100755 index 000000000..cf0753837 --- /dev/null +++ b/bin/jscodefmt @@ -0,0 +1,42 @@ +#!/usr/bin/env node + +const fs = require("fs"); +const argv = require("minimist")(process.argv.slice(2)); +const recast = require("recast"); +const babylon = require("babylon"); + +const filename = argv["_"][0]; +const printWidth = argv['print-width'] || 80; +const tabWidth = argv['tab-width'] || 2; + +var babylonOptions = { + sourceType: 'module', + allowImportExportEverywhere: false, + allowReturnOutsideFunction: false, + plugins: [ + 'asyncFunctions', + 'asyncGenerators', + 'classConstructorCall', + 'classProperties', + 'decorators', + 'doExpressions', + 'exponentiationOperator', + 'exportExtensions', + 'flow', + 'functionSent', + 'functionBind', + 'jsx', + 'objectRestSpread', + 'trailingFunctionCommas' + ] +}; + +const ast = recast.parse(fs.readFileSync(filename, "utf8"), { + parser: { + parse: function(source) { + return babylon.parse(source, babylonOptions); + } + } +}); + +console.log(recast.prettyPrint(ast, { tabWidth, wrapColumn: printWidth }).code); diff --git a/package.json b/package.json new file mode 100644 index 000000000..2b4a501ea --- /dev/null +++ b/package.json @@ -0,0 +1,9 @@ +{ + "title": "jscodefmt", + "bin": { "jscodefmt": "./bin/jscodefmt" }, + "dependencies": { + "babylon": "^6.14.1", + "minimist": "^1.2.0", + "recast": "git+https://github.com/jlongster/recast.git#print-all!" + } +}