• Stars
    star
    1,694
  • Rank 27,544 (Top 0.6 %)
  • Language
    JavaScript
  • License
    Other
  • Created over 10 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

For formatting, searching, and rewriting JavaScript.

jsfmt

NPM version Build Status Dependency Status Coverage Status

For formatting, searching, and rewriting JavaScript. Analogous to gofmt.

Installation

npm install -g jsfmt

Usage

$ 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.

Formatting

For formatting jsfmt uses esformatter.

.jsfmtrc

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:

  • a local .jsfmtrc or the first found looking in ./ ../ ../../ ../../../ etc.
  • $HOME/.jsfmtrc
  • $HOME/.jsfmt/config
  • $HOME/.config/jsfmt
  • $HOME/.config/jsfmt/config
  • /etc/jsfmtrc
  • /etc/jsfmt/config

jsfmt will also attempt to pickup and use the configured indent variable from your .jshintrc configuration file, if present.

Rewriting

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.

Example

Rewrite occurences of _.reduce to use native reduce:

jsfmt --rewrite "_.reduce(a, b, c) -> a.reduce(b, c)" reduce.js

Searching

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.

Example

Find occurences of _.reduce:

jsfmt --search "_.reduce(a, b, c)" reduce.js

Validating

The --validate flag will print any errors found by esprima while parsing the JavaScript.

Example

jsfmt --validate bad.js

API

Formatting

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

Example

var jsfmt = require('jsfmt');
var fs = require('fs');

var js = fs.readFileSync('unformatted.js');
var config = jsfmt.getConfig();

js = jsfmt.format(js, config);

Rewriting

jsfmt.rewrite(<javascript_string>, <rewrite_rule>) // Returns rewritten JavaScript

Example

var jsfmt = require('jsfmt');
var fs = require('fs');

var js = fs.readFileSync('each.js');

js = jsfmt.rewrite(js, "_.each(a, b) -> a.forEach(b)");

Searching

jsfmt.search(<javascript_string>, <search_expression>) // Returns array of matches

Example

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);
});

Validating

jsfmt.validate(<javascript_string>) // Returns errors found while parsing JavaScript
jsfmt.validateJSON(<JSON_string>) // Returns errors found while parsing JSON

Example

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]);
}

Plugins

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.

JSX

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.

Links

Changelog

v0.4.0

  • Added two new command-line args for AST formatting. Note that these are experimental and may be removed.
  • Removed --config option in favor of .jsfmtrc and better docs around rc.
  • Updated esformatter and using new esformatter plugin for automatic brace insertion.
  • Updated style guide to include esformatter changes.
  • Fixes and cleanup for shebang.
  • Support for variable arguments using ES6 rest syntax.
  • General rewrite cleanup.
  • Changing exit code to -1 on missing arg failure.
  • Updates to rc and other dependencies.

v0.3.2

  • Adding support for UnaryExpression
  • Fixing bug where rewrite types were not being set properly

v0.3.1

  • Fixed bug when searching for expressions within BlockStatement or Program body
  • Added JSON support

v0.3.0

  • Added CONTRIBUTING
  • Added tests
  • Added Gruntfile for development
  • Added CI support
  • Added style guide
  • Added default formatting config
  • Exposed jsfmt.getConfig api method for loading jsfmt config
  • Exposed jsfmt.format(js[, options]) api method for formatting
  • Added --validate option and exposed jsfmt.validate api method
  • Pinned dependencies

v0.2.0

  • Add rc and --config config.json support for formatting configuration
  • Making --format the default action
  • Fix support for shebang at the top of js files, e.g. #!/usr/bin/env node
  • Fix jsfmt diff mode where whitespace was unaccounted for due to -b git diff option

v0.1.1

  • Initial release

License

Apache License, Version 2.0. Copyright 2014 Rdio, Inc.

More Repositories

1

rdio-simple

Simple Libraries to access the Rdio Web Service API
Java
204
star
2

vernacular

C#
170
star
3

bujagali

templating awesome
JavaScript
102
star
4

rdio-python

A Python library for the Rdio Web Service API
Python
69
star
5

algorithms

The engineering blog for Rdio's engineering team.
JavaScript
68
star
6

jquery.rdio.js

A jQuery plugin for the Rdio Web Playback API
JavaScript
64
star
7

jsapi-examples

Examples for the beta JavaScript API
JavaScript
47
star
8

thor

A Finagle based image processor.
Scala
39
star
9

hello-web-playback

A web playback example using the Rdio Web Playback API
JavaScript
38
star
10

rdioquiz-ios

'Guess the Artist' for iPhone, powered by the Rdio API
Objective-C
37
star
11

hello-ios-playback

A playback example using the Rdio iOS SDK
Objective-C
35
star
12

hello-android

A playback example using the Rdio Android SDK
Java
33
star
13

rdio-php

A PHP library for the Rdio Web Service API
PHP
27
star
14

api

A public issue tracker for the Rdio API
26
star
15

collection-chronology

Visualize an Rdio collection chronologically and listen in-page.
CSS
24
star
16

tangoandcache

C#
20
star
17

playlist-import-example

An example of importing playlists into Rdio
Python
9
star
18

rdioquiz

Guess who's playing the song from your heavy rotation, your network's heavy rotation or top albums from all Rdio users.
Python
9
star
19

translate-toolkit

Rdio fork of the translate toolkit
Python
6
star
20

hello-flash-playback

A flash playback example using the Rdio Web Playback API
4
star
21

hello-ios-swift

A playback example using the Rdio iOS SDK with Swift
Objective-C
3
star
22

playback-console

A simple playground for experimenting with the Rdio Web Playback API
JavaScript
3
star
23

rdio-infrastructure-opensource

Open source parts of rdio infrastructure
Shell
3
star
24

mono-heapdump

C
2
star
25

pgpool2

C
1
star
26

rdio-simple-play-example

An example of rdio-simple for Java and the Play! framework
Java
1
star
27

java-api-example

A very simple example of using the Rdio API from Java
Java
1
star