Skip to content

Feature: Add method handling to mixin processing in KDL #2100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions inputfiles/addedTypes.jsonc
Original file line number Diff line number Diff line change
@@ -1,28 +1,4 @@
{
"mixins": {
"mixin": {
"DocumentOrShadowRoot": {
// Manually moved from Document
// See https://github.com/w3c/csswg-drafts/issues/5886 and https://github.com/w3c/csswg-drafts/issues/556
"methods": {
"method": {
"elementFromPoint": {
"name": "elementFromPoint",
"overrideSignatures": [
"elementFromPoint(x: number, y: number): Element | null"
]
},
"elementsFromPoint": {
"name": "elementsFromPoint",
"overrideSignatures": [
"elementsFromPoint(x: number, y: number): Element[]"
]
}
}
}
}
}
},
"interfaces": {
"interface": {
// ImportMeta is not a true DOM interface, but we are forced to declare it as one in order to emit method definitions.
Expand Down
15 changes: 15 additions & 0 deletions inputfiles/patches/cssom-view.kdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Manually moved from Document
// See https://github.com/w3c/csswg-drafts/issues/5886 and https://github.com/w3c/csswg-drafts/issues/556
interface-mixin DocumentOrShadowRoot {
method elementFromPoint returns=Element nullable=#true {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nullable is logically a property of the return type rather than the method, so in this case it would be also nicer to use separate type node as below.

param x type=long
param y type=long
}
method elementsFromPoint {
type sequence {
type Element
}
param x type=long
param y type=long
}
}
40 changes: 39 additions & 1 deletion src/build/patches.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parse, type Value, type Node } from "kdljs";
import type { Enum, Event, Property, Interface, WebIdl } from "./types";
import type { Enum, Event, Property, Interface, WebIdl, Method } from "./types";
import { readdir, readFile } from "fs/promises";
import { merge } from "./helpers.js";

Expand Down Expand Up @@ -94,6 +94,7 @@ function handleMixin(node: Node): DeepPartial<Interface> {

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

for (const child of node.children) {
switch (child.name) {
Expand All @@ -105,6 +106,11 @@ function handleMixin(node: Node): DeepPartial<Interface> {
property[propName] = handleProperty(child);
break;
}
case "method": {
const methodName = child.values[0] as string;
method[methodName] = handleMethod(child);
break;
}
default:
throw new Error(`Unknown node name: ${child.name}`);
}
Expand All @@ -114,6 +120,7 @@ function handleMixin(node: Node): DeepPartial<Interface> {
name,
events: { event },
properties: { property },
methods: { method },
...optionalMember("extends", "string", node.properties?.extends),
} as DeepPartial<Interface>;
}
Expand Down Expand Up @@ -142,6 +149,37 @@ function handleProperty(child: Node): Partial<Property> {
};
}

/**
* Handles a child node of type "method" and adds it to the method object.
* @param child The child node to handle.
*/
function handleMethod(child: Node): Partial<Method> {
const name = child.values[0] as string;
const type = child.children[0];
const returnType = child.properties.returns as string;
const nullable = child.properties.nullable as boolean;

const params = child.children
.filter((c) => c.values[0] != "sequence")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should filter in based on the type param, not filter out based on name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

.map((c) => ({
name: c.values[0] as string,
type: c.properties.type as string,
}));

const signature: Method["signature"] = [
{
type: type.name == "type" ? (type.values[0] as string) : returnType,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll probably get similar situation on other things than methods, this should probably be done in a helper function so that others can reuse it, something like handleTyped.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have made it a helper function

param: params,
nullable,
subtype:
type.name == "type"
? { type: type.children[0].values[0] as string }
: undefined,
},
];
return { name, signature };
}

/**
* Collect all file URLs in a directory.
*/
Expand Down