Skip to content

Commit

Permalink
chore: use curly (#8519)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrandolf committed Jun 14, 2022
1 parent 0678343 commit e6442dd
Show file tree
Hide file tree
Showing 68 changed files with 1,507 additions and 590 deletions.
4 changes: 4 additions & 0 deletions .eslintrc.js
Expand Up @@ -21,6 +21,8 @@ module.exports = {
extends: ['plugin:prettier/recommended'],

rules: {
// Brackets keep code readable.
curly: [2, 'all'],
// Error if files are not formatted with Prettier correctly.
'prettier/prettier': 2,
// syntax preferences
Expand Down Expand Up @@ -130,6 +132,8 @@ module.exports = {
],
plugins: ['eslint-plugin-tsdoc'],
rules: {
// Brackets keep code readable.
curly: [2, 'all'],
// Error if comments do not adhere to `tsdoc`.
'tsdoc/syntax': 2,
'no-unused-vars': 0,
Expand Down
7 changes: 5 additions & 2 deletions examples/block-images.js
Expand Up @@ -23,8 +23,11 @@ const puppeteer = require('puppeteer');
const page = await browser.newPage();
await page.setRequestInterception(true);
page.on('request', (request) => {
if (request.resourceType() === 'image') request.abort();
else request.continue();
if (request.resourceType() === 'image') {
request.abort();
} else {
request.continue();
}
});
await page.goto('https://news.google.com/news/');
await page.screenshot({ path: 'news.png', fullPage: true });
Expand Down
137 changes: 101 additions & 36 deletions src/common/Accessibility.ts
Expand Up @@ -196,13 +196,19 @@ export class Accessibility {
needle = defaultRoot.find(
(node) => node.payload.backendDOMNodeId === backendNodeId
);
if (!needle) return null;
if (!needle) {
return null;
}
}
if (!interestingOnly) {
return this.serializeTree(needle)[0] ?? null;
}
if (!interestingOnly) return this.serializeTree(needle)[0] ?? null;

const interestingNodes = new Set<AXNode>();
this.collectInterestingNodes(interestingNodes, defaultRoot, false);
if (!interestingNodes.has(needle)) return null;
if (!interestingNodes.has(needle)) {
return null;
}
return this.serializeTree(needle, interestingNodes)[0] ?? null;
}

Expand All @@ -211,13 +217,18 @@ export class Accessibility {
interestingNodes?: Set<AXNode>
): SerializedAXNode[] {
const children: SerializedAXNode[] = [];
for (const child of node.children)
for (const child of node.children) {
children.push(...this.serializeTree(child, interestingNodes));
}

if (interestingNodes && !interestingNodes.has(node)) return children;
if (interestingNodes && !interestingNodes.has(node)) {
return children;
}

const serializedNode = node.serialize();
if (children.length) serializedNode.children = children;
if (children.length) {
serializedNode.children = children;
}
return [serializedNode];
}

Expand All @@ -226,11 +237,16 @@ export class Accessibility {
node: AXNode,
insideControl: boolean
): void {
if (node.isInteresting(insideControl)) collection.add(node);
if (node.isLeafNode()) return;
if (node.isInteresting(insideControl)) {
collection.add(node);
}
if (node.isLeafNode()) {
return;
}
insideControl = insideControl || node.isControl();
for (const child of node.children)
for (const child of node.children) {
this.collectInterestingNodes(collection, child, insideControl);
}
}
}

Expand Down Expand Up @@ -258,14 +274,22 @@ class AXNode {
this.#richlyEditable = property.value.value === 'richtext';
this.#editable = true;
}
if (property.name === 'focusable') this.#focusable = property.value.value;
if (property.name === 'hidden') this.#hidden = property.value.value;
if (property.name === 'focusable') {
this.#focusable = property.value.value;
}
if (property.name === 'hidden') {
this.#hidden = property.value.value;
}
}
}

