Skip to content

Commit

Permalink
chore(null): null correctness for axe-utils (#3036)
Browse files Browse the repository at this point in the history
#### Description of changes

null-correctness for axe-utils plus the handful of implicitly references `*.d.ts` files

#### Pull request checklist
<!-- If a checklist item is not applicable to this change, write "n/a" in the checkbox -->
- [x] Addresses an existing issue: #2869
- [x] Ran `yarn fastpass`
- [n/a] Added/updated relevant unit test(s) (and ran `yarn test`)
- [n/a] Verified code coverage for the changes made. Check coverage report at: `<rootDir>/test-results/unit/coverage`
- [x] PR title *AND* final merge commit title both start with a semantic tag (`fix:`, `chore:`, `feat(feature-name):`, `refactor:`). See `CONTRIBUTING.md`.
- [n/a] (UI changes only) Added screenshots/GIFs to description above
- [n/a] (UI changes only) Verified usability with NVDA/JAWS
  • Loading branch information
dbjorge authored Jul 7, 2020
1 parent 675ae80 commit f764cee
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
27 changes: 16 additions & 11 deletions src/scanner/axe-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { DictionaryStringTo } from '../types/common-types';

export type ImageCodedAs = 'Decorative' | 'Meaningful';

export function getMatchesFromRule(ruleId: string): (node: any, virtualNode: any) => boolean {
export function getMatchesFromRule(
ruleId: string,
): ((node: any, virtualNode: any) => boolean) | undefined {
return axe._audit.defaultConfig.rules.filter(rule => rule.id === ruleId)[0].matches;
}

Expand Down Expand Up @@ -38,19 +40,22 @@ export function getPropertyValuesMatching(
for (let i = 0; i < attrs.length; i++) {
const name = attrs[i].name;
if (regex.test(name)) {
dictionary[name] = node.getAttribute(name);
dictionary[name] = node.getAttribute(name)!;
}
}
}
return dictionary;
}

export function getAttributes(node: HTMLElement, attributes: string[]): DictionaryStringTo<string> {
const retDict: DictionaryStringTo<string> = {};
export function getAttributes(
node: HTMLElement,
attributes: string[],
): DictionaryStringTo<string | null> {
const retDict: DictionaryStringTo<string | null> = {};
attributes
.filter(atributeName => node.hasAttribute(atributeName))
.filter(attributeName => node.hasAttribute(attributeName))
.forEach(attributeName => {
const attributeValue = node.getAttribute(attributeName);
const attributeValue = node.getAttribute(attributeName)!;
retDict[attributeName] = attributeValue.length > 0 ? attributeValue : null;
});

Expand All @@ -65,7 +70,7 @@ export function hasCustomWidgetMarkup(node: HTMLElement): boolean {
return tabIndex === '-1' || Object.keys(ariaValues).length > 0 || hasRole;
}

export function getImageCodedAs(node: HTMLElement): ImageCodedAs {
export function getImageCodedAs(node: HTMLElement): ImageCodedAs | null {
const role = node.getAttribute('role');
const alt = node.getAttribute('alt');

Expand All @@ -85,8 +90,8 @@ export function getImageCodedAs(node: HTMLElement): ImageCodedAs {
return null;
}

export function isWhiteSpace(text: string): boolean {
return text && text.length > 0 && text.trim() === '';
export function isWhiteSpace(text: string | null): boolean {
return text != null && text.length > 0 && text.trim() === '';
}

export function hasBackgoundImage(node: HTMLElement): boolean {
Expand All @@ -96,8 +101,8 @@ export function hasBackgoundImage(node: HTMLElement): boolean {
return computedBackgroundImage !== 'none';
}

export function getImageType(node: HTMLElement): string {
let imageType: string;
export function getImageType(node: HTMLElement): string | null {
let imageType: string | null = null;
if (node.tagName.toLowerCase() === 'img') {
imageType = '<img>';
} else if (node.tagName.toLowerCase() === 'i') {
Expand Down
2 changes: 0 additions & 2 deletions src/scanner/iruleresults.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { HyperlinkDefinition } from 'views/content/content-page';

export interface AxeRule {
id: string;
nodes: AxeNodeResult[];
Expand Down
4 changes: 4 additions & 0 deletions tsconfig.strictNullChecks.json
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@
"./src/injected/frameCommunicators/window-message.ts",
"./src/injected/iframe-detector.ts",
"./src/injected/scan-incomplete-warning-detector.ts",
"./src/injected/scanner.d.ts",
"./src/injected/stylesheet-init.ts",
"./src/injected/tab-order-property-bag.ts",
"./src/injected/visualization/focus-indicator.ts",
Expand Down Expand Up @@ -224,9 +225,12 @@
"./src/reports/react-static-renderer.ts",
"./src/reports/report-name-generator-builder.ts",
"./src/reports/report-name-generator.ts",
"./src/scanner/axe-extension.d.ts",
"./src/scanner/axe-options.ts",
"./src/scanner/axe-rule-overrides.ts",
"./src/scanner/axe-utils.ts",
"./src/scanner/document-utils.ts",
"./src/scanner/iruleresults.d.ts",
"./src/scanner/scan-options.ts",
"./src/tests/common/get-automation-id-selector.ts",
"./src/tests/electron/common/element-identifiers/common-selectors.ts",
Expand Down

0 comments on commit f764cee

Please sign in to comment.