Giter Site home page Giter Site logo

jsfmt's Introduction

jsfmt

NPM version Build Status Dependency Status

jsfmt formats javascript and allows AST searching and rewriting. Analogous to gofmt.

Installation

npm install -g jsfmt

Why

Javascript formatters exist but most (all?) work on just strings, not the AST. Using Esprima under the hood we have access to the full AST and can do useful things like intelligent find and replace as in gofmt.

Usage

$ jsfmt --help
Usage:
  jsfmt [--no-format] [--diff|--list|--write] [--rewrite PATTERN|--search PATTERN] [<file>...]
  jsfmt (--version | --help)

Options:
  -h --help                      Show this help text
  -v --version                   Show jsfmt version
  -d --diff                      Show diff against original file
  -l --list                      List the files which differ from jsfmt output
  --no-format                    Do not format the input file(s)
  -w --write                     Overwrite the original file with jsfmt output
  -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.

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

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

Rewriting

The rewrite rule 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 flag 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.

Searching

The search rule is very similar but just outputs expressions that match the given search expression.

API

Searching:

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

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

jsfmt.search(js, "R.Component.create(a, { dependencies: z })").forEach(function(matches, wildcards) {
  console.log(wildcards.z);
});

Rewriting:

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

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

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

Examples

Rewrite occurences of _.reduce to use native reduce:

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

Before:

var values = [1, 2, 3, 4];
_.reduce(values, function(sum, value) {
  return sum + value;
}, 0);

After:

var values = [1, 2, 3, 4];
values.reduce(function(sum, value) {
  return sum + value;
}, 0);

Links

Changelog

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.

jsfmt's People

Contributors

jimfleming avatar brettlangdon avatar pgilad avatar bfontaine avatar vjeux avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.