Skip to content

Commit

Permalink
Refactor transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyGu committed Feb 7, 2017
1 parent c3f30ee commit 409cb77
Showing 1 changed file with 37 additions and 41 deletions.
78 changes: 37 additions & 41 deletions lib/transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ class Transformer {
impl: content.impl
}));

const interfaces = this._interfaces = Object.create(null);
const dictionaries = this._dictionaries = Object.create(null);
const typedefs = this._typedefs = Object.create(null);
const interfaces = this._interfaces = new Map();
const dictionaries = this._dictionaries = new Map();
const typedefs = this._typedefs = new Map();
const customTypes = this._customTypes = new Set();

// first we're gathering all full interfaces and ignore partial ones
Expand All @@ -96,7 +96,7 @@ class Transformer {
implSuffix: this.options.implSuffix,
customTypes
});
interfaces[obj.name] = obj;
interfaces.set(obj.name, obj);
break;
case "implements":
break; // handled later
Expand All @@ -106,13 +106,13 @@ class Transformer {
}

obj = new Dictionary(instruction, { customTypes });
dictionaries[obj.name] = obj;
customTypes.add(instruction.name);
dictionaries.set(obj.name, obj);
customTypes.add(obj.name);
break;
case "typedef":
obj = new Typedef(instruction, { customTypes });
typedefs[obj.name] = obj;
customTypes.add(instruction.name);
typedefs.set(obj.name, obj);
customTypes.add(obj.name);
break;
default:
if (!this.options.suppressErrors) {
Expand All @@ -125,6 +125,7 @@ class Transformer {
// second we add all partial members and handle implements
for (const file of parsed) {
for (const instruction of file.idl) {
let obj;
let oldMembers;
let extAttrs;
switch (instruction.type) {
Expand All @@ -133,31 +134,34 @@ class Transformer {
break;
}

if (this.options.suppressErrors && !interfaces[instruction.name]) {
obj = interfaces.get(instruction.name);
if (this.options.suppressErrors && !obj) {
break;
}
oldMembers = interfaces[instruction.name].idl.members;
oldMembers = obj.idl.members;
oldMembers.push.apply(oldMembers, instruction.members);
extAttrs = interfaces[instruction.name].idl.extAttrs;
extAttrs = obj.idl.extAttrs;
extAttrs.push.apply(extAttrs, instruction.extAttrs);
break;
case "dictionary":
if (!instruction.partial) {
break;
}
if (this.options.suppressErrors && !dictionaries[instruction.name]) {
obj = dictionaries.get(instruction.name);
if (this.options.suppressErrors && !obj) {
break;
}
oldMembers = dictionaries[instruction.name].idl.members;
oldMembers = obj.idl.members;
oldMembers.push.apply(oldMembers, instruction.members);
extAttrs = dictionaries[instruction.name].idl.extAttrs;
extAttrs = obj.idl.extAttrs;
extAttrs.push.apply(extAttrs, instruction.extAttrs);
break;
case "implements":
if (this.options.suppressErrors && !interfaces[instruction.target]) {
obj = interfaces.get(instruction.target);
if (this.options.suppressErrors && !obj) {
break;
}
interfaces[instruction.target].implements(instruction.implements);
obj.implements(instruction.implements);
break;
}
}
Expand All @@ -169,20 +173,23 @@ class Transformer {
yield fs.writeFile(this.options.utilPath, utilsText);

let interfaceStub = yield fs.readFile(__dirname + "/output/interfaceStub.js");
let keys = Object.keys(this._interfaces).concat(Object.keys(this._dictionaries)).concat(Object.keys(this._typedefs));
for (let key of keys) {
if (this._interfaces[key]) {
yield fs.writeFile(path.join(outputDir, this._interfaces[key].name + ".js"), interfaceStub);
} else if (this._dictionaries[key]) {
yield fs.writeFile(path.join(outputDir, this._dictionaries[key].name + ".js"), "");
} else {
yield fs.writeFile(path.join(outputDir, this._typedefs[key].name + ".js"), "");
}
for (const key of this._interfaces.keys()) {
yield fs.writeFile(path.join(outputDir, key + ".js"), interfaceStub);
}
// TODO: use spread syntax to merge the two following blocks
for (const key of this._dictionaries.keys()) {
yield fs.writeFile(path.join(outputDir, key + ".js"), "");
}
for (const key of this._typedefs.keys()) {
yield fs.writeFile(path.join(outputDir, key + ".js"), "");
}

keys = Object.keys(this._interfaces);
for (let i = 0; i < keys.length; ++i) {
const obj = this._interfaces[keys[i]];
let relativeUtils = path.relative(outputDir, this.options.utilPath).replace(/\\/g, '/');
if (relativeUtils[0] !== ".") {
relativeUtils = "./" + relativeUtils;
}

for (const obj of this._interfaces.values()) {
let source = obj.toString();

const implDir = path.relative(outputDir, obj.opts.implDir).replace(/\\/g, "/"); // fix windows file paths
Expand All @@ -191,11 +198,6 @@ class Transformer {
implFile = "./" + implFile;
}

let relativeUtils = path.relative(outputDir, this.options.utilPath).replace(/\\/g, '/');
if (relativeUtils[0] !== ".") {
relativeUtils = "./" + relativeUtils;
}

source = `"use strict";
const conversions = require("webidl-conversions");
Expand All @@ -205,16 +207,10 @@ const Impl = require("${implFile}.js");\n`;
yield fs.writeFile(path.join(outputDir, obj.name + ".js"), source);
}

keys = Object.keys(this._dictionaries).concat(Object.keys(this._typedefs));
for (let i = 0; i < keys.length; ++i) {
const obj = this._dictionaries[keys[i]] || this._typedefs[keys[i]];
// TODO: use spread syntax to simplify
for (const obj of Array.from(this._dictionaries.values()).concat(Array.from(this._typedefs.values()))) {
let source = obj.toString();

let relativeUtils = path.relative(outputDir, this.options.utilPath).replace(/\\/g, '/');
if (relativeUtils[0] !== ".") {
relativeUtils = "./" + relativeUtils;
}

source = `"use strict";
const conversions = require("webidl-conversions");
Expand Down

0 comments on commit 409cb77

Please sign in to comment.