Skip to content

Commit b938004

Browse files
refactor: remove use of TS enums (#118)
* refactor: remove use of TS enums * sec: upgrade example `cross-spawn` dep
1 parent c8e7919 commit b938004

File tree

8 files changed

+93
-86
lines changed

8 files changed

+93
-86
lines changed

examples/puppeteer/yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,9 +1090,9 @@ create-jest@^29.7.0:
10901090
prompts "^2.0.1"
10911091

10921092
cross-spawn@^7.0.3:
1093-
version "7.0.3"
1094-
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
1095-
integrity sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==
1093+
version "7.0.6"
1094+
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.6.tgz#8a58fe78f00dcd70c370451759dfbfaf03e8ee9f"
1095+
integrity sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==
10961096
dependencies:
10971097
path-key "^3.1.0"
10981098
shebang-command "^2.0.0"

examples/typescript-legacy/yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -988,9 +988,9 @@ create-jest@^29.7.0:
988988
prompts "^2.0.1"
989989

990990
cross-spawn@^7.0.3:
991-
version "7.0.3"
992-
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
993-
integrity sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==
991+
version "7.0.6"
992+
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.6.tgz#8a58fe78f00dcd70c370451759dfbfaf03e8ee9f"
993+
integrity sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==
994994
dependencies:
995995
path-key "^3.1.0"
996996
shebang-command "^2.0.0"

examples/typescript-nodenext/yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -988,9 +988,9 @@ create-jest@^29.7.0:
988988
prompts "^2.0.1"
989989

990990
cross-spawn@^7.0.3:
991-
version "7.0.3"
992-
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
993-
integrity sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==
991+
version "7.0.6"
992+
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.6.tgz#8a58fe78f00dcd70c370451759dfbfaf03e8ee9f"
993+
integrity sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==
994994
dependencies:
995995
path-key "^3.1.0"
996996
shebang-command "^2.0.0"

examples/vue/yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,9 +1522,9 @@ convert-source-map@^2.0.0:
15221522
integrity sha512-Kvp459HrV2FEJ1CAsi1Ku+MY3kasH19TFykTz2xWmMeq6bk2NU3XXvfJ+Q61m0xktWwt+1HSYf3JZsTms3aRJg==
15231523

15241524
cross-spawn@^7.0.0, cross-spawn@^7.0.2, cross-spawn@^7.0.3:
1525-
version "7.0.3"
1526-
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
1527-
integrity sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==
1525+
version "7.0.6"
1526+
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.6.tgz#8a58fe78f00dcd70c370451759dfbfaf03e8ee9f"
1527+
integrity sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==
15281528
dependencies:
15291529
path-key "^3.1.0"
15301530
shebang-command "^2.0.0"

src/Virtual.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
ERR_VIRTUAL_MISSING_CONTAINER,
88
ERR_VIRTUAL_NOT_STARTED,
99
} from "./errors";
10-
import { getLiveSpokenPhrase, Live } from "./getLiveSpokenPhrase";
10+
import { getLiveSpokenPhrase, LIVE } from "./getLiveSpokenPhrase";
1111
import { flattenTree } from "./flattenTree";
1212
import { getElementNode } from "./commands/getElementNode";
1313
import { getItemText } from "./getItemText";
@@ -180,10 +180,8 @@ const defaultUserEventOptions = {
180180
* "heading, Section Heading, level 1",
181181
* "Section Text",
182182
* "article",
183-
* "banner",
184183
* "heading, Article Header Heading, level 1",
185184
* "Article Header Text",
186-
* "end of banner",
187185
* "Article Text",
188186
* "end of article",
189187
* "end of region",
@@ -360,8 +358,8 @@ export class Virtual {
360358
#spokenPhraseLogWithoutLiveRegions() {
361359
return this.#spokenPhraseLog.filter(
362360
(spokenPhrase) =>
363-
!spokenPhrase.startsWith(Live.ASSERTIVE) &&
364-
!spokenPhrase.startsWith(Live.POLITE)
361+
!spokenPhrase.startsWith(LIVE.ASSERTIVE) &&
362+
!spokenPhrase.startsWith(LIVE.POLITE)
365363
);
366364
}
367365

src/getLiveSpokenPhrase.ts

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { getRole } from "./getNodeAccessibilityData/getRole";
55
import { isElement } from "./isElement";
66
import { sanitizeString } from "./sanitizeString";
77

