1
1
'use strict' ;
2
- const { isStaticRequire, isMethodCall} = require ( './ast/index.js' ) ;
2
+ const { isStaticRequire, isMethodCall, isLiteral } = require ( './ast/index.js' ) ;
3
3
4
4
const MESSAGE_ID = 'no-process-exit' ;
5
5
const messages = {
6
6
[ MESSAGE_ID ] : 'Only use `process.exit()` in CLI apps. Throw an error instead.' ,
7
7
} ;
8
8
9
+ const isWorkerThreads = node =>
10
+ isLiteral ( node , 'node:worker_threads' )
11
+ || isLiteral ( node , 'worker_threads' ) ;
12
+
9
13
/** @param {import('eslint').Rule.RuleContext } context */
10
14
const create = context => {
11
15
const startsWithHashBang = context . sourceCode . lines [ 0 ] . indexOf ( '#!' ) === 0 ;
@@ -24,8 +28,7 @@ const create = context => {
24
28
context . on ( 'CallExpression' , callExpression => {
25
29
if (
26
30
isStaticRequire ( callExpression )
27
- // TODO: Support `node:worker_threads`
28
- && callExpression . arguments [ 0 ] . value === 'worker_threads'
31
+ && isWorkerThreads ( callExpression . arguments [ 0 ] )
29
32
) {
30
33
requiredWorkerThreadsModule = true ;
31
34
}
@@ -35,8 +38,7 @@ const create = context => {
35
38
context . on ( 'ImportDeclaration' , importDeclaration => {
36
39
if (
37
40
importDeclaration . source . type === 'Literal'
38
- // TODO: Support `node:worker_threads`
39
- && importDeclaration . source . value === 'worker_threads'
41
+ && isWorkerThreads ( importDeclaration . source )
40
42
) {
41
43
requiredWorkerThreadsModule = true ;
42
44
}
@@ -54,6 +56,11 @@ const create = context => {
54
56
processEventHandler = node ;
55
57
}
56
58
} ) ;
59
+ context . onExit ( 'CallExpression' , node => {
60
+ if ( node === processEventHandler ) {
61
+ processEventHandler = undefined ;
62
+ }
63
+ } ) ;
57
64
58
65
// Check `process.exit` call
59
66
context . on ( 'CallExpression' , node => {
@@ -70,12 +77,6 @@ const create = context => {
70
77
}
71
78
} ) ;
72
79
73
- context . onExit ( 'CallExpression' , node => {
74
- if ( node === processEventHandler ) {
75
- processEventHandler = undefined ;
76
- }
77
- } ) ;
78
-
79
80
context . onExit ( 'Program' , function * ( ) {
80
81
if ( requiredWorkerThreadsModule ) {
81
82
return ;
0 commit comments