Skip to content

Commit 4691ddb

Browse files
committed
Move types in supplement.d.ts to class properties
1 parent 9d0c8f3 commit 4691ddb

23 files changed

+193
-204
lines changed

lib/productions/argument.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ import {
1414
dictionaryIncludesRequiredField,
1515
} from "../validators/helpers.js";
1616

17+
/** @import {Type} from "./type.js" */
18+
1719
export class Argument extends Base {
20+
/** @type {Type} */
21+
idlType;
22+
/** @type {Default | null} */
23+
default;
24+
1825
/**
1926
* @param {import("../tokeniser.js").Tokeniser} tokeniser
2027
*/

lib/productions/array-base.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1+
/** @import {Token} from "../tokeniser.js" */
2+
13
export class ArrayBase extends Array {
4+
/** @type {Record<string, Token | undefined>} */
5+
tokens;
6+
/** @type {Token[]} */
7+
source;
8+
parent;
9+
210
constructor({ source, tokens }) {
311
super();
412
Object.defineProperties(this, {

lib/productions/attribute.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ import {
1010
autoParenter,
1111
} from "./helpers.js";
1212

13+
/** @import {Type} from "./type.js" */
14+
1315
export class Attribute extends Base {
16+
/** @type {Type} */
17+
idlType;
18+
1419
/**
1520
* @param {import("../tokeniser.js").Tokeniser} tokeniser
1621
* @param {object} [options]

lib/productions/base.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
1+
/** @import {ExtendedAttributes} from "./extended-attributes.js" */
2+
/** @import {Token} from "../tokeniser.js" */
3+
/** @import {Definitions} from "../validator.js" */
4+
/** @import {Writer} from "../writer.js" */
5+
16
export class Base {
7+
/** @type {Record<string, Token | undefined>} */
8+
tokens;
9+
/** @type {Token[]} */
10+
source;
11+
/** @type {ExtendedAttributes | undefined} */
12+
extAttrs;
13+
/** @type {this} */
14+
this;
15+
/** @type {*} */
16+
parent;
17+
218
/**
319
* @param {object} initializer
420
* @param {Base["source"]} initializer.source
@@ -13,6 +29,20 @@ export class Base {
1329
});
1430
}
1531

32+
/**
33+
* @param {Definitions} defs
34+
* @returns {IterableIterator<any>}
35+
*/
36+
// eslint-disable-next-line no-unused-vars
37+
*validate(defs) {}
38+
39+
/**
40+
* @param {Writer} w
41+
* @returns {*}
42+
*/
43+
// eslint-disable-next-line no-unused-vars
44+
write(w) {}
45+
1646
toJSON() {
1747
const json = { type: undefined, name: undefined, inheritance: undefined };
1848
let proto = this;

lib/productions/callback-interface.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Container } from "./container.js";
1+
import { Container, parseContainer } from "./container.js";
22
import { Operation } from "./operation.js";
33
import { Constant } from "./constant.js";
44

@@ -15,7 +15,7 @@ export class CallbackInterface extends Container {
1515
if (!tokens.base) {
1616
return;
1717
}
18-
return Container.parse(
18+
return parseContainer(
1919
tokeniser,
2020
new CallbackInterface({ source: tokeniser.source, tokens }),
2121
{

lib/productions/callback.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@ import {
77
} from "./helpers.js";
88
import { validationError } from "../error.js";
99

10+
/** @import {Type} from "./type.js" */
11+
/** @import {Argument} from "./argument.js" */
12+
1013
export class CallbackFunction extends Base {
14+
/** @type {Type} */
15+
idlType;
16+
/** @type {Argument[]} */
17+
arguments;
18+
1119
/**
1220
* @param {import("../tokeniser.js").Tokeniser} tokeniser
1321
*/

lib/productions/constant.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import {
99
} from "./helpers.js";
1010

1111
export class Constant extends Base {
12+
/** @type {Type} */
13+
idlType;
14+
1215
/**
1316
* @param {import("../tokeniser.js").Tokeniser} tokeniser
1417
*/

lib/productions/constructor.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { Base } from "./base.js";
22
import { argument_list, autoParenter } from "./helpers.js";
33

4+
/** @import {Argument} from "./argument.js" */
5+
46
export class Constructor extends Base {
7+
/** @type {Argument[]} */
8+
arguments;
9+
510
/**
611
* @param {import("../tokeniser.js").Tokeniser} tokeniser
712
*/

lib/productions/container.js

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import { Base } from "./base.js";
22
import { ExtendedAttributes } from "./extended-attributes.js";
33
import { unescape, autoParenter } from "./helpers.js";
44

5+
/** @import {Tokeniser} from "../tokeniser.js" */
6+
57
/**
6-
* @param {import("../tokeniser.js").Tokeniser} tokeniser
8+
* @param {Tokeniser} tokeniser
79
*/
810
function inheritance(tokeniser) {
911
const colon = tokeniser.consume(":");
@@ -19,7 +21,7 @@ function inheritance(tokeniser) {
1921
/**
2022
* Parser callback.
2123
* @callback ParserCallback
22-
* @param {import("../tokeniser.js").Tokeniser} tokeniser
24+
* @param {Tokeniser} tokeniser
2325
* @param {...*} args
2426
*/
2527

@@ -30,45 +32,11 @@ function inheritance(tokeniser) {
3032
*/
3133

3234
export class Container extends Base {
33-
/**
34-
* @param {import("../tokeniser.js").Tokeniser} tokeniser
35-
* @param {*} instance TODO: This should be {T extends Container}, but see https://github.com/microsoft/TypeScript/issues/4628
36-
* @param {*} args
37-
*/
38-
static parse(tokeniser, instance, { inheritable, allowedMembers }) {
39-
const { tokens, type } = instance;
40-
tokens.name =
41-
tokeniser.consumeKind("identifier") ||
42-
tokeniser.error(`Missing name in ${type}`);
43-
tokeniser.current = instance;
44-
instance = autoParenter(instance);
45-
if (inheritable) {
46-
Object.assign(tokens, inheritance(tokeniser));
47-
}
48-
tokens.open = tokeniser.consume("{") || tokeniser.error(`Bodyless ${type}`);
49-
instance.members = [];
50-
while (true) {
51-
tokens.close = tokeniser.consume("}");
52-
if (tokens.close) {
53-
tokens.termination =
54-
tokeniser.consume(";") ||
55-
tokeniser.error(`Missing semicolon after ${type}`);
56-
return instance.this;
57-
}
58-
const ea = ExtendedAttributes.parse(tokeniser);
59-
let mem;
60-
for (const [parser, ...args] of allowedMembers) {
61-
mem = autoParenter(parser(tokeniser, ...args));
62-
if (mem) {
63-
break;
64-
}
65-
}
66-
if (!mem) {
67-
tokeniser.error("Unknown member");
68-
}
69-
mem.extAttrs = ea;
70-
instance.members.push(mem.this);
71-
}
35+
/** @type {any[]} */
36+
members;
37+
38+
get type() {
39+
return "";
7240
}
7341

7442
get partial() {
@@ -125,3 +93,50 @@ export class Container extends Base {
12593
);
12694
}
12795
}
96+
97+
/**
98+
* @param {Tokeniser} tokeniser
99+
* @param {Container} instance
100+
* @param {object} args
101+
* @param {boolean} [args.inheritable]
102+
* @param {AllowedMember[]} [args.allowedMembers]
103+
*/
104+
export function parseContainer(
105+
tokeniser,
106+
instance,
107+
{ inheritable, allowedMembers },
108+
) {
109+
const { tokens, type } = instance;
110+
tokens.name =
111+
tokeniser.consumeKind("identifier") ||
112+
tokeniser.error(`Missing name in ${type}`);
113+
tokeniser.current = instance;
114+
instance = autoParenter(instance);
115+
if (inheritable) {
116+
Object.assign(tokens, inheritance(tokeniser));
117+
}
118+
tokens.open = tokeniser.consume("{") || tokeniser.error(`Bodyless ${type}`);
119+
instance.members = [];
120+
while (true) {
121+
tokens.close = tokeniser.consume("}");
122+
if (tokens.close) {
123+
tokens.termination =
124+
tokeniser.consume(";") ||
125+
tokeniser.error(`Missing semicolon after ${type}`);
126+
return instance.this;
127+
}
128+
const ea = ExtendedAttributes.parse(tokeniser);
129+
let mem;
130+
for (const [parser, ...args] of allowedMembers) {
131+
mem = autoParenter(parser(tokeniser, ...args));
132+
if (mem) {
133+
break;
134+
}
135+
}
136+
if (!mem) {
137+
tokeniser.error("Unknown member");
138+
}
139+
mem.extAttrs = ea;
140+
instance.members.push(mem.this);
141+
}
142+
}

lib/productions/default.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { Base } from "./base.js";
22
import { const_data, const_value } from "./helpers.js";
33

4+
/** @import {Token} from "../tokeniser.js" */
5+
46
export class Default extends Base {
7+
/** @type {Token[]} */
8+
expression;
9+
510
/**
611
* @param {import("../tokeniser.js").Tokeniser} tokeniser
712
*/

0 commit comments

Comments
 (0)