Skip to content

Commit

Permalink
feat: Support ES5 environments (#141)
Browse files Browse the repository at this point in the history
* feat: Support ES5 environments

* yarn format

* add changeset
  • Loading branch information
eps1lon committed Mar 13, 2020
1 parent 176d2a2 commit e80a1fb
Show file tree
Hide file tree
Showing 10 changed files with 292 additions and 23 deletions.
6 changes: 6 additions & 0 deletions .browserslistrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ie 11
edge >= 14
firefox >= 52
chrome >= 49
safari >= 10
node 10.0
5 changes: 5 additions & 0 deletions .changeset/tasty-shrimps-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"dom-accessibility-api": minor
---

Support ES5 environments
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ tests/cypress/videos
# jest results
/junit.xml
/coverage
/yarn-error.log
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ import { computeAccessibleName } from "dom-accessibility-api";
I'm not an editor of any of the referenced specs (nor very experience with using them) so if you got any insights, something catches
your eye please open an issue.

## Supported environments

```bash
ie 11
edge >= 14
firefox >= 52
chrome >= 49
safari >= 10
node 10.0
```

or check the published `.browserslistrc`

## progress

Using https://github.com/web-platform-tests/wpt. Be sure to init submodules when
Expand Down
3 changes: 3 additions & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ steps:
- script: yarn lint
displayName: "Lint code"

- script: yarn test:types
displayName: "Test types"

- script: |
yarn build
displayName: "Build"
Expand Down
14 changes: 12 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,25 @@
"type": "git",
"url": "https://github.com/eps1lon/dom-accessibility-api.git"
},
"dependencies": {
"core-js-pure": "^3.6.4"
},
"files": [
".browserslistrc",
"dist/"
],
"scripts": {
"build": "tsc -p tsconfig.json",
"build": "yarn build:clean && yarn build:source && yarn build:types",
"build:clean": "rimraf dist",
"build:source": "babel sources --extensions \".ts\" --ignore \"**/__tests__/**/*\" --out-dir dist/ --source-maps",
"build:types": "tsc -p tsconfig.json --emitDeclarationOnly",
"format": "prettier \"**/*.{json,js,md,ts,yml}\" --write --ignore-path .prettierignore",
"lint": "eslint --report-unused-disable-directives \"sources/**/*.ts\"",
"release:prepare": "yarn changeset version",
"release": "yarn changeset publish",
"test": "jest",
"test:ci": "jest --ci --config jest.ci.config.js --runInBand",
"test:types": "tsc -p tsconfig.json --noEmit",
"test:wpt:jsdom": "mocha tests/wpt-jsdom/run-wpts.js",
"test:wpt:browser": "concurrently --success first --kill-others \"yarn test:wpt:browser:run\" \"yarn test:wpt:browser:server\"",
"test:wpt:browser:run": "cypress run --project tests",
Expand All @@ -29,14 +37,15 @@
"wpt:update": "git submodule update --recursive --remote && cd tests/wpt && python wpt.py manifest --path ../wpt-jsdom/wpt-manifest.json"
},
"devDependencies": {
"@babel/cli": "^7.8.4",
"@babel/core": "^7.8.4",
"@babel/preset-env": "^7.8.4",
"@babel/preset-typescript": "^7.8.3",
"@changesets/cli": "^2.5.2",
"@testing-library/dom": "^6.15.0",
"@types/jest": "^25.1.4",
"@typescript-eslint/parser": "^2.22.0",
"@typescript-eslint/eslint-plugin": "^2.22.0",
"@typescript-eslint/parser": "^2.22.0",
"concurrently": "^5.1.0",
"cypress": "^4.1.0",
"eslint": "^6.8.0",
Expand All @@ -53,6 +62,7 @@
"q": "^1.5.1",
"request": "^2.88",
"request-promise-native": "^1.0.8",
"rimraf": "^3.0.2",
"serve": "^11.3.0",
"typescript": "^3.8.3"
},
Expand Down
11 changes: 6 additions & 5 deletions sources/accessible-name.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* implements https://w3c.github.io/accname/
*/

