Skip to content

Commit be66486

Browse files
committed
address review comments
1 parent f28b580 commit be66486

4 files changed

Lines changed: 209 additions & 87 deletions

File tree

src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,12 @@ export {AxAuditDtxTransport} from './services/ios/accessibility-audit/dtx-transp
6565
export type {InvokeOptions as AxInvokeOptions} from './services/ios/accessibility-audit/dtx-transport.js';
6666
export {AX_OBJECT_TYPE, deserializeAxObject} from './services/ios/accessibility-audit/ax-deserialize.js';
6767
export {AxPoint} from './services/ios/accessibility-audit/ax-values.js';
68-
export {serializeAxElement, toAxElement, toInspectedElement} from './services/ios/accessibility-audit/ax-element.js';
68+
export {
69+
serializeAxAttribute,
70+
serializeAxElement,
71+
toAxElement,
72+
toInspectedElement,
73+
} from './services/ios/accessibility-audit/ax-element.js';
6974
export type {
7075
AxElement,
7176
AxElementAttribute,

src/services/ios/accessibility-audit/ax-element.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {util} from '@appium/support';
2+
13
import {AX_OBJECT_TYPE} from './ax-deserialize.js';
24

35
/**
@@ -73,17 +75,22 @@ function toBuffer(value: unknown): Buffer | undefined {
7375
return undefined;
7476
}
7577

76-
/** Parses a deserialized `AXAuditElement_v1`. */
78+
/**
79+
* Parses a deserialized `AXAuditElement_v1`.
80+
*
81+
* The `_v1` suffixes are the daemon's own wire keys, not our assumption. A
82+
* future shape would carry different keys, so this returns `undefined` rather
83+
* than misreading one.
84+
*/
7785
export function toAxElement(value: unknown): AxElement | undefined {
78-
if (typeof value !== 'object' || value === null) {
86+
if (!util.isPlainObject(value)) {
7987
return undefined;
8088
}
8189
const fields = value as Record<string, unknown>;
8290
const platformValue = fields.PlatformElementValue_v1;
83-
const container =
84-
typeof platformValue === 'object' && platformValue !== null
85-
? ((platformValue as Record<string, unknown>)['NS.data'] ?? platformValue)
86-
: undefined;
91+
const container = util.isPlainObject(platformValue)
92+
? ((platformValue as Record<string, unknown>)['NS.data'] ?? platformValue)
93+
: undefined;
8794
const platformElement = toBuffer(container);
8895
if (!platformElement) {
8996
return undefined;
@@ -113,7 +120,7 @@ export function serializeAxElement(element: AxElement): Record<string, unknown>
113120
}
114121

115122
function toAttribute(value: unknown): AxElementAttribute | undefined {
116-
if (typeof value !== 'object' || value === null) {
123+
if (!util.isPlainObject(value)) {
117124
return undefined;
118125
}
119126
const fields = value as Record<string, unknown>;
@@ -134,21 +141,14 @@ function toAttribute(value: unknown): AxElementAttribute | undefined {
134141

135142
/** Drops the decoder's type tag so the object round-trips as the daemon sent it. */
136143
function stripTag(fields: Record<string, unknown>): Record<string, unknown> {
137-
const out: Record<string, unknown> = {};
138-
for (const [key, value] of Object.entries(fields)) {
139-
if (key !== AX_OBJECT_TYPE) {
140-
out[key] = value;
141-
}
142-
}
143-
return out;
144+
return Object.fromEntries(Object.entries(fields).filter(([key]) => key !== AX_OBJECT_TYPE));
144145
}
145146

146147
/** Rebuilds an attribute descriptor for the wire. */
147148
export function serializeAxAttribute(attribute: AxElementAttribute): Record<string, unknown> {
148-
const value: Record<string, unknown> = {};
149-
for (const [key, inner] of Object.entries(attribute.raw)) {
150-
value[key] = {ObjectType: 'passthrough', Value: inner};
151-
}
149+
const value = Object.fromEntries(
150+
Object.entries(attribute.raw).map(([key, inner]) => [key, {ObjectType: 'passthrough', Value: inner}]),
151+
);
152152
return {
153153
ObjectType: 'AXAuditElementAttribute_v1',
154154
Value: {ObjectType: 'passthrough', Value: value},
@@ -157,11 +157,11 @@ export function serializeAxAttribute(attribute: AxElementAttribute): Record<stri
157157

158158
/** Parses the payload of an inbound `hostInspectorCurrentElementChanged:`. */
159159
export function toInspectedElement(value: unknown): AxInspectedElement {
160-
const fields = (typeof value === 'object' && value !== null ? value : {}) as Record<string, unknown>;
160+
const fields = (util.isPlainObject(value) ? value : {}) as Record<string, unknown>;
161161
const rawSections = Array.isArray(fields.InspectorSectionsValue_v1) ? fields.InspectorSectionsValue_v1 : [];
162162
const sections: AxInspectorSection[] = [];
163163
for (const rawSection of rawSections) {
164-
if (typeof rawSection !== 'object' || rawSection === null) {
164+
if (!util.isPlainObject(rawSection)) {
165165
continue;
166166
}
167167
const section = rawSection as Record<string, unknown>;

0 commit comments

Comments
 (0)