├── .gitignore ├── .jshintrc ├── Gruntfile.js ├── LICENSE ├── README.md ├── package.json └── tasks └── fixmyjs.js /.gitignore: -------------------------------------------------------------------------------- 1 | *.DS_Store 2 | node_modules 3 | tmp 4 | *.sublime-* 5 | .DS_Store 6 | test -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "asi": false, 3 | "boss": true, 4 | "camelcase": true, 5 | "curly": true, 6 | "eqeqeq": true, 7 | "eqnull": true, 8 | "esnext": true, 9 | "immed": true, 10 | "latedef": false, 11 | "laxcomma": false, 12 | "mocha": true, 13 | "newcap": true, 14 | "noarg": true, 15 | "node": true, 16 | "sub": true, 17 | "undef": true, 18 | "unused": true 19 | } -------------------------------------------------------------------------------- /Gruntfile.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * grunt-fixmyjs 3 | * 4 | * Copyright (c) 2014-2015, Jon Schlinkert. 5 | * Licensed under the MIT License. 6 | */ 7 | 8 | 'use strict'; 9 | 10 | module.exports = function(grunt) { 11 | 12 | // Report the elapsed execution time of tasks. 13 | require('time-grunt')(grunt); 14 | 15 | // Project configuration. 16 | grunt.initConfig({ 17 | jshint: { 18 | all: ['Gruntfile.js', 'tasks/*.js'], 19 | options: {jshintrc: '.jshintrc'} 20 | }, 21 | 22 | // Before generating any new files, remove any previously-created files. 23 | clean: {tests: ['tmp/**/*']}, 24 | 25 | fixmyjs: { 26 | options: { 27 | curly: true, 28 | quotmark: 'single', 29 | plusplus: true, 30 | asi: false 31 | }, 32 | test: { 33 | files: [ 34 | { 35 | expand: true, 36 | cwd: 'fixtures', 37 | src: ['**/*.js'], 38 | dest: 'tmp/', 39 | ext: '.js' 40 | } 41 | ] 42 | } 43 | } 44 | }); 45 | 46 | // Actually load this plugin's task(s). 47 | grunt.loadTasks('tasks'); 48 | 49 | // These plugins provide necessary tasks. 50 | grunt.loadNpmTasks('grunt-contrib-jshint'); 51 | grunt.loadNpmTasks('grunt-contrib-clean'); 52 | 53 | // Whenever the "test" task is run, first clean the "tmp" dir, then run this 54 | // plugin's task(s), then test the result. 55 | grunt.registerTask('test', ['fixmyjs']); // 'nodeunit' 56 | 57 | // By default, lint and run all tests. 58 | grunt.registerTask('default', ['jshint', 'test']); 59 | 60 | }; 61 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013-2015, Jon Schlinkert. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # grunt-fixmyjs 2 | 3 | > Automatically fix silly lint errors. 4 | 5 | Please go to [fixmyjs](https://github.com/jshint/fixmyjs) to create issues or ask questions related to that project. This is just a grunt plugin for running fixmyjs. 6 | 7 | ## Getting Started 8 | _If you haven't used [grunt][] before, be sure to check out the [Getting Started][] guide._ 9 | 10 | From the same directory as your project's [Gruntfile][Getting Started] and [package.json][], install this plugin with the following command: 11 | 12 | ```bash 13 | npm install grunt-fixmyjs --save-dev 14 | ``` 15 | 16 | Once that's done, add this line to your project's Gruntfile: 17 | 18 | ```js 19 | grunt.loadNpmTasks('grunt-fixmyjs'); 20 | ``` 21 | 22 | If the plugin has been installed correctly, running `grunt --help` at the command line should list the newly-installed plugin's task or tasks. In addition, the plugin should be listed in package.json as a `devDependency`, which ensures that it will be installed whenever the `npm install` command is run. 23 | 24 | [grunt]: http://gruntjs.com/ 25 | [Getting Started]: https://github.com/gruntjs/grunt/blob/devel/docs/getting_started.md 26 | [package.json]: https://npmjs.org/doc/json.html 27 | 28 | ## The "fixmyjs" task 29 | 30 | ### Overview 31 | In your project's Gruntfile, add a section named `fixmyjs` to the data object passed into `grunt.initConfig()`. 32 | 33 | ```js 34 | grunt.initConfig({ 35 | fixmyjs: { 36 | options: { 37 | // Task-specific options go here. 38 | }, 39 | your_target: { 40 | // Target-specific file lists and/or options go here. 41 | } 42 | } 43 | }); 44 | ``` 45 | 46 | ### Options 47 | 48 | #### config 49 | Type: `Object` 50 | Default value: `.jshintrc` 51 | 52 | Load your own config file. 53 | 54 | #### diff 55 | Type: `Boolean` 56 | Default value: `False` 57 | 58 | Similar to dry-run 59 | 60 | #### legacy 61 | Type: `Boolean` 62 | Default value: `False` 63 | 64 | Use legacy fixmyjs 65 | 66 | #### indentpref 67 | Type: `String` 68 | Default value: `spaces` 69 | Options: `tabs|spaces` 70 | 71 | Your indentation preference 72 | 73 | #### patch 74 | Type: `Boolean` 75 | Default value: `False` 76 | 77 | Output a patch file to stdout 78 | 79 | #### dry 80 | Type: `Boolean` 81 | Default value: `False` 82 | 83 | Performs a dry-run and shows you a diff 84 | 85 | #### silent 86 | Type: `Boolean` 87 | Default value: `False` 88 | 89 | A useless option. 90 | 91 | ## Currently supports 92 | 93 | * `asi` Missing semicolons. 94 | * `camelcase|snakecase` Enforces camelCase and snake_case convention. 95 | * `curly` Adds curly braces to statements. 96 | * `debugger` Removes debugger statements 97 | * `plusplus` Converts plusplus and minusminus. 98 | * `quotmark` Enforces single and double quote style. 99 | * Adds parenthesis when invoking a constructor 100 | * Adds the radix parameter to parseInt 101 | * Convert to use array literal and object literal 102 | * Dot notation conversion 103 | * Extra trailing commas 104 | * Leading and trailing zeroes on decimals. 105 | * Missing whitespaces. 106 | * Mixed spaces/tabs 107 | * Proper indentation 108 | * Removes deletion of variables 109 | * Removes `undefined` when assigning to variables 110 | * Removes unnecessary semicolons 111 | * Uses isNaN function rather than comparing to NaN 112 | 113 | ### Usage Examples 114 | 115 | ```js 116 | grunt.initConfig({ 117 | fixmyjs: { 118 | options: { 119 | config: '.jshintrc', 120 | indentpref: 'spaces' 121 | }, 122 | test: { 123 | files: [ 124 | {expand: true, cwd: 'test/fixtures', src: ['**/*.js'], dest: 'test/actual/', ext: '.js'} 125 | ] 126 | } 127 | } 128 | }); 129 | ``` 130 | 131 | ## Related projects 132 | 133 | * [grunt-prettify](https://github.com/jonschlinkert/grunt-prettify) 134 | * [grunt-refactor](https://github.com/jonschlinkert/grunt-refactor) 135 | * [grunt-js2coffee](https://github.com/jonschlinkert/grunt-js2coffee) 136 | 137 | 138 | ## Contributing 139 | In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [grunt][]. 140 | 141 | 142 | ## Author 143 | 144 | * [Jon Schlinkert](https://github.com/jonschlinkert) 145 | 146 | 147 | ## Release History 148 | 149 | * 2013-08-17   v0.1.0   First commit. 150 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "grunt-fixmyjs", 3 | "description": "Automatically fix JavaScript lint errors.", 4 | "version": "0.3.0", 5 | "homepage": "https://github.com/jonschlinkert/grunt-fixmyjs", 6 | "author": { 7 | "name": "Jon Schlinkert", 8 | "url": "https://github.com/jonschlinkert" 9 | }, 10 | "repository": "jonschlinkert/grunt-fixmyjs", 11 | "bugs": { 12 | "url": "https://github.com/jonschlinkert/grunt-fixmyjs/issues" 13 | }, 14 | "license": "MIT", 15 | "files": [ 16 | "tasks/" 17 | ], 18 | "main": "Gruntfile.js", 19 | "engines": { 20 | "node": ">= 0.8.0" 21 | }, 22 | "scripts": { 23 | "test": "grunt test" 24 | }, 25 | "dependencies": { 26 | "chalk": "^0.5.1", 27 | "fixmyjs": "^1.0.3", 28 | "jshint": "^2.6.0", 29 | "json5": "^0.4.0" 30 | }, 31 | "devDependencies": { 32 | "grunt": "^0.4.5", 33 | "grunt-contrib-clean": "^0.6.0", 34 | "grunt-contrib-jshint": "^0.10.0", 35 | "grunt-contrib-nodeunit": "^0.4.1", 36 | "time-grunt": "^1.0.0" 37 | }, 38 | "keywords": [ 39 | "fix", 40 | "fixmyjs", 41 | "grunt", 42 | "gruntplugin", 43 | "hint", 44 | "js", 45 | "jshint", 46 | "lint", 47 | "linting", 48 | "refactor", 49 | "task" 50 | ] 51 | } -------------------------------------------------------------------------------- /tasks/fixmyjs.js: -------------------------------------------------------------------------------- 1 | /* 2 | * grunt-fixmyjs 3 | * https://github.com/jonschlinkert/grunt-fixmyjs 4 | * 5 | * Copyright (c) 2013 Jon Schlinkert 6 | * Licensed under the MIT license. 7 | */ 8 | 9 | 'use strict'; 10 | 11 | module.exports = function(grunt) { 12 | 13 | var chalk = require('chalk'); 14 | var fixmyjs = require('fixmyjs'); 15 | var JSON5 = require('json5'); 16 | var _ = grunt.util._; 17 | 18 | grunt.task.registerMultiTask('fixmyjs', 'Fix your JavaScript.', function() { 19 | var options = this.options({ 20 | config: '.jshintrc' 21 | }); 22 | 23 | // Extend default options with options from specified jshintrc file 24 | if (options.config) { 25 | options = _.extend(options, JSON5.parse(grunt.file.read(options.config))); 26 | } 27 | 28 | // Iterate over all specified file groups. 29 | this.files.forEach(function(fp) { 30 | var srcFile = fp.src.filter(function(filepath) { 31 | if (!grunt.file.exists(filepath)) { 32 | grunt.log.warn('Source file "' + filepath + '" not found.'); 33 | return false; 34 | } else { 35 | return true; 36 | } 37 | }).map(grunt.file.read).join(grunt.util.normalizelf(grunt.util.linefeed)); 38 | 39 | var escaped; 40 | // First, comment out '#!/usr/bin/env node' since it breaks fixmyjs. 41 | if (srcFile.indexOf('#!/usr') === 0) { 42 | escaped = true; 43 | srcFile = '//' + srcFile; 44 | } 45 | 46 | // Handle options. 47 | var fixedjs = fix(srcFile, options); 48 | if (fixedjs.length < 1) { 49 | grunt.log.warn('Destination not written because dest file was empty.'); 50 | } else { 51 | if (escaped) { 52 | fixedjs = fixedjs.slice(2); 53 | } 54 | grunt.file.write(fp.dest, fixedjs); 55 | 56 | // Print a success message. 57 | grunt.log.ok('File "' + fp.dest + '" fixed' + chalk.green('...OK')); 58 | } 59 | }); 60 | }); 61 | 62 | function fix(source, options) { 63 | try { 64 | if (options.legacy) { 65 | var jshint = require('jshint').JSHINT; 66 | jshint(source, options); 67 | return fixmyjs(jshint.data(), source, options).run(); 68 | } 69 | return fixmyjs.fix(source, options); 70 | } catch (e) { 71 | grunt.log.error(e); 72 | grunt.fail.warn('JavaScript fixification failed.'); 73 | } 74 | } 75 | }; 76 | --------------------------------------------------------------------------------