Skip to content

Remove redundant helper functions #988

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ jobs:
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: '15'

- run: npm install
- run: npm run build
Expand Down
10 changes: 6 additions & 4 deletions .github/workflows/test_typescript.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ jobs:
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: '15'

- name: Run TypeScript Compiler Tests with new dom.d.ts
- name: Run TypeScript Compiler Tests with new dom.d.ts
run: |
# Get local dependencies
npm install
Expand All @@ -24,17 +26,17 @@ jobs:

cd TypeScript
npm i


# Run TypeScript's tests with the new DOM libs, but don't fail
npm test || true
gulp baseline-accept

# The git diff now is the difference between tests
# The git diff now is the difference between tests
git diff > baseline-changes.diff

- name: Danger
run: npm run danger -- ci
env:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"name": "tsjs-lib-generator",
"name": "typescript-dom-lib-generator",
"private": true,
"engines": {
"node": ">=15"
},
"scripts": {
"build": "tsc -p ./tsconfig.json && node ./lib/index.js",
"fetch-idl": "tsc -p ./tsconfig.json && node ./lib/idlfetcher.js",
Expand Down
12 changes: 6 additions & 6 deletions src/emitter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as Browser from "./types";
import { mapToArray, distinct, map, toNameMap, mapDefined, arrayToMap, flatMap, integerTypes, baseTypeConversionMap } from "./helpers";
import { mapToArray, distinct, map, toNameMap, mapDefined, arrayToMap, integerTypes, baseTypeConversionMap } from "./helpers";
import { collectLegacyNamespaceTypes } from "./legacy-namespace";

export const enum Flavor {
Expand Down Expand Up @@ -131,14 +131,14 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor, iterator: boo
getElements(webidl.mixins, "mixin"));

const allInterfacesMap = toNameMap(allInterfaces);
const allLegacyWindowAliases = flatMap(allInterfaces, i => i["legacy-window-alias"]);
const allLegacyWindowAliases = allInterfaces.flatMap(i => i["legacy-window-alias"]);
const allDictionariesMap = webidl.dictionaries ? webidl.dictionaries.dictionary : {};
const allEnumsMap = webidl.enums ? webidl.enums.enum : {};
const allCallbackFunctionsMap = webidl["callback-functions"] ? webidl["callback-functions"]!["callback-function"] : {};
const allTypeDefsMap = new Set(webidl.typedefs && webidl.typedefs.typedef.map(td => td["new-type"]));

/// Event name to event type map
const eNameToEType = arrayToMap(flatMap(allNonCallbackInterfaces, i => i.events ? i.events.event : []), e => e.name, e => eventTypeMap[e.name] || e.type);
const eNameToEType = arrayToMap(allNonCallbackInterfaces.flatMap(i => i.events ? i.events.event : []), e => e.name, e => eventTypeMap[e.name] || e.type);

/// Tag name to element name map
const tagNameToEleName = getTagNameToElementNameMap();
Expand All @@ -149,7 +149,7 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor, iterator: boo

/// Distinct event type list, used in the "createEvent" function
const distinctETypeList = distinct(
flatMap(allNonCallbackInterfaces, i => i.events ? i.events.event.map(e => e.type) : [])
allNonCallbackInterfaces.flatMap(i => i.events ? i.events.event.map(e => e.type) : [])
.concat(allNonCallbackInterfaces.filter(i => i.extends && i.extends.endsWith("Event") && i.name.endsWith("Event")).map(i => i.name))
).sort();

Expand Down Expand Up @@ -235,7 +235,7 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor, iterator: boo

const iExtends = i.extends && i.extends.replace(/<.*>$/, '');
const parentWithEventHandler = allInterfacesMap[iExtends] && getParentEventHandler(allInterfacesMap[iExtends]) || [];
const mixinsWithEventHandler = flatMap(i.implements || [], i => getParentEventHandler(allInterfacesMap[i]));
const mixinsWithEventHandler = (i.implements || []).flatMap(i => getParentEventHandler(allInterfacesMap[i]));

return distinct(parentWithEventHandler.concat(mixinsWithEventHandler));
}
Expand All @@ -246,7 +246,7 @@ export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor, iterator: boo
return (hasConst ? [i] : []).concat(getParentsWithConstant(i));
}

const mixinsWithConstant = flatMap(i.implements || [], i => getParentConstant(allInterfacesMap[i]));
const mixinsWithConstant = (i.implements || []).flatMap(i => getParentConstant(allInterfacesMap[i]));

return distinct(mixinsWithConstant);
}
Expand Down
23 changes: 0 additions & 23 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,29 +147,6 @@ export function toNameMap<T extends { name: string }>(array: T[]) {
return result;
}

export function isArray(value: any): value is ReadonlyArray<{}> {
return Array.isArray ? Array.isArray(value) : value instanceof Array;
}

export function flatMap<T, U>(array: ReadonlyArray<T> | undefined, mapfn: (x: T, i: number) => U | ReadonlyArray<U> | undefined): U[] {
let result: U[] | undefined;
if (array) {
result = [];
for (let i = 0; i < array.length; i++) {
const v = mapfn(array[i], i);
if (v) {
if (isArray(v)) {
result.push(...v);
}
else {
result.push(v);
}
}
}
}
return result || [];
}

export function concat<T>(a: T[] | undefined, b: T[] | undefined): T[] {
return !a ? b || [] : a.concat(b || []);
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es2017",
"target": "es2019",
"module": "commonjs",
"outDir": "./lib",
"strict": true,
Expand Down