8+
type ValueOf<T> = T[keyof T];
9+
810
/**
911
* Live region roles:
1012
*
@@ -36,31 +38,30 @@ import { sanitizeString } from "./sanitizeString";
3638
* - https://www.w3.org/TR/wai-aria-1.2/#aria-live
3739
*/
3840

39-
export enum Live {
40-
ASSERTIVE = "assertive",
41-
OFF = "off",
42-
POLITE = "polite",
43-
}
41+
export const LIVE = {
42+
ASSERTIVE: "assertive",
43+
OFF: "off",
44+
POLITE: "polite",
45+
};
4446

45-
enum Relevant {
46-
ADDITIONS = "additions",
47-
ALL = "all",
48-
REMOVALS = "removals",
49-
TEXT = "text",
50-
}
47+
const RELEVANT = {
48+
ADDITIONS: "additions",
49+
ALL: "all",
50+
REMOVALS: "removals",
51+
TEXT: "text",
52+
};
5153

52-
const RELEVANT_VALUES = new Set(Object.values(Relevant));
54+
const RELEVANT_VALUES = new Set(Object.values(RELEVANT));
5355
const DEFAULT_ATOMIC = false;
54-
const DEFAULT_LIVE = Live.OFF;
55-
const DEFAULT_RELEVANT = [Relevant.ADDITIONS, Relevant.TEXT];
56+
const DEFAULT_LIVE = LIVE.OFF;
57+
const DEFAULT_RELEVANT = [RELEVANT.ADDITIONS, RELEVANT.TEXT];
5658

