Skip to content
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

fix: Allow processors to add package imports #162

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
27 changes: 25 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"use strict";
const { extname } = require("path");

function getDefault(dflt) {
switch (dflt.type) {
Expand Down Expand Up @@ -65,14 +66,36 @@ 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, "_");
}

const PACKAGE_NAME_REGEX = /^(?:@([^/]+?)[/])?([^/]+?)$/u;

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

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

// If `name` is a package name or has a file extension, then use it as-is,
// otherwise append the `.js` file extension:
const importPath = PACKAGE_NAME_REGEX.test(name) || extname(name) ? name : `${name}.js`;
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")`;
Expand Down