Skip to content

Commit 2ab0e5d

Browse files
authored
refactor(writer): split to each module (#568)
* refactor(writer): split to each module * split operation writing * split attribute writing * split constructor writing * split iterable writing * split const writing * split field writing * split container writing * split typedef writing * split includes writing * split callback writing * split enum writing * remove table * no null propagation yet * no need to pass argument
1 parent 9a1585e commit 2ab0e5d

16 files changed

+348
-322
lines changed

lib/productions/argument.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,19 @@ export class Argument extends Base {
106106
}
107107
}
108108
}
109+
110+
/** @param {import("../writer.js").Writer} w */
111+
write(w) {
112+
return w.ts.wrap([
113+
this.extAttrs.write(w),
114+
w.token(this.tokens.optional),
115+
w.ts.type(this.idlType.write(w)),
116+
w.token(this.tokens.variadic),
117+
w.name_token(this.tokens.name, { data: this }),
118+
this.default ? this.default.write(w) : "",
119+
w.token(this.tokens.separator),
120+
]);
121+
}
109122
}
110123

111124
/**

lib/productions/attribute.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,21 @@ export class Attribute extends Base {
9595
}
9696
}
9797
}
98+
99+
/** @param {import("../writer.js").Writer} w */
100+
write(w) {
101+
const { parent } = this;
102+
return w.ts.definition(
103+
w.ts.wrap([
104+
this.extAttrs.write(w),
105+
w.token(this.tokens.special),
106+
w.token(this.tokens.readonly),
107+
w.token(this.tokens.base),
108+
w.ts.type(this.idlType.write(w)),
109+
w.name_token(this.tokens.name, { data: this, parent }),
110+
w.token(this.tokens.termination),
111+
]),
112+
{ data: this, parent }
113+
);
114+
}
98115
}

lib/productions/callback.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,22 @@ export class CallbackFunction extends Base {
4646
yield* this.extAttrs.validate(defs);
4747
yield* this.idlType.validate(defs);
4848
}
49+
50+
/** @param {import("../writer.js").Writer} w */
51+
write(w) {
52+
return w.ts.definition(
53+
w.ts.wrap([
54+
this.extAttrs.write(w),
55+
w.token(this.tokens.base),
56+
w.name_token(this.tokens.name, { data: this }),
57+
w.token(this.tokens.assign),
58+
w.ts.type(this.idlType.write(w)),
59+
w.token(this.tokens.open),
60+
...this.arguments.map((arg) => arg.write(w)),
61+
w.token(this.tokens.close),
62+
w.token(this.tokens.termination),
63+
]),
64+
{ data: this }
65+
);
66+
}
4967
}

lib/productions/constant.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,21 @@ export class Constant extends Base {
5353
get value() {
5454
return const_data(this.tokens.value);
5555
}
56+
57+
/** @param {import("../writer.js").Writer} w */
58+
write(w) {
59+
const { parent } = this;
60+
return w.ts.definition(
61+
w.ts.wrap([
62+
this.extAttrs.write(w),
63+
w.token(this.tokens.base),
64+
w.ts.type(this.idlType.write(w)),
65+
w.name_token(this.tokens.name, { data: this, parent }),
66+
w.token(this.tokens.assign),
67+
w.token(this.tokens.value),
68+
w.token(this.tokens.termination),
69+
]),
70+
{ data: this, parent }
71+
);
72+
}
5673
}

lib/productions/constructor.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,20 @@ export class Constructor extends Base {
3838
yield* argument.validate(defs);
3939
}
4040
}
41+
42+
/** @param {import("../writer.js").Writer} w */
43+
write(w) {
44+
const { parent } = this;
45+
return w.ts.definition(
46+
w.ts.wrap([
47+
this.extAttrs.write(w),
48+
w.token(this.tokens.base, w.ts.nameless, { data: this, parent }),
49+
w.token(this.tokens.open),
50+
w.ts.wrap(this.arguments.map((arg) => arg.write(w))),
51+
w.token(this.tokens.close),
52+
w.token(this.tokens.termination),
53+
]),
54+
{ data: this, parent }
55+
);
56+
}
4157
}

lib/productions/container.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,37 @@ export class Container extends Base {
7979
}
8080
}
8181
}
82+
83+
/** @param {import("../writer.js").Writer} w */
84+
write(w) {
85+
const inheritance = () => {
86+
if (!this.tokens.inheritance) {
87+
return "";
88+
}
89+
return w.ts.wrap([
90+
w.token(this.tokens.colon),
91+
w.ts.trivia(this.tokens.inheritance.trivia),
92+
w.ts.inheritance(
93+
w.reference(this.tokens.inheritance.value, { context: this })
94+
),
95+
]);
96+
};
97+
98+
return w.ts.definition(
99+
w.ts.wrap([
100+
this.extAttrs.write(w),
101+
w.token(this.tokens.callback),
102+
w.token(this.tokens.partial),
103+
w.token(this.tokens.base),
104+
w.token(this.tokens.mixin),
105+
w.name_token(this.tokens.name, { data: this }),
106+
inheritance(),
107+
w.token(this.tokens.open),
108+
w.ts.wrap(this.members.map((m) => m.write(w))),
109+
w.token(this.tokens.close),
110+
w.token(this.tokens.termination),
111+
]),
112+
{ data: this }
113+
);
114+
}
82115
}

