Skip to content

Commit

Permalink
no-process-exit: Ignore when using node:worker_threads module (#2136
Browse files Browse the repository at this point in the history
)
  • Loading branch information
fisker committed May 18, 2023
1 parent cb6782a commit 2907805
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
23 changes: 12 additions & 11 deletions rules/no-process-exit.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
'use strict';
const {isStaticRequire, isMethodCall} = require('./ast/index.js');
const {isStaticRequire, isMethodCall, isLiteral} = require('./ast/index.js');

const MESSAGE_ID = 'no-process-exit';
const messages = {
[MESSAGE_ID]: 'Only use `process.exit()` in CLI apps. Throw an error instead.',
};

const isWorkerThreads = node =>
isLiteral(node, 'node:worker_threads')
|| isLiteral(node, 'worker_threads');

/** @param {import('eslint').Rule.RuleContext} context */
const create = context => {
const startsWithHashBang = context.sourceCode.lines[0].indexOf('#!') === 0;
Expand All @@ -24,8 +28,7 @@ const create = context => {
context.on('CallExpression', callExpression => {
if (
isStaticRequire(callExpression)
// TODO: Support `node:worker_threads`
&& callExpression.arguments[0].value === 'worker_threads'
&& isWorkerThreads(callExpression.arguments[0])
) {
requiredWorkerThreadsModule = true;
}
Expand All @@ -35,8 +38,7 @@ const create = context => {
context.on('ImportDeclaration', importDeclaration => {
if (
importDeclaration.source.type === 'Literal'
// TODO: Support `node:worker_threads`
&& importDeclaration.source.value === 'worker_threads'
&& isWorkerThreads(importDeclaration.source)
) {
requiredWorkerThreadsModule = true;
}
Expand All @@ -54,6 +56,11 @@ const create = context => {
processEventHandler = node;
}
});
context.onExit('CallExpression', node => {
if (node === processEventHandler) {
processEventHandler = undefined;
}
});

// Check `process.exit` call
context.on('CallExpression', node => {
Expand All @@ -70,12 +77,6 @@ const create = context => {
}
});

context.onExit('CallExpression', node => {
if (node === processEventHandler) {
processEventHandler = undefined;
}
});

context.onExit('Program', function * () {
if (requiredWorkerThreadsModule) {
return;
Expand Down
8 changes: 8 additions & 0 deletions test/no-process-exit.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ test({
const {workerData, parentPort} = require('worker_threads');
process.exit(1);
`,
outdent`
const {workerData, parentPort} = require('node:worker_threads');
process.exit(1);
`,
outdent`
import {workerData, parentPort} from 'worker_threads';
process.exit(1);
Expand All @@ -37,6 +41,10 @@ test({
import foo from 'worker_threads';
process.exit(1);
`,
outdent`
import foo from 'node:worker_threads';
process.exit(1);
`,
// Not `CallExpression`
'new process.exit(1);',
// Not `MemberExpression`
Expand Down

0 comments on commit 2907805

Please sign in to comment.