Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
192 lines (173 loc) · 5.88 KB
/
Copy pathindex.js
File metadata and controls
192 lines (173 loc) · 5.88 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
'use strict'
const { format } = require('node:util')
/**
* @namespace processWarning
*/
/**
* Represents a warning item with details.
* @typedef {Function} WarningItem
* @param {*} [a] Possible message interpolation value.
* @param {*} [b] Possible message interpolation value.
* @param {*} [c] Possible message interpolation value.
* @property {string} name - The name of the warning.
* @property {string} code - The code associated with the warning.
* @property {string} message - The warning message.
* @property {boolean} emitted - Indicates if the warning has been emitted.
* @property {function} format - Formats the warning message.
* @returns {boolean} emit - Indicates if the warning has been emitted by this call.
*/
/**
* Options for creating a process warning.
* @typedef {Object} ProcessWarningOptions
* @property {string} name - The name of the warning.
* @property {string} code - The code associated with the warning.
* @property {string} message - The warning message.
* @property {boolean} [unlimited=false] - If true, allows unlimited emissions of the warning.
*/
/**
* Represents the process warning functionality.
* @typedef {Object} ProcessWarning
* @property {function} createWarning - Creates a warning item.
* @property {function} createDeprecation - Creates a deprecation warning item.
* @property {function} spyWarning - Spy a warning item.
*/
/**
* Represents the spy data.
* @typedef {Object} WarningSpyData
* @property {object} calls - Arguments of WarningItem calls with.
* @property {function} callCount - Number of counts called the WarningItem.
* @property {function} reset - Reset the calls data and state of WarningItem.
* @property {function} restore - Remove spy from WarningItem.
*/
const kWarningFn = Symbol('process-warning.fn')
const kWarningSpyData = Symbol('process-warning.spyData')
/**
* Spy a warning item.
* @function
* @memberof processWarning
* @param {WarningItem} warning - The warning item to spy.
* @returns {WarningSpyData} The created spy data.
*/
function spyWarning (warning) {
// Do not double spy the same warning
if (warning[kWarningSpyData] === null) {
const warningFn = warning[kWarningFn]
warning[kWarningFn] = function (a, b, c) {
const args = []
// since warning always call by fn(a, b, c)
// it need to remove the trailing undefined arguments
if (c) {
args.push(a, b, c)
} else if (b) {
args.push(a, b)
} else if (a) {
args.push(a)
}
warning[kWarningSpyData].calls.push({
arguments: args,
result: warningFn(a, b, c)
})
}
const spyData = {
calls: [],
callCount () {
return spyData.calls.length
},
reset () {
warning.emitted = false
spyData.calls.length = 0
},
restore () {
spyData.reset()
warning[kWarningFn] = warningFn
warning[kWarningSpyData] = null
}
}
warning[kWarningSpyData] = spyData
}
return warning[kWarningSpyData]
}
/**
* Creates a deprecation warning item.
* @function
* @memberof processWarning
* @param {ProcessWarningOptions} params - Options for creating the warning.
* @returns {WarningItem} The created deprecation warning item.
*/
function createDeprecation (params) {
return createWarning({ ...params, name: 'DeprecationWarning' })
}
/**
* Creates a warning item.
* @function
* @memberof processWarning
* @param {ProcessWarningOptions} params - Options for creating the warning.
* @returns {WarningItem} The created warning item.
* @throws {Error} Throws an error if name, code, or message is empty, or if opts.unlimited is not a boolean.
*/
function createWarning ({ name, code, message, unlimited = false } = {}) {
if (!name) throw new Error('Warning name must not be empty')
if (!code) throw new Error('Warning code must not be empty')
if (!message) throw new Error('Warning message must not be empty')
if (typeof unlimited !== 'boolean') throw new Error('Warning opts.unlimited must be a boolean')
code = code.toUpperCase()
const warningFn = unlimited === true
? function (a, b, c) {
warning.emitted = true
process.emitWarning(warning.format(a, b, c), warning.name, warning.code)
return true
}
: function (a, b, c) {
if (warning.emitted === true && warning.unlimited !== true) {
return false
}
warning.emitted = true
process.emitWarning(warning.format(a, b, c), warning.name, warning.code)
return true
}
const warningContainer = {
[name]: function (a, b, c) {
return warning[kWarningFn](a, b, c)
}
}
const warning = warningContainer[name]
warning.emitted = false
warning.message = message
warning.unlimited = unlimited
warning.code = code
warning[kWarningFn] = warningFn
warning[kWarningSpyData] = null
/**
* Formats the warning message.
* @param {*} [a] Possible message interpolation value.
* @param {*} [b] Possible message interpolation value.
* @param {*} [c] Possible message interpolation value.
* @returns {string} The formatted warning message.
*/
warning.format = function (a, b, c) {
let formatted
if (a && b && c) {
formatted = format(message, a, b, c)
} else if (a && b) {
formatted = format(message, a, b)
} else if (a) {
formatted = format(message, a)
} else {
formatted = message
}
return formatted
}
return warning
}
/**
* Module exports containing the process warning functionality.
* @namespace
* @property {function} createWarning - Creates a warning item.
* @property {function} createDeprecation - Creates a deprecation warning item.
* @property {function} spyWarning - Spy a warning item.
* @property {ProcessWarning} processWarning - Represents the process warning functionality.
*/
const out = { createWarning, createDeprecation, spyWarning }
module.exports = out
module.exports.default = out
module.exports.processWarning = out