Skip to content

Commit ce2f04d

Browse files
committed
fix: use type guard
1 parent 31c56b5 commit ce2f04d

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

src/suggestions.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,10 @@ function canSuggest(
9797
currentMethod: string,
9898
requestedMethod: string | undefined,
9999
data: string | null,
100-
) {
100+
): data is string {
101101
return (
102-
data &&
102+
typeof data === 'string' &&
103+
data.length > 0 &&
103104
(!requestedMethod ||
104105
requestedMethod.toLowerCase() === currentMethod.toLowerCase())
105106
)
@@ -137,14 +138,9 @@ export function getSuggestedQuery(
137138

138139
const placeholderText = element.getAttribute('placeholder')
139140
if (canSuggest('PlaceholderText', method, placeholderText)) {
140-
return makeSuggestion(
141-
'PlaceholderText',
142-
element,
143-
placeholderText as string,
144-
{
145-
variant,
146-
},
147-
)
141+
return makeSuggestion('PlaceholderText', element, placeholderText, {
142+
variant,
143+
})
148144
}
149145

150146
const textContent = normalize(getNodeText(element))
@@ -160,17 +156,17 @@ export function getSuggestedQuery(
160156

161157
const alt = element.getAttribute('alt')
162158
if (canSuggest('AltText', method, alt)) {
163-
return makeSuggestion('AltText', element, alt as string, {variant})
159+
return makeSuggestion('AltText', element, alt, {variant})
164160
}
165161

166162
const title = element.getAttribute('title')
167163
if (canSuggest('Title', method, title)) {
168-
return makeSuggestion('Title', element, title as string, {variant})
164+
return makeSuggestion('Title', element, title, {variant})
169165
}
170166

171167
const testId = element.getAttribute(getConfig().testIdAttribute)
172168
if (canSuggest('TestId', method, testId)) {
173-
return makeSuggestion('TestId', element, testId as string, {variant})
169+
return makeSuggestion('TestId', element, testId, {variant})
174170
}
175171

176172
return undefined

0 commit comments

Comments
 (0)