[typescript] TypeScript-ify accessibility/AccessibilityModel.js

Bug: chromium:1011811
Change-Id: Iec3dcb19760dd5128c7c24a8c7f02eccb3803ed2
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2387722
Reviewed-by: Simon Zünd <[email protected]>
Commit-Queue: Peter Marshall <[email protected]>
diff --git a/front_end/accessibility/AccessibilityModel.js b/front_end/accessibility/AccessibilityModel.js
index f5ffb5e..3ff17f2 100644
--- a/front_end/accessibility/AccessibilityModel.js
+++ b/front_end/accessibility/AccessibilityModel.js
@@ -2,11 +2,24 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-// @ts-nocheck
-// TODO(crbug.com/1011811): Enable TypeScript compiler checks
-
 import * as SDK from '../sdk/sdk.js';
 
+/** @enum {string} */
+export const CoreAxPropertyName = {
+  Name: 'name',
+  Description: 'description',
+  Value: 'value',
+  Role: 'role',
+};
+
+/** @typedef {{
+ *   name: (CoreAxPropertyName | Protocol.Accessibility.AXPropertyName),
+ *   value: Protocol.Accessibility.AXValue
+ * }}
+ */
+// @ts-ignore typedef
+export let CoreOrProtocolAxProperty;
+
 /**
  * @unrestricted
  */
@@ -72,20 +85,20 @@
   }
 
   /**
-   * @return {!Array<!Protocol.Accessibility.AXProperty>}
+   * @return {!Array<!CoreOrProtocolAxProperty>}
    */
   coreProperties() {
+    /** @type {!Array<!CoreOrProtocolAxProperty>} */
     const properties = [];
 
     if (this._name) {
-      properties.push(/** @type {!Protocol.Accessibility.AXProperty} */ ({name: 'name', value: this._name}));
+      properties.push({name: CoreAxPropertyName.Name, value: this._name});
     }
     if (this._description) {
-      properties.push(
-          /** @type {!Protocol.Accessibility.AXProperty} */ ({name: 'description', value: this._description}));
+      properties.push({name: CoreAxPropertyName.Description, value: this._description});
     }
     if (this._value) {
-      properties.push(/** @type {!Protocol.Accessibility.AXProperty} */ ({name: 'value', value: this._value}));
+      properties.push({name: CoreAxPropertyName.Value, value: this._value});
     }
 
     return properties;
@@ -155,23 +168,23 @@
   }
 
   highlightDOMNode() {
-    if (!this.deferredDOMNode()) {
+    const deferredNode = this.deferredDOMNode();
+    if (!deferredNode) {
       return;
     }
-
     // Highlight node in page.
-    this.deferredDOMNode().highlight();
+    deferredNode.highlight();
   }
 
   /**
    * @return {!Array<!AccessibilityNode>}
    */
   children() {
-    const children = [];
     if (!this._childIds) {
-      return children;
+      return [];
     }
 
+    const children = [];
     for (const childId of this._childIds) {
       const child = this._accessibilityModel.axNodeForId(childId);
       if (child) {
@@ -226,15 +239,16 @@
 
   /**
    * @param {!SDK.DOMModel.DOMNode} node
-   * @return {!Promise}
+   * @return {!Promise<void>}
    */
   async requestPartialAXTree(node) {
-    const payloads = await this._agent.getPartialAXTree(node.id, undefined, undefined, true);
-    if (!payloads) {
+    const {nodes} = await this._agent.invoke_getPartialAXTree(
+        {nodeId: node.id, backendNodeId: undefined, objectId: undefined, fetchRelatives: true});
+    if (!nodes) {
       return;
     }
 
-    for (const payload of payloads) {
+    for (const payload of nodes) {
       new AccessibilityNode(this, payload);
     }
 
@@ -250,7 +264,7 @@
    * @return {?AccessibilityNode}
    */
   axNodeForId(axId) {
-    return this._axIdToAXNode.get(axId);
+    return this._axIdToAXNode.get(axId) || null;
   }
 
   /**
diff --git a/front_end/accessibility/AccessibilityNodeView.js b/front_end/accessibility/AccessibilityNodeView.js
index bf5be91..75ba5c5 100644
--- a/front_end/accessibility/AccessibilityNodeView.js
+++ b/front_end/accessibility/AccessibilityNodeView.js
@@ -9,7 +9,7 @@
 import * as SDK from '../sdk/sdk.js';
 import * as UI from '../ui/ui.js';
 
-import {AccessibilityNode} from './AccessibilityModel.js';  // eslint-disable-line no-unused-vars
+import {AccessibilityNode, CoreAxPropertyName, CoreOrProtocolAxProperty} from './AccessibilityModel.js';  // eslint-disable-line no-unused-vars
 import {AXAttributes, AXNativeSourceTypes, AXSourceTypes} from './AccessibilityStrings.js';
 import {AccessibilitySubPane} from './AccessibilitySubPane.js';
 
@@ -97,7 +97,7 @@
     treeOutline.element.classList.remove('hidden');
 
     /**
-     * @param {!Protocol.Accessibility.AXProperty} property
+     * @param {!CoreOrProtocolAxProperty} property
      */
     function addProperty(property) {
       treeOutline.appendChild(
@@ -108,8 +108,12 @@
       addProperty(property);
     }
 
-    const roleProperty = /** @type {!Protocol.Accessibility.AXProperty} */ ({name: 'role', value: axNode.role()});
-    addProperty(roleProperty);
+    const role = axNode.role();
+    if (role) {
+      /** @type {!CoreOrProtocolAxProperty} */
+      const roleProperty = {name: CoreAxPropertyName.Role, value: role};
+      addProperty(roleProperty);
+    }
     for (const property of /** @type {!Array.<!Protocol.Accessibility.AXProperty>} */ (axNode.properties())) {
       addProperty(property);
     }
@@ -296,7 +300,7 @@
  */
 export class AXNodePropertyTreePropertyElement extends AXNodePropertyTreeElement {
   /**
-   * @param {!Protocol.Accessibility.AXProperty} property
+   * @param {!CoreOrProtocolAxProperty} property
    * @param {!AccessibilityNode} axNode
    */
   constructor(property, axNode) {
diff --git a/front_end/protocol_client/InspectorBackend.js b/front_end/protocol_client/InspectorBackend.js
index 059c03c..413d58c 100644
--- a/front_end/protocol_client/InspectorBackend.js
+++ b/front_end/protocol_client/InspectorBackend.js
@@ -650,6 +650,13 @@
   // Agent accessors, keep alphabetically sorted.
 
   /**
+   * @return {!ProtocolProxyApi.AccessibilityApi}
+   */
+  accessibilityAgent() {
+    throw new Error('Implemented in InspectorBackend.js');
+  }
+
+  /**
    * @return {!ProtocolProxyApi.ApplicationCacheApi}
    */
   applicationCacheAgent() {