For formatting, searching, and rewriting JavaScript. Analogous to gofmt
.
npm install -g jsfmt
$ jsfmt --help
Usage:
jsfmt [--no-format] [--save-ast] [--diff|--list|--write] [--validate] [--rewrite PATTERN|--search PATTERN] [--json|--ast] [<file>...]
jsfmt (--version | --help)
Options:
-h --help Show this help text
--version Show jsfmt version
-d --diff Show diff against original file
-l --list List the files which differ from jsfmt output
-v --validate Validate the input file(s)
--no-format Do not format the input file(s)
-w --write Overwrite the original file with jsfmt output
-j --json Tell jsfmt that the file being parsed is json
-a --ast Tell jsfmt that the file being parsed is in JSON AST
--save-ast Output the resulting js in JSON AST format
-r=PATTERN --rewrite PATTERN Rewrite rule (e.g., 'a.slice(b, len(a) -> a.slice(b)')
-s=PATTERN --search PATTERN Search rule (e.g., 'a.slice')
If no path is given it will read from stdin
. A directory path will recurse over all *.js files in the directory.
Note that the AST options (--ast
and --save-ast
) are experimental and may be removed.
For formatting jsfmt
uses esformatter.
Any of the esformatter formatting
options can be overwritten via a .jsfmtrc
file. The file is parsed using
rc, which accepts either a json
or ini
formatted file.
A .jsfmtrc
will be read if it exists in any of the following directories:
jsfmt
will also attempt to pickup and use the configured indent
variable from your .jshintrc
configuration file, if present.
The --rewrite
flag allows rewriting portions of the JavaScript's AST before formatting. This is especially handy for intelligent renaming and handling API changes from a library. The rewrite rule must be a string of the form:
pattern -> replacement
Both pattern
and replacement
must be valid JavaScript. In pattern
, single-character lowercase identifiers serve as wildcards matching arbitrary expressions; those expressions will be substituted for the same identifiers in the replacement
.
Rewrite occurences of _.reduce
to use native reduce:
jsfmt --rewrite "_.reduce(a, b, c) -> a.reduce(b, c)" reduce.js
The --search
flag allows searching through a JavaScript's AST. The search rule is very similar to the rewrite rule but just outputs expressions that match the given search expression. The search expression must be valid JavaScript.
Find occurences of _.reduce
:
jsfmt --search "_.reduce(a, b, c)" reduce.js
The --validate
flag will print any errors found by esprima while parsing the JavaScript.
jsfmt --validate bad.js
jsfmt.format(<javascript_string>, <config_object>) // Returns formatted JavaScript
jsfmt.formatJSON(<JSON_string>, <config_object>) // Returns formatted JSON
var config = jsfmt.getConfig(); // Loads the jsfmt config from the appropriate rc file or default config object
var jsfmt = require('jsfmt');
var fs = require('fs');
var js = fs.readFileSync('unformatted.js');
var config = jsfmt.getConfig();
js = jsfmt.format(js, config);
jsfmt.rewrite(<javascript_string>, <rewrite_rule>) // Returns rewritten JavaScript
var jsfmt = require('jsfmt');
var fs = require('fs');
var js = fs.readFileSync('each.js');
js = jsfmt.rewrite(js, "_.each(a, b) -> a.forEach(b)");
jsfmt.search(<javascript_string>, <search_expression>) // Returns array of matches
var jsfmt = require('jsfmt');
var fs = require('fs');
var js = fs.readFileSync('component.js');
jsfmt.search(js, "R.Component.create(a, { dependencies: z })").forEach(function(matches, wildcards) {
console.log(wildcards.z);
});
jsfmt.validate(<javascript_string>) // Returns errors found while parsing JavaScript
jsfmt.validateJSON(<JSON_string>) // Returns errors found while parsing JSON
var jsfmt = require('jsfmt');
var fs = require('fs');
var js = fs.readFileSync('each.js');
var errors = jsfmt.validate(js);
for (var i = 0; i < errors.length; i++) {
console.error(errors[i]);
}
Since jsfmt
uses esformatter
under the covers for formatting your code you can utilize any esformatter
plugins with jsfmt
. Please see https://github.com/millermedeiros/esformatter/#plugins for more information.
There exists a plugin esformatter-jsx which provides support for formatting JSX with esformatter
. Please see https://github.com/royriojas/esformatter-jsx/wiki/Usage-with-jsfmt for more information on setting up with jsfmt
.
--config
option in favor of .jsfmtrc and better docs around rc.-1
on missing arg failure.rc
and other dependencies.UnaryExpression
jsfmt.getConfig
api method for loading jsfmt configjsfmt.format(js[, options])
api method for formatting--validate
option and exposed jsfmt.validate
api method--config config.json
support for formatting configuration--format
the default action#!/usr/bin/env node
-b
git diff optionApache License, Version 2.0. Copyright 2014 Rdio, Inc.