Skip to content

Commit 4ceae94

Browse files
committed
cleaning up Object.create cases
1 parent 1e33ba6 commit 4ceae94

4 files changed

Lines changed: 56 additions & 70 deletions

File tree

src/slang-nodes/ModifierDefinition.ts

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ import type { AstPath, Doc, ParserOptions } from 'prettier';
1212
import type { AstNode } from '../slang-nodes';
1313
import type { PrintFunction, SlangNode } from '../types';
1414

15-
const objectConfig = {
16-
writable: true,
17-
enumerable: true,
18-
configurable: true
19-
};
20-
2115
export class ModifierDefinition implements SlangNode {
2216
readonly kind = NonterminalKind.ModifierDefinition;
2317

@@ -74,27 +68,24 @@ export class ModifierDefinition implements SlangNode {
7468
trailingOffset: 0
7569
};
7670

77-
this.parameters = Object.create(ParametersDeclaration.prototype, {
78-
kind: {
79-
value: NonterminalKind.ParametersDeclaration,
80-
...objectConfig
81-
},
82-
loc: {
83-
value: { ...parametersLoc },
84-
...objectConfig
85-
},
86-
comments: { value: [], ...objectConfig },
87-
parameters: {
88-
value: Object.create(Parameters.prototype, {
89-
kind: { value: NonterminalKind.Parameters, ...objectConfig },
90-
loc: { value: { ...parametersLoc }, ...objectConfig },
91-
comments: { value: [], ...objectConfig },
92-
items: { value: [], ...objectConfig },
93-
separators: { value: [], ...objectConfig }
94-
}) as Parameters,
95-
...objectConfig
71+
this.parameters = Object.assign(
72+
Object.create(ParametersDeclaration.prototype) as ParametersDeclaration,
73+
{
74+
kind: NonterminalKind.ParametersDeclaration,
75+
loc: { ...parametersLoc },
76+
comments: [],
77+
parameters: Object.assign(
78+
Object.create(Parameters.prototype) as Parameters,
79+
{
80+
kind: NonterminalKind.Parameters,
81+
loc: { ...parametersLoc },
82+
comments: [],
83+
items: [],
84+
separators: []
85+
}
86+
)
9687
}
97-
}) as ParametersDeclaration;
88+
);
9889
}
9990
}
10091

src/slang-nodes/ModifierInvocation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class ModifierInvocation implements SlangNode {
5252
this.arguments.variant.arguments.items.length === 0 && // no arguments
5353
!ast.arguments!.variant.cst.children().some((child) => isComment(child)) // no comments, at this point we need to check the CST
5454
) {
55-
this.arguments = undefined;
55+
delete this.arguments;
5656
}
5757
};
5858
}

src/slang-utils/create-hug-function.ts

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ import { TupleExpression } from '../slang-nodes/TupleExpression.js';
88
import { TupleValues } from '../slang-nodes/TupleValues.js';
99
import { TupleValue } from '../slang-nodes/TupleValue.js';
1010

11-
const objectConfig = {
12-
writable: true,
13-
enumerable: true,
14-
configurable: true
15-
};
16-
1711
export function createHugFunction(
1812
huggableOperators: string[]
1913
): (node: Expression) => Expression {
@@ -26,42 +20,39 @@ export function createHugFunction(
2620
) {
2721
const { loc } = node;
2822

29-
return Object.create(Expression.prototype, {
30-
kind: { value: NonterminalKind.Expression, ...objectConfig },
31-
loc: { value: { ...loc }, ...objectConfig },
32-
comments: { value: [], ...objectConfig },
33-
variant: {
34-
value: Object.create(TupleExpression.prototype, {
35-
kind: { value: NonterminalKind.TupleExpression, ...objectConfig },
36-
loc: { value: { ...loc }, ...objectConfig },
37-
comments: { value: [], ...objectConfig },
38-
items: {
39-
value: Object.create(TupleValues.prototype, {
40-
kind: { value: NonterminalKind.TupleValues, ...objectConfig },
41-
loc: { value: { ...loc }, ...objectConfig },
42-
comments: { value: [], ...objectConfig },
43-
items: {
44-
value: [
45-
Object.create(TupleValue.prototype, {
46-
kind: {
47-
value: NonterminalKind.TupleValue,
48-
...objectConfig
49-
},
50-
loc: { value: { ...loc }, ...objectConfig },
51-
comments: { value: [], ...objectConfig },
52-
expression: { value: node, ...objectConfig }
53-
}) as TupleValue
54-
],
55-
...objectConfig
56-
},
57-
separators: { value: [], ...objectConfig }
58-
}) as TupleValues,
59-
...objectConfig
60-
}
61-
}) as TupleExpression,
62-
...objectConfig
63-
}
64-
}) as Expression;
23+
return Object.assign(Object.create(Expression.prototype) as Expression, {
24+
kind: NonterminalKind.Expression,
25+
loc: { ...loc },
26+
comments: [],
27+
variant: Object.assign(
28+
Object.create(TupleExpression.prototype) as TupleExpression,
29+
{
30+
kind: NonterminalKind.TupleExpression,
31+
loc: { ...loc },
32+
comments: [],
33+
items: Object.assign(
34+
Object.create(TupleValues.prototype) as TupleValues,
35+
{
36+
kind: NonterminalKind.TupleValues,
37+
loc: { ...loc },
38+
comments: [],
39+
items: [
40+
Object.assign(
41+
Object.create(TupleValue.prototype) as TupleValue,
42+
{
43+
kind: NonterminalKind.TupleValue,
44+
loc: { ...loc },
45+
comments: [],
46+
expression: node
47+
}
48+
)
49+
],
50+
separators: []
51+
}
52+
)
53+
}
54+
)
55+
});
6556
}
6657

6758
return node;

src/slang-utils/create-kind-check-function.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import type { Node } from '@nomicfoundation/slang/cst';
2+
import type {
3+
NonterminalKind,
4+
TerminalKind
5+
} from '@nomicfoundation/slang/kinds';
26
import type { StrictAstNode } from '../slang-nodes';
37

48
export function createKindCheckFunction(
5-
kindsArray: string[]
9+
kindsArray: (keyof typeof TerminalKind | keyof typeof NonterminalKind)[]
610
): (node: StrictAstNode | Node) => boolean {
711
const kinds = new Set(kindsArray);
812
return (node: StrictAstNode | Node): boolean => kinds.has(node.kind);

0 commit comments

Comments
 (0)