Skip to content

Commit 68bc13d

Browse files
rniemeyersindresorhus
authored andcommitted
Close #81 PR: Support fix option.
1 parent 41aebbf commit 68bc13d

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

index.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ module.exports = function (options) {
2222
checker.registerDefaultRules();
2323

2424
var configPath = options.configPath;
25+
var shouldFix = options.fix;
26+
2527
delete options.esnext;
2628
delete options.configPath;
29+
delete options.fix;
2730

2831
if (configPath) {
2932
if (typeof options === 'object' && Object.keys(options).length) {
@@ -57,7 +60,18 @@ module.exports = function (options) {
5760
}
5861

5962
try {
60-
var errors = checker.checkString(file.contents.toString(), file.relative);
63+
var fixResults;
64+
var errors;
65+
var contents = file.contents.toString();
66+
67+
if (shouldFix) {
68+
fixResults = checker.fixString(contents, file.relative);
69+
errors = fixResults.errors;
70+
file.contents = new Buffer(fixResults.output);
71+
} else {
72+
errors = checker.checkString(contents, file.relative);
73+
}
74+
6175
var errorList = errors.getErrorList();
6276

6377
file.jscs = {

readme.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ $ npm install --save-dev gulp-jscs
1616

1717
## Usage
1818

19+
### Reporting Only
20+
1921
```js
2022
var gulp = require('gulp');
2123
var jscs = require('gulp-jscs');
@@ -26,6 +28,20 @@ gulp.task('default', function () {
2628
});
2729
```
2830

31+
### Fixing and Reporting
32+
33+
```js
34+
var gulp = require('gulp');
35+
var jscs = require('gulp-jscs');
36+
37+
gulp.task('default', function () {
38+
return gulp.src('src/app.js')
39+
.pipe(jscs({
40+
fix: true
41+
}))
42+
.pipe(gulp.dest('src'));
43+
});
44+
```
2945

3046
## Results
3147

@@ -61,6 +77,8 @@ Alternatively you can set the `configPath` *(default: `'.jscsrc'`)* option to th
6177
Set `esnext: true` if you want your code to be parsed as ES6 using the harmony
6278
version of the esprima parser.
6379

80+
Set `fix: true` if you want jscs to attempt to auto-fix your files. Be sure to pipe to `gulp.dest` if you use this option.
81+
6482

6583
## License
6684

test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,29 @@ it('should accept both esnext and configPath options', function(cb) {
107107
stream.end();
108108
});
109109

110+
it('should accept the fix option', function (cb) {
111+
var data = '';
112+
113+
var stream = jscs({
114+
fix: true,
115+
configPath: '.jscsrc'
116+
});
117+
118+
stream.on('data', function (file) {
119+
assert.equal(file.contents.toString(), 'var x = {a: 1, b: 2}');
120+
});
121+
122+
stream.on('end', cb);
123+
124+
stream.write(new gutil.File({
125+
base: __dirname,
126+
path: __dirname + '/fixture.js',
127+
contents: new Buffer('var x = { a: 1, b: 2 }')
128+
}));
129+
130+
stream.end();
131+
});
132+
110133
it('should throw when passing both configPath and code style options', function () {
111134
assert.throws(jscs.bind(null, {
112135
configPath: '.jscsrc',

0 commit comments

Comments
 (0)