Skip to content

Small refactorings #599

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 3 commits into from
Jan 24, 2019
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
29 changes: 10 additions & 19 deletions src/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ function isEventHandler(p: Browser.Property) {
return typeof p["event-handler"] === "string";
}

export function emitWebIDl(webidl: Browser.WebIdl, flavor: Flavor) {
export function emitWebIdl(webidl: Browser.WebIdl, flavor: Flavor) {
// Global print target
const printer = createTextWriter("\n");

Expand Down Expand Up @@ -573,21 +573,12 @@ export function emitWebIDl(webidl: Browser.WebIdl, flavor: Flavor) {
!!iNameToEhList[i.name].find(e => e.name === p.name));
}

function emitProperty(prefix: string, i: Browser.Interface, emitScope: EmitScope, p: Browser.Property, conflictedMembers: Set<string>) {
function printLine(content: string) {
if (conflictedMembers.has(p.name)) {
printer.printLineToStack(content);
}
else {
printer.printLine(content);
}
}

emitComments(p, printLine);
function emitProperty(prefix: string, i: Browser.Interface, emitScope: EmitScope, p: Browser.Property) {
emitComments(p, printer.printLine);

// Treat window.name specially because of https://github.com/Microsoft/TypeScript/issues/9850
if (p.name === "name" && i.name === "Window" && emitScope === EmitScope.All) {
printLine("declare const name: never;");
printer.printLine("declare const name: never;");
}
else {
let pType: string;
Expand All @@ -610,7 +601,7 @@ export function emitWebIDl(webidl: Browser.WebIdl, flavor: Flavor) {
const requiredModifier = p.required === undefined || p.required === 1 ? "" : "?";
pType = p.nullable ? makeNullable(pType) : pType;
const readOnlyModifier = p["read-only"] === 1 && prefix === "" ? "readonly " : "";
printLine(`${prefix}${readOnlyModifier}${p.name}${requiredModifier}: ${pType};`);
printer.printLine(`${prefix}${readOnlyModifier}${p.name}${requiredModifier}: ${pType};`);
}
}

Expand All @@ -623,17 +614,17 @@ export function emitWebIDl(webidl: Browser.WebIdl, flavor: Flavor) {
}
}

function emitProperties(prefix: string, emitScope: EmitScope, i: Browser.Interface, conflictedMembers: Set<string>) {
function emitProperties(prefix: string, emitScope: EmitScope, i: Browser.Interface) {
if (i.properties) {
mapToArray(i.properties.property)
.filter(m => matchScope(emitScope, m))
.filter(p => !isCovariantEventHandler(i, p))
.sort(compareName)
.forEach(p => emitProperty(prefix, i, emitScope, p, conflictedMembers));
.forEach(p => emitProperty(prefix, i, emitScope, p));
}
}

function emitMethod(prefix: string, _i: Browser.Interface, m: Browser.Method, conflictedMembers: Set<string>) {
function emitMethod(prefix: string, m: Browser.Method, conflictedMembers: Set<string>) {
function printLine(content: string) {
if (m.name && conflictedMembers.has(m.name)) {
printer.printLineToStack(content);
Expand Down Expand Up @@ -682,7 +673,7 @@ export function emitWebIDl(webidl: Browser.WebIdl, flavor: Flavor) {
mapToArray(i.methods.method)
.filter(m => matchScope(emitScope, m) && !(prefix !== "" && (m.name === "addEventListener" || m.name === "removeEventListener")))
.sort(compareName)
.forEach(m => emitMethod(prefix, i, m, conflictedMembers));
.forEach(m => emitMethod(prefix, m, conflictedMembers));
}

// The window interface inherited some methods from "Object",
Expand All @@ -708,7 +699,7 @@ export function emitWebIDl(webidl: Browser.WebIdl, flavor: Flavor) {
/// Emit the properties and methods of a given interface
function emitMembers(prefix: string, emitScope: EmitScope, i: Browser.Interface) {
const conflictedMembers = extendConflictsBaseTypes[i.name] ? extendConflictsBaseTypes[i.name].memberNames : new Set();
emitProperties(prefix, emitScope, i, conflictedMembers);
emitProperties(prefix, emitScope, i);
const methodPrefix = prefix.startsWith("declare var") ? "declare function " : "";
emitMethods(methodPrefix, emitScope, i, conflictedMembers);
if (emitScope === EmitScope.InstanceOnly) {
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@ import * as Browser from "./types";
import * as fs from "fs";
import * as path from "path";
import { merge, resolveExposure, markAsDeprecated, mapToArray } from "./helpers";
import { Flavor, emitWebIDl } from "./emitter";
import { Flavor, emitWebIdl } from "./emitter";
import { convert } from "./widlprocess";
import { getExposedTypes } from "./expose";

function emitDomWorker(webidl: Browser.WebIdl, tsWorkerOutput: string, forceKnownWorkerTypes: Set<string>) {
const worker = getExposedTypes(webidl, "Worker", forceKnownWorkerTypes);
const result = emitWebIDl(worker, Flavor.Worker);
const result = emitWebIdl(worker, Flavor.Worker);
fs.writeFileSync(tsWorkerOutput, result);
return;
}

function emitDomWeb(webidl: Browser.WebIdl, tsWebOutput: string, forceKnownWindowTypes: Set<string>) {
const browser = getExposedTypes(webidl, "Window", forceKnownWindowTypes);

const result = emitWebIDl(browser, Flavor.Web);
const result = emitWebIdl(browser, Flavor.Web);
fs.writeFileSync(tsWebOutput, result);
return;
}

function emitES6DomIterators(webidl: Browser.WebIdl, tsWebIteratorsOutput: string) {
fs.writeFileSync(tsWebIteratorsOutput, emitWebIDl(webidl, Flavor.ES6Iterators));
fs.writeFileSync(tsWebIteratorsOutput, emitWebIdl(webidl, Flavor.ES6Iterators));
}

function emitDom() {
Expand Down