Skip to content

Commit

Permalink
Merge pull request #1338 from chriskrycho/use-assign
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjblue committed Sep 22, 2021
2 parents f4decfb + 7bac4e8 commit 629186d
Showing 1 changed file with 8 additions and 21 deletions.
29 changes: 8 additions & 21 deletions packages/@glimmer/syntax/lib/v2-a/objects/node.ts
@@ -1,3 +1,5 @@
import { assign } from '@glimmer/util';

import { SourceSpan } from '../../source/span';

export interface BaseNodeFields {
Expand Down Expand Up @@ -54,13 +56,13 @@ export function node<T extends string>(
return {
fields<Fields extends object>(): TypedNodeConstructor<T, BaseNodeFields & Fields> {
return class {
readonly loc: SourceSpan;
// SAFETY: initialized via `assign` in the constructor.
declare readonly loc: SourceSpan;
readonly type: T;

constructor(fields: BaseNodeFields & Fields) {
this.type = type;
this.loc = fields.loc;
copy(fields, (this as unknown) as ConstructingTypedNode<Fields>);
assign(this, fields);
}
} as TypedNodeConstructor<T, BaseNodeFields & Fields>;
},
Expand All @@ -69,23 +71,18 @@ export function node<T extends string>(
return {
fields<Fields>(): NodeConstructor<Fields & BaseNodeFields> {
return class {
readonly loc: SourceSpan;
// SAFETY: initialized via `assign` in the constructor.
declare readonly loc: SourceSpan;

constructor(fields: BaseNodeFields & Fields) {
this.loc = fields.loc;

copy(fields, (this as unknown) as ConstructingNode<Fields>);
assign(this, fields);
}
} as NodeConstructor<BaseNodeFields & Fields>;
},
};
}
}

type ConstructingTypedNode<Fields> = Fields & BaseNodeFields;

type ConstructingNode<Fields> = BaseNodeFields & Fields;

export interface NodeConstructor<Fields> {
new (fields: Fields): Readonly<Fields>;
}
Expand All @@ -95,13 +92,3 @@ type TypedNode<T extends string, Fields> = { type: T } & Readonly<Fields>;
export interface TypedNodeConstructor<T extends string, Fields> {
new (options: Fields): TypedNode<T, Fields>;
}

function keys<O extends object>(object: O): (keyof O)[] {
return Object.keys(object) as (keyof O)[];
}

function copy<O extends object>(object1: O, object2: O) {
for (let key of keys(object1)) {
object2[key] = object1[key];
}
}

0 comments on commit 629186d

Please sign in to comment.