Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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: 1 addition & 1 deletion lib/constructs/attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class Attribute {
let idlConversion;
if (typeof this.idl.idlType.idlType === "string" && !this.idl.idlType.nullable &&
this.ctx.enumerations.has(this.idl.idlType.idlType)) {
requires.add(this.idl.idlType.idlType);
requires.addRelative(this.idl.idlType.idlType);
idlConversion = `
V = \`\${V}\`;
if (!${this.idl.idlType.idlType}.enumerationValues.has(V)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/constructs/dictionary.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class Dictionary {
`;

if (this.idl.inheritance) {
this.requires.add(this.idl.inheritance);
this.requires.addRelative(this.idl.inheritance);
}
this.str = `
${this.requires.generate()}
Expand Down
4 changes: 2 additions & 2 deletions lib/constructs/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,11 +503,11 @@ class Interface {
this.requires.addRaw("ctorRegistry", "utils.ctorRegistrySymbol");

if (this.idl.inheritance !== null) {
this.requires.add(this.idl.inheritance);
this.requires.addRelative(this.idl.inheritance);
}

for (const iface of this.consequentialInterfaces()) {
this.requires.add(iface);
this.requires.addRelative(iface);
}

this.str = `
Expand Down
2 changes: 1 addition & 1 deletion lib/parameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ module.exports.generateOverloadConversions = function (ctx, typeOfOp, name, pare
// Avoid requiring the interface itself
if (iface !== parent.name) {
fn = `is${iface}`;
requires.add(iface, "is");
requires.addRelative(iface, "is");
} else {
fn = "module.exports.is";
}
Expand Down
4 changes: 2 additions & 2 deletions lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ function generateTypeConversion(ctx, name, idlType, argAttrs = [], parentName, e
// Avoid requiring the interface itself
if (idlType.idlType !== parentName) {
fn = `convert${idlType.idlType}`;
requires.add(idlType.idlType, "convert");
requires.addRelative(idlType.idlType, "convert");
} else {
fn = `module.exports.convert`;
}
Expand Down Expand Up @@ -174,7 +174,7 @@ function generateTypeConversion(ctx, name, idlType, argAttrs = [], parentName, e
// Avoid requiring the interface itself
if (iface !== parentName) {
fn = `is${iface}`;
requires.add(iface, "is");
requires.addRelative(iface, "is");
} else {
fn = "module.exports.is";
}
Expand Down
25 changes: 22 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"use strict";
const path = require("path");

function getDefault(dflt) {
switch (dflt.type) {
Expand Down Expand Up @@ -65,17 +66,35 @@ function stringifyPropertyName(prop) {
return typeof prop === "symbol" ? symbolName(prop) : JSON.stringify(propertyName(prop));
}

function toKey(type, func = "") {
return String(func + type).replace(/[./-]+/g, " ").trim().replace(/ /g, "_");
}

class RequiresMap extends Map {
constructor(ctx) {
super();
this.ctx = ctx;
}

add(type, func = "") {
const key = (func + type).replace(/[./-]+/g, " ").trim().replace(/ /g, "_");
const key = toKey(type, func);

const importPath = (type.includes("/") || type.startsWith(".")) && !path.extname(type) ? type + ".js" : type;
Comment thread
ExE-Boss marked this conversation as resolved.
Outdated
let req = `require(${JSON.stringify(importPath)})`;

if (func) {
req += `.${func}`;
}

this.addRaw(key, req);
return key;
}

addRelative(type, func = "") {
const key = toKey(type, func);

const path = type.startsWith(".") ? type : `./${type}`;
let req = `require("${path}.js")`;
const importPath = type.startsWith(".") ? type : `./${type}`;
let req = `require("${importPath}.js")`;

if (func) {
req += `.${func}`;
Expand Down
Loading