Skip to content

Commit e789d3a

Browse files
committed
Add no-process-exit rule
1 parent 4ee1493 commit e789d3a

File tree

5 files changed

+84
-1
lines changed

5 files changed

+84
-1
lines changed

docs/rules/no-process-exit.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Disallow `process.exit()`
2+
3+
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`.
4+
5+
6+
## Fail
7+
8+
```js
9+
process.exit(0);
10+
```
11+
12+
13+
## Pass
14+
15+
```js
16+
#!/usr/bin/env node
17+
process.exit(0);
18+
```

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
module.exports = {
44
rules: {
5+
'no-process-exit': require('./rules/no-process-exit'),
56
'throw-new-error': require('./rules/throw-new-error')
67
},
78
configs: {
@@ -14,6 +15,7 @@ module.exports = {
1415
sourceType: 'module'
1516
},
1617
rules: {
18+
'xo/no-process-exit': 'error',
1719
'xo/throw-new-error': 'error'
1820
}
1921
}

readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ Configure it in `package.json`.
3131
"xo"
3232
],
3333
"rules": {
34-
"xo/throw-new-error": "error",
34+
"xo/no-process-exit": "error",
35+
"xo/throw-new-error": "error"
3536
}
3637
}
3738
}
@@ -40,6 +41,7 @@ Configure it in `package.json`.
4041

4142
## Rules
4243

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

4547

rules/no-process-exit.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
module.exports = function (context) {
4+
var startsWithHashBang = context.getSourceCode().lines[0].indexOf('#!') === 0;
5+
if (startsWithHashBang) {
6+
return {};
7+
}
8+
9+
return {
10+
CallExpression: function (node) {
11+
var callee = node.callee;
12+
13+
if (callee.type === 'MemberExpression' &&
14+
callee.object.name === 'process' &&
15+
callee.property.name === 'exit') {
16+
context.report(node, 'Only use process.exit() in CLI apps. Throw an error instead.');
17+
}
18+
}
19+
};
20+
};

test/no-process-exit.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import test from 'ava';
2+
import {RuleTester} from 'eslint';
3+
import rule from '../rules/no-process-exit';
4+
5+
const ruleTester = new RuleTester({
6+
env: {
7+
es6: true
8+
}
9+
});
10+
11+
const errors = [{
12+
ruleId: 'no-process-exit',
13+
message: 'Only use process.exit() in CLI apps. Throw an error instead.',
14+
type: 'CallExpression'
15+
}];
16+
17+
test(() => {
18+
ruleTester.run('no-process-exit', rule, {
19+
valid: [
20+
'Process.exit()',
21+
'const exit = process.exit;',
22+
'x(process.exit)',
23+
'#!/usr/bin/env node\nprocess.exit(0);',
24+
''
25+
],
26+
invalid: [
27+
{
28+
code: 'process.exit();',
29+
errors
30+
},
31+
{
32+
code: 'process.exit(1);',
33+
errors
34+
},
35+
{
36+
code: 'x(process.exit(1));',
37+
errors
38+
}
39+
]
40+
});
41+
});

0 commit comments

Comments
 (0)