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) {
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 = {
Original file line number Diff line number Diff line change @@ -16,6 +16,8 @@ $ npm install --save-dev gulp-jscs
1616
1717## Usage
1818
19+ ### Reporting Only
20+
1921``` js
2022var gulp = require (' gulp' );
2123var 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
6177Set ` esnext: true ` if you want your code to be parsed as ES6 using the harmony
6278version 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
Original file line number Diff line number Diff 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+
110133it ( 'should throw when passing both configPath and code style options' , function ( ) {
111134 assert . throws ( jscs . bind ( null , {
112135 configPath : '.jscsrc' ,
You can’t perform that action at this time.
0 commit comments