lib/productions/default.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,12 @@ export class Default extends Base {
4848
get negative() {
4949
return const_data(this.expression[0]).negative;
5050
}
51+
52+
/** @param {import("../writer.js").Writer)} w */
53+
write(w) {
54+
return w.ts.wrap([
55+
w.token(this.tokens.assign),
56+
...this.expression.map((t) => w.token(t)),
57+
]);
58+
}
5159
}

lib/productions/enum.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ class EnumValue extends Token {
1919
get value() {
2020
return super.value.slice(1, -1);
2121
}
22+
23+
/** @param {import("../writer.js").Writer} w */
24+
write(w) {
25+
const { parent } = this;
26+
return w.ts.wrap([
27+
w.ts.trivia(this.tokens.value.trivia),
28+
w.ts.definition(
29+
w.ts.wrap(['"', w.ts.name(this.value, { data: this, parent }), '"']),
30+
{ data: this, parent }
31+
),
32+
w.token(this.tokens.separator),
33+
]);
34+
}
2235
}
2336

2437
export class Enum extends Base {
@@ -61,4 +74,20 @@ export class Enum extends Base {
6174
get name() {
6275
return unescape(this.tokens.name.value);
6376
}
77+
78+
/** @param {import("../writer.js").Writer} w */
79+
write(w) {
80+
return w.ts.definition(
81+
w.ts.wrap([
82+
this.extAttrs.write(w),
83+
w.token(this.tokens.base),
84+
w.name_token(this.tokens.name, { data: this }),
85+
w.token(this.tokens.open),
86+
w.ts.wrap(this.values.map((v) => v.write(w))),
87+
w.token(this.tokens.close),
88+
w.token(this.tokens.termination),
89+
]),
90+
{ data: this }
91+
);
92+
}
6493
}

lib/productions/extended-attributes.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,32 @@ class ExtendedAttributeParameters extends Base {
8989
}
9090
return null;
9191
}
92+
93+
/** @param {import("../writer.js").Writer)} w */
94+
write(w) {
95+
function extended_attribute_listitem(item) {
96+
return w.ts.wrap([
97+
w.token(item.tokens.value),
98+
w.token(item.tokens.separator),
99+
]);
100+
}
101+
const { rhsType } = this;
102+
return w.ts.wrap([
103+
w.token(this.tokens.assign),
104+
w.reference_token(this.tokens.secondaryName, this.parent),
105+
w.token(this.tokens.open),
106+
...(!this.list
107+
? []
108+
: this.list.map((p) => {
109+
return rhsType === "identifier-list"
110+
? w.identifier(p, this)
111+
: rhsType && rhsType.endsWith("-list")
112+
? extended_attribute_listitem(p)
113+
: p.write(w);
114+
})),
115+
w.token(this.tokens.close),
116+
]);
117+
}
92118
}
93119

94120
export class SimpleExtendedAttribute extends Base {
@@ -164,6 +190,20 @@ information.`;
164190
yield* arg.validate(defs);
165191
}
166192
}
193+
194+
/** @param {import("../writer.js").Writer)} w */
195+
write(w) {
196+
return w.ts.wrap([
197+
w.ts.trivia(this.tokens.name.trivia),
198+
w.ts.extendedAttribute(
199+
w.ts.wrap([
200+
w.ts.extendedAttributeReference(this.name),
201+
this.params.write(w),
202+
])
203+
),
204+
w.token(this.tokens.separator),
205+
]);
206+
}
167207
}
168208

169209
/**
@@ -215,4 +255,14 @@ export class ExtendedAttributes extends ArrayBase {
215255
yield* extAttr.validate(defs);
216256
}
217257
}
258+
259+
/** @param {import("../writer.js").Writer)} w */
260+
write(w) {
261+
if (!this.length) return "";
262+
return w.ts.wrap([
263+
w.token(this.tokens.open),
264+
...this.map((ea) => ea.write(w)),
265+
w.token(this.tokens.close),
266+
]);
267+
}
218268
}

lib/productions/field.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,20 @@ export class Field extends Base {
4545
*validate(defs) {
4646
yield* this.idlType.validate(defs);
4747
}
48+
49+
/** @param {import("../writer.js").Writer} w */
50+
write(w) {
51+
const { parent } = this;
52+
return w.ts.definition(
53+
w.ts.wrap([
54+
this.extAttrs.write(w),
55+
w.token(this.tokens.required),
56+
w.ts.type(this.idlType.write(w)),
57+
w.name_token(this.tokens.name, { data: this, parent }),
58+
this.default ? this.default.write(w) : "",
59+
w.token(this.tokens.termination),
60+
]),
61+
{ data: this, parent }
62+
);
63+
}
4864
}

0 commit comments

Comments
 (0)