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

refactor(interface): Use default export for implementation #141

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,33 @@ interface SomeInterface {
combined with the JavaScript implementation class file

```js
exports.implementation = class SomeInterfaceImpl {
module.exports = class SomeInterfaceImpl {
add(x, y) {
return x + y;
}
};
```

> Note: It's also possible to use ES2015 module default export syntax:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not true; jsdom only operates on CommonJS modules.

Copy link
Contributor Author

@ExE-Boss ExE-Boss Oct 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, you do need to transpile ES2015 modules to be able to use them in Node.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. That is not a supported scenario, so please remove it.

>
> ```js
> export default class SomeInterfaceImpl {
> add(x, y) {
> return x + y;
> }
> };
> ```

will generate a JavaScript wrapper class file roughly like this:

```js
const conversions = require("webidl-conversions");
const impl = require("./utils.js").implSymbol;
const utils = require("./utils.js");
const impl = utils.implSymbol;

const Impl = require("./SomeInterface-impl.js").implementation;
// utils.importStar is roughly equivalent to Babel's _interopRequireWildcard()
// and TypeScript's __importStar() functions.
const Impl = utils.importStar(require("./SomeInterface-impl.js")).default;

class SomeInterface {
constructor() {
Expand Down
4 changes: 2 additions & 2 deletions lib/constructs/attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ class Attribute {

if (this.static) {
brandCheck = "";
getterBody = `return Impl.implementation["${this.idl.name}"];`;
setterBody = `Impl.implementation["${this.idl.name}"] = V;`;
getterBody = `return Impl.default["${this.idl.name}"];`;
setterBody = `Impl.default["${this.idl.name}"] = V;`;
} else if (shouldReflect) {
if (!reflector[this.idl.idlType.idlType]) {
throw new Error("Unknown reflector type: " + this.idl.idlType.idlType);
Expand Down
6 changes: 3 additions & 3 deletions lib/constructs/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ class Interface {
_mixedIntoPredicates: [],
is(obj) {
if (obj) {
if (utils.hasOwn(obj, impl) && obj[impl] instanceof Impl.implementation) {
if (utils.hasOwn(obj, impl) && obj[impl] instanceof Impl.default) {
return true;
}
for (const isMixedInto of module.exports._mixedIntoPredicates) {
Expand All @@ -541,7 +541,7 @@ class Interface {
},
isImpl(obj) {
if (obj) {
if (obj instanceof Impl.implementation) {
if (obj instanceof Impl.default) {
return true;
}

Expand Down Expand Up @@ -1162,7 +1162,7 @@ class Interface {

this._internalSetup(obj);
Object.defineProperty(obj, impl, {
value: new Impl.implementation(constructorArgs, privateData),
value: new Impl.default(constructorArgs, privateData),
configurable: true
});
`;
Expand Down
2 changes: 1 addition & 1 deletion lib/constructs/operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Operation {
`;
}

const callOn = this.static ? "Impl.implementation" : "this[impl]";
const callOn = this.static ? "Impl.default" : "this[impl]";
// In case of stringifiers, use the named implementation function rather than hardcoded "toString".
// All overloads will have the same name, so pick the first one.
const implFunc = this.idls[0].name || this.name;
Expand Down
49 changes: 48 additions & 1 deletion lib/output/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,52 @@ const namedSetNew = Symbol("named property set new");
const namedSetExisting = Symbol("named property set existing");
const namedDelete = Symbol("named property delete");

/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not interested in this addition. Please remove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which? The JSDoc or the entire utils.importStar(…) function?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The importStar function itself.

* Used to cache importStar results.
*/
const importStarCache = new WeakMap();

/**
* Mostly copied from TypeScript and Babel.
*
* @template T
* @param {T} obj
* @return {T extends { default: any }
* ? T : T extends string | number | bigint | boolean | symbol | null | undefined
* ? { default: T } : { default: T } & { [K in keyof T]: T[K] }}
*/
function importStar(obj) {
if (obj && obj.__esModule) {
return obj;
}

if (obj === null || (typeof obj !== "object" && typeof obj !== "function")) {
return { default: obj };
}

if (importStarCache.has(obj)) {
return importStarCache.get(obj);
}

const newObj = {};

for (const key in obj) {
if (hasOwn(obj, key) && key !== "default") {
Object.defineProperty(
newObj,
key,
Object.getOwnPropertyDescriptor(obj, key)
);
}
}

newObj.default = obj;

importStarCache.set(obj, newObj);

return newObj;
}

module.exports = exports = {
isObject,
hasOwn,
Expand All @@ -97,5 +143,6 @@ module.exports = exports = {
namedGet,
namedSetNew,
namedSetExisting,
namedDelete
namedDelete,
importStar
};
2 changes: 1 addition & 1 deletion lib/transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ class Transformer {
const conversions = require("webidl-conversions");
const utils = require("${relativeUtils}");
${source}
const Impl = require("${implFile}.js");
const Impl = utils.importStar(require("${implFile}.js"));
`;

source = this._prettify(source);
Expand Down
Loading