Skip to content

Commit 8b102ab

Browse files
committed
Using PrintableNode as a more correct type for StrictAstNode
1 parent f860d65 commit 8b102ab

18 files changed

Lines changed: 152 additions & 52 deletions

src/slang-comments/handlers/handle-storage-layout-specifier-comments.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,48 @@
11
import { NonterminalKind } from '@nomicfoundation/slang/cst';
22
import { util } from 'prettier';
3+
import { createKindCheckFunction } from '../../slang-utils/create-kind-check-function.js';
4+
import { TerminalNode } from '../../slang-nodes/TerminalNode.js';
35

6+
import type { Expression } from '../../slang-nodes/Expression.js';
7+
import type { PrintableNode } from '../../slang-nodes/types.d.ts';
48
import type { HandlerParams } from './types.d.ts';
59

610
const { addLeadingComment } = util;
711

12+
const isExpression = createKindCheckFunction([
13+
NonterminalKind.AssignmentExpression,
14+
NonterminalKind.ConditionalExpression,
15+
NonterminalKind.OrExpression,
16+
NonterminalKind.AndExpression,
17+
NonterminalKind.EqualityExpression,
18+
NonterminalKind.InequalityExpression,
19+
NonterminalKind.BitwiseOrExpression,
20+
NonterminalKind.BitwiseXorExpression,
21+
NonterminalKind.BitwiseAndExpression,
22+
NonterminalKind.ShiftExpression,
23+
NonterminalKind.AdditiveExpression,
24+
NonterminalKind.MultiplicativeExpression,
25+
NonterminalKind.ExponentiationExpression,
26+
NonterminalKind.PostfixExpression,
27+
NonterminalKind.PrefixExpression,
28+
NonterminalKind.FunctionCallExpression,
29+
NonterminalKind.CallOptionsExpression,
30+
NonterminalKind.MemberAccessExpression,
31+
NonterminalKind.IndexAccessExpression,
32+
NonterminalKind.NewExpression,
33+
NonterminalKind.TupleExpression,
34+
NonterminalKind.TypeExpression,
35+
NonterminalKind.ArrayExpression,
36+
NonterminalKind.HexNumberExpression,
37+
NonterminalKind.DecimalNumberExpression,
38+
NonterminalKind.StringLiteral,
39+
NonterminalKind.StringLiterals,
40+
NonterminalKind.HexStringLiteral,
41+
NonterminalKind.HexStringLiterals,
42+
NonterminalKind.UnicodeStringLiterals,
43+
NonterminalKind.AddressType
44+
]) as (node: PrintableNode) => node is Expression['variant'];
45+
846
export default function handleStorageLayoutSpecifierComments({
947
enclosingNode,
1048
followingNode,
@@ -14,7 +52,10 @@ export default function handleStorageLayoutSpecifierComments({
1452
return false;
1553
}
1654

17-
if (followingNode?.kind === NonterminalKind.Expression) {
55+
if (
56+
followingNode &&
57+
(isExpression(followingNode) || followingNode instanceof TerminalNode)
58+
) {
1859
addLeadingComment(followingNode, comment);
1960
return true;
2061
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import type { Comment, StrictAstNode } from '../../slang-nodes/types.d.ts';
1+
import type { Comment, PrintableNode } from '../../slang-nodes/types.d.ts';
22

33
interface HandlerParams {
44
text: string;
5-
precedingNode?: StrictAstNode;
6-
enclosingNode?: StrictAstNode;
7-
followingNode?: StrictAstNode;
5+
precedingNode?: PrintableNode;
6+
enclosingNode?: PrintableNode;
7+
followingNode?: PrintableNode;
88
comment: Comment;
99
}

src/slang-nodes/CommentNode.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { TerminalNode } from '@nomicfoundation/slang/cst';
22
import type { Location } from '../types.d.ts';
3-
import type { StrictAstNode } from './types.d.ts';
3+
import type { PrintableNode } from './types.d.ts';
44

55
export class CommentNode {
66
loc: Location;
@@ -13,11 +13,11 @@ export class CommentNode {
1313

1414
placement?: 'endOfLine' | 'ownLine' | 'remaining';
1515

16-
precedingNode?: StrictAstNode;
16+
precedingNode?: PrintableNode;
1717

18-
enclosingNode?: StrictAstNode;
18+
enclosingNode?: PrintableNode;
1919

20-
followingNode?: StrictAstNode;
20+
followingNode?: PrintableNode;
2121

2222
constructor(ast: TerminalNode, offset: number) {
2323
this.loc = {

src/slang-nodes/ConditionalExpression.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import { Expression } from './Expression.js';
88
import type * as ast from '@nomicfoundation/slang/ast';
99
import type { AstPath, Doc, ParserOptions } from 'prettier';
1010
import type { CollectedMetadata, PrintFunction } from '../types.d.ts';
11-
import type { AstNode, StrictAstNode } from './types.d.ts';
11+
import type { AstNode, PrintableNode } from './types.d.ts';
1212

1313
const { group, hardline, ifBreak, indent, line } = doc.builders;
1414

1515
function experimentalTernaries(
1616
node: ConditionalExpression,
17-
path: AstPath<StrictAstNode>,
17+
path: AstPath<PrintableNode>,
1818
print: PrintFunction,
1919
{ useTabs, tabWidth }: ParserOptions<AstNode>
2020
): Doc {
@@ -77,7 +77,7 @@ function experimentalTernaries(
7777
}
7878

7979
function traditionalTernaries(
80-
path: AstPath<StrictAstNode>,
80+
path: AstPath<PrintableNode>,
8181
print: PrintFunction
8282
): Doc {
8383
return group([

src/slang-nodes/MemberAccessExpression.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import { TerminalNode } from './TerminalNode.js';
1111
import type * as ast from '@nomicfoundation/slang/ast';
1212
import type { AstPath, Doc, ParserOptions } from 'prettier';
1313
import type { CollectedMetadata, PrintFunction } from '../types.d.ts';
14-
import type { AstNode, ChainableExpression, StrictAstNode } from './types.d.ts';
14+
import type { AstNode, ChainableExpression, PrintableNode } from './types.d.ts';
1515

1616
const { group, indent, label, softline } = doc.builders;
1717

1818
const separatorLabel = Symbol('separator');
1919

2020
function isEndOfChain(
2121
node: ChainableExpression,
22-
path: AstPath<StrictAstNode>
22+
path: AstPath<PrintableNode>
2323
): boolean {
2424
for (let i = 1, current = node, parent; ; i++, current = parent) {
2525
parent = path.getNode(i)!;

src/slang-nodes/SlangNode.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import type {
1414
CollectedMetadata,
1515
SlangAstNode
1616
} from '../types.d.ts';
17-
import type { Comment, StrictAstNode } from './types.d.ts';
17+
import type { Comment, PrintableNode } from './types.d.ts';
1818
import type { TerminalNode } from './TerminalNode.ts';
1919

2020
function reversedIterator<T>(children: T[]): Iterable<T> {
@@ -121,7 +121,7 @@ export abstract class SlangNode {
121121
}
122122

123123
updateMetadata(
124-
...childNodes: (StrictAstNode | TerminalNode | undefined)[]
124+
...childNodes: (PrintableNode | TerminalNode | undefined)[]
125125
): void {
126126
const { loc } = this;
127127
// calculate correct loc object

src/slang-nodes/types.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,8 @@ export type StrictPolymorphicNode = Extract<
474474
{ variant: StrictAstNode | TerminalNode }
475475
>;
476476

477+
export type PrintableNode = Exclude<StrictAstNode, StrictPolymorphicNode>;
478+
477479
export type Collection = Extract<StrictAstNode, { items: unknown[] }>;
478480

479481
export type StrictCollection = Extract<

src/slang-printers/create-binary-operation-printer.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import type { AstPath, Doc, ParserOptions } from 'prettier';
66
import type {
77
AstNode,
88
BinaryOperation,
9-
StrictAstNode
9+
PrintableNode
1010
} from '../slang-nodes/types.d.ts';
1111
import type { PrintFunction } from '../types.d.ts';
1212

1313
const { group, line } = doc.builders;
1414

1515
function rightOperandPrint(
1616
{ operator, leftOperand }: BinaryOperation,
17-
path: AstPath<StrictAstNode>,
17+
path: AstPath<PrintableNode>,
1818
print: PrintFunction,
1919
options: ParserOptions<AstNode>
2020
): Doc {
@@ -37,15 +37,15 @@ function rightOperandPrint(
3737

3838
export const createBinaryOperationPrinter =
3939
(
40-
groupRulesBuilder: (path: AstPath<StrictAstNode>) => (document: Doc) => Doc,
40+
groupRulesBuilder: (path: AstPath<PrintableNode>) => (document: Doc) => Doc,
4141
indentRulesBuilder: (
4242
node: BinaryOperation,
43-
path: AstPath<StrictAstNode>
43+
path: AstPath<PrintableNode>
4444
) => (document: Doc) => Doc
4545
) =>
4646
(
4747
node: BinaryOperation,
48-
path: AstPath<StrictAstNode>,
48+
path: AstPath<PrintableNode>,
4949
print: PrintFunction,
5050
options: ParserOptions<AstNode>
5151
): Doc => {

src/slang-printers/print-binary-operation.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ import type { AstPath, Doc, ParserOptions } from 'prettier';
88
import type {
99
AstNode,
1010
BinaryOperation,
11-
StrictAstNode
11+
PrintableNode
1212
} from '../slang-nodes/types.d.ts';
1313
import type { PrintFunction } from '../types.d.ts';
1414

1515
const { group, indent } = doc.builders;
1616

1717
export const binaryGroupRulesBuilder =
1818
(shouldGroup: (node: BinaryOperation) => boolean) =>
19-
(path: AstPath<StrictAstNode>) =>
19+
(path: AstPath<PrintableNode>) =>
2020
(document: Doc): Doc => {
2121
const parent = path.parent!;
2222
if (!isBinaryOperation(parent)) return group(document);
@@ -31,8 +31,8 @@ const isStatementWithoutIndentedOperation = createKindCheckFunction([
3131
]);
3232

3333
export const shouldNotIndent = (
34-
node: StrictAstNode,
35-
path: AstPath<StrictAstNode>,
34+
node: PrintableNode,
35+
path: AstPath<PrintableNode>,
3636
index: number
3737
): boolean =>
3838
isStatementWithoutIndentedOperation(node) ||
@@ -41,7 +41,7 @@ export const shouldNotIndent = (
4141

4242
export const binaryIndentRulesBuilder =
4343
(shouldIndent: (node: BinaryOperation) => boolean) =>
44-
(node: BinaryOperation, path: AstPath<StrictAstNode>) =>
44+
(node: BinaryOperation, path: AstPath<PrintableNode>) =>
4545
(document: Doc): Doc => {
4646
for (let i = 1, current = node, parent; ; i++, current = parent) {
4747
parent = path.getNode(i)!;
@@ -57,7 +57,7 @@ export const printBinaryOperation = (
5757
shouldGroupAndIndent: (node: BinaryOperation) => boolean
5858
): ((
5959
node: BinaryOperation,
60-
path: AstPath<StrictAstNode>,
60+
path: AstPath<PrintableNode>,
6161
print: PrintFunction,
6262
options: ParserOptions<AstNode>
6363
) => Doc) =>

src/slang-printers/print-comments.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { AstPath, Doc, ParserOptions } from 'prettier';
77
import type {
88
AstNode,
99
Comment,
10-
StrictAstNode
10+
PrintableNode
1111
} from '../slang-nodes/types.d.ts';
1212

1313
const { hardline, line } = doc.builders;
@@ -17,8 +17,8 @@ function isPrintable(comment: Comment): boolean {
1717
}
1818

1919
export function printComments(
20-
node: StrictAstNode,
21-
path: AstPath<StrictAstNode>,
20+
node: PrintableNode,
21+
path: AstPath<PrintableNode>,
2222
options: ParserOptions<AstNode>
2323
): Doc[] {
2424
const lastPrintableIndex = (node.comments ?? []).findLastIndex(isPrintable);

0 commit comments

Comments
 (0)