├── .editorconfig ├── .gitattributes ├── .gitignore ├── .jshintrc ├── .travis.yml ├── LICENSE ├── README.md ├── index.js ├── package.json └── test ├── expected ├── after.js ├── afterEqEqEq.js └── afterNoEqEqEq.js ├── fixtures ├── before.js └── invalid.js └── main.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 2 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | 15 | [test/fixtures/*] 16 | insert_final_newline = false 17 | trim_trailing_whitespace = false 18 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules/ 3 | temp/ 4 | coverage 5 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "node": true, 3 | "esnext": true, 4 | "bitwise": true, 5 | "camelcase": true, 6 | "curly": true, 7 | "eqeqeq": true, 8 | "immed": true, 9 | "indent": 4, 10 | "latedef": true, 11 | "newcap": true, 12 | "noarg": true, 13 | "quotmark": "double", 14 | "regexp": true, 15 | "undef": true, 16 | "unused": true, 17 | "strict": true, 18 | "trailing": true, 19 | "smarttabs": true, 20 | "white": true 21 | } 22 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '0.10' 4 | - '0.11' 5 | after_script: 6 | - npm run coveralls 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2014 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gulp-fixmyjs 2 | [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependency Status][depstat-image]][depstat-url] 3 | 4 | 5 | > [fixmyjs](https://github.com/jshint/fixmyjs) plugin for [gulp](https://github.com/wearefractal/gulp) 6 | 7 | ## Usage 8 | 9 | First, install `gulp-fixmyjs` as a development dependency: 10 | 11 | ```shell 12 | npm install --save-dev gulp-fixmyjs 13 | ``` 14 | 15 | Then, add it to your `gulpfile.js`: 16 | 17 | ```javascript 18 | var fixmyjs = require("gulp-fixmyjs"); 19 | 20 | gulp.src("./src/*.js") 21 | .pipe(fixmyjs({ 22 | // JSHint settings here 23 | })) 24 | .pipe(gulp.dest("./src")); 25 | ``` 26 | 27 | ## API 28 | 29 | ### fixmyjs(options) 30 | 31 | #### options.legacy 32 | Type: **Boolean**, Default: false 33 | 34 | Enables legacy mode 35 | 36 | #### options.lookup 37 | Type: **Boolean**, Default: true 38 | 39 | Enables looking up the .jshintrc configuration file 40 | 41 | #### JSHint options 42 | Any of the [JSHint options](http://www.jshint.com/docs/options/) will be passed to fixmyjs, and would get precedence over .jshintrc file 43 | 44 | 45 | 46 | ## License 47 | 48 | [MIT License](http://en.wikipedia.org/wiki/MIT_License) 49 | 50 | [npm-url]: https://npmjs.org/package/gulp-fixmyjs 51 | [npm-image]: https://badge.fury.io/js/gulp-fixmyjs.svg 52 | 53 | [travis-url]: http://travis-ci.org/kirjs/gulp-fixmyjs 54 | [travis-image]: https://secure.travis-ci.org/kirjs/gulp-fixmyjs.svg?branch=master 55 | 56 | [coveralls-url]: https://coveralls.io/r/kcherkashin/gulp-fixmyjs 57 | [coveralls-image]: https://img.shields.io/coveralls/kcherkashin/gulp-fixmyjs.svg 58 | 59 | [depstat-url]: https://david-dm.org/kirjs/gulp-fixmyjs 60 | [depstat-image]: https://david-dm.org/kirjs/gulp-fixmyjs.svg 61 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var through = require("through2"), 2 | gutil = require("gulp-util"), 3 | fixmyjs = require("fixmyjs"), 4 | RcLoader = require('rcloader'), 5 | jshint = require('jshint').JSHINT; 6 | 7 | module.exports = function (options) { 8 | "use strict"; 9 | 10 | options = options || {}; 11 | if (options.lookup === void 0) { 12 | options.lookup = true; 13 | } 14 | if (options.legacy === void 0) { 15 | options.legacy = false; 16 | } 17 | 18 | if (options.lookup) { 19 | var rcLoader = new RcLoader('.jshintrc', options, {}); 20 | } 21 | 22 | var fixJS = function (contents, config) { 23 | return fixmyjs.fix(contents, config); 24 | }; 25 | 26 | 27 | if (options.legacy) { 28 | fixJS = function (contents, config) { 29 | jshint(contents, config); 30 | return fixmyjs(jshint.data(), contents, config).run(); 31 | }; 32 | } 33 | 34 | 35 | function error(message) { 36 | return new gutil.PluginError("gulp-fixmyjs", message); 37 | } 38 | 39 | function gulpFixmyjs(file, enc, callback) { 40 | /*jshint validthis:true*/ 41 | 42 | 43 | // Do nothing if no contents 44 | if (file.isNull()) { 45 | this.push(file); 46 | return callback(); 47 | } 48 | 49 | 50 | if (file.isStream()) { 51 | this.emit("error", error("Stream content is not supported")); 52 | return callback(); 53 | } 54 | 55 | 56 | if (file.isBuffer()) { 57 | var fix = function (err, config) { 58 | if (err) { 59 | this.emit("error", error("Error when reading .jshint config:\n" + err)); 60 | } else { 61 | try { 62 | file.contents = new Buffer(fixJS(String(file.contents), config)); 63 | this.push(file); 64 | } catch (e) { 65 | this.emit("error", error("Error when running fixmyjs:\n" + e.stack)); 66 | } 67 | } 68 | return callback(); 69 | }.bind(this); 70 | 71 | 72 | if (options.lookup) { 73 | rcLoader.for(file.path, fix); 74 | } else { 75 | fix(null, options); 76 | } 77 | 78 | 79 | } 80 | } 81 | 82 | 83 | return through.obj(gulpFixmyjs); 84 | }; 85 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gulp-fixmyjs", 3 | "version": "1.0.2", 4 | "description": "fixmyjs plugin for gulp", 5 | "keywords": [ 6 | "fix", 7 | "gulpplugin", 8 | "fixmyjs", 9 | "jshint", 10 | "linting", 11 | "js", 12 | "lint" 13 | ], 14 | "author": { 15 | "name": "Kirill Cherkashin", 16 | "url": "https://github.com/kirjs" 17 | }, 18 | "repository": "kirjs/gulp-fixmyjs", 19 | "files": [ 20 | "index.js" 21 | ], 22 | "scripts": { 23 | "test": "istanbul test _mocha --report html -- test/*.js --reporter spec", 24 | "test-debug": "node --debug-brk istanbul test _mocha --report html -- test/*.js --reporter spec", 25 | "coveralls": "istanbul cover _mocha --report lcovonly -- -R spec && istanbul-coveralls" 26 | }, 27 | "dependencies": { 28 | "fixmyjs": "*", 29 | "gulp-util": "^3.0.1", 30 | "jshint": "~2.9.3", 31 | "rcloader": "~0.2.1", 32 | "through2": "*" 33 | }, 34 | "devDependencies": { 35 | "coveralls": "*", 36 | "event-stream": "*", 37 | "istanbul": "*", 38 | "istanbul-coveralls": "^1.0.1", 39 | "mocha": "*", 40 | "mocha-lcov-reporter": "*", 41 | "should": "^11.1.0" 42 | }, 43 | "engines": { 44 | "node": ">=0.9.0", 45 | "npm": ">=1.2.10" 46 | }, 47 | "license": "MIT" 48 | } 49 | -------------------------------------------------------------------------------- /test/expected/after.js: -------------------------------------------------------------------------------- 1 | var x = 1; 2 | v == 3; -------------------------------------------------------------------------------- /test/expected/afterEqEqEq.js: -------------------------------------------------------------------------------- 1 | var x = 1; 2 | v === 3; -------------------------------------------------------------------------------- /test/expected/afterNoEqEqEq.js: -------------------------------------------------------------------------------- 1 | var x = 1; 2 | v == 3; -------------------------------------------------------------------------------- /test/fixtures/before.js: -------------------------------------------------------------------------------- 1 | var x = 1 2 | v == 3 -------------------------------------------------------------------------------- /test/fixtures/invalid.js: -------------------------------------------------------------------------------- 1 | I @M 1n\/@l1d JS -------------------------------------------------------------------------------- /test/main.js: -------------------------------------------------------------------------------- 1 | /*global describe, it*/ 2 | "use strict"; 3 | 4 | var fs = require("fs"), 5 | es = require("event-stream"), 6 | should = require("should"); 7 | 8 | require("mocha"); 9 | 10 | delete require.cache[require.resolve("../")]; 11 | 12 | var gutil = require("gulp-util"), 13 | fixmyjs = require("../"); 14 | 15 | describe("gulp-fixmyjs", function () { 16 | 17 | var expectedFile = new gutil.File({ 18 | path: "test/expected/after.js", 19 | cwd: "test/", 20 | base: "test/expected", 21 | contents: fs.readFileSync("test/expected/after.js") 22 | }); 23 | 24 | it("should produce expected file via buffer", function (done) { 25 | 26 | var srcFile = new gutil.File({ 27 | path: "test/fixtures/before.js", 28 | cwd: "test/", 29 | base: "test/fixtures", 30 | contents: fs.readFileSync("test/fixtures/before.js") 31 | }); 32 | 33 | var stream = fixmyjs({lookup: false}); 34 | 35 | stream.on("error", function (err) { 36 | should.exist(err); 37 | done(err); 38 | }); 39 | 40 | stream.on("data", function (newFile) { 41 | 42 | should.exist(newFile); 43 | should.exist(newFile.contents); 44 | 45 | String(newFile.contents).should.equal(String(expectedFile.contents)); 46 | done(); 47 | }); 48 | 49 | stream.write(srcFile); 50 | stream.end(); 51 | }); 52 | 53 | 54 | describe("handling js hint parameters", function () { 55 | 56 | it("Doesn't not replace == to === if eqeqeq is false", function (done) { 57 | var expectedFileNoEqEqEq = new gutil.File({ 58 | path: "test/expected/afterNoEqEqEq.js", 59 | cwd: "test/", 60 | base: "test/expected", 61 | contents: fs.readFileSync("test/expected/afterNoEqEqEq.js") 62 | }); 63 | 64 | var srcFile = new gutil.File({ 65 | path: "test/fixtures/before.js", 66 | cwd: "test/", 67 | base: "test/fixtures", 68 | contents: fs.readFileSync("test/fixtures/before.js") 69 | }); 70 | 71 | var stream = fixmyjs({lookup: false, eqeqeq: false}); 72 | 73 | stream.on("error", function (err) { 74 | should.exist(err); 75 | done(err); 76 | }); 77 | 78 | stream.on("data", function (newFile) { 79 | 80 | should.exist(newFile); 81 | should.exist(newFile.contents); 82 | 83 | String(newFile.contents).should.equal(String(expectedFileNoEqEqEq.contents)); 84 | done(); 85 | }); 86 | 87 | stream.write(srcFile); 88 | stream.end(); 89 | }); 90 | it("Does replace == to === if eqeqeq is true", function (done) { 91 | var expectedFileEqEqEq = new gutil.File({ 92 | path: "test/expected/afterEqEqEq.js", 93 | cwd: "test/", 94 | base: "test/expected", 95 | contents: fs.readFileSync("test/expected/afterEqEqEq.js") 96 | }); 97 | 98 | var srcFile = new gutil.File({ 99 | path: "test/fixtures/before.js", 100 | cwd: "test/", 101 | base: "test/fixtures", 102 | contents: fs.readFileSync("test/fixtures/before.js") 103 | }); 104 | 105 | var stream = fixmyjs({lookup: false, eqeqeq: true}); 106 | 107 | stream.on("error", function (err) { 108 | should.exist(err); 109 | done(err); 110 | }); 111 | 112 | stream.on("data", function (newFile) { 113 | should.exist(newFile); 114 | should.exist(newFile.contents); 115 | String(newFile.contents).should.equal(String(expectedFileEqEqEq.contents)); 116 | done(); 117 | }); 118 | 119 | stream.write(srcFile); 120 | stream.end(); 121 | }); 122 | 123 | }); 124 | 125 | it("should error on invalid JS", function (done) { 126 | 127 | var srcFile = new gutil.File({ 128 | path: "test/fixtures/invalid.js", 129 | cwd: "test/", 130 | base: "test/fixtures", 131 | contents: fs.readFileSync("test/fixtures/invalid.js") 132 | }); 133 | 134 | var stream = fixmyjs({lookup: false}); 135 | stream.on("error", function (err) { 136 | should.exist(err); 137 | done(); 138 | }); 139 | 140 | stream.on("data", function (newFile) { 141 | newFile.contents.pipe(es.wait(function (err, data) { 142 | done(err); 143 | })); 144 | }); 145 | 146 | stream.write(srcFile); 147 | stream.end(); 148 | }); 149 | 150 | it("should error on stream", function (done) { 151 | 152 | var srcFile = new gutil.File({ 153 | path: "test/fixtures/before.js", 154 | cwd: "test/", 155 | base: "test/fixtures", 156 | contents: fs.createReadStream("test/fixtures/before.js") 157 | }); 158 | 159 | var stream = fixmyjs({lookup: false}); 160 | 161 | stream.on("error", function (err) { 162 | should.exist(err); 163 | done(); 164 | }); 165 | 166 | stream.on("data", function (newFile) { 167 | newFile.contents.pipe(es.wait(function (err, data) { 168 | done(err); 169 | })); 170 | }); 171 | 172 | stream.write(srcFile); 173 | stream.end(); 174 | }); 175 | }); --------------------------------------------------------------------------------