1
1
'use strict' ;
2
- const { methodCallSelector} = require ( './selectors/index.js' ) ;
3
- const { isStaticRequire} = require ( './ast/index.js' ) ;
2
+ const { isStaticRequire, isMethodCall} = require ( './ast/index.js' ) ;
4
3
5
4
const MESSAGE_ID = 'no-process-exit' ;
6
5
const messages = {
7
6
[ MESSAGE_ID ] : 'Only use `process.exit()` in CLI apps. Throw an error instead.' ,
8
7
} ;
9
8
10
- const processOnOrOnceCallSelector = methodCallSelector ( {
11
- object : 'process' ,
12
- methods : [ 'on' , 'once' ] ,
13
- minimumArguments : 1 ,
14
- } ) ;
15
- const processExitCallSelector = methodCallSelector ( {
16
- object : 'process' ,
17
- method : 'exit' ,
18
- } ) ;
19
-
20
9
/** @param {import('eslint').Rule.RuleContext } context */
21
10
const create = context => {
22
11
const startsWithHashBang = context . sourceCode . lines [ 0 ] . indexOf ( '#!' ) === 0 ;
@@ -31,53 +20,74 @@ const create = context => {
31
20
let requiredWorkerThreadsModule = false ;
32
21
const problemNodes = [ ] ;
33
22
34
- return {
35
- CallExpression ( callExpression ) {
36
- // `require('worker_threads')`
37
- if (
38
- isStaticRequire ( callExpression )
39
- // TODO: Support `node:worker_threads`
40
- && callExpression . arguments [ 0 ] . value === 'worker_threads'
41
- ) {
42
- requiredWorkerThreadsModule = true ;
43
- }
44
- } ,
45
- ImportDeclaration ( importDeclaration ) {
46
- // `import workerThreads from 'worker_threads'`
47
- if (
48
- importDeclaration . source . type === 'Literal'
49
- // TODO: Support `node:worker_threads`
50
- && importDeclaration . source . value === 'worker_threads'
51
- ) {
52
- requiredWorkerThreadsModule = true ;
53
- }
54
- } ,
55
- // Check `process.on` / `process.once` call
56
- [ processOnOrOnceCallSelector ] ( node ) {
23
+ // `require('worker_threads')`
24
+ context . on ( 'CallExpression' , callExpression => {
25
+ if (
26
+ isStaticRequire ( callExpression )
27
+ // TODO: Support `node:worker_threads`
28
+ && callExpression . arguments [ 0 ] . value === 'worker_threads'
29
+ ) {
30
+ requiredWorkerThreadsModule = true ;
31
+ }
32
+ } ) ;
33
+
34
+ // `import workerThreads from 'worker_threads'`
35
+ context . on ( 'ImportDeclaration' , importDeclaration => {
36
+ if (
37
+ importDeclaration . source . type === 'Literal'
38
+ // TODO: Support `node:worker_threads`
39
+ && importDeclaration . source . value === 'worker_threads'
40
+ ) {
41
+ requiredWorkerThreadsModule = true ;
42
+ }
43
+ } ) ;
44
+
45
+ // Check `process.on` / `process.once` call
46
+ context . on ( 'CallExpression' , node => {
47
+ if ( isMethodCall ( node , {
48
+ object : 'process' ,
49
+ methods : [ 'on' , 'once' ] ,
50
+ minimumArguments : 1 ,
51
+ optionalCall : false ,
52
+ optionalMember : false ,
53
+ } ) ) {
57
54
processEventHandler = node ;
58
- } ,
59
- // Check `process.exit` call
60
- [ processExitCallSelector ] ( node ) {
61
- if ( ! processEventHandler ) {
62
- problemNodes . push ( node ) ;
63
- }
64
- } ,
65
- 'CallExpression:exit' ( node ) {
66
- if ( node === processEventHandler ) {
67
- processEventHandler = undefined ;
68
- }
69
- } ,
70
- * 'Program:exit' ( ) {
71
- if ( ! requiredWorkerThreadsModule ) {
72
- for ( const node of problemNodes ) {
73
- yield {
74
- node,
75
- messageId : MESSAGE_ID ,
76
- } ;
77
- }
78
- }
79
- } ,
80
- } ;
55
+ }
56
+ } ) ;
57
+
58
+ // Check `process.exit` call
59
+ context . on ( 'CallExpression' , node => {
60
+ if (
61
+ ! processEventHandler
62
+ && isMethodCall ( node , {
63
+ object : 'process' ,
64
+ method : 'exit' ,
65
+ optionalCall : false ,
66
+ optionalMember : false ,
67
+ } )
68
+ ) {
69
+ problemNodes . push ( node ) ;
70
+ }
71
+ } ) ;
72
+
73
+ context . on ( 'CallExpression:exit' , node => {
74
+ if ( node === processEventHandler ) {
75
+ processEventHandler = undefined ;
76
+ }
77
+ } ) ;
78
+
79
+ context . on ( 'Program:exit' , function * ( ) {
80
+ if ( requiredWorkerThreadsModule ) {
81
+ return ;
82
+ }
83
+
84
+ for ( const node of problemNodes ) {
85
+ yield {
86
+ node,
87
+ messageId : MESSAGE_ID ,
88
+ } ;
89
+ }
90
+ } ) ;
81
91
} ;
82
92
83
93
/** @type {import('eslint').Rule.RuleModule } */
0 commit comments