Skip to content

Commit

Permalink
#50 More type-safe withChild signature
Browse files Browse the repository at this point in the history
  • Loading branch information
alessiostalla committed Dec 6, 2023
1 parent 482260d commit b88fd50
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
21 changes: 13 additions & 8 deletions src/transformation/transformation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ export class PropertyRef<Obj, Value> {
public readonly get: (o: Obj) => Value | undefined,
public readonly set: (o: Obj, v: Value) => void) {}

static get<Obj, Value>(name: string): PropertyRef<Obj, Value> {
static get<Obj, Value>(name: string | symbol | number): PropertyRef<Obj, Value> {
if (typeof name == "symbol") {
name = name.toString();
} else if (typeof name == "number") {
name = name + "";
}
return new PropertyRef<Obj, Value>(
name,
obj => obj[name],
Expand All @@ -28,8 +33,8 @@ export class PropertyRef<Obj, Value> {
}

export type ChildDef<Source, Target, Child> = {
source: string | PropertyRef<Source, any> | ((s: Source) => any | undefined),
target?: string | PropertyRef<Target, Child> | ((t: Target, c?: Child) => void),
source: keyof Source | PropertyRef<Source, any> | ((s: Source) => any | undefined),
target?: keyof Target | PropertyRef<Target, Child> | ((t: Target, c?: Child) => void),
name?: string,
scopedToType?: any
};
Expand Down Expand Up @@ -77,8 +82,8 @@ export class NodeFactory<Source, Output extends Node> {
}
}
let source = child.source;
if (typeof source == "string") {
source = PropertyRef.get(source);
if (typeof source == "string" || typeof source == "symbol" || typeof source == "number") {
source = PropertyRef.get(source as any);
}
if (source instanceof PropertyRef) {
source = source.get;
Expand All @@ -89,7 +94,7 @@ export class NodeFactory<Source, Output extends Node> {
// given we have no setter we MUST set the children at construction
this.childrenSetAtConstruction = true;
}
if (typeof target == "string") {
if (typeof target == "string" || typeof target == "symbol" || typeof target == "number") {
target = PropertyRef.get(target);
}
if (target instanceof PropertyRef) {
Expand Down Expand Up @@ -345,7 +350,7 @@ export class ASTTransformer {
}

public registerNodeFactory<S, T extends Node>(
nodeClass: any,
nodeClass: new(...args: any) => S,
factory: (type: S, transformer: ASTTransformer, factory: NodeFactory<S, T>) => T | undefined
) : NodeFactory<S, T> {

Expand All @@ -363,7 +368,7 @@ export class ASTTransformer {
return nodeFactory;
}

public registerIdentityTransformation<T extends Node>(nodeClass: any) : NodeFactory<T, T> {
public registerIdentityTransformation<T extends Node>(nodeClass: new(...args: any[]) => T) : NodeFactory<T, T> {
return this.registerNodeFactory(nodeClass, (node: T) => node);
}
}
Expand Down
9 changes: 3 additions & 6 deletions tests/mapping.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {expect} from "chai";

import {ASTTransformer, Child, GenericErrorNode, GenericNode, Mapped, Node, Position, PropertyRef} from "../src";
import {ASTTransformer, Child, GenericErrorNode, GenericNode, Mapped, Node, Position} from "../src";
import {SimpleLangLexer} from "./parser/SimpleLangLexer";
import {CharStreams, CommonTokenStream} from "antlr4ts";
import {CompilationUnitContext, DisplayStmtContext, SetStmtContext, SimpleLangParser} from "./parser/SimpleLangParser";
Expand Down Expand Up @@ -166,11 +166,8 @@ describe('ParseTreeToASTTransformer', function () {

const configure = function(transformer: ASTTransformer) : void {

transformer.registerNodeFactory(CompilationUnitContext, source => new CU())
.withChild({
source: PropertyRef.get("statement"),
target: PropertyRef.get("statements")
});
transformer.registerNodeFactory(CompilationUnitContext, () => new CU())
.withChild({ source: "statement", target: "statements" });

transformer.registerNodeFactory<DisplayStmtContext, DisplayIntStatement>(
DisplayStmtContext,
Expand Down

0 comments on commit b88fd50

Please sign in to comment.