Skip to content

Commit 3dfa407

Browse files
authored
removing Expression from the AST tree (#1294)
1 parent 0c50634 commit 3dfa407

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+303
-283
lines changed

src/slang-comments/handlers/add-collection-first-comment.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { util } from 'prettier';
22

3-
import type { Comment, NodeCollection } from '../../slang-nodes/types.d.ts';
3+
import type { Comment, StrictCollection } from '../../slang-nodes/types.d.ts';
44

55
const { addDanglingComment, addLeadingComment } = util;
66

77
export default function addCollectionFirstComment(
8-
node: NodeCollection,
8+
node: StrictCollection,
99
comment: Comment
1010
): void {
1111
if (node.items.length === 0) {

src/slang-comments/handlers/add-collection-last-comment.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { util } from 'prettier';
22

3-
import type { Comment, NodeCollection } from '../../slang-nodes/types.d.ts';
3+
import type { Comment, StrictCollection } from '../../slang-nodes/types.d.ts';
44

55
const { addDanglingComment, addTrailingComment } = util;
66

77
export default function addCollectionLastComment(
8-
node: NodeCollection,
8+
node: StrictCollection,
99
comment: Comment
1010
): void {
1111
if (node.items.length === 0) {

src/slang-nodes/AdditiveExpression.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { NonterminalKind } from '@nomicfoundation/slang/cst';
22
import { printBinaryOperation } from '../slang-printers/print-binary-operation.js';
33
import { createHugFunction } from '../slang-utils/create-hug-function.js';
44
import { createKindCheckFunction } from '../slang-utils/create-kind-check-function.js';
5+
import { extractVariant } from '../slang-utils/extract-variant.js';
56
import { SlangNode } from './SlangNode.js';
67
import { Expression } from './Expression.js';
78

@@ -28,18 +29,20 @@ const printAdditiveExpression = printBinaryOperation(
2829
export class AdditiveExpression extends SlangNode {
2930
readonly kind = NonterminalKind.AdditiveExpression;
3031

31-
leftOperand: Expression;
32+
leftOperand: Expression['variant'];
3233

3334
operator: string;
3435

35-
rightOperand: Expression;
36+
rightOperand: Expression['variant'];
3637

3738
constructor(ast: ast.AdditiveExpression, options: ParserOptions<AstNode>) {
3839
super(ast);
3940

40-
this.leftOperand = new Expression(ast.leftOperand, options);
41+
this.leftOperand = extractVariant(new Expression(ast.leftOperand, options));
4142
this.operator = ast.operator.unparse();
42-
this.rightOperand = new Expression(ast.rightOperand, options);
43+
this.rightOperand = extractVariant(
44+
new Expression(ast.rightOperand, options)
45+
);
4346

4447
this.updateMetadata(this.leftOperand, this.rightOperand);
4548

src/slang-nodes/AndExpression.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { NonterminalKind } from '@nomicfoundation/slang/cst';
22
import { printLogicalOperation } from '../slang-printers/print-logical-operation.js';
3+
import { extractVariant } from '../slang-utils/extract-variant.js';
34
import { SlangNode } from './SlangNode.js';
45
import { Expression } from './Expression.js';
56

@@ -11,18 +12,20 @@ import type { AstNode } from './types.d.ts';
1112
export class AndExpression extends SlangNode {
1213
readonly kind = NonterminalKind.AndExpression;
1314

14-
leftOperand: Expression;
15+
leftOperand: Expression['variant'];
1516

1617
operator: string;
1718

18-
rightOperand: Expression;
19+
rightOperand: Expression['variant'];
1920

2021
constructor(ast: ast.AndExpression, options: ParserOptions<AstNode>) {
2122
super(ast);
2223

23-
this.leftOperand = new Expression(ast.leftOperand, options);
24+
this.leftOperand = extractVariant(new Expression(ast.leftOperand, options));
2425
this.operator = ast.operator.unparse();
25-
this.rightOperand = new Expression(ast.rightOperand, options);
26+
this.rightOperand = extractVariant(
27+
new Expression(ast.rightOperand, options)
28+
);
2629

2730
this.updateMetadata(this.leftOperand, this.rightOperand);
2831
}

src/slang-nodes/ArrayTypeName.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { NonterminalKind } from '@nomicfoundation/slang/cst';
22
import { printVariant } from '../slang-printers/print-variant.js';
3+
import { extractVariant } from '../slang-utils/extract-variant.js';
34
import { SlangNode } from './SlangNode.js';
45
import { TypeName } from './TypeName.js';
56
import { Expression } from './Expression.js';
@@ -14,14 +15,14 @@ export class ArrayTypeName extends SlangNode {
1415

1516
operand: TypeName;
1617

17-
index?: Expression;
18+
index?: Expression['variant'];
1819

1920
constructor(ast: ast.ArrayTypeName, options: ParserOptions<AstNode>) {
2021
super(ast);
2122

2223
this.operand = new TypeName(ast.operand, options);
2324
if (ast.index) {
24-
this.index = new Expression(ast.index, options);
25+
this.index = extractVariant(new Expression(ast.index, options));
2526
}
2627

2728
this.updateMetadata(this.operand, this.index);
@@ -31,7 +32,7 @@ export class ArrayTypeName extends SlangNode {
3132
return [
3233
path.call(printVariant(print), 'operand'),
3334
'[',
34-
path.call(printVariant(print), 'index'),
35+
path.call(print, 'index'),
3536
']'
3637
];
3738
}

src/slang-nodes/ArrayValues.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { NonterminalKind } from '@nomicfoundation/slang/cst';
22
import { printSeparatedList } from '../slang-printers/print-separated-list.js';
3-
import { printVariant } from '../slang-printers/print-variant.js';
3+
import { extractVariant } from '../slang-utils/extract-variant.js';
44
import { SlangNode } from './SlangNode.js';
55
import { Expression } from './Expression.js';
66

@@ -12,15 +12,17 @@ import type { AstNode } from './types.d.ts';
1212
export class ArrayValues extends SlangNode {
1313
readonly kind = NonterminalKind.ArrayValues;
1414

15-
items: Expression[];
15+
items: Expression['variant'][];
1616

1717
constructor(ast: ast.ArrayValues, options: ParserOptions<AstNode>) {
1818
super(ast, true);
1919

20-
this.items = ast.items.map((item) => new Expression(item, options));
20+
this.items = ast.items.map((item) =>
21+
extractVariant(new Expression(item, options))
22+
);
2123
}
2224

2325
print(path: AstPath<ArrayValues>, print: PrintFunction): Doc {
24-
return printSeparatedList(path.map(printVariant(print), 'items'));
26+
return printSeparatedList(path.map(print, 'items'));
2527
}
2628
}

src/slang-nodes/AssignmentExpression.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { NonterminalKind } from '@nomicfoundation/slang/cst';
22
import { isBinaryOperation } from '../slang-utils/is-binary-operation.js';
33
import { printIndentedGroupOrSpacedDocument } from '../slang-printers/print-indented-group-or-spaced-document.js';
4-
import { printVariant } from '../slang-printers/print-variant.js';
4+
import { extractVariant } from '../slang-utils/extract-variant.js';
55
import { SlangNode } from './SlangNode.js';
66
import { Expression } from './Expression.js';
77
import { TerminalNode } from './TerminalNode.js';
@@ -14,31 +14,32 @@ import type { AstNode } from './types.d.ts';
1414
export class AssignmentExpression extends SlangNode {
1515
readonly kind = NonterminalKind.AssignmentExpression;
1616

17-
leftOperand: Expression;
17+
leftOperand: Expression['variant'];
1818

1919
operator: string;
2020

21-
rightOperand: Expression;
21+
rightOperand: Expression['variant'];
2222

2323
constructor(ast: ast.AssignmentExpression, options: ParserOptions<AstNode>) {
2424
super(ast);
2525

26-
this.leftOperand = new Expression(ast.leftOperand, options);
26+
this.leftOperand = extractVariant(new Expression(ast.leftOperand, options));
2727
this.operator = ast.operator.unparse();
28-
this.rightOperand = new Expression(ast.rightOperand, options);
28+
this.rightOperand = extractVariant(
29+
new Expression(ast.rightOperand, options)
30+
);
2931

3032
this.updateMetadata(this.leftOperand, this.rightOperand);
3133
}
3234

3335
print(path: AstPath<AssignmentExpression>, print: PrintFunction): Doc {
34-
const rightOperandVariant = this.rightOperand.variant;
3536
return [
36-
path.call(printVariant(print), 'leftOperand'),
37+
path.call(print, 'leftOperand'),
3738
` ${this.operator}`,
3839
printIndentedGroupOrSpacedDocument(
39-
path.call(printVariant(print), 'rightOperand'),
40-
!(rightOperandVariant instanceof TerminalNode) &&
41-
isBinaryOperation(rightOperandVariant)
40+
path.call(print, 'rightOperand'),
41+
!(this.rightOperand instanceof TerminalNode) &&
42+
isBinaryOperation(this.rightOperand)
4243
)
4344
];
4445
}

src/slang-nodes/BitwiseAndExpression.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { NonterminalKind } from '@nomicfoundation/slang/cst';
22
import { printBinaryOperation } from '../slang-printers/print-binary-operation.js';
33
import { createHugFunction } from '../slang-utils/create-hug-function.js';
44
import { createKindCheckFunction } from '../slang-utils/create-kind-check-function.js';
5+
import { extractVariant } from '../slang-utils/extract-variant.js';
56
import { SlangNode } from './SlangNode.js';
67
import { Expression } from './Expression.js';
78

@@ -24,18 +25,20 @@ const printBitwiseAndExpression = printBinaryOperation(
2425
export class BitwiseAndExpression extends SlangNode {
2526
readonly kind = NonterminalKind.BitwiseAndExpression;
2627

27-
leftOperand: Expression;
28+
leftOperand: Expression['variant'];
2829

2930
operator: string;
3031

31-
rightOperand: Expression;
32+
rightOperand: Expression['variant'];
3233

3334
constructor(ast: ast.BitwiseAndExpression, options: ParserOptions<AstNode>) {
3435
super(ast);
3536

36-
this.leftOperand = new Expression(ast.leftOperand, options);
37+
this.leftOperand = extractVariant(new Expression(ast.leftOperand, options));
3738
this.operator = ast.operator.unparse();
38-
this.rightOperand = new Expression(ast.rightOperand, options);
39+
this.rightOperand = extractVariant(
40+
new Expression(ast.rightOperand, options)
41+
);
3942

4043
this.updateMetadata(this.leftOperand, this.rightOperand);
4144

src/slang-nodes/BitwiseOrExpression.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { NonterminalKind } from '@nomicfoundation/slang/cst';
22
import { printBinaryOperation } from '../slang-printers/print-binary-operation.js';
33
import { createHugFunction } from '../slang-utils/create-hug-function.js';
44
import { createKindCheckFunction } from '../slang-utils/create-kind-check-function.js';
5+
import { extractVariant } from '../slang-utils/extract-variant.js';
56
import { SlangNode } from './SlangNode.js';
67
import { Expression } from './Expression.js';
78

@@ -34,18 +35,20 @@ const printBitwiseOrExpression = printBinaryOperation(
3435
export class BitwiseOrExpression extends SlangNode {
3536
readonly kind = NonterminalKind.BitwiseOrExpression;
3637

37-
leftOperand: Expression;
38+
leftOperand: Expression['variant'];
3839

3940
operator: string;
4041

41-
rightOperand: Expression;
42+
rightOperand: Expression['variant'];
4243

4344
constructor(ast: ast.BitwiseOrExpression, options: ParserOptions<AstNode>) {
4445
super(ast);
4546

46-
this.leftOperand = new Expression(ast.leftOperand, options);
47+
this.leftOperand = extractVariant(new Expression(ast.leftOperand, options));
4748
this.operator = ast.operator.unparse();
48-
this.rightOperand = new Expression(ast.rightOperand, options);
49+
this.rightOperand = extractVariant(
50+
new Expression(ast.rightOperand, options)
51+
);
4952

5053
this.updateMetadata(this.leftOperand, this.rightOperand);
5154

src/slang-nodes/BitwiseXorExpression.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { NonterminalKind } from '@nomicfoundation/slang/cst';
22
import { printBinaryOperation } from '../slang-printers/print-binary-operation.js';
33
import { createHugFunction } from '../slang-utils/create-hug-function.js';
44
import { createKindCheckFunction } from '../slang-utils/create-kind-check-function.js';
5+
import { extractVariant } from '../slang-utils/extract-variant.js';
56
import { SlangNode } from './SlangNode.js';
67
import { Expression } from './Expression.js';
78

@@ -24,18 +25,20 @@ const printBitwiseXorExpression = printBinaryOperation(
2425
export class BitwiseXorExpression extends SlangNode {
2526
readonly kind = NonterminalKind.BitwiseXorExpression;
2627

27-
leftOperand: Expression;
28+
leftOperand: Expression['variant'];
2829

2930
operator: string;
3031

31-
rightOperand: Expression;
32+
rightOperand: Expression['variant'];
3233

3334
constructor(ast: ast.BitwiseXorExpression, options: ParserOptions<AstNode>) {
3435
super(ast);
3536

36-
this.leftOperand = new Expression(ast.leftOperand, options);
37+
this.leftOperand = extractVariant(new Expression(ast.leftOperand, options));
3738
this.operator = ast.operator.unparse();
38-
this.rightOperand = new Expression(ast.rightOperand, options);
39+
this.rightOperand = extractVariant(
40+
new Expression(ast.rightOperand, options)
41+
);
3942

4043
this.updateMetadata(this.leftOperand, this.rightOperand);
4144

0 commit comments

Comments
 (0)