Skip to content

Commit

Permalink
feat: add type check for compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
FeelyChau committed Dec 6, 2023
1 parent 665c0f4 commit f9a70a2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
31 changes: 31 additions & 0 deletions src/living/compatible.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { NodeImpl } from './nodes/Node';

type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y
? 1
: 2
? true
: false;

type Expect<T extends true> = T;

type FindValidFields<T> = {
[K in keyof T]: T[K] extends never ? never : K;
}[keyof T];

type CheckStrictCompatible<
StandardType,
ImplType extends StandardType,
Checked = {
[k in keyof StandardType]: Equal<StandardType[k], ImplType[k]> extends true
? never
: [StandardType[k], ImplType[k]];
}
> = Pick<Checked, FindValidFields<Checked>>;

/**
* Check the consistency between the jsar-dom implementation and the standard dom implementation,
* return the incompatible fields.
*/
type IncompatibleFields = CheckStrictCompatible<Node, NodeImpl>;

type cases = [Expect<Equal<{}, IncompatibleFields>>];
13 changes: 7 additions & 6 deletions src/living/nodes/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export class NodeImpl extends EventTarget implements Node {
return documentBaseURLSerialized(this._ownerDocument);
}

get parentNode(): ParentNodeImpl {
get parentNode(): ParentNode {
return domSymbolTree.parent(this);
}

Expand Down Expand Up @@ -275,15 +275,15 @@ export class NodeImpl extends EventTarget implements Node {
return this.nodeType === this.DOCUMENT_NODE ? null : this._ownerDocument as Document;
}

get nextSibling() {
get nextSibling(): ChildNode | null {
return domSymbolTree.nextSibling(this);
}

get previousSibling() {
get previousSibling(): ChildNode | null {
return domSymbolTree.previousSibling(this);
}

get parentElement() {
get parentElement(): HTMLElement | null {
const parentNode = domSymbolTree.parent(this);
return parentNode !== null && parentNode.nodeType === NodeImpl.ELEMENT_NODE ? parentNode : null;
}
Expand Down Expand Up @@ -509,8 +509,9 @@ export class NodeImpl extends EventTarget implements Node {
removeChild<T extends Node>(child: T): T {
return this._preRemove(child as unknown as NodeImpl) as unknown as T;
}
replaceChild<T1 extends Node, T2 extends Node>(node: T1, child: T1): T2 {
return this._replace(node as unknown as NodeImpl, child as unknown as NodeImpl) as unknown as T2;

replaceChild<T extends Node>(node: Node, child: T): T {
return this._replace(node as unknown as NodeImpl, child as unknown as NodeImpl) as unknown as T;
}

// https://dom.spec.whatwg.org/#concept-node-ensure-pre-insertion-validity
Expand Down
12 changes: 12 additions & 0 deletions src/type.utils.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export type Equal<X, Y> =
(<T>() => T extends X ? 1 : 2) extends
(<T>() => T extends Y ? 1 : 2) ? true : false;

export type NotEqual<X, Y> = true extends Equal<X, Y> ? false : true;
export type Expect<T extends true> = T;

export type FindValidFields<T> = {
[K in keyof T]: T[K] extends never ? never : K;
}[keyof T];

export type CheckStrictCompatible<StandardType, ImplType extends StandardType, Checked = {[k in keyof StandardType]: Equal<StandardType[k], ImplType[k]> extends true ? never : [StandardType[k], ImplType[k]]}> = Pick<Checked, FindValidFields<Checked>>;

0 comments on commit f9a70a2

Please sign in to comment.