From 6c0f0466dd8a2e7fb2d98f48eac5ff5ec4033b25 Mon Sep 17 00:00:00 2001 From: Peter deHaan Date: Wed, 21 Oct 2015 17:10:41 -0700 Subject: [PATCH] Build: Add eslint and jscs presets & update code --- .eslintignore | 1 + .eslintrc | 3 ++ .jscsrc | 3 ++ .jshintignore | 2 - .jshintrc | 36 ------------- .travis.yml | 1 + index.js | 100 ++++++++++++++++++++++-------------- lib/inspectStream.js | 12 +++-- lib/isStream.js | 2 +- package.json | 9 +++- test/.eslintrc | 3 ++ test/File.js | 116 +++++++++++++++++++++--------------------- test/cloneBuffer.js | 2 +- test/inspectStream.js | 2 +- 14 files changed, 150 insertions(+), 142 deletions(-) create mode 100644 .eslintignore create mode 100644 .eslintrc create mode 100644 .jscsrc delete mode 100644 .jshintignore delete mode 100644 .jshintrc create mode 100644 test/.eslintrc diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..404abb2 --- /dev/null +++ b/.eslintignore @@ -0,0 +1 @@ +coverage/ diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..a5a4a17 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,3 @@ +{ + "extends": "gulp" +} diff --git a/.jscsrc b/.jscsrc new file mode 100644 index 0000000..703b33f --- /dev/null +++ b/.jscsrc @@ -0,0 +1,3 @@ +{ + "preset": "gulp" +} diff --git a/.jshintignore b/.jshintignore deleted file mode 100644 index ba2a97b..0000000 --- a/.jshintignore +++ /dev/null @@ -1,2 +0,0 @@ -node_modules -coverage diff --git a/.jshintrc b/.jshintrc deleted file mode 100644 index 373d6c3..0000000 --- a/.jshintrc +++ /dev/null @@ -1,36 +0,0 @@ -{ - "indent": 2, - "smarttabs": true, - "latedef": "nofunc", - "undef": true, - "unused": true, - "trailing": true, - "laxcomma": false, - "eqeqeq": true, - "eqnull": true, - "boss": true, - "expr": true, - "sub": true, - "newcap": true, - "quotmark": true, - "loopfunc": true, - "lastsemic": true, - "funcscope": true, - "browser": true, - "nonstandard": true, - "jquery": true, - "devel": true, - "node": true, - "esnext": true, - "globals": { - "describe": false, - "xdescribe": false, - "it": false, - "xit": false, - "before": false, - "beforeEach": false, - "after": false, - "afterEach": false, - "define": false - } -} diff --git a/.travis.yml b/.travis.yml index 8b95a9f..1415eb5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,3 +3,4 @@ node_js: - "0.10" after_script: - npm run coveralls + - npm run lint diff --git a/index.js b/index.js index c8f113f..cd733cc 100644 --- a/index.js +++ b/index.js @@ -10,19 +10,21 @@ var Stream = require('stream'); var replaceExt = require('replace-ext'); function File(file) { - if (!file) file = {}; + if (!file) { + file = {}; + } - // record path change + // Record path change var history = file.path ? [file.path] : file.history; this.history = history || []; this.cwd = file.cwd || process.cwd(); this.base = file.base || this.cwd; - // stat = files stats object + // Stat = files stats object this.stat = file.stat || null; - // contents = stream, buffer, or null if not read + // Contents = stream, buffer, or null if not read this.contents = file.contents || null; this._isVinyl = true; @@ -40,7 +42,7 @@ File.prototype.isNull = function() { return isNull(this.contents); }; -// TODO: should this be moved to vinyl-fs? +// TODO: Should this be moved to vinyl-fs? File.prototype.isDirectory = function() { return this.isNull() && this.stat && this.stat.isDirectory(); }; @@ -49,19 +51,19 @@ File.prototype.clone = function(opt) { if (typeof opt === 'boolean') { opt = { deep: opt, - contents: true + contents: true, }; } else if (!opt) { opt = { deep: true, - contents: true + contents: true, }; } else { opt.deep = opt.deep === true; opt.contents = opt.contents !== false; } - // clone our file contents + // Clone our file contents var contents; if (this.isStream()) { contents = this.contents.pipe(new Stream.PassThrough()); @@ -75,12 +77,12 @@ File.prototype.clone = function(opt) { base: this.base, stat: (this.stat ? cloneStats(this.stat) : null), history: this.history.slice(), - contents: contents + contents: contents, }); - // clone our custom properties + // Clone our custom properties Object.keys(this).forEach(function(key) { - // ignore built-in fields + // Ignore built-in fields if (key === '_contents' || key === 'stat' || key === 'history' || key === 'path' || key === 'base' || key === 'cwd') { @@ -92,8 +94,12 @@ File.prototype.clone = function(opt) { }; File.prototype.pipe = function(stream, opt) { - if (!opt) opt = {}; - if (typeof opt.end === 'undefined') opt.end = true; + if (!opt) { + opt = {}; + } + if (typeof opt.end === 'undefined') { + opt.end = true; + } if (this.isStream()) { return this.contents.pipe(stream, opt); @@ -107,19 +113,21 @@ File.prototype.pipe = function(stream, opt) { return stream; } - // isNull - if (opt.end) stream.end(); + // Check if isNull + if (opt.end) { + stream.end(); + } return stream; }; File.prototype.inspect = function() { var inspect = []; - // use relative path if possible + // Use relative path if possible var filePath = (this.base && this.path) ? this.relative : this.path; if (filePath) { - inspect.push('"'+filePath+'"'); + inspect.push('"' + filePath + '"'); } if (this.isBuffer()) { @@ -130,15 +138,15 @@ File.prototype.inspect = function() { inspect.push(inspectStream(this.contents)); } - return ''; + return ''; }; File.isVinyl = function(file) { return file && file._isVinyl === true; }; -// virtual attributes -// or stuff with extra logic +// Virtual attributes +// Or stuff with extra logic Object.defineProperty(File.prototype, 'contents', { get: function() { return this._contents; @@ -148,52 +156,68 @@ Object.defineProperty(File.prototype, 'contents', { throw new Error('File.contents can only be a Buffer, a Stream, or null.'); } this._contents = val; - } + }, }); -// TODO: should this be moved to vinyl-fs? +// TODO: Should this be moved to vinyl-fs? Object.defineProperty(File.prototype, 'relative', { get: function() { - if (!this.base) throw new Error('No base specified! Can not get relative.'); - if (!this.path) throw new Error('No path specified! Can not get relative.'); + if (!this.base) { + throw new Error('No base specified! Can not get relative.'); + } + if (!this.path) { + throw new Error('No path specified! Can not get relative.'); + } return path.relative(this.base, this.path); }, set: function() { throw new Error('File.relative is generated from the base and path attributes. Do not modify it.'); - } + }, }); Object.defineProperty(File.prototype, 'dirname', { get: function() { - if (!this.path) throw new Error('No path specified! Can not get dirname.'); + if (!this.path) { + throw new Error('No path specified! Can not get dirname.'); + } return path.dirname(this.path); }, set: function(dirname) { - if (!this.path) throw new Error('No path specified! Can not set dirname.'); + if (!this.path) { + throw new Error('No path specified! Can not set dirname.'); + } this.path = path.join(dirname, path.basename(this.path)); - } + }, }); Object.defineProperty(File.prototype, 'basename', { get: function() { - if (!this.path) throw new Error('No path specified! Can not get basename.'); + if (!this.path) { + throw new Error('No path specified! Can not get basename.'); + } return path.basename(this.path); }, set: function(basename) { - if (!this.path) throw new Error('No path specified! Can not set basename.'); + if (!this.path) { + throw new Error('No path specified! Can not set basename.'); + } this.path = path.join(path.dirname(this.path), basename); - } + }, }); Object.defineProperty(File.prototype, 'extname', { get: function() { - if (!this.path) throw new Error('No path specified! Can not get extname.'); + if (!this.path) { + throw new Error('No path specified! Can not get extname.'); + } return path.extname(this.path); }, set: function(extname) { - if (!this.path) throw new Error('No path specified! Can not set extname.'); + if (!this.path) { + throw new Error('No path specified! Can not set extname.'); + } this.path = replaceExt(this.path, extname); - } + }, }); Object.defineProperty(File.prototype, 'path', { @@ -201,13 +225,15 @@ Object.defineProperty(File.prototype, 'path', { return this.history[this.history.length - 1]; }, set: function(path) { - if (typeof path !== 'string') throw new Error('path should be string'); + if (typeof path !== 'string') { + throw new Error('path should be string'); + } - // record history only when path changed + // Record history only when path changed if (path && path !== this.path) { this.history.push(path); } - } + }, }); module.exports = File; diff --git a/lib/inspectStream.js b/lib/inspectStream.js index d36df6f..942f23d 100644 --- a/lib/inspectStream.js +++ b/lib/inspectStream.js @@ -1,11 +1,15 @@ var isStream = require('./isStream'); module.exports = function(stream) { - if (!isStream(stream)) return; + if (!isStream(stream)) { + return; + } var streamType = stream.constructor.name; - // avoid StreamStream - if (streamType === 'Stream') streamType = ''; + // Avoid StreamStream + if (streamType === 'Stream') { + streamType = ''; + } - return '<'+streamType+'Stream>'; + return '<' + streamType + 'Stream>'; }; diff --git a/lib/isStream.js b/lib/isStream.js index 9ce0929..6b54e12 100644 --- a/lib/isStream.js +++ b/lib/isStream.js @@ -2,4 +2,4 @@ var Stream = require('stream').Stream; module.exports = function(o) { return !!o && o instanceof Stream; -}; \ No newline at end of file +}; diff --git a/package.json b/package.json index b67ad01..aa542ad 100644 --- a/package.json +++ b/package.json @@ -17,17 +17,22 @@ }, "devDependencies": { "buffer-equal": "0.0.1", + "eslint": "^1.7.3", + "eslint-config-gulp": "^2.0.0", "event-stream": "^3.1.0", "istanbul": "^0.3.0", "istanbul-coveralls": "^1.0.1", - "jshint": "^2.4.1", + "jscs": "^2.3.5", + "jscs-preset-gulp": "^1.0.0", "lodash.templatesettings": "^3.1.0", "mocha": "^2.0.0", "rimraf": "^2.2.5", "should": "^7.0.0" }, "scripts": { - "test": "mocha && jshint lib", + "lint": "eslint . && jscs *.js lib/ test/", + "pretest": "npm run lint", + "test": "mocha", "coveralls": "istanbul cover _mocha && istanbul-coveralls" }, "engines": { diff --git a/test/.eslintrc b/test/.eslintrc new file mode 100644 index 0000000..06b940f --- /dev/null +++ b/test/.eslintrc @@ -0,0 +1,3 @@ +{ + "extends": "gulp/test" +} diff --git a/test/File.js b/test/File.js index 66aef52..c9b04cb 100644 --- a/test/File.js +++ b/test/File.js @@ -32,7 +32,7 @@ describe('File', function() { it('should default base to cwd', function(done) { var cwd = '/'; - var file = new File({cwd: cwd}); + var file = new File({ cwd: cwd }); file.base.should.equal(cwd); done(); }); @@ -69,21 +69,21 @@ describe('File', function() { it('should set base to given value', function(done) { var val = '/'; - var file = new File({base: val}); + var file = new File({ base: val }); file.base.should.equal(val); done(); }); it('should set cwd to given value', function(done) { var val = '/'; - var file = new File({cwd: val}); + var file = new File({ cwd: val }); file.cwd.should.equal(val); done(); }); it('should set path to given value', function(done) { var val = '/test.coffee'; - var file = new File({path: val}); + var file = new File({ path: val }); file.path.should.equal(val); file.history.should.eql([val]); done(); @@ -91,7 +91,7 @@ describe('File', function() { it('should set history to given value', function(done) { var val = '/test.coffee'; - var file = new File({history: [val]}); + var file = new File({ history: [val] }); file.path.should.equal(val); file.history.should.eql([val]); done(); @@ -99,14 +99,14 @@ describe('File', function() { it('should set stat to given value', function(done) { var val = {}; - var file = new File({stat: val}); + var file = new File({ stat: val }); file.stat.should.equal(val); done(); }); it('should set contents to given value', function(done) { var val = new Buffer('test'); - var file = new File({contents: val}); + var file = new File({ contents: val }); file.contents.should.equal(val); done(); }); @@ -115,20 +115,20 @@ describe('File', function() { describe('isBuffer()', function() { it('should return true when the contents are a Buffer', function(done) { var val = new Buffer('test'); - var file = new File({contents: val}); + var file = new File({ contents: val }); file.isBuffer().should.equal(true); done(); }); it('should return false when the contents are a Stream', function(done) { var val = new Stream(); - var file = new File({contents: val}); + var file = new File({ contents: val }); file.isBuffer().should.equal(false); done(); }); it('should return false when the contents are a null', function(done) { - var file = new File({contents: null}); + var file = new File({ contents: null }); file.isBuffer().should.equal(false); done(); }); @@ -137,20 +137,20 @@ describe('File', function() { describe('isStream()', function() { it('should return false when the contents are a Buffer', function(done) { var val = new Buffer('test'); - var file = new File({contents: val}); + var file = new File({ contents: val }); file.isStream().should.equal(false); done(); }); it('should return true when the contents are a Stream', function(done) { var val = new Stream(); - var file = new File({contents: val}); + var file = new File({ contents: val }); file.isStream().should.equal(true); done(); }); it('should return false when the contents are a null', function(done) { - var file = new File({contents: null}); + var file = new File({ contents: null }); file.isStream().should.equal(false); done(); }); @@ -159,20 +159,20 @@ describe('File', function() { describe('isNull()', function() { it('should return false when the contents are a Buffer', function(done) { var val = new Buffer('test'); - var file = new File({contents: val}); + var file = new File({ contents: val }); file.isNull().should.equal(false); done(); }); it('should return false when the contents are a Stream', function(done) { var val = new Stream(); - var file = new File({contents: val}); + var file = new File({ contents: val }); file.isNull().should.equal(false); done(); }); it('should return true when the contents are a null', function(done) { - var file = new File({contents: null}); + var file = new File({ contents: null }); file.isNull().should.equal(true); done(); }); @@ -182,25 +182,25 @@ describe('File', function() { var fakeStat = { isDirectory: function() { return true; - } + }, }; it('should return false when the contents are a Buffer', function(done) { var val = new Buffer('test'); - var file = new File({contents: val, stat: fakeStat}); + var file = new File({ contents: val, stat: fakeStat }); file.isDirectory().should.equal(false); done(); }); it('should return false when the contents are a Stream', function(done) { var val = new Stream(); - var file = new File({contents: val, stat: fakeStat}); + var file = new File({ contents: val, stat: fakeStat }); file.isDirectory().should.equal(false); done(); }); it('should return true when the contents are a null', function(done) { - var file = new File({contents: null, stat: fakeStat}); + var file = new File({ contents: null, stat: fakeStat }); file.isDirectory().should.equal(true); done(); }); @@ -212,7 +212,7 @@ describe('File', function() { cwd: '/', base: '/test/', path: '/test/test.coffee', - contents: new Buffer('test') + contents: new Buffer('test'), }; var file = new File(options); var file2 = file.clone(); @@ -231,7 +231,7 @@ describe('File', function() { cwd: '/', base: '/test/', path: '/test/test.js', - contents: new Buffer('test') + contents: new Buffer('test'), }; var file = new File(options); @@ -254,7 +254,7 @@ describe('File', function() { cwd: '/', base: '/test/', path: '/test/test.coffee', - contents: contents + contents: contents, }; var file = new File(options); var file2 = file.clone(); @@ -285,7 +285,7 @@ describe('File', function() { cwd: '/', base: '/test/', path: '/test/test.coffee', - contents: null + contents: null, }; var file = new File(options); var file2 = file.clone(); @@ -304,7 +304,7 @@ describe('File', function() { base: '/test/', path: '/test/test.js', contents: new Buffer('test'), - stat: fs.statSync(__filename) + stat: fs.statSync(__filename), }; var file = new File(options); @@ -324,7 +324,7 @@ describe('File', function() { base: '/test/', path: '/test/test.js', contents: new Buffer('test'), - stat: fs.statSync(__filename) + stat: fs.statSync(__filename), }; var file = new File(options); @@ -341,7 +341,7 @@ describe('File', function() { cwd: '/', base: '/test/', path: '/test/test.coffee', - contents: null + contents: null, }; var file = new File(options); @@ -364,7 +364,7 @@ describe('File', function() { cwd: '/', base: '/test/', path: '/test/test.coffee', - contents: null + contents: null, }; var file = new File(options); @@ -375,12 +375,12 @@ describe('File', function() { file2.history.should.eql([ '/test/test.coffee', '/test/test.js', - '/test/test-938di2s.js' + '/test/test-938di2s.js', ]); file2.history.should.not.equal([ '/test/test.coffee', '/test/test.js', - '/test/test-938di2s.js' + '/test/test-938di2s.js', ]); file2.path.should.eql('/test/test-938di2s.js'); @@ -392,7 +392,7 @@ describe('File', function() { cwd: '/', base: '/test/', path: '/test/test.coffee', - contents: null + contents: null, }; var file = new File(options); @@ -428,7 +428,7 @@ describe('File', function() { cwd: '/', base: '/test/', path: '/test/test.coffee', - contents: new Buffer('test') + contents: new Buffer('test'), }; var file = new File(options); var stream = new Stream.PassThrough(); @@ -450,7 +450,7 @@ describe('File', function() { cwd: '/', base: '/test/', path: '/test/test.coffee', - contents: new Stream.PassThrough() + contents: new Stream.PassThrough(), }; var file = new File(options); var stream = new Stream.PassThrough(); @@ -471,7 +471,7 @@ describe('File', function() { cwd: '/', base: '/test/', path: '/test/test.coffee', - contents: null + contents: null, }; var file = new File(options); var stream = new Stream.PassThrough(); @@ -490,7 +490,7 @@ describe('File', function() { cwd: '/', base: '/test/', path: '/test/test.coffee', - contents: new Buffer('test') + contents: new Buffer('test'), }; var file = new File(options); var stream = new Stream.PassThrough(); @@ -503,7 +503,7 @@ describe('File', function() { stream.on('end', function() { throw new Error('should not end'); }); - var ret = file.pipe(stream, {end: false}); + var ret = file.pipe(stream, { end: false }); ret.should.equal(stream, 'should return the stream'); }); @@ -513,7 +513,7 @@ describe('File', function() { cwd: '/', base: '/test/', path: '/test/test.coffee', - contents: new Stream.PassThrough() + contents: new Stream.PassThrough(), }; var file = new File(options); var stream = new Stream.PassThrough(); @@ -526,7 +526,7 @@ describe('File', function() { stream.on('end', function() { throw new Error('should not end'); }); - var ret = file.pipe(stream, {end: false}); + var ret = file.pipe(stream, { end: false }); ret.should.equal(stream, 'should return the stream'); file.contents.write(testChunk); @@ -537,7 +537,7 @@ describe('File', function() { cwd: '/', base: '/test/', path: '/test/test.coffee', - contents: null + contents: null, }; var file = new File(options); var stream = new Stream.PassThrough(); @@ -547,7 +547,7 @@ describe('File', function() { stream.on('end', function() { throw new Error('should not end'); }); - var ret = file.pipe(stream, {end: false}); + var ret = file.pipe(stream, { end: false }); ret.should.equal(stream, 'should return the stream'); process.nextTick(done); }); @@ -563,7 +563,7 @@ describe('File', function() { it('should return correct format when Buffer and no path', function(done) { var val = new Buffer('test'); var file = new File({ - contents: val + contents: val, }); file.inspect().should.equal('>'); done(); @@ -575,7 +575,7 @@ describe('File', function() { cwd: '/', base: '/test/', path: '/test/test.coffee', - contents: val + contents: val, }); file.inspect().should.equal('>'); done(); @@ -586,7 +586,7 @@ describe('File', function() { var file = new File({ cwd: '/', path: '/test/test.coffee', - contents: val + contents: val, }); delete file.base; file.inspect().should.equal('>'); @@ -598,7 +598,7 @@ describe('File', function() { cwd: '/', base: '/test/', path: '/test/test.coffee', - contents: new Stream.PassThrough() + contents: new Stream.PassThrough(), }); file.inspect().should.equal('>'); done(); @@ -609,7 +609,7 @@ describe('File', function() { cwd: '/', base: '/test/', path: '/test/test.coffee', - contents: null + contents: null, }); file.inspect().should.equal(''); done(); @@ -691,7 +691,7 @@ describe('File', function() { var file = new File({ cwd: '/', base: '/test/', - path: '/test/test.coffee' + path: '/test/test.coffee', }); file.relative.should.equal('test.coffee'); done(); @@ -700,7 +700,7 @@ describe('File', function() { it('should return a relative path from cwd', function(done) { var file = new File({ cwd: '/', - path: '/test/test.coffee' + path: '/test/test.coffee', }); file.relative.should.equal(path.join('test','test.coffee')); done(); @@ -723,7 +723,7 @@ describe('File', function() { var file = new File({ cwd: '/', base: '/test/', - path: '/test/test.coffee' + path: '/test/test.coffee', }); file.dirname.should.equal('/test'); done(); @@ -743,7 +743,7 @@ describe('File', function() { var file = new File({ cwd: '/', base: '/test/', - path: '/test/test.coffee' + path: '/test/test.coffee', }); file.dirname = '/test/foo'; file.path.should.equal('/test/foo/test.coffee'); @@ -767,7 +767,7 @@ describe('File', function() { var file = new File({ cwd: '/', base: '/test/', - path: '/test/test.coffee' + path: '/test/test.coffee', }); file.basename.should.equal('test.coffee'); done(); @@ -787,7 +787,7 @@ describe('File', function() { var file = new File({ cwd: '/', base: '/test/', - path: '/test/test.coffee' + path: '/test/test.coffee', }); file.basename = 'foo.png'; file.path.should.equal('/test/foo.png'); @@ -811,7 +811,7 @@ describe('File', function() { var file = new File({ cwd: '/', base: '/test/', - path: '/test/test.coffee' + path: '/test/test.coffee', }); file.extname.should.equal('.coffee'); done(); @@ -831,7 +831,7 @@ describe('File', function() { var file = new File({ cwd: '/', base: '/test/', - path: '/test/test.coffee' + path: '/test/test.coffee', }); file.extname = '.png'; file.path.should.equal('/test/test.png'); @@ -844,7 +844,7 @@ describe('File', function() { it('should record history when instantiation', function() { var file = new File({ cwd: '/', - path: '/test/test.coffee' + path: '/test/test.coffee', }); file.path.should.eql('/test/test.coffee'); @@ -854,7 +854,7 @@ describe('File', function() { it('should record history when path change', function() { var file = new File({ cwd: '/', - path: '/test/test.coffee' + path: '/test/test.coffee', }); file.path = '/test/test.js'; @@ -869,7 +869,7 @@ describe('File', function() { it('should not record history when set the same path', function() { var file = new File({ cwd: '/', - path: '/test/test.coffee' + path: '/test/test.coffee', }); file.path = '/test/test.coffee'; @@ -877,7 +877,7 @@ describe('File', function() { file.path.should.eql('/test/test.coffee'); file.history.should.eql(['/test/test.coffee']); - // ignore when set empty string + // Ignore when set empty string file.path = ''; file.path.should.eql('/test/test.coffee'); file.history.should.eql(['/test/test.coffee']); @@ -886,7 +886,7 @@ describe('File', function() { it('should throw when set path null', function() { var file = new File({ cwd: '/', - path: null + path: null, }); should.not.exist(file.path); diff --git a/test/cloneBuffer.js b/test/cloneBuffer.js index 34b3e7c..20ea8b5 100644 --- a/test/cloneBuffer.js +++ b/test/cloneBuffer.js @@ -17,7 +17,7 @@ describe('cloneBuffer()', function() { var testBuffer = new Buffer('test'); var testBuffer2 = cloneBuffer(testBuffer); - // test that changes dont modify both pointers + // Test that changes dont modify both pointers testBuffer2.write('w'); testBuffer.toString('utf8').should.equal('test', 'original should stay the same'); diff --git a/test/inspectStream.js b/test/inspectStream.js index fe1802c..eb82a3d 100644 --- a/test/inspectStream.js +++ b/test/inspectStream.js @@ -50,4 +50,4 @@ describe('inspectStream()', function() { should.not.exist(inspectStream(null)); done(); }); -}); \ No newline at end of file +});