Skip to content

Commit ff96bb7

Browse files
committed
PolymorphicNode now has a print function so there's no need for printing variant logic in genericPrint
1 parent caac045 commit ff96bb7

3 files changed

Lines changed: 11 additions & 12 deletions

File tree

src/slang-nodes/PolymorphicNode.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
import { SlangNode } from './SlangNode.js';
22

3-
import type { SlangAstNode } from '../types.d.ts';
3+
import type { AstPath, Doc } from 'prettier';
4+
import type { PrintFunction, SlangAstNode } from '../types.d.ts';
5+
import type { StrictPolymorphicNode } from './types.js';
6+
7+
export abstract class PolymorphicNode extends SlangNode {
8+
abstract variant: StrictPolymorphicNode['variant'];
49

5-
export class PolymorphicNode extends SlangNode {
610
constructor(ast: SlangAstNode) {
711
super(ast);
812
}
13+
14+
print(path: AstPath<PolymorphicNode>, print: PrintFunction): Doc {
15+
return path.call(print, 'variant');
16+
}
917
}

src/slang-utils/is-polymorphic-node.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/slangPrinter.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { isBlockComment } from './slang-utils/is-comment.js';
22
import { locEnd, locStart } from './slang-utils/loc.js';
3-
import { isPolymorphicNode } from './slang-utils/is-polymorphic-node.js';
43

54
import type { AstPath, Doc, ParserOptions } from 'prettier';
65
import type { AstNode, StrictAstNode } from './slang-nodes/types.d.ts';
@@ -29,6 +28,7 @@ function ignoreComments(path: AstPath<AstNode>): void {
2928
// parser. `updateMetadata` is an internal function.
3029
case 'kind':
3130
case 'loc':
31+
case 'print':
3232
case 'updateMetadata':
3333
break;
3434
// The key `comments` will contain every comment for this node.
@@ -64,7 +64,6 @@ function genericPrint(
6464
return options.originalText.slice(locStart(node), locEnd(node));
6565
}
6666

67-
if (isPolymorphicNode(node)) return path.call(print, 'variant');
6867
// Since each node has a print function with a specific AstPath, the union of
6968
// all nodes into AstNode creates a print function with an AstPath of the
7069
// intersection of all nodes. This forces us to cast this with a never type.

0 commit comments

Comments
 (0)