11import { defineParameterType } from "@cucumber/cucumber" ;
22import type { AriaRole } from "@playwright/test" ;
3- import { roleAliases , validAriaRoles } from "./roles.ts" ;
3+ import {
4+ allowedRolePhrases ,
5+ preferredPhraseByRole ,
6+ validAriaRoles ,
7+ } from "./roles.ts" ;
48
59defineParameterType ( {
610 name : "role" ,
@@ -9,30 +13,29 @@ defineParameterType({
913 transformer : ( text : string ) : AriaRole => {
1014 const input = text . trim ( ) . toLowerCase ( ) ;
1115
12- // 1) Exact match
13- if ( validAriaRoles . has ( input ) ) {
14- return input as AriaRole ;
15- }
16-
17- // 2) Compact variant: remove spaces and hyphens
18- const compact = input . replace ( / [ \s - ] + / g, "" ) ;
19- if ( validAriaRoles . has ( compact ) ) {
20- return compact as AriaRole ;
16+ // Accept only canonical phrases to reduce variants
17+ const canonical = allowedRolePhrases [ input ] ;
18+ if ( canonical ) {
19+ return canonical ;
2120 }
2221
23- // 3) Aliases
24- const alias = roleAliases [ input ] ;
25- if ( alias ) {
26- return alias ;
22+ // If user provided an ARIA role directly, recommend the canonical phrase
23+ if ( validAriaRoles . has ( input ) ) {
24+ const role = input as AriaRole ;
25+ const preferred = preferredPhraseByRole [ role ] ;
26+ if ( preferred && preferred !== input ) {
27+ throw new Error (
28+ `Use canonical role phrase "${ preferred } " instead of "${ input } ".` ,
29+ ) ;
30+ }
31+ // Role equals its canonical phrase (e.g., "button", "link", "checkbox")
32+ return role ;
2733 }
2834
29- // 4) Helpful error
30- const sample = Array . from ( validAriaRoles ) . slice ( 0 , 15 ) . join ( ", " ) ;
35+ // Helpful error with allowed phrases
36+ const examples = Object . keys ( allowedRolePhrases ) . slice ( 0 , 10 ) . join ( ", " ) ;
3137 throw new Error (
32- `Unknown role "${ text } ".\n` +
33- `- Tried: "${ input } " and "${ compact } ".\n` +
34- "- Add an alias in roleAliases if this is a valid custom phrase.\n" +
35- `- Example known roles: ${ sample } ...` ,
38+ `Unknown role phrase "${ text } ". Use one of the canonical phrases (e.g., ${ examples } ...).` ,
3639 ) ;
3740 } ,
3841} ) ;
0 commit comments