Skip to content

Commit 6e41ba0

Browse files
authored
feat: support constructor as a member (#300)
* feat: support constructor as a member * more tests * regenerate
1 parent ccc9fca commit 6e41ba0

File tree

6 files changed

+171
-35
lines changed

6 files changed

+171
-35
lines changed

lib/productions/constructor.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Base } from "./base.js";
2+
import { argument_list } from "./helpers.js";
3+
4+
export class Constructor extends Base {
5+
/**
6+
* @param {import("../tokeniser").Tokeniser} tokeniser
7+
*/
8+
static parse(tokeniser) {
9+
const base = tokeniser.consume("constructor");
10+
if (!base) {
11+
return;
12+
}
13+
const tokens = { base };
14+
tokens.open = tokeniser.consume("(") || tokeniser.error("No argument list in constructor");
15+
const args = argument_list(tokeniser);
16+
tokens.close = tokeniser.consume(")") || tokeniser.error("Unterminated constructor");
17+
tokens.termination = tokeniser.consume(";") || tokeniser.error("No semicolon after constructor");
18+
const ret = new Constructor({ tokens });
19+
ret.arguments = args;
20+
return ret;
21+
}
22+
23+
get type() {
24+
return "constructor";
25+
}
26+
}

lib/productions/interface.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { IterableLike } from "./iterable.js";
66
import { stringifier, autofixAddExposedWindow } from "./helpers.js";
77
import { validationError } from "../error.js";
88
import { checkInterfaceMemberDuplication } from "../validators/interface.js";
9+
import { Constructor } from "./constructor.js";
910

1011
/**
1112
* @param {import("../tokeniser").Tokeniser} tokeniser
@@ -30,6 +31,7 @@ export class Interface extends Container {
3031
inheritable: !partial,
3132
allowedMembers: [
3233
[Constant.parse],
34+
[Constructor.parse],
3335
[static_member],
3436
[stringifier],
3537
[IterableLike.parse],

lib/tokeniser.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const nonRegexTerminals = [
5454
"async",
5555
"boolean",
5656
"byte",
57+
"constructor",
5758
"double",
5859
"false",
5960
"float",

lib/writer.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,17 @@ export function write(ast, { templates: ts = templates } = {}) {
153153
]), { data: it, parent });
154154
}
155155

156+
function constructor(it, parent) {
157+
return ts.definition(ts.wrap([
158+
extended_attributes(it.extAttrs),
159+
token(it.tokens.base),
160+
token(it.tokens.open),
161+
ts.wrap(it.arguments.map(argument)),
162+
token(it.tokens.close),
163+
token(it.tokens.termination)
164+
]), { data: it, parent });
165+
}
166+
156167
function inheritance(inh) {
157168
if (!inh.tokens.inheritance) {
158169
return "";
@@ -275,6 +286,7 @@ export function write(ast, { templates: ts = templates } = {}) {
275286
namespace: container,
276287
operation,
277288
attribute,
289+
constructor,
278290
dictionary: container,
279291
field,
280292
const: const_,

test/syntax/baseline/constructor.json

Lines changed: 124 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,128 @@
44
"name": "Circle",
55
"inheritance": null,
66
"members": [
7+
{
8+
"type": "constructor",
9+
"arguments": [],
10+
"extAttrs": []
11+
},
12+
{
13+
"type": "constructor",
14+
"arguments": [
15+
{
16+
"type": "argument",
17+
"name": "radius",
18+
"extAttrs": [],
19+
"idlType": {
20+
"type": "argument-type",
21+
"extAttrs": [],
22+
"generic": "",
23+
"nullable": false,
24+
"union": false,
25+
"idlType": "float"
26+
},
27+
"default": null,
28+
"optional": false,
29+
"variadic": false
30+
}
31+
],
32+
"extAttrs": []
33+
},
34+
{
35+
"type": "constructor",
36+
"arguments": [
37+
{
38+
"type": "argument",
39+
"name": "str",
40+
"extAttrs": [],
41+
"idlType": {
42+
"type": "argument-type",
43+
"extAttrs": [],
44+
"generic": "",
45+
"nullable": false,
46+
"union": false,
47+
"idlType": "string"
48+
},
49+
"default": {
50+
"type": "string",
51+
"value": ""
52+
},
53+
"optional": true,
54+
"variadic": false
55+
}
56+
],
57+
"extAttrs": []
58+
},
59+
{
60+
"type": "constructor",
61+
"arguments": [
62+
{
63+
"type": "argument",
64+
"name": "seq",
65+
"extAttrs": [],
66+
"idlType": {
67+
"type": "argument-type",
68+
"extAttrs": [],
69+
"generic": "sequence",
70+
"nullable": false,
71+
"union": false,
72+
"idlType": [
73+
{
74+
"type": "argument-type",
75+
"extAttrs": [],
76+
"generic": "",
77+
"nullable": false,
78+
"union": false,
79+
"idlType": "string"
80+
}
81+
]
82+
},
83+
"default": null,
84+
"optional": false,
85+
"variadic": false
86+
}
87+
],
88+
"extAttrs": []
89+
},
90+
{
91+
"type": "constructor",
92+
"arguments": [
93+
{
94+
"type": "argument",
95+
"name": "union",
96+
"extAttrs": [],
97+
"idlType": {
98+
"type": "argument-type",
99+
"extAttrs": [],
100+
"generic": "",
101+
"nullable": false,
102+
"union": true,
103+
"idlType": [
104+
{
105+
"type": null,
106+
"extAttrs": [],
107+
"generic": "",
108+
"nullable": false,
109+
"union": false,
110+
"idlType": "Type1"
111+
},
112+
{
113+
"type": null,
114+
"extAttrs": [],
115+
"generic": "",
116+
"nullable": false,
117+
"union": false,
118+
"idlType": "Type2"
119+
}
120+
]
121+
},
122+
"default": null,
123+
"optional": false,
124+
"variadic": false
125+
}
126+
],
127+
"extAttrs": []
128+
},
7129
{
8130
"type": "attribute",
9131
"name": "r",
@@ -65,42 +187,12 @@
65187
"readonly": true
66188
}
67189
],
68-
"extAttrs": [
69-
{
70-
"type": "extended-attribute",
71-
"name": "Constructor",
72-
"rhs": null,
73-
"arguments": []
74-
},
75-
{
76-
"type": "extended-attribute",
77-
"name": "Constructor",
78-
"rhs": null,
79-
"arguments": [
80-
{
81-
"type": "argument",
82-
"name": "radius",
83-
"extAttrs": [],
84-
"idlType": {
85-
"type": "argument-type",
86-
"extAttrs": [],
87-
"generic": "",
88-
"nullable": false,
89-
"union": false,
90-
"idlType": "float"
91-
},
92-
"default": null,
93-
"optional": false,
94-
"variadic": false
95-
}
96-
]
97-
}
98-
],
190+
"extAttrs": [],
99191
"partial": false
100192
},
101193
{
102194
"type": "eof",
103195
"value": "",
104-
"trivia": ""
196+
"trivia": "\n"
105197
}
106198
]

test/syntax/idl/constructor.webidl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
// Extracted from http://dev.w3.org/2006/webapi/WebIDL/ on 2011-05-06
2-
[Constructor,
3-
Constructor(float radius)]
42
interface Circle {
3+
constructor();
4+
constructor(float radius);
5+
constructor(optional string str = "");
6+
constructor(sequence<string> seq);
7+
constructor((Type1 or Type2) union);
58
attribute float r;
69
attribute float cx;
710
attribute float cy;
811
readonly attribute float circumference;
9-
};
12+
};

0 commit comments

Comments
 (0)