Comparing version 1.1.0 to 1.3.0
#!/usr/bin/env node | ||
var standard = require('../') | ||
standard(process.cwd()) | ||
var flag = process.argv[2] | ||
if (flag === '--version') { | ||
console.log(require('../package.json').version) | ||
process.exit(0) | ||
} | ||
standard({ | ||
cwd: process.cwd(), | ||
verbose: flag === '--verbose' || flag === '-v' | ||
}) |
116
index.js
var cp = require('child_process') | ||
var find = require('find') | ||
var findRoot = require('find-root') | ||
var glob = require('glob') | ||
var Minimatch = require('minimatch').Minimatch | ||
var path = require('path') | ||
@@ -12,2 +14,3 @@ var split = require('split') | ||
var JSCS_REPORTER = path.join(__dirname, 'lib', 'jscs-reporter.js') | ||
var JSCS_REPORTER_VERBOSE = path.join(__dirname, 'lib', 'jscs-reporter-verbose.js') | ||
@@ -19,14 +22,87 @@ if (/^win/.test(process.platform)) { | ||
module.exports = function (dir) { | ||
find.file(/\.js$/, dir || process.cwd(), function (files) { | ||
var DEFAULT_IGNORE = [ | ||
'node_modules/**', | ||
'.git/**', | ||
'**/*.min.js', | ||
'**/bundle.js' | ||
] | ||
var FILE_RE = /(.*?):/ | ||
var LINE_RE = /.*?:(\d+)/ | ||
var COL_RE = /.*?:\d+:(\d+)/ | ||
module.exports = function (opts) { | ||
if (!opts) opts = {} | ||
var errors = [] | ||
var root | ||
try { | ||
root = findRoot(process.cwd()) | ||
} catch (e) {} | ||
var ignore = [].concat(DEFAULT_IGNORE) // globs to ignore | ||
if (root) { | ||
var packageOpts = require(path.join(root, 'package.json')).standard | ||
if (packageOpts) ignore = ignore.concat(packageOpts.ignore) | ||
} | ||
if (opts.ignore) ignore = ignore.concat(opts.ignore) | ||
ignore = ignore.map(function (pattern) { | ||
return new Minimatch(pattern) | ||
}) | ||
glob('**/*.js', { | ||
cwd: opts.cwd || process.cwd() | ||
}, function (err, files) { | ||
if (err) return error(err) | ||
files = files.filter(function (file) { | ||
return !/[\/\\]node_modules[\/\\]|[\/\\].git[\/\\]|\.min.js$|\/bundle.js$/.test(file) | ||
return !ignore.some(function (mm) { | ||
return mm.match(file) | ||
}) | ||
}) | ||
var jshintArgs = ['--config', JSHINTRC, '--reporter', 'unix'].concat(files) | ||
var jscsArgs = ['--config', JSCSRC, '--reporter', JSCS_REPORTER].concat(files) | ||
var jshintArgs = ['--config', JSHINTRC, '--reporter', 'unix'] | ||
var jscsReporter = opts.verbose ? JSCS_REPORTER_VERBOSE : JSCS_REPORTER | ||
var jscsArgs = ['--config', JSCSRC, '--reporter', jscsReporter] | ||
if (opts.verbose) { | ||
jshintArgs.push('--verbose') | ||
jscsArgs.push('--verbose') | ||
} | ||
jshintArgs = jshintArgs.concat(files) | ||
jscsArgs = jscsArgs.concat(files) | ||
var jshint = spawn(JSHINT, jshintArgs, function (jshintErr) { | ||
var jscs = spawn(JSCS, jscsArgs, function (jscsErr) { | ||
if (jshintErr || jscsErr) { | ||
console.error('Error: Code style check failed:') | ||
var errMap = {} | ||
errors | ||
.filter(function (str) { // de-duplicate errors | ||
if (errMap[str]) return false | ||
errMap[str] = true | ||
return true | ||
}) | ||
.sort(function (a, b) { | ||
// sort by line number (merges jshint and jscs output) | ||
var fileA = FILE_RE.exec(a)[1] | ||
var fileB = FILE_RE.exec(b)[1] | ||
var lineA = Number(LINE_RE.exec(a)[1]) | ||
var lineB = Number(LINE_RE.exec(b)[1]) | ||
var colA = Number(COL_RE.exec(a)[1]) | ||
var colB = Number(COL_RE.exec(b)[1]) | ||
if (fileA !== fileB) return fileA < fileB ? -1 : 1 | ||
if (lineA !== lineB) return lineA - lineB | ||
return colA - colB | ||
}) | ||
.forEach(function (str) { | ||
console.error(' ' + str) // indent | ||
}) | ||
process.exit(1) | ||
@@ -43,5 +119,12 @@ } | ||
}) | ||
.error(function (err) { | ||
if (err) error(err) | ||
}) | ||
function stderrPipe (readable) { | ||
readable | ||
.pipe(split()) | ||
.on('data', function (line) { | ||
if (line === '') return | ||
if (/^\d+ errors?/.test(line)) return | ||
errors.push(line) | ||
}) | ||
} | ||
} | ||
@@ -63,16 +146,1 @@ | ||
} | ||
var errored = false | ||
function stderrPipe (readable) { | ||
readable | ||
.pipe(split()) | ||
.on('data', function (line) { | ||
if (line === '') return | ||
if (/^\d+ errors?/.test(line)) return | ||
if (!errored) { | ||
errored = true | ||
console.error('Error: Code style check failed:') | ||
} | ||
console.error(' ' + line) | ||
}) | ||
} |
{ | ||
"name": "standard", | ||
"description": "JavaScript Standard Style", | ||
"version": "1.1.0", | ||
"version": "1.3.0", | ||
"author": { | ||
@@ -17,5 +17,7 @@ "name": "Feross Aboukhadijeh", | ||
"dependencies": { | ||
"find": "^0.1.7", | ||
"find-root": "^0.1.1", | ||
"glob": "^4.3.5", | ||
"jscs": "^1.10.0", | ||
"jshint": "^2.6.0", | ||
"minimatch": "^2.0.1", | ||
"split": "^0.3.2" | ||
@@ -22,0 +24,0 @@ }, |
@@ -30,12 +30,13 @@ # JavaScript Standard Style | ||
- **No semicolons** | ||
- [It's totally fine.][1] *[Really!][2]* | ||
- Never start a line with `(` or `[`: | ||
- This is the *one* gotcha with omitting semicolons – *automatically checked* for you! | ||
- [It's totally][1] [fine.][2] *[Really!][3]* | ||
- Never start a line with `(` or `[` | ||
- This is the **only** gotcha with omitting semicolons – *automatically checked for you!* | ||
- Always prefix with `;` like this `;[1, 2, 3].join(' ')` | ||
- Spaces after keywords: | ||
- Spaces after keywords | ||
- `if (condition) { ... }` | ||
- Spaces before/after function definitions:, like this: | ||
- Spaces before/after function definitions | ||
- `function name (arg1, arg2) { ... }` | ||
- Always name the context variable `self`: | ||
- Always name the context variable `self` | ||
- `var self = this` | ||
- Checks for accidental use of [`window.self`][4] when `var self = this` is forgotten | ||
- Always use `===` instead of `==` | ||
@@ -46,2 +47,4 @@ - Dozens of sanity checks to catch bugs (unused variables, typos, etc.) | ||
[2]: http://inimino.org/~inimino/blog/javascript_semicolons | ||
[3]: https://github.com/maxogden/messages/issues/18 | ||
[4]: https://developer.mozilla.org/en-US/docs/Web/API/Window.self | ||
@@ -54,6 +57,6 @@ To get a better idea, take a look at | ||
The easiest way to use `standard` is to install it globally as a Node command line | ||
program. To do so, simply run the following command in your terminal (flag `-g` installs | ||
`standard` globally on your system, omit it if you want to install in the current working | ||
directory): | ||
The easiest way to use JavaScript Standard Style to check your code is to install it | ||
globally as a Node command line program. To do so, simply run the following command in | ||
your terminal (flag `-g` installs `standard` globally on your system, omit it if you want | ||
to install in the current working directory): | ||
@@ -73,5 +76,2 @@ ```bash | ||
The paths `node_modules/`, `.git/`, `*.min.js`, and `bundle.js` are automatically excluded | ||
when looking for `.js` files to style check. | ||
### What you might do if you're clever | ||
@@ -103,4 +103,74 @@ | ||
## FAQ | ||
### Why would I use JavaScript Standard Style? | ||
The beauty of JavaScript Standard Style is that it's simple. No one wants to maintain | ||
multiple hundred-line `.jshintrc` and `.jscs` for every module/project they maintain. | ||
Enough of this madness! | ||
This module saves you time in two ways: | ||
- **No configuration.** Just drop it in. The easiest way to enforce consistent style in | ||
your module/project. | ||
- **Catch style errors before they're submitted in PRs.** Saves precious code review time | ||
by eliminating back-and-forth between maintainer and contributor. | ||
### How do I ignore files? | ||
The paths `node_modules/`, `.git/`, `*.min.js`, and `bundle.js` are automatically excluded | ||
when looking for `.js` files to style check. | ||
Sometimes you need to ignore additional folders or specific minfied files. To do that, add | ||
a `standard.ignore` property to `package.json`: | ||
```json | ||
"standard": { | ||
"ignore": ["**/out/**", "**/lib/select2/**", "**/lib/ckeditor/**"] | ||
} | ||
``` | ||
### How do I hide a certain warning? | ||
In rare cases, you'll need to break a rule and hide the warning generated by `standard`. | ||
`standard` uses [`jshint`](http://jshint.com/) and [`jscs`](http://jscs.info/) | ||
under-the-hood and you can hide their warnings as you normally would if you used each | ||
linter directly. | ||
To get verbose output (so you can find the particular rule name to ignore), run: | ||
```bash | ||
$ standard --verbose | ||
Error: Code style check failed: | ||
routes/error.js:20:36: 'next' is defined but never used. (W098) | ||
routes/submit.js:85:2: Expected indentation of 2 characters (validateIndentation) | ||
``` | ||
The first warning is `jshint` (always starts with a `W`). You can hide it with a | ||
`/* jshint -W098 */` comment. Re-enable with a `/* jshint +W098 */` comment. | ||
Example: | ||
```js | ||
/* jshint -W098 */ | ||
app.use(function (err, req, res, next) { | ||
res.render('error', { err: err }) | ||
}) | ||
/* jshint +W098 */ | ||
``` | ||
The second warning is from `jscs` (always a long camel-case string), which you can hide | ||
with a `// jscs:disable validateIndentation` comment. Re-enable with a | ||
`// jscs:enable validateIndentation` comment. | ||
### Can you please add more config options? | ||
No. Use `jshint` or `jscs` directly if you want that. | ||
Pro tip: Just use `standard` and move on. There are actual real problems that you could | ||
spend your time solving :p | ||
## License | ||
MIT. Copyright (c) [Feross Aboukhadijeh](http://feross.org). |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15626
10
159
172
6
1
+ Addedfind-root@^0.1.1
+ Addedglob@^4.3.5
+ Addedminimatch@^2.0.1
+ Addedfind-root@0.1.2(transitive)
+ Addedglob@4.5.3(transitive)
- Removedfind@^0.1.7
- Removedfind@0.1.7(transitive)
- Removedtraverse-chain@0.1.0(transitive)