5759
function getSpokenPhraseForNode(node: Node) {
5860
return (
5961
getAccessibleName(node) ||
6062
getAccessibleValue(node) ||
6163
// `node.textContent` is only `null` if the `node` is a `document` or a
6264
// `doctype`. We don't consider either.
63-
6465
sanitizeString(node.textContent!)
6566
);
6667
}
@@ -141,36 +142,36 @@ function getTextSpokenPhrase({
141142
}
142143

143144
const relevantToSpokenPhraseMap = {
144-
[Relevant.ADDITIONS]: getAdditionsSpokenPhrase,
145-
[Relevant.ALL]: getAllSpokenPhrase,
146-
[Relevant.REMOVALS]: getRemovalsSpokenPhrase,
147-
[Relevant.TEXT]: getTextSpokenPhrase,
145+
[RELEVANT.ADDITIONS]: getAdditionsSpokenPhrase,
146+
[RELEVANT.ALL]: getAllSpokenPhrase,
147+
[RELEVANT.REMOVALS]: getRemovalsSpokenPhrase,
148+
[RELEVANT.TEXT]: getTextSpokenPhrase,
148149
};
149150

150151
const roleToImplicitLiveRegionStatesAndPropertiesMap: Record<
151152
string,
152-
{ atomic?: boolean; live: Live }
153+
{ atomic?: boolean; live: ValueOf<typeof LIVE> }
153154
> = {
154155
alert: {
155156
atomic: true,
156-
live: Live.ASSERTIVE,
157+
live: LIVE.ASSERTIVE,
157158
},
158159
log: {
159-
live: Live.POLITE,
160+
live: LIVE.POLITE,
160161
},
161162
marquee: {
162-
live: Live.OFF,
163+
live: LIVE.OFF,
163164
},
164165
status: {
165166
atomic: true,
166-
live: Live.POLITE,
167+
live: LIVE.POLITE,
167168
},
168169
timer: {
169-
live: Live.OFF,
170+
live: LIVE.OFF,
170171
},
171172
alertdialog: {
172173
atomic: true,
173-
live: Live.ASSERTIVE,
174+
live: LIVE.ASSERTIVE,
174175
},
175176
};
176177

@@ -189,11 +190,16 @@ function getLiveRegionAttributes(
189190
relevant,
190191
}: {
191192
atomic?: boolean;
192-
live?: Live;
193+
live?: ValueOf<typeof LIVE>;
193194
liveTarget?: Element;
194-
relevant?: Relevant[];
195+
relevant?: ValueOf<typeof RELEVANT>[];
195196
} = {}
196-
): { atomic: boolean; live: Live; liveTarget?: Element; relevant: Relevant[] } {
197+
): {
198+
atomic: boolean;
199+
live: ValueOf<typeof LIVE>;
200+
liveTarget?: Element;
201+
relevant: ValueOf<typeof RELEVANT>[];
202+
} {
197203
// TODO: it would be far better if worked with the accessibility tree rather
198204
// than reconstructing here and making assumptions (though probable) about
199205
// the allowed roles or inherited presentational roles.
@@ -213,7 +219,7 @@ function getLiveRegionAttributes(
213219
}
214220

215221
if (typeof live === "undefined" && target.hasAttribute("aria-live")) {
216-
live = target.getAttribute("aria-live") as Live;
222+
live = target.getAttribute("aria-live") as ValueOf<typeof LIVE>;
217223
liveTarget = target;
218224
}
219225

@@ -234,11 +240,11 @@ function getLiveRegionAttributes(
234240
.getAttribute("aria-relevant")!
235241
.split(" ")
236242
.filter(
237-
(token) => !!RELEVANT_VALUES.has(token as Relevant)
238-
) as Relevant[];
243+
(token) => !!RELEVANT_VALUES.has(token as ValueOf<typeof RELEVANT>)
244+
) as ValueOf<typeof RELEVANT>[];
239245

240-
if (relevant.includes(Relevant.ALL)) {
241-
relevant = [Relevant.ALL];
246+
if (relevant.includes(RELEVANT.ALL)) {
247+
relevant = [RELEVANT.ALL];
242248
}
243249
}
244250

@@ -289,7 +295,7 @@ export function getLiveSpokenPhrase({
289295
target: getElementFromNode(target),
290296
});
291297

292-
if (live === Live.OFF || !liveTarget) {
298+
if (live === LIVE.OFF || !liveTarget) {
293299
return "";
294300
}
295301

src/getNodeAccessibilityData/getAccessibleAttributeLabels/mapAttributeNameAndValueToLabel.ts

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,24 @@ import { getAccessibleValue } from "../getAccessibleValue";
33
import { getItemText } from "../../getItemText";
44
import { getNodeByIdRef } from "../../getNodeByIdRef";
55

6-
enum State {
7-
BUSY = "busy",
8-
CHECKED = "checked",
9-
CURRENT = "current item",
10-
DISABLED = "disabled",
11-
EXPANDED = "expanded",
12-
INVALID = "invalid",
13-
MODAL = "modal",
14-
MULTI_SELECTABLE = "multi-selectable",
15-
PARTIALLY_CHECKED = "partially checked",
16-
PARTIALLY_PRESSED = "partially pressed",
17-
PRESSED = "pressed",
18-
READ_ONLY = "read only",
19-
REQUIRED = "required",
20-
SELECTED = "selected",
21-
}
6+
type ValueOf<T> = T[keyof T];
7+
8+
const STATE = {
9+
BUSY: "busy",
10+
CHECKED: "checked",
11+
CURRENT: "current item",
12+
DISABLED: "disabled",
13+
EXPANDED: "expanded",
14+
INVALID: "invalid",
15+
MODAL: "modal",
16+
MULTI_SELECTABLE: "multi-selectable",
17+
PARTIALLY_CHECKED: "partially checked",
18+
PARTIALLY_PRESSED: "partially pressed",
19+
PRESSED: "pressed",
20+
READ_ONLY: "read only",
21+
REQUIRED: "required",
22+
SELECTED: "selected",
23+
};
2224

2325
// https://www.w3.org/TR/wai-aria-1.2/#state_prop_def
2426
const ariaPropertyToVirtualLabelMap: Record<
@@ -35,8 +37,8 @@ const ariaPropertyToVirtualLabelMap: Record<
3537
}),
3638
"aria-braillelabel": null, // Currently won't do - not implementing a braille screen reader
3739
"aria-brailleroledescription": null, // Currently won't do - not implementing a braille screen reader
38-
"aria-busy": state(State.BUSY),
39-
"aria-checked": tristate(State.CHECKED, State.PARTIALLY_CHECKED),
40+
"aria-busy": state(STATE.BUSY),
41+
"aria-checked": tristate(STATE.CHECKED, STATE.PARTIALLY_CHECKED),
4042
"aria-colcount": integer("column count"),
4143
"aria-colindex": integer("column index"),
4244
"aria-colindextext": string("column index"),
@@ -48,16 +50,16 @@ const ariaPropertyToVirtualLabelMap: Record<
4850
location: "current location",
4951
date: "current date",
5052
time: "current time",
51-
true: State.CURRENT,
52-
false: `not ${State.CURRENT}`,
53+
true: STATE.CURRENT,
54+
false: `not ${STATE.CURRENT}`,
5355
}),
5456
"aria-describedby": null, // Handled by accessible description
5557
"aria-description": null, // Handled by accessible description
5658
"aria-details": idRefs("linked details", "linked details", false),
57-
"aria-disabled": state(State.DISABLED),
59+
"aria-disabled": state(STATE.DISABLED),
5860
"aria-dropeffect": null, // Deprecated in WAI-ARIA 1.1
5961
"aria-errormessage": errorMessageIdRefs("error message", "error messages"),
60-
"aria-expanded": state(State.EXPANDED),
62+
"aria-expanded": state(STATE.EXPANDED),
6163
"aria-flowto": idRefs("alternate reading order", "alternate reading orders"), // Handled by virtual.perform()
6264
"aria-grabbed": null, // Deprecated in WAI-ARIA 1.1
6365
"aria-haspopup": token({
@@ -78,34 +80,34 @@ const ariaPropertyToVirtualLabelMap: Record<
7880
"aria-hidden": null, // Excluded from accessibility tree
7981
"aria-invalid": token({
8082
grammar: "grammatical error detected",
81-
false: `not ${State.INVALID}`,
83+
false: `not ${STATE.INVALID}`,
8284
spelling: "spelling error detected",
83-
true: State.INVALID,
85+
true: STATE.INVALID,
8486
}),
8587
"aria-keyshortcuts": string("key shortcuts"),
8688
"aria-label": null, // Handled by accessible name
8789
"aria-labelledby": null, // Handled by accessible name
8890
"aria-level": integer("level"),
8991
"aria-live": null, // Handled by live region logic
90-
"aria-modal": state(State.MODAL),
91-
"aria-multiselectable": state(State.MULTI_SELECTABLE),
92+
"aria-modal": state(STATE.MODAL),
93+
"aria-multiselectable": state(STATE.MULTI_SELECTABLE),
9294
"aria-orientation": token({
9395
horizontal: "orientated horizontally",
9496
vertical: "orientated vertically",
9597
}),
9698
"aria-owns": null, // Handled by accessibility tree construction
9799
"aria-placeholder": string("placeholder"),
98100
"aria-posinset": integer("position"),
99-
"aria-pressed": tristate(State.PRESSED, State.PARTIALLY_PRESSED),
100-
"aria-readonly": state(State.READ_ONLY),
101+
"aria-pressed": tristate(STATE.PRESSED, STATE.PARTIALLY_PRESSED),
102+
"aria-readonly": state(STATE.READ_ONLY),
101103
"aria-relevant": null, // Handled by live region logic
102-
"aria-required": state(State.REQUIRED),
104+
"aria-required": state(STATE.REQUIRED),
103105
"aria-roledescription": null, // Handled by accessible description
104106
"aria-rowcount": integer("row count"),
105107
"aria-rowindex": integer("row index"),
106108
"aria-rowindextext": string("row index"),
107109
"aria-rowspan": integer("row span"),
108-
"aria-selected": state(State.SELECTED),
110+
"aria-selected": state(STATE.SELECTED),
109111
"aria-setsize": integer("set size"),
110112
"aria-sort": token({
111113
ascending: "sorted in ascending order",
@@ -126,7 +128,7 @@ interface MapperArgs {
126128
node?: HTMLElement;
127129
}
128130

129-
function state(stateValue: State) {
131+
function state(stateValue: ValueOf<typeof STATE>) {
130132
return function stateMapper({ attributeValue, negative }: MapperArgs) {
131133
if (negative) {
132134
return attributeValue !== "false" ? `not ${stateValue}` : stateValue;
@@ -198,7 +200,10 @@ function idRef(propertyName: string) {
198200
};
199201
}
200202

201-
function tristate(stateValue: State, mixedValue: State) {
203+
function tristate(
204+
stateValue: ValueOf<typeof STATE>,
205+
mixedValue: ValueOf<typeof STATE>
206+
) {
202207
return function stateMapper({ attributeValue }: MapperArgs) {
203208
if (attributeValue === "mixed") {
204209
return mixedValue;

src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,8 @@ import { StartOptions, Virtual } from "./Virtual";
5353
* "heading, Section Heading, level 1",
5454
* "Section Text",
5555
* "article",
56-
* "banner",
5756
* "heading, Article Header Heading, level 1",
5857
* "Article Header Text",
59-
* "end of banner",
6058
* "Article Text",
6159
* "end of article",
6260
* "end of region",

0 commit comments

Comments
 (0)