diff --git a/README.md b/README.md index f45ee123..db9b7eab 100644 --- a/README.md +++ b/README.md @@ -112,7 +112,7 @@ var result = WebIDL2.write(tree, { * Called only once for each types, e.g. `Document`, `Promise`, or `sequence`. * @param type The `wrap()`ed result of references and syntatic bracket strings. */ - type: type => type, + type: (type, { data }) => type, /** * Receives the return value of `reference()`. String if it's absent. */ diff --git a/lib/productions/argument.js b/lib/productions/argument.js index dd6f00af..e12edee2 100644 --- a/lib/productions/argument.js +++ b/lib/productions/argument.js @@ -112,7 +112,7 @@ export class Argument extends Base { return w.ts.wrap([ this.extAttrs.write(w), w.token(this.tokens.optional), - w.ts.type(this.idlType.write(w)), + w.ts.type(this.idlType.write(w), { data: this.idlType }), w.token(this.tokens.variadic), w.name_token(this.tokens.name, { data: this }), this.default ? this.default.write(w) : "", diff --git a/lib/productions/attribute.js b/lib/productions/attribute.js index 0dab2516..b20efac6 100644 --- a/lib/productions/attribute.js +++ b/lib/productions/attribute.js @@ -114,7 +114,7 @@ export class Attribute extends Base { w.token(this.tokens.special), w.token(this.tokens.readonly), w.token(this.tokens.base), - w.ts.type(this.idlType.write(w)), + w.ts.type(this.idlType.write(w), { data: this.idlType }), w.name_token(this.tokens.name, { data: this, parent }), w.token(this.tokens.termination), ]), diff --git a/lib/productions/callback.js b/lib/productions/callback.js index 56dba106..24a63958 100644 --- a/lib/productions/callback.js +++ b/lib/productions/callback.js @@ -55,7 +55,7 @@ export class CallbackFunction extends Base { w.token(this.tokens.base), w.name_token(this.tokens.name, { data: this }), w.token(this.tokens.assign), - w.ts.type(this.idlType.write(w)), + w.ts.type(this.idlType.write(w), { data: this.idlType }), w.token(this.tokens.open), ...this.arguments.map((arg) => arg.write(w)), w.token(this.tokens.close), diff --git a/lib/productions/constant.js b/lib/productions/constant.js index 9fd90146..4d5b6763 100644 --- a/lib/productions/constant.js +++ b/lib/productions/constant.js @@ -62,7 +62,7 @@ export class Constant extends Base { w.ts.wrap([ this.extAttrs.write(w), w.token(this.tokens.base), - w.ts.type(this.idlType.write(w)), + w.ts.type(this.idlType.write(w), { data: this.idlType }), w.name_token(this.tokens.name, { data: this, parent }), w.token(this.tokens.assign), w.token(this.tokens.value), diff --git a/lib/productions/field.js b/lib/productions/field.js index 1cd44ed7..40c87612 100644 --- a/lib/productions/field.js +++ b/lib/productions/field.js @@ -53,7 +53,7 @@ export class Field extends Base { w.ts.wrap([ this.extAttrs.write(w), w.token(this.tokens.required), - w.ts.type(this.idlType.write(w)), + w.ts.type(this.idlType.write(w), { data: this.idlType }), w.name_token(this.tokens.name, { data: this, parent }), this.default ? this.default.write(w) : "", w.token(this.tokens.termination), diff --git a/lib/productions/operation.js b/lib/productions/operation.js index f4c561be..fd6fa9cd 100644 --- a/lib/productions/operation.js +++ b/lib/productions/operation.js @@ -80,7 +80,7 @@ export class Operation extends Base { const { parent } = this; const body = this.idlType ? [ - w.ts.type(this.idlType.write(w)), + w.ts.type(this.idlType.write(w), { data: this.idlType }), w.name_token(this.tokens.name, { data: this, parent }), w.token(this.tokens.open), w.ts.wrap(this.arguments.map((arg) => arg.write(w))), diff --git a/lib/productions/typedef.js b/lib/productions/typedef.js index 031898f7..d9e5b579 100644 --- a/lib/productions/typedef.js +++ b/lib/productions/typedef.js @@ -47,7 +47,7 @@ export class Typedef extends Base { w.ts.wrap([ this.extAttrs.write(w), w.token(this.tokens.base), - w.ts.type(this.idlType.write(w)), + w.ts.type(this.idlType.write(w), { data: this.idlType }), w.name_token(this.tokens.name, { data: this }), w.token(this.tokens.termination), ]), diff --git a/test/custom-production.js b/test/custom-production.js index d5191ff0..78699f64 100644 --- a/test/custom-production.js +++ b/test/custom-production.js @@ -41,7 +41,7 @@ class CustomAttribute extends Base { w.ts.wrap([ this.extAttrs.write(w), w.token(this.tokens.base), - w.ts.type(this.idlType.write(w)), + w.ts.type(this.idlType.write(w), { data: this.idlType }), w.name_token(this.tokens.name, { data: this, parent }), w.token(this.tokens.termination), ]), diff --git a/test/writer.js b/test/writer.js index 52d04175..69222d99 100644 --- a/test/writer.js +++ b/test/writer.js @@ -252,4 +252,31 @@ describe("Writer template functions", () => { "interface[interface Misaki {interface:const[ const short MICHELLE = 1;] };]", ); }); + + it("gives idlType object", () => { + const types = []; + rewrite( + ` + interface Mizuki { + attribute long attr; + short? op(long long arg); + }; + dictionary Ena { + sequence mem; + }; + `, + { + type(type, { data }) { + types.push(data); + }, + }, + ); + + expect(types[0].idlType).toBe("long"); + expect(types[1].idlType).toBe("short"); + expect(types[1].nullable).toBe(true); + expect(types[2].idlType).toBe("long long"); + expect(types[3].generic).toBe("sequence"); + expect(types[3].idlType[0].idlType).toBe("unsigned short"); + }); });