Skip to content

Commit 7e30978

Browse files
committed
Merge pull request rdio#59 from jimfleming/v0_3_0
v0.3.0 release
2 parents c419556 + fc4bc48 commit 7e30978

File tree

2 files changed

+98
-34
lines changed

2 files changed

+98
-34
lines changed

README.md

Lines changed: 97 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,13 @@ jsfmt
66
[![Dependency Status](https://david-dm.org/rdio/jsfmt.png)](https://david-dm.org/)
77
[![Coverage Status](https://coveralls.io/repos/rdio/jsfmt/badge.png)](https://coveralls.io/r/rdio/jsfmt)
88

9-
`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/).
1010

1111
Installation
1212
---
1313

1414
`npm install -g jsfmt`
1515

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-
2116
Usage
2217
---
2318

@@ -40,6 +35,11 @@ Options:
4035

4136
If no path is given it will read from `stdin`. A directory path will recurse over all *.js files in the directory.
4237

38+
Formatting
39+
---
40+
41+
For formatting `jsfmt` uses [esformatter](https://github.com/millermedeiros/esformatter).
42+
4343
### .jsfmtrc
4444

4545
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
4949
`jsfmt` will also attempt to pickup and use the configured `indent`
5050
variable from your `.jshintrc` configuration file, if present.
5151

52+
A config file can be manually specified using `--config config.json`.
53+
5254
Rewriting
5355
---
5456

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:
5658

5759
pattern -> replacement
5860

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

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

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.
84+
85+
### Example
86+
87+
jsfmt --validate bad.js
6488

6589
API
6690
---
6791

68-
Searching:
92+
### Formatting
93+
94+
```javascript
95+
jsfmt.format(<javascript_string>, <config_object>) // Returns formatted JavaScript
96+
```
97+
98+
```javascript
99+
var config = jsfmt.getConfig(); // Loads the jsfmt config from the appropriate rc file or default config object
100+
```
101+
102+
#### Example
69103

70104
```javascript
71105
var jsfmt = require('jsfmt');
72106
var fs = require('fs');
73107

74-
var js = fs.readFileSync('my_file.js');
108+
var js = fs.readFileSync('unformatted.js');
109+
var config = jsfmt.getConfig();
75110

76-
jsfmt.search(js, "R.Component.create(a, { dependencies: z })").forEach(function(matches, wildcards) {
77-
console.log(wildcards.z);
78-
});
111+
js = jsfmt.format(js, config);
112+
```
113+
114+
### Rewriting
115+
116+
```javascript
117+
jsfmt.rewrite(<javascript_string>, <rewrite_rule>) // Returns rewritten JavaScript
79118
```
80119

81-
Rewriting:
120+
#### Example
82121

83122
```javascript
84123
var jsfmt = require('jsfmt');
85124
var fs = require('fs');
86125

87-
var js = fs.readFileSync('my_file.js');
126+
var js = fs.readFileSync('each.js');
88127

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

92-
Examples
93-
---
131+
### Searching
94132

95-
Rewrite occurences of `_.reduce` to use native reduce:
133+
```javascript
134+
jsfmt.search(<javascript_string>, <search_expression>) // Returns array of matches
135+
```
136+
137+
#### Example
96138

97-
```bash
98-
jsfmt --rewrite "_.reduce(a, b, c) -> a.reduce(b, c)" examples/reduce.js
139+
```javascript
140+
var jsfmt = require('jsfmt');
141+
var fs = require('fs');
142+
143+
var js = fs.readFileSync('component.js');
144+
145+
jsfmt.search(js, "R.Component.create(a, { dependencies: z })").forEach(function(matches, wildcards) {
146+
console.log(wildcards.z);
147+
});
99148
```
100149

101-
Before:
150+
### Validating
102151

103152
```javascript
104-
var values = [1, 2, 3, 4];
105-
_.reduce(values, function(sum, value) {
106-
return sum + value;
107-
}, 0);
153+
jsfmt.validate(<javascript_string>) // Returns errors found while parsing JavaScript
108154
```
109155

110-
After:
156+
#### Example
111157

112158
```javascript
113-
var values = [1, 2, 3, 4];
114-
values.reduce(function(sum, value) {
115-
return sum + value;
116-
}, 0);
159+
var jsfmt = require('jsfmt');
160+
var fs = require('fs');
161+
162+
var js = fs.readFileSync('each.js');
163+
var errors = jsfmt.validate(js);
164+
165+
for (var i = 0; i < errors.length; i++) {
166+
console.error(errors[i]);
167+
}
117168
```
118169

119170
Links
120171
---
121172

122-
- Atom Package - https://atom.io/packages/atom-jsfmt - "Automatically run jsfmt every time you save a javascript source file."
173+
- Atom Package - https://atom.io/packages/atom-jsfmt - "Automatically run jsfmt every time you save a JavaScript source file."
123174
- Grunt Task - https://github.com/james2doyle/grunt-jsfmt - "A task for the jsfmt library."
124175
- Emacs Plugin - https://github.com/brettlangdon/jsfmt.el - "Run jsfmt from within emacs"
125176

126177
Changelog
127178
---
128179

180+
### v0.3.0
181+
182+
- Added CONTRIBUTING
183+
- Added tests
184+
- Added Gruntfile for development
185+
- Added CI support
186+
- Added style guide
187+
- Added default formatting config
188+
- Exposed `jsfmt.getConfig` api method for loading jsfmt config
189+
- Exposed `jsfmt.format(js[, options])` api method for formatting
190+
- Added `--validate` option and exposed `jsfmt.validate` api method
191+
- Pinned dependencies
192+
129193
### v0.2.0
130194

131195
- Add [rc](https://github.com/dominictarr/rc) and `--config config.json` support for formatting configuration

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "jsfmt",
33
"description": "gofmt for javascript",
4-
"version": "0.2.0",
4+
"version": "0.3.0",
55
"homepage": "https://github.com/rdio/jsfmt",
66
"main": "./lib/index.js",
77
"engines": {

0 commit comments

Comments
 (0)