forked from eslint-community/eslint-plugin-n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess-exit-as-throw.js
166 lines (151 loc) · 5.01 KB
/
process-exit-as-throw.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* eslint-disable eslint-plugin/prefer-message-ids */
/**
* @author Toru Nagashima
* See LICENSE file in root directory for full license.
*/
"use strict"
/** @type {typeof import('../types-code-path-analysis/code-path-analyzer.js')} */
const CodePathAnalyzer = safeRequire(
"eslint/lib/linter/code-path-analysis/code-path-analyzer",
"eslint/lib/code-path-analysis/code-path-analyzer"
)
/** @type {typeof import('../types-code-path-analysis/code-path-segment.js')} */
const CodePathSegment = safeRequire(
"eslint/lib/linter/code-path-analysis/code-path-segment",
"eslint/lib/code-path-analysis/code-path-segment"
)
/** @type {typeof import('../types-code-path-analysis/code-path.js')} */
const CodePath = safeRequire(
"eslint/lib/linter/code-path-analysis/code-path",
"eslint/lib/code-path-analysis/code-path"
)
const originalLeaveNode = CodePathAnalyzer?.prototype?.leaveNode
/**
* Imports a specific module.
* @param {...string} moduleNames - module names to import.
* @returns {*} The imported object, or null.
*/
function safeRequire(...moduleNames) {
for (const moduleName of moduleNames) {
try {
return require(moduleName)
} catch {
// Ignore.
}
}
return null
}
/* istanbul ignore next */
/**
* Copied from https://github.com/eslint/eslint/blob/16fad5880bb70e9dddbeab8ed0f425ae51f5841f/lib/code-path-analysis/code-path-analyzer.js#L137
*
* @param {import('../types-code-path-analysis/code-path-analyzer.js')} analyzer - The instance.
* @param {import('eslint').Rule.Node} node - The current AST node.
* @returns {void}
*/
function forwardCurrentToHead(analyzer, node) {
const codePath = analyzer.codePath
const state = CodePath.getState(codePath)
const currentSegments = state.currentSegments
const headSegments = state.headSegments
const end = Math.max(currentSegments.length, headSegments.length)
let i = 0
let currentSegment = null
let headSegment = null
// Fires leaving events.
for (i = 0; i < end; ++i) {
currentSegment = currentSegments[i]
headSegment = headSegments[i]
if (currentSegment !== headSegment && currentSegment) {
if (currentSegment.reachable) {
analyzer.emitter.emit(
"onCodePathSegmentEnd",
currentSegment,
node
)
}
}
}
// Update state.
state.currentSegments = headSegments
// Fires entering events.
for (i = 0; i < end; ++i) {
currentSegment = currentSegments[i]
headSegment = headSegments[i]
if (currentSegment !== headSegment && headSegment) {
CodePathSegment.markUsed(headSegment)
if (headSegment.reachable) {
analyzer.emitter.emit(
"onCodePathSegmentStart",
headSegment,
node
)
}
}
}
}
/**
* Checks whether a given node is `process.exit()` or not.
*
* @param {import('eslint').Rule.Node} node - A node to check.
* @returns {boolean} `true` if the node is `process.exit()`.
*/
function isProcessExit(node) {
return (
node.type === "CallExpression" &&
node.callee.type === "MemberExpression" &&
node.callee.computed === false &&
node.callee.object.type === "Identifier" &&
node.callee.object.name === "process" &&
node.callee.property.type === "Identifier" &&
node.callee.property.name === "exit"
)
}
/**
* The function to override `CodePathAnalyzer.prototype.leaveNode` in order to
* address `process.exit()` as throw.
*
* @this {import('../types-code-path-analysis/code-path-analyzer.js')}
* @param {import('eslint').Rule.Node} node - A node to be left.
* @returns {void}
*/
function overrideLeaveNode(node) {
if (isProcessExit(node)) {
this.currentNode = node
forwardCurrentToHead(this, node)
CodePath.getState(this.codePath).makeThrow()
this.original.leaveNode(node)
this.currentNode = null
} else {
originalLeaveNode.call(this, node)
}
}
const visitor =
CodePathAnalyzer == null
? {}
: {
Program: function installProcessExitAsThrow() {
CodePathAnalyzer.prototype.leaveNode = overrideLeaveNode
},
"Program:exit": function restoreProcessExitAsThrow() {
CodePathAnalyzer.prototype.leaveNode = originalLeaveNode
},
}
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
docs: {
description:
"require that `process.exit()` expressions use the same code path as `throw`",
recommended: true,
url: "https://github.com/eslint-community/eslint-plugin-n/blob/HEAD/docs/rules/process-exit-as-throw.md",
},
type: "problem",
fixable: null,
schema: [],
supported: CodePathAnalyzer != null,
},
create() {
return visitor
},
}