You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`jsfmt` formats javascript and allows AST searching and rewriting. Analogous to [`gofmt`](http://golang.org/cmd/gofmt/).
9
+
For formatting, searching, and rewriting JavaScript. Analogous to [`gofmt`](http://golang.org/cmd/gofmt/).
10
10
11
11
Installation
12
12
---
13
13
14
14
`npm install -g jsfmt`
15
15
16
-
Why
17
-
---
18
-
19
-
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`.
20
-
21
16
Usage
22
17
---
23
18
@@ -40,6 +35,11 @@ Options:
40
35
41
36
If no path is given it will read from `stdin`. A directory path will recurse over all *.js files in the directory.
42
37
38
+
Formatting
39
+
---
40
+
41
+
For formatting `jsfmt` uses [esformatter](https://github.com/millermedeiros/esformatter).
42
+
43
43
### .jsfmtrc
44
44
45
45
Any of the [esformatter](https://github.com/millermedeiros/esformatter) formatting
@@ -49,83 +49,147 @@ options can be overwritten via a `.jsfmtrc` file. The file is parsed using
49
49
`jsfmt` will also attempt to pickup and use the configured `indent`
50
50
variable from your `.jshintrc` configuration file, if present.
51
51
52
+
A config file can be manually specified using `--config config.json`.
53
+
52
54
Rewriting
53
55
---
54
56
55
-
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:
57
+
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:
56
58
57
59
pattern -> replacement
58
60
59
-
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`.
61
+
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`.
60
62
61
-
### Searching
63
+
### Example
64
+
65
+
Rewrite occurences of `_.reduce` to use native reduce:
66
+
67
+
jsfmt --rewrite "_.reduce(a, b, c) -> a.reduce(b, c)" reduce.js
68
+
69
+
Searching
70
+
---
71
+
72
+
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.
62
73
63
-
The search rule is very similar but just outputs expressions that match the given search expression.
74
+
### Example
75
+
76
+
Find occurences of `_.reduce`:
77
+
78
+
jsfmt --search "_.reduce(a, b, c)" reduce.js
79
+
80
+
Validating
81
+
---
82
+
83
+
The `--validate` flag will print any errors found by esprima while parsing the JavaScript.
0 commit comments