Skip to content

Commit 0fc35b2

Browse files
committed
remove ability to pass JSCS config to jscs()
JSCS config should be in an external file so other tools can make use of it too. fixes #70
1 parent cb899f6 commit 0fc35b2

File tree

5 files changed

+61
-45
lines changed

5 files changed

+61
-45
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
sudo: false
22
language: node_js
33
node_js:
4-
- 'iojs'
4+
- 'stable'
55
- '0.12'
66
- '0.10'

index.js

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,29 @@
22
var path = require('path');
33
var gutil = require('gulp-util');
44
var through = require('through2');
5+
var tildify = require('tildify');
56
var Checker = require('jscs');
67
var loadConfigFile = require('jscs/lib/cli-config');
7-
var assign = require('object-assign');
8-
var tildify = require('tildify');
9-
10-
module.exports = function (options) {
11-
options = options || '.jscsrc';
12-
13-
if (typeof options === 'string') {
14-
options = {configPath: options};
15-
}
168

17-
options = assign({}, options);
9+
module.exports = function (opts) {
10+
opts = opts || {};
1811

1912
var checker = new Checker();
2013

2114
checker.registerDefaultRules();
2215

23-
var configPath = options.configPath;
24-
var shouldFix = options.fix;
25-
26-
delete options.configPath;
27-
delete options.fix;
16+
try {
17+
checker.configure(loadConfigFile.load(opts.configPath));
18+
} catch (err) {
19+
err.message = 'Unable to load JSCS config file';
2820

29-
if (configPath) {
30-
if (typeof options === 'object' && Object.keys(options).length) {
31-
throw new Error('configPath option is not compatible with code style options');
21+
if (opts.configPath) {
22+
err.message += ' at ' + tildify(path.resolve(opts.configPath));
3223
}
3324

34-
try {
35-
checker.configure(loadConfigFile.load(configPath));
36-
} catch (err) {
37-
err.message = 'Unable to load JSCS config file at ' + tildify(path.resolve(configPath)) + '\n' + err.message;
38-
throw err;
39-
}
40-
} else {
41-
checker.configure(options);
25+
err.message += '\n' + err.message;
26+
27+
throw err;
4228
}
4329

4430
return through.obj(function (file, enc, cb) {
@@ -61,7 +47,7 @@ module.exports = function (options) {
6147
var errors;
6248
var contents = file.contents.toString();
6349

64-
if (shouldFix) {
50+
if (opts.fix) {
6551
fixResults = checker.fixString(contents, file.relative);
6652
errors = fixResults.errors;
6753
file.contents = new Buffer(fixResults.output);

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
"node": ">=0.10.0"
1414
},
1515
"scripts": {
16-
"pretest": "jscs *.js reporters",
1716
"test": "xo && mocha"
1817
},
1918
"files": [
20-
"index.js"
19+
"index.js",
20+
"reporters"
2121
],
2222
"keywords": [
2323
"gulpplugin",
@@ -36,13 +36,13 @@
3636
"dependencies": {
3737
"gulp-util": "^3.0.4",
3838
"jscs": "^2.1.1",
39-
"object-assign": "^4.0.1",
4039
"through2": "^2.0.0",
4140
"tildify": "^1.0.0"
4241
},
4342
"devDependencies": {
4443
"mocha": "*",
4544
"stream-assert": "^2.0.2",
45+
"temp-write": "^1.1.2",
4646
"xo": "*"
4747
}
4848
}

readme.md

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,26 @@ gulp.task('default', () => {
4242
});
4343
```
4444

45+
### Reporting & fixing & failing on lint error
46+
47+
```js
48+
const gulp = require('gulp');
49+
const jscs = require('gulp-jscs');
50+
51+
gulp.task('default', () => {
52+
return gulp.src('src/app.js')
53+
.pipe(jscs({fix: true}))
54+
.pipe(jscs.reporter())
55+
.pipe(jscs.reporter('fail'))
56+
.pipe(gulp.dest('src'));
57+
});
58+
```
59+
4560

4661
## Results
4762

48-
A `jscs` object will be attached to the file object which can be used for custom error reporting. An example with one error might look like this:
63+
A `jscs` object will be attached to the file object.
64+
An example with one error might look like this:
4965

5066
```js
5167
{
@@ -64,20 +80,39 @@ A `jscs` object will be attached to the file object which can be used for custom
6480

6581
## API
6682

83+
JSCS [config](http://jscs.info/overview.html#options) should be placed in a `.jscsrc` file.
84+
6785
### jscs([options])
6886

6987
#### options
7088

71-
See the JSCS [options](http://jscs.info/overview.html#options).
89+
##### fix
90+
91+
Type: `boolean`
92+
Default: `false`
93+
94+
Make JSCS attempt to auto-fix your files.
95+
Be sure to pipe to `gulp.dest` if you use this option.
7296

73-
Alternatively you can set the `configPath` *(default: `'.jscsrc'`)* option to the path of a [.jscsrc](http://jscs.info/rules.html) file.
97+
##### configPath
7498

75-
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.
99+
Type: `string`
100+
Default: JSCS will search for the config file up to your home directory.
101+
102+
Set the path to the JSCS config file.
103+
Only use this option when it can't be found automatically.
76104

77105
### jscs.reporter([reporter])
78106

107+
#### reporter
108+
109+
Type: `string`
110+
Default: `console`
111+
79112
See the JSCS [reporter docs](http://jscs.info/overview#-reporter-r) for supported input.
80113

114+
Can be used multiple times in the same pipeline.
115+
81116
This plugin also ships with some custom reporters:
82117

83118
- `fail` - Emits an error at the end of the stream if there are lint errors.

test.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var path = require('path');
44
var assert = require('assert');
55
var gutil = require('gulp-util');
66
var streamAssert = require('stream-assert');
7+
var tempWrite = require('temp-write');
78
var jscs = require('./');
89

910
var stdoutWrite = process.stdout.write;
@@ -53,7 +54,9 @@ it('should check code style of JS files', function (cb) {
5354
});
5455

5556
it('should check code style of JS files using a preset', function (cb) {
56-
var stream = jscs({preset: 'google'});
57+
var stream = jscs({
58+
configPath: tempWrite.sync(JSON.stringify({preset: 'google'}))
59+
});
5760

5861
stream
5962
.pipe(streamAssert.first(function (file) {
@@ -126,8 +129,7 @@ it('should accept configPath options', function (cb) {
126129

127130
it('should accept the fix option', function (cb) {
128131
var stream = jscs({
129-
fix: true,
130-
configPath: '.jscsrc'
132+
fix: true
131133
});
132134

133135
stream.on('data', function (file) {
@@ -145,13 +147,6 @@ it('should accept the fix option', function (cb) {
145147
stream.end();
146148
});
147149

148-
it('should throw when passing both configPath and code style options', function () {
149-
assert.throws(jscs.bind(null, {
150-
configPath: '.jscsrc',
151-
preset: 'airbnb'
152-
}), /configPath/);
153-
});
154-
155150
it('should not mutate the options object passed as argument', function () {
156151
var options = {foo: true};
157152
jscs(options);

0 commit comments

Comments
 (0)