Skip to content

Commit 6369932

Browse files
committed
chore(valid-title): Fix types
1 parent e283efc commit 6369932

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

src/rules/valid-title.ts

+27-26
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,14 @@ export default createRule({
123123
return
124124
}
125125

126-
let argument: ESTree.Node = node.arguments[0]
127-
if (!argument) return
126+
const [argument] = node.arguments
127+
const title = dereference(context, argument) ?? argument
128+
if (!title) return
128129

129-
// Attempt to dereference the argument if it's a variable
130-
argument = dereference(context, argument) ?? argument
131-
132-
if (!isStringNode(argument)) {
130+
if (!isStringNode(title)) {
133131
if (
134-
argument.type === 'BinaryExpression' &&
135-
doesBinaryExpressionContainStringNode(argument)
132+
title.type === 'BinaryExpression' &&
133+
doesBinaryExpressionContainStringNode(title)
136134
) {
137135
return
138136
}
@@ -143,21 +141,21 @@ export default createRule({
143141
(call.type === 'test' && ignoreTypeOfTestName) ||
144142
(call.type === 'step' && ignoreTypeOfStepName)
145143
) &&
146-
(argument as ESTree.Node).type !== 'TemplateLiteral'
144+
(title as ESTree.Node).type !== 'TemplateLiteral'
147145
) {
148146
context.report({
149-
loc: argument.loc!,
147+
loc: title.loc!,
150148
messageId: 'titleMustBeString',
151149
})
152150
}
153151

154152
return
155153
}
156154

157-
const title = getStringValue(argument)
155+
const titleString = getStringValue(title)
158156
const functionName = call.type
159157

160-
if (!title) {
158+
if (!titleString) {
161159
context.report({
162160
data: { functionName: call.type },
163161
messageId: 'emptyTitle',
@@ -168,52 +166,55 @@ export default createRule({
168166
}
169167

170168
if (disallowedWords.length > 0) {
171-
const disallowedMatch = disallowedWordsRegexp.exec(title)
169+
const disallowedMatch = disallowedWordsRegexp.exec(titleString)
172170

173171
if (disallowedMatch) {
174172
context.report({
175173
data: { word: disallowedMatch[1] },
176174
messageId: 'disallowedWord',
177-
node: argument,
175+
node: title,
178176
})
179177

180178
return
181179
}
182180
}
183181

184-
if (ignoreSpaces === false && title.trim().length !== title.length) {
182+
if (
183+
ignoreSpaces === false &&
184+
titleString.trim().length !== titleString.length
185+
) {
185186
context.report({
186187
fix: (fixer) => [
187188
fixer.replaceTextRange(
188-
argument.range!,
189-
quoteStringValue(argument)
189+
title.range!,
190+
quoteStringValue(title)
190191
.replace(/^([`'"]) +?/u, '$1')
191192
.replace(/ +?([`'"])$/u, '$1'),
192193
),
193194
],
194195
messageId: 'accidentalSpace',
195-
node: argument,
196+
node: title,
196197
})
197198
}
198199

199-
const [firstWord] = title.split(' ')
200+
const [firstWord] = titleString.split(' ')
200201
if (firstWord.toLowerCase() === functionName) {
201202
context.report({
202203
fix: (fixer) => [
203204
fixer.replaceTextRange(
204-
argument.range!,
205-
quoteStringValue(argument).replace(/^([`'"]).+? /u, '$1'),
205+
title.range!,
206+
quoteStringValue(title).replace(/^([`'"]).+? /u, '$1'),
206207
),
207208
],
208209
messageId: 'duplicatePrefix',
209-
node: argument,
210+
node: title,
210211
})
211212
}
212213

213214
const [mustNotMatchPattern, mustNotMatchMessage] =
214215
mustNotMatchPatterns[functionName] ?? []
215216

216-
if (mustNotMatchPattern && mustNotMatchPattern.test(title)) {
217+
if (mustNotMatchPattern && mustNotMatchPattern.test(titleString)) {
217218
context.report({
218219
data: {
219220
functionName,
@@ -223,7 +224,7 @@ export default createRule({
223224
messageId: mustNotMatchMessage
224225
? 'mustNotMatchCustom'
225226
: 'mustNotMatch',
226-
node: argument,
227+
node: title,
227228
})
228229

229230
return
@@ -232,15 +233,15 @@ export default createRule({
232233
const [mustMatchPattern, mustMatchMessage] =
233234
mustMatchPatterns[functionName] ?? []
234235

235-
if (mustMatchPattern && !mustMatchPattern.test(title)) {
236+
if (mustMatchPattern && !mustMatchPattern.test(titleString)) {
236237
context.report({
237238
data: {
238239
functionName,
239240
message: mustMatchMessage ?? '',
240241
pattern: String(mustMatchPattern),
241242
},
242243
messageId: mustMatchMessage ? 'mustMatchCustom' : 'mustMatch',
243-
node: argument,
244+
node: title,
244245
})
245246

246247
return

0 commit comments

Comments
 (0)