#isPlainTextField(): boolean {
if (this.#richlyEditable) return false;
if (this.#editable) return true;
if (this.#richlyEditable) {
return false;
}
if (this.#editable) {
return true;
}
return this.#role === 'textbox' || this.#role === 'searchbox';
}

Expand All @@ -288,22 +312,30 @@ class AXNode {
}

public find(predicate: (x: AXNode) => boolean): AXNode | null {
if (predicate(this)) return this;
if (predicate(this)) {
return this;
}
for (const child of this.children) {
const result = child.find(predicate);
if (result) return result;
if (result) {
return result;
}
}
return null;
}

public isLeafNode(): boolean {
if (!this.children.length) return true;
if (!this.children.length) {
return true;
}

// These types of objects may have children that we use as internal
// implementation details, but we want to expose them as leaves to platform
// accessibility APIs because screen readers might be confused if they find
// any children.
if (this.#isPlainTextField() || this.#isTextOnlyObject()) return true;
if (this.#isPlainTextField() || this.#isTextOnlyObject()) {
return true;
}

// Roles whose children are only presentational according to the ARIA and
// HTML5 Specs should be hidden from screen readers.
Expand All @@ -324,9 +356,15 @@ class AXNode {
}

// Here and below: Android heuristics
if (this.#hasFocusableChild()) return false;
if (this.#focusable && this.#name) return true;
if (this.#role === 'heading' && this.#name) return true;
if (this.#hasFocusableChild()) {
return false;
}
if (this.#focusable && this.#name) {
return true;
}
if (this.#role === 'heading' && this.#name) {
return true;
}
return false;
}

Expand Down Expand Up @@ -361,27 +399,41 @@ class AXNode {

public isInteresting(insideControl: boolean): boolean {
const role = this.#role;
if (role === 'Ignored' || this.#hidden || this.#ignored) return false;
if (role === 'Ignored' || this.#hidden || this.#ignored) {
return false;
}

if (this.#focusable || this.#richlyEditable) return true;
if (this.#focusable || this.#richlyEditable) {
return true;
}

// If it's not focusable but has a control role, then it's interesting.
if (this.isControl()) return true;
if (this.isControl()) {
return true;
}

// A non focusable child of a control is not interesting
if (insideControl) return false;
if (insideControl) {
return false;
}

return this.isLeafNode() && !!this.#name;
}

public serialize(): SerializedAXNode {
const properties = new Map<string, number | string | boolean>();
for (const property of this.payload.properties || [])
for (const property of this.payload.properties || []) {
properties.set(property.name.toLowerCase(), property.value.value);
if (this.payload.name) properties.set('name', this.payload.name.value);
if (this.payload.value) properties.set('value', this.payload.value.value);
if (this.payload.description)
}
if (this.payload.name) {
properties.set('name', this.payload.name.value);
}
if (this.payload.value) {
properties.set('value', this.payload.value.value);
}
if (this.payload.description) {
properties.set('description', this.payload.description.value);
}

const node: SerializedAXNode = {
role: this.#role,
Expand All @@ -407,7 +459,9 @@ class AXNode {
properties.get(key) as string;

for (const userStringProperty of userStringProperties) {
if (!properties.has(userStringProperty)) continue;
if (!properties.has(userStringProperty)) {
continue;
}

node[userStringProperty] = getUserStringPropertyValue(userStringProperty);
}
Expand Down Expand Up @@ -440,17 +494,22 @@ class AXNode {
// RootWebArea's treat focus differently than other nodes. They report whether
// their frame has focus, not whether focus is specifically on the root
// node.
if (booleanProperty === 'focused' && this.#role === 'RootWebArea')
if (booleanProperty === 'focused' && this.#role === 'RootWebArea') {
continue;
}
const value = getBooleanPropertyValue(booleanProperty);
if (!value) continue;
if (!value) {
continue;
}
node[booleanProperty] = getBooleanPropertyValue(booleanProperty);
}

type TristateProperty = 'checked' | 'pressed';
const tristateProperties: TristateProperty[] = ['checked', 'pressed'];
for (const tristateProperty of tristateProperties) {
if (!properties.has(tristateProperty)) continue;
if (!properties.has(tristateProperty)) {
continue;
}
const value = properties.get(tristateProperty);
node[tristateProperty] =
value === 'mixed' ? 'mixed' : value === 'true' ? true : false;
Expand All @@ -465,7 +524,9 @@ class AXNode {
const getNumericalPropertyValue = (key: NumbericalProperty): number =>
properties.get(key) as number;
for (const numericalProperty of numericalProperties) {
if (!properties.has(numericalProperty)) continue;
if (!properties.has(numericalProperty)) {
continue;
}
node[numericalProperty] = getNumericalPropertyValue(numericalProperty);
}

Expand All @@ -484,19 +545,23 @@ class AXNode {
properties.get(key) as string;
for (const tokenProperty of tokenProperties) {
const value = getTokenPropertyValue(tokenProperty);
if (!value || value === 'false') continue;
if (!value || value === 'false') {
continue;
}
node[tokenProperty] = getTokenPropertyValue(tokenProperty);
}
return node;
}

public static createTree(payloads: Protocol.Accessibility.AXNode[]): AXNode {
const nodeById = new Map<string, AXNode>();
for (const payload of payloads)
for (const payload of payloads) {
nodeById.set(payload.nodeId, new AXNode(payload));
}
for (const node of nodeById.values()) {
for (const childId of node.payload.childIds || [])
for (const childId of node.payload.childIds || []) {
node.children.push(nodeById.get(childId)!);
}
}
return nodeById.values().next().value;
}
Expand Down
3 changes: 2 additions & 1 deletion src/common/AriaQueryHandler.ts
Expand Up @@ -76,8 +76,9 @@ function parseAriaSelector(selector: string): ARIAQueryOption {
return '';
}
);
if (defaultName && !queryOptions.name)
if (defaultName && !queryOptions.name) {
queryOptions.name = normalizeValue(defaultName);
}
return queryOptions;
}

Expand Down
18 changes: 13 additions & 5 deletions src/common/Browser.ts
Expand Up @@ -285,11 +285,12 @@ export class Browser extends EventEmitter {

this.#defaultContext = new BrowserContext(this.#connection, this);
this.#contexts = new Map();
for (const contextId of contextIds)
for (const contextId of contextIds) {
this.#contexts.set(
contextId,
new BrowserContext(this.#connection, this, contextId)
);
}

this.#targets = new Map();
this.#connection.on(ConnectionEmittedEvents.Disconnected, () =>
Expand Down Expand Up @@ -441,7 +442,9 @@ export class Browser extends EventEmitter {
}

async #targetDestroyed(event: { targetId: string }): Promise<void> {
if (this.#ignoredTargets.has(event.targetId)) return;
if (this.#ignoredTargets.has(event.targetId)) {
return;
}
const target = this.#targets.get(event.targetId);
if (!target) {
throw new Error(
Expand All @@ -460,7 +463,9 @@ export class Browser extends EventEmitter {
}

#targetInfoChanged(event: Protocol.Target.TargetInfoChangedEvent): void {
if (this.#ignoredTargets.has(event.targetInfo.targetId)) return;
if (this.#ignoredTargets.has(event.targetInfo.targetId)) {
return;
}
const target = this.#targets.get(event.targetInfo.targetId);
if (!target) {
throw new Error(
Expand Down Expand Up @@ -580,7 +585,9 @@ export class Browser extends EventEmitter {
this.on(BrowserEmittedEvents.TargetCreated, check);
this.on(BrowserEmittedEvents.TargetChanged, check);
try {
if (!timeout) return await targetPromise;
if (!timeout) {
return await targetPromise;
}
this.targets().forEach(check);
return await waitWithTimeout(targetPromise, 'target', timeout);
} finally {
Expand Down Expand Up @@ -827,8 +834,9 @@ export class BrowserContext extends EventEmitter {
const protocolPermissions = permissions.map((permission) => {
const protocolPermission =
WEB_PERMISSION_TO_PROTOCOL_PERMISSION.get(permission);
if (!protocolPermission)
if (!protocolPermission) {
throw new Error('Unknown permission: ' + permission);
}
return protocolPermission;
});
await this.#connection.send('Browser.grantPermissions', {
Expand Down
8 changes: 6 additions & 2 deletions src/common/BrowserWebSocketTransport.ts
Expand Up @@ -34,10 +34,14 @@ export class BrowserWebSocketTransport implements ConnectionTransport {
constructor(ws: WebSocket) {
this.#ws = ws;
this.#ws.addEventListener('message', (event) => {
if (this.onmessage) this.onmessage.call(null, event.data);
if (this.onmessage) {
this.onmessage.call(null, event.data);
}
});
this.#ws.addEventListener('close', () => {
if (this.onclose) this.onclose.call(null);
if (this.onclose) {
this.onclose.call(null);
}
});
// Silently ignore all errors - we don't know what to do with them.
this.#ws.addEventListener('error', () => {});
Expand Down

0 comments on commit e6442dd

Please sign in to comment.