Skip to content

Commit 94aedd5

Browse files
Address review comment to add hasLabelWithHtmlId since this function is needed for the id=case.
1 parent 029a956 commit 94aedd5

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

lib/util/labelUtils.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,20 @@ const hasAssociatedLabelViaHtmlFor = (openingElement: TSESTree.JSXOpeningElement
338338
if (info.kind === "template") {
339339
const templ = info.template as string;
340340
const src = getSourceText(context);
341-
return new RegExp(`<(?:Label|label)[^>]*\\bhtmlFor\\s*=\\s*\\{\\s*${escapeForRegExp(templ)}\\s*\\}`, "i").test(src);
341+
// Build a pattern which matches the template's literal parts but allows any expression
342+
// inside `${...}` placeholders. This lets templates with non-Identifier expressions
343+
// (e.g. `${a.b}`) match the canonicalized template produced from the AST.
344+
const placeholderRe = /\$\{[^}]*\}/g;
345+
let pattern = "";
346+
let idx = 0;
347+
let m: RegExpExecArray | null;
348+
while ((m = placeholderRe.exec(templ)) !== null) {
349+
pattern += escapeForRegExp(templ.slice(idx, m.index));
350+
pattern += "\\$\\{[^}]*\\}";
351+
idx = m.index + m[0].length;
352+
}
353+
pattern += escapeForRegExp(templ.slice(idx));
354+
return new RegExp(`<(?:Label|label)[^>]*\\bhtmlFor\\s*=\\s*\\{\\s*${pattern}\\s*\\}`, "i").test(src);
342355
}
343356

344357
return false;

0 commit comments

Comments
 (0)