Skip to content

Commit

Permalink
chore(valid-title): Fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
mskelton committed Oct 19, 2024
1 parent e283efc commit 6369932
Showing 1 changed file with 27 additions and 26 deletions.
53 changes: 27 additions & 26 deletions src/rules/valid-title.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,14 @@ export default createRule({
return
}

let argument: ESTree.Node = node.arguments[0]
if (!argument) return
const [argument] = node.arguments
const title = dereference(context, argument) ?? argument
if (!title) return

// Attempt to dereference the argument if it's a variable
argument = dereference(context, argument) ?? argument

if (!isStringNode(argument)) {
if (!isStringNode(title)) {
if (
argument.type === 'BinaryExpression' &&
doesBinaryExpressionContainStringNode(argument)
title.type === 'BinaryExpression' &&
doesBinaryExpressionContainStringNode(title)
) {
return
}
Expand All @@ -143,21 +141,21 @@ export default createRule({
(call.type === 'test' && ignoreTypeOfTestName) ||
(call.type === 'step' && ignoreTypeOfStepName)
) &&
(argument as ESTree.Node).type !== 'TemplateLiteral'
(title as ESTree.Node).type !== 'TemplateLiteral'
) {
context.report({
loc: argument.loc!,
loc: title.loc!,
messageId: 'titleMustBeString',
})
}

return
}

const title = getStringValue(argument)
const titleString = getStringValue(title)
const functionName = call.type

if (!title) {
if (!titleString) {
context.report({
data: { functionName: call.type },
messageId: 'emptyTitle',
Expand All @@ -168,52 +166,55 @@ export default createRule({
}

if (disallowedWords.length > 0) {
const disallowedMatch = disallowedWordsRegexp.exec(title)
const disallowedMatch = disallowedWordsRegexp.exec(titleString)

if (disallowedMatch) {
context.report({
data: { word: disallowedMatch[1] },
messageId: 'disallowedWord',
node: argument,
node: title,
})

return
}
}

if (ignoreSpaces === false && title.trim().length !== title.length) {
if (
ignoreSpaces === false &&
titleString.trim().length !== titleString.length
) {
context.report({
fix: (fixer) => [
fixer.replaceTextRange(
argument.range!,
quoteStringValue(argument)
title.range!,
quoteStringValue(title)
.replace(/^([`'"]) +?/u, '$1')
.replace(/ +?([`'"])$/u, '$1'),
),
],
messageId: 'accidentalSpace',
node: argument,
node: title,
})
}

const [firstWord] = title.split(' ')
const [firstWord] = titleString.split(' ')
if (firstWord.toLowerCase() === functionName) {
context.report({
fix: (fixer) => [
fixer.replaceTextRange(
argument.range!,
quoteStringValue(argument).replace(/^([`'"]).+? /u, '$1'),
title.range!,
quoteStringValue(title).replace(/^([`'"]).+? /u, '$1'),
),
],
messageId: 'duplicatePrefix',
node: argument,
node: title,
})
}

const [mustNotMatchPattern, mustNotMatchMessage] =
mustNotMatchPatterns[functionName] ?? []

if (mustNotMatchPattern && mustNotMatchPattern.test(title)) {
if (mustNotMatchPattern && mustNotMatchPattern.test(titleString)) {
context.report({
data: {
functionName,
Expand All @@ -223,7 +224,7 @@ export default createRule({
messageId: mustNotMatchMessage
? 'mustNotMatchCustom'
: 'mustNotMatch',
node: argument,
node: title,
})

return
Expand All @@ -232,15 +233,15 @@ export default createRule({
const [mustMatchPattern, mustMatchMessage] =
mustMatchPatterns[functionName] ?? []

if (mustMatchPattern && !mustMatchPattern.test(title)) {
if (mustMatchPattern && !mustMatchPattern.test(titleString)) {
context.report({
data: {
functionName,
message: mustMatchMessage ?? '',
pattern: String(mustMatchPattern),
},
messageId: mustMatchMessage ? 'mustMatchCustom' : 'mustMatch',
node: argument,
node: title,
})

return
Expand Down

0 comments on commit 6369932

Please sign in to comment.