-
-
Notifications
You must be signed in to change notification settings - Fork 172
/
no-path-concat.js
212 lines (199 loc) · 6.29 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
/**
* @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-utils")
/**
* Get the first char of the specified template element.
* @param {TemplateLiteral} node The `TemplateLiteral` node to get.
* @param {number} i The number of template elements to get first char.
* @param {Set<Node>} sepNodes The nodes of `path.sep`.
* @param {import("escope").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
}
if (element !== "") {
outNextChars.push(element[0])
return
}
if (node.expressions.length > i) {
collectFirstChars(
node.expressions[i],
sepNodes,
globalScope,
outNextChars
)
}
}
/**
* Get the first char of a given node.
* @param {TemplateLiteral} node The `TemplateLiteral` node to get.
* @param {Set<Node>} sepNodes The nodes of `path.sep`.
* @param {import("escope").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)
break
}
// fallthrough
default: {
const str = getStringIfConstant(node, globalScope)
if (str) {
outNextChars.push(str[0])
}
}
}
}
/**
* 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 {Identifier} node The `__dirname` or `__filename` node to check.
* @param {Set<Node>} sepNodes The nodes of `path.sep`.
* @param {import("escope").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) {
const parent = node.parent
const nextChars = []
if (
parent.type === "BinaryExpression" &&
parent.operator === "+" &&
parent.left === node
) {
collectFirstChars(
parent.right,
sepNodes,
globalScope,
/* out */ nextChars
)
} else if (parent.type === "TemplateLiteral") {
collectFirstCharsOfTemplateElement(
parent,
parent.expressions.indexOf(node) + 1,
sepNodes,
globalScope,
/* out */ nextChars
)
}
return nextChars.some(isPathSeparator)
}
module.exports = {
meta: {
type: "suggestion",
docs: {
description:
"disallow string concatenation with `__dirname` and `__filename`",
category: "Possible Errors",
recommended: false,
url:
"https://github.com/mysticatea/eslint-plugin-node/blob/v11.1.0/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"() {
const globalScope = context.getScope()
const tracker = new ReferenceTracker(globalScope)
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 (isConcat(node, sepNodes, globalScope)) {
context.report({
node: node.parent,
messageId: "usePathFunctions",
})
}
}
},
}
},
}