File tree Expand file tree Collapse file tree 3 files changed +56
-1
lines changed Expand file tree Collapse file tree 3 files changed +56
-1
lines changed Original file line number Diff line number Diff line change @@ -22,8 +22,11 @@ module.exports = function (options) {
22
22
checker . registerDefaultRules ( ) ;
23
23
24
24
var configPath = options . configPath ;
25
+ var shouldFix = options . fix ;
26
+
25
27
delete options . esnext ;
26
28
delete options . configPath ;
29
+ delete options . fix ;
27
30
28
31
if ( configPath ) {
29
32
if ( typeof options === 'object' && Object . keys ( options ) . length ) {
@@ -57,7 +60,18 @@ module.exports = function (options) {
57
60
}
58
61
59
62
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
+
61
75
var errorList = errors . getErrorList ( ) ;
62
76
63
77
file . jscs = {
Original file line number Diff line number Diff line change @@ -16,6 +16,8 @@ $ npm install --save-dev gulp-jscs
16
16
17
17
## Usage
18
18
19
+ ### Reporting Only
20
+
19
21
``` js
20
22
var gulp = require (' gulp' );
21
23
var jscs = require (' gulp-jscs' );
@@ -26,6 +28,20 @@ gulp.task('default', function () {
26
28
});
27
29
```
28
30
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
+ ```
29
45
30
46
## Results
31
47
@@ -61,6 +77,8 @@ Alternatively you can set the `configPath` *(default: `'.jscsrc'`)* option to th
61
77
Set ` esnext: true ` if you want your code to be parsed as ES6 using the harmony
62
78
version of the esprima parser.
63
79
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
+
64
82
65
83
## License
66
84
Original file line number Diff line number Diff line change @@ -107,6 +107,29 @@ it('should accept both esnext and configPath options', function(cb) {
107
107
stream . end ( ) ;
108
108
} ) ;
109
109
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
+
110
133
it ( 'should throw when passing both configPath and code style options' , function ( ) {
111
134
assert . throws ( jscs . bind ( null , {
112
135
configPath : '.jscsrc' ,
You can’t perform that action at this time.
0 commit comments