forked from mysticatea/eslint-plugin-node
-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathno-path-concat.js
237 lines (221 loc) · 7.19 KB
/
no-path-concat.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/**
* @author Nicholas C. Zakas
* See LICENSE file in root directory for full license.
*/
"use strict"
const path = require("path")
const {
READ,
ReferenceTracker,
getStringIfConstant,
} = require("@eslint-community/eslint-utils")
const { hasParentNode } = require("../util/has-parent-node.js")
const { getSourceCode } = require("../util/eslint-compat")
/**
* Get the first char of the specified template element.
* @param {import('estree').TemplateLiteral} node The `TemplateLiteral` node to get.
* @param {number} i The number of template elements to get first char.
* @param {Set<import('estree').Node>} sepNodes The nodes of `path.sep`.
* @param {import("eslint").Scope.Scope} globalScope The global scope object.
* @param {string[]} outNextChars The array to collect chars.
* @returns {void}
*/
function collectFirstCharsOfTemplateElement(
node,
i,
sepNodes,
globalScope,
outNextChars
) {
const element = node.quasis[i]?.value.cooked
if (element == null) {
return
}
const first = element[0]
if (first) {
outNextChars.push(first)
return
}
if (node.expressions.length > i) {
collectFirstChars(
node.expressions.at(i),
sepNodes,
globalScope,
outNextChars
)
}
}
/**
* Get the first char of a given node.
* @param {import('estree').Node | undefined} node The `TemplateLiteral` node to get.
* @param {Set<import('estree').Node>} sepNodes The nodes of `path.sep`.
* @param {import("eslint").Scope.Scope} globalScope The global scope object.
* @param {string[]} outNextChars The array to collect chars.
* @returns {void}
*/
function collectFirstChars(node, sepNodes, globalScope, outNextChars) {
switch (node?.type) {
case "AssignmentExpression":
collectFirstChars(node.right, sepNodes, globalScope, outNextChars)
break
case "BinaryExpression":
collectFirstChars(node.left, sepNodes, globalScope, outNextChars)
break
case "ConditionalExpression":
collectFirstChars(
node.consequent,
sepNodes,
globalScope,
outNextChars
)
collectFirstChars(
node.alternate,
sepNodes,
globalScope,
outNextChars
)
break
case "LogicalExpression":
collectFirstChars(node.left, sepNodes, globalScope, outNextChars)
collectFirstChars(node.right, sepNodes, globalScope, outNextChars)
break
case "SequenceExpression":
collectFirstChars(
node.expressions[node.expressions.length - 1],
sepNodes,
globalScope,
outNextChars
)
break
case "TemplateLiteral":
collectFirstCharsOfTemplateElement(
node,
0,
sepNodes,
globalScope,
outNextChars
)
break
case "Identifier":
case "MemberExpression":
if (sepNodes.has(node)) {
outNextChars.push(path.sep)
} else {
const str = getStringIfConstant(node, globalScope)?.at(0)
if (str) {
outNextChars.push(str)
}
}
break
default: {
if (node) {
const str = getStringIfConstant(node, globalScope)?.at(0)
if (str) {
outNextChars.push(str)
}
}
}
}
}
/**
* Check if a char is a path separator or not.
* @param {string} c The char to check.
* @returns {boolean} `true` if the char is a path separator.
*/
function isPathSeparator(c) {
return c === "/" || c === path.sep
}
/**
* Check if the given Identifier node is followed by string concatenation with a
* path separator.
* @param {import('estree').Identifier} node The `__dirname` or `__filename` node to check.
* @param {Set<import('estree').Node>} sepNodes The nodes of `path.sep`.
* @param {import("eslint").Scope.Scope} globalScope The global scope object.
* @returns {boolean} `true` if the given Identifier node is followed by string
* concatenation with a path separator.
*/
function isConcat(node, sepNodes, globalScope) {
if (!hasParentNode(node)) {
return false
}
/** @type {string[]} */
const nextChars = []
if (
node.parent.type === "BinaryExpression" &&
node.parent.operator === "+" &&
node.parent.left === node
) {
collectFirstChars(
node.parent.right,
sepNodes,
globalScope,
/* out */ nextChars
)
} else if (node.parent.type === "TemplateLiteral") {
collectFirstCharsOfTemplateElement(
node.parent,
node.parent.expressions.indexOf(node) + 1,
sepNodes,
globalScope,
/* out */ nextChars
)
}
return nextChars.some(isPathSeparator)
}
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: "suggestion",
docs: {
description:
"disallow string concatenation with `__dirname` and `__filename`",
recommended: false,
url: "https://github.com/eslint-community/eslint-plugin-n/blob/HEAD/docs/rules/no-path-concat.md",
},
fixable: null,
schema: [],
messages: {
usePathFunctions:
"Use path.join() or path.resolve() instead of string concatenation.",
},
},
create(context) {
return {
"Program:exit"(programNode) {
const sourceCode = getSourceCode(context)
const globalScope =
sourceCode.getScope?.(programNode) ?? context.getScope()
const tracker = new ReferenceTracker(globalScope)
/** @type {Set<import('estree').Node>} */
const sepNodes = new Set()
// Collect `paht.sep` references
for (const { node } of tracker.iterateCjsReferences({
path: { sep: { [READ]: true } },
})) {
sepNodes.add(node)
}
for (const { node } of tracker.iterateEsmReferences({
path: { sep: { [READ]: true } },
})) {
sepNodes.add(node)
}
// Verify `__dirname` and `__filename`
for (const { node } of tracker.iterateGlobalReferences({
__dirname: { [READ]: true },
__filename: { [READ]: true },
})) {
if (
node.type === "Identifier" &&
hasParentNode(node) &&
isConcat(node, sepNodes, globalScope)
) {
context.report({
node: node.parent,
messageId: "usePathFunctions",
})
}
}
},
}
},
}