import ArrayFrom from "core-js-pure/features/array/from";
import Set from "core-js-pure/stable/set";
import getRole from "./getRole";
import {
isElement,
Expand Down Expand Up @@ -135,7 +136,7 @@ function idRefs(node: Node, attributeName: string): Element[] {
* @param node
*/
function queryChildNodes(node: Node): Node[] {
return Array.from(node.childNodes).concat(idRefs(node, "aria-owns"));
return ArrayFrom(node.childNodes).concat(idRefs(node, "aria-owns"));
}

/**
Expand Down Expand Up @@ -192,7 +193,7 @@ function querySelectorAllSubtree(
const elements = [];

for (const root of [element, ...idRefs(element, "aria-owns")]) {
elements.push(...Array.from(root.querySelectorAll(selectors)));
elements.push(...ArrayFrom(root.querySelectorAll(selectors)));
}

return elements;
Expand Down Expand Up @@ -381,7 +382,7 @@ export function computeAccessibleName(
}

consultedNodes.add(input);
return Array.from(labels)
return ArrayFrom(labels)
.map(element => {
return computeTextAlternative(element, {
isEmbeddedInLabel: true,
Expand Down Expand Up @@ -477,7 +478,7 @@ export function computeAccessibleName(
// defined per test `name_heading_combobox`
return isHTMLInputElement(current) ? current.value : "";
}
return Array.from(selectedOptions)
return ArrayFrom(selectedOptions)
.map(selectedOption => {
return computeTextAlternative(selectedOption, {
isEmbeddedInLabel: context.isEmbeddedInLabel,
Expand Down
99 changes: 99 additions & 0 deletions sources/core-js-pure.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// copied from https://github.com/microsoft/TypeScript/blob/eaeee9cc31bdc3a16f982a2e7b784573c977fdfa/lib/
// but with `unknown` instead of `any`
interface IteratorYieldResult<TYield> {
done?: false;
value: TYield;
}

interface IteratorReturnResult<TReturn> {
done: true;
value: TReturn;
}

type IteratorResult<T, TReturn = unknown> =
| IteratorYieldResult<T>
| IteratorReturnResult<TReturn>;

interface Iterator<T, TReturn = unknown, TNext = undefined> {
// NOTE: 'next' is defined using a tuple to ensure we report the correct assignability errors in all places.
next(...args: [] | [TNext]): IteratorResult<T, TReturn>;
return?(value?: TReturn): IteratorResult<T, TReturn>;
throw?(e?: unknown): IteratorResult<T, TReturn>;
}

interface Iterable<T> {
[Symbol.iterator](): Iterator<T>;
}

interface IterableIterator<T> extends Iterator<T> {
[Symbol.iterator](): IterableIterator<T>;
}
interface SymbolConstructor {
/**
* A method that returns the default iterator for an object. Called by the semantics of the
* for-of statement.
*/
readonly iterator: symbol;
}

declare const Symbol: SymbolConstructor;

declare module "core-js-pure/stable/set" {
interface Set<T> {
add(value: T): this;
clear(): void;
delete(value: T): boolean;
forEach(
callbackfn: (value: T, value2: T, set: Set<T>) => void,
thisArg?: unknown
): void;
has(value: T): boolean;
readonly size: number;
}

interface Set<T> {
/** Iterates over values in the set. */
[Symbol.iterator](): IterableIterator<T>;
/**
* Returns an iterable of [v,v] pairs for every value `v` in the set.
*/
entries(): IterableIterator<[T, T]>;
/**
* Despite its name, returns an iterable of the values in the set,
*/
keys(): IterableIterator<T>;

/**
* Returns an iterable of values in the set.
*/
values(): IterableIterator<T>;
}

interface SetConstructor {
new <T = unknown>(values?: readonly T[] | null): Set<T>;
readonly prototype: Set<unknown>;
}

const Set: SetConstructor;
export default Set;
}

declare module "core-js-pure/features/array/from" {
/**
* Creates an array from an iterable object.
* @param iterable An iterable object to convert to an array.
*/
export default function from<T>(iterable: Iterable<T> | ArrayLike<T>): T[];

/**
* Creates an array from an iterable object.
* @param iterable An iterable object to convert to an array.
* @param mapfn A mapping function to call on every element of the array.
* @param thisArg Value of 'this' used to invoke the mapfn.
*/
export default function from<T, U>(
iterable: Iterable<T> | ArrayLike<T>,
mapfn: (v: T, k: number) => U,
thisArg?: unknown
): U[];
}
14 changes: 8 additions & 6 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"compilerOptions": {
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es6" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */,
"target": "es5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
"lib": [
"dom",
"es6"
"ES5",
"DOM"
] /* Specify library files to be included in the compilation. */,
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
Expand All @@ -23,7 +23,8 @@
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
/* since we transpile with babel */
"isolatedModules": true /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */,

/* Strict Type-Checking Options */
"strict": true /* Enable all strict type-checking options. */,
Expand All @@ -47,7 +48,8 @@
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
/* make sure `@types/node` is ignored */
"types": [] /* Type declaration files to be included in compilation. */,
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
Expand All @@ -62,6 +64,6 @@
/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
"skipLibCheck": true
"skipLibCheck": false
}
}

0 comments on commit e80a1fb

Please sign in to comment.