Skip to content

Commit a62a343

Browse files
committed
Feature: Add method handling to mixin processing in KDL
1 parent 23819c7 commit a62a343

File tree

3 files changed

+33
-25
lines changed

3 files changed

+33
-25
lines changed

inputfiles/addedTypes.jsonc

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,4 @@
11
{
2-
"mixins": {
3-
"mixin": {
4-
"DocumentOrShadowRoot": {
5-
// Manually moved from Document
6-
// See https://github.com/w3c/csswg-drafts/issues/5886 and https://github.com/w3c/csswg-drafts/issues/556
7-
"methods": {
8-
"method": {
9-
"elementFromPoint": {
10-
"name": "elementFromPoint",
11-
"overrideSignatures": [
12-
"elementFromPoint(x: number, y: number): Element | null"
13-
]
14-
},
15-
"elementsFromPoint": {
16-
"name": "elementsFromPoint",
17-
"overrideSignatures": [
18-
"elementsFromPoint(x: number, y: number): Element[]"
19-
]
20-
}
21-
}
22-
}
23-
}
24-
}
25-
},
262
"callbackInterfaces": {
273
"interface": {}
284
},

inputfiles/patches/dom.kdl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Manually moved from Document
2+
// See https://github.com/w3c/csswg-drafts/issues/5886 and https://github.com/w3c/csswg-drafts/issues/556
3+
interface-mixin DocumentOrShadowRoot {
4+
method elementFromPoint returns="Element | null" {
5+
param x type=number
6+
param y type=number
7+
}
8+
method elementsFromPoint returns="Element[]" {
9+
param x type=number
10+
param y type=number
11+
}
12+
}

src/build/patches.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { parse, type Node } from "kdljs";
2-
import type { Enum, Event, Property, Interface, WebIdl } from "./types";
2+
import type { Enum, Event, Property, Interface, WebIdl, Method } from "./types";
33
import { readdir, readFile } from "fs/promises";
44
import { merge } from "./helpers.js";
55

@@ -76,6 +76,7 @@ function handleMixin(node: Node): DeepPartial<Interface> {
7676

7777
const event: Event[] = [];
7878
const property: Record<string, Partial<Property>> = {};
79+
const method: Record<string, Partial<Method>> = {};
7980

8081
for (const child of node.children) {
8182
switch (child.name) {
@@ -87,6 +88,11 @@ function handleMixin(node: Node): DeepPartial<Interface> {
8788
property[propName] = handleProperty(child);
8889
break;
8990
}
91+
case "method": {
92+
const methodName = child.values[0] as string;
93+
method[methodName] = handleMethod(child);
94+
break;
95+
}
9096
default:
9197
throw new Error(`Unknown node name: ${child.name}`);
9298
}
@@ -96,6 +102,7 @@ function handleMixin(node: Node): DeepPartial<Interface> {
96102
name,
97103
events: { event },
98104
properties: { property },
105+
methods: { method },
99106
} as DeepPartial<Interface>;
100107
if (node.properties.extends) {
101108
result.extends = node.properties.extends as string;
@@ -125,6 +132,19 @@ function handleProperty(child: Node): Partial<Property> {
125132
};
126133
}
127134

135+
/**
136+
* Handles a child node of type "method" and adds it to the method object.
137+
* @param child The child node to handle.
138+
*/
139+
function handleMethod(child: Node): Partial<Method> {
140+
const name = child.values[0] as string;
141+
// Build the overrideSignatures array with the method signature string
142+
const overrideSignatures = [
143+
`${name}(${child.children.map((c) => `${c.values[0]}: ${c.properties.type}`).join(", ")}): ${child.properties.returns || "void"}`,
144+
];
145+
return { name, overrideSignatures };
146+
}
147+
128148
/**
129149
* Collect all file URLs in a directory.
130150
*/

0 commit comments

Comments
 (0)