Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add no-process-exit rule #1

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
20 changes: 20 additions & 0 deletions rules/no-process-exit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

module.exports = function (context) {
var startsWithHashBang = context.getSourceCode().lines[0].indexOf('#!') === 0;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add an empty line below here.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you just do the early return here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about that, and lazily did not try it out, but it seems to work 👍

if (startsWithHashBang) {
return {};
}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add an empty line below here.


if (callee.type === 'MemberExpression' &&
callee.object.name === 'process' &&
callee.property.name === 'exit') {
context.report(node, 'Only use process.exit() in CLI apps. Throw an error instead.');
}
}
};
};
41 changes: 41 additions & 0 deletions test/no-process-exit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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',
message: 'Only use process.exit() in CLI apps. Throw an error instead.',
type: 'CallExpression'
}];

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