Skip to content

Commit

Permalink
Add no-process-exit rule (#1)
Browse files Browse the repository at this point in the history
Adds `no-process-exit` rule. Same as the ESLint core rule, but with the exception of files that start with a hasbang `#!`.

@sindresorhus asked me if I could implement this rule, and I thought it was fit to make a PR here.
  • Loading branch information
jfmengels authored and sindresorhus committed Apr 12, 2016
1 parent 4ee1493 commit 7d10fc0
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 1 deletion.
18 changes: 18 additions & 0 deletions docs/rules/no-process-exit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Disallow `process.exit()`

This rule is an extension to [ESLint's `no-process-exit`](http://eslint.org/docs/rules/no-process-exit) rule, that allows `process.exit()` to be called in files that start with a hashbang `#!/usr/bin/env node`.


## Fail

```js
process.exit(0);
```


## Pass

```js
#!/usr/bin/env node
process.exit(0);
```
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module.exports = {
rules: {
'no-process-exit': require('./rules/no-process-exit'),
'throw-new-error': require('./rules/throw-new-error')
},
configs: {
Expand All @@ -14,6 +15,7 @@ module.exports = {
sourceType: 'module'
},
rules: {
'xo/no-process-exit': 'error',
'xo/throw-new-error': 'error'
}
}
Expand Down
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ Configure it in `package.json`.
"xo"
],
"rules": {
"xo/throw-new-error": "error",
"xo/no-process-exit": "error",
"xo/throw-new-error": "error"
}
}
}
Expand All @@ -40,6 +41,7 @@ Configure it in `package.json`.

## Rules

- [no-process-exit](docs/rules/no-process-exit.md) - Disallow `process.exit()`.
- [throw-new-error](docs/rules/throw-new-error.md) - Require `new` when throwing an error. *(fixable)*


Expand Down
18 changes: 18 additions & 0 deletions rules/no-process-exit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';
module.exports = function (context) {
var startsWithHashBang = context.getSourceCode().lines[0].indexOf('#!') === 0;

if (startsWithHashBang) {
return {};
}

return {
CallExpression: function (node) {
var callee = node.callee;

if (callee.type === 'MemberExpression' && callee.object.name === 'process' && callee.property.name === 'exit') {
context.report(node, 'Only use `process.exit()` in CLI apps. Instead, throw an error.');
}
}
};
};
37 changes: 37 additions & 0 deletions test/no-process-exit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import test from 'ava';
import {RuleTester} from 'eslint';
import rule from '../rules/no-process-exit';

const ruleTester = new RuleTester({
env: {
es6: true
}
});

const errors = [{ruleId: 'no-process-exit'}];

test(() => {
ruleTester.run('no-process-exit', rule, {
valid: [
'#!/usr/bin/env node\n\nprocess.exit();',
'Process.exit()',
'const x = process.exit;',
'x(process.exit)',
''
],
invalid: [
{
code: 'process.exit();',
errors
},
{
code: 'process.exit(1);',
errors
},
{
code: 'x(process.exit(1));',
errors
}
]
});
});

0 comments on commit 7d10fc0

Please sign in to comment.