Skip to content

Commit 4ba5e73

Browse files
committed
removing undefined from AstPath<PrintableNode | undefined> since it's not required
1 parent 0dd8d24 commit 4ba5e73

8 files changed

Lines changed: 56 additions & 43 deletions

File tree

src/index.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import slangPrint from './slangPrinter.js';
1010
import { isBlockComment, isComment } from './slang-utils/is-comment.js';
1111
import { locEnd, locStart } from './slang-utils/loc.js';
1212
import { hasPrettierIgnore } from './slang-utils/has-prettier-ignore.js';
13+
import { getVisitorKeys } from './slang-utils/get-visitor-keys.js';
1314

1415
import type {
1516
AstPath,
@@ -57,10 +58,9 @@ const parsers = {
5758

5859
const antlrCanAttachComment = ({ type }: { type: string }): boolean =>
5960
typeof type === 'string' && type !== 'BlockComment' && type !== 'LineComment';
60-
const canAttachComment = (node: PrintableNode | undefined): boolean =>
61-
node !== undefined &&
62-
node.kind && // Make sure it's not Location
63-
!isComment(node);
61+
const canAttachComment = (node: PrintableNode): boolean =>
62+
// Make sure it's not Location
63+
node.kind && !isComment(node);
6464

6565
// https://prettier.io/docs/en/plugins.html#printers
6666
const antlrPrinter = {
@@ -75,18 +75,19 @@ const antlrPrinter = {
7575
print: antlrPrint,
7676
printComment: comments.printComment
7777
};
78-
const slangPrinter: Printer<PrintableNode | undefined> = {
78+
const slangPrinter: Printer<PrintableNode> = {
7979
canAttachComment,
8080
handleComments,
8181
isBlockComment,
8282
massageAstNode,
8383
print: slangPrint as (
84-
path: AstPath<PrintableNode | undefined>,
85-
options: ParserOptions<PrintableNode | undefined>,
86-
print: (path: AstPath<PrintableNode | undefined>) => Doc,
84+
path: AstPath<PrintableNode>,
85+
options: ParserOptions<PrintableNode>,
86+
print: (path: AstPath<PrintableNode>) => Doc,
8787
args?: unknown
8888
) => Doc,
8989
hasPrettierIgnore,
90+
getVisitorKeys,
9091
printComment
9192
};
9293

src/slang-comments/printer.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ import { isComment } from '../slang-utils/is-comment.js';
33
import type { AstPath, Doc } from 'prettier';
44
import type { PrintableNode } from '../slang-nodes/types.d.ts';
55

6-
export function printComment({
7-
node: comment
8-
}: AstPath<PrintableNode | undefined>): Doc {
6+
export function printComment({ node: comment }: AstPath<PrintableNode>): Doc {
97
if (isComment(comment)) {
108
return comment.print();
119
}
Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { doc, util } from 'prettier';
22
import { printComment } from '../slang-comments/printer.js';
3-
import { joinExisting } from '../slang-utils/join-existing.js';
43
import { locEnd } from '../slang-utils/loc.js';
54

65
import type { AstPath, Doc, ParserOptions } from 'prettier';
76
import type { Comment, PrintableNode } from '../slang-nodes/types.d.ts';
87

9-
const { hardline, line } = doc.builders;
8+
const { hardline } = doc.builders;
109

1110
function isPrintable(comment: Comment): boolean {
1211
return !comment.trailing && !comment.leading && !comment.printed;
@@ -21,20 +20,21 @@ export function printComments(
2120
if (lastPrintableIndex === -1) {
2221
return [];
2322
}
24-
return joinExisting(
25-
line,
26-
path.map(({ node: comment }, index) => {
27-
if (!isPrintable(comment)) {
28-
return '';
29-
}
30-
comment.printed = true;
31-
return [
32-
printComment(path),
33-
index !== lastPrintableIndex &&
34-
util.isNextLineEmpty(options.originalText, locEnd(comment))
35-
? hardline
36-
: ''
37-
];
38-
}, 'comments')
39-
);
23+
return path.map(({ node: comment }, index) => {
24+
if (!isPrintable(comment)) {
25+
return '';
26+
}
27+
comment.printed = true;
28+
return [
29+
printComment(path),
30+
index !== lastPrintableIndex
31+
? [
32+
hardline,
33+
util.isNextLineEmpty(options.originalText, locEnd(comment))
34+
? hardline
35+
: ''
36+
]
37+
: ''
38+
];
39+
}, 'comments');
4040
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { PrintableNode } from '../slang-nodes/types.js';
2+
3+
const ignoredKeys = new Set([
4+
'kind',
5+
'loc',
6+
'comments',
7+
'print',
8+
'isEmpty',
9+
'updateMetadata',
10+
'cleanModifierInvocationArguments',
11+
'getSingleExpression'
12+
]);
13+
14+
export function getVisitorKeys(
15+
node: PrintableNode,
16+
nonTraversableKeys: Set<string>
17+
): string[] {
18+
return Object.keys(node).filter(
19+
(key) => !nonTraversableKeys.has(key) && !ignoredKeys.has(key)
20+
);
21+
}

src/slang-utils/has-prettier-ignore.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ import { isBlockComment, isComment } from './is-comment.js';
33
import type { AstPath } from 'prettier';
44
import type { PrintableNode } from '../slang-nodes/types.js';
55

6-
export function hasPrettierIgnore({
7-
node
8-
}: AstPath<PrintableNode | undefined>): boolean {
9-
if (node === undefined || isComment(node)) return false;
6+
export function hasPrettierIgnore({ node }: AstPath<PrintableNode>): boolean {
7+
if (isComment(node)) return false;
108

119
// Prettier sets SourceUnit's comments to undefined after assigning comments
1210
// to each node.

src/slang-utils/is-comment.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@ import type {
1111
export const isBlockComment = createKindCheckFunction([
1212
TerminalKind.MultiLineComment,
1313
TerminalKind.MultiLineNatSpecComment
14-
]) as (
15-
node: PrintableNode | Comment | Node | undefined
16-
) => node is BlockComment;
14+
]) as (node: PrintableNode | Comment | Node) => node is BlockComment;
1715

1816
export const isComment = createKindCheckFunction([
1917
TerminalKind.MultiLineComment,
2018
TerminalKind.MultiLineNatSpecComment,
2119
TerminalKind.SingleLineComment,
2220
TerminalKind.SingleLineNatSpecComment
23-
]) as (node: PrintableNode | Comment | Node | undefined) => node is Comment;
21+
]) as (node: PrintableNode | Comment | Node) => node is Comment;

src/slangSolidityParser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// https://prettier.io/docs/en/plugins.html#parsers
22
import { SourceUnit as SlangSourceUnit } from '@nomicfoundation/slang/ast';
33
import { createParser } from './slang-utils/create-parser.js';
4+
import { locStart } from './slang-utils/loc.js';
45
import { SourceUnit } from './slang-nodes/SourceUnit.js';
56

67
import type { ParserOptions } from 'prettier';
@@ -23,6 +24,6 @@ export default function parse(
2324

2425
// Because of comments being extracted like a Russian doll, the order needs
2526
// to be fixed at the end.
26-
parsed.comments = comments.sort((a, b) => a.loc.start - b.loc.start);
27+
parsed.comments = comments.sort((a, b) => locStart(a) - locStart(b));
2728
return parsed;
2829
}

src/types.d.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@ interface AstLocation extends Location {
2525
}
2626

2727
type PrintFunction = (
28-
selector?:
29-
| string
30-
| number
31-
| (string | number)[]
32-
| AstPath<PrintableNode | undefined>
28+
selector?: string | number | (string | number)[] | AstPath<PrintableNode>
3329
) => Doc;
3430

3531
// This the union of all the types in the namespace `ast`.

0 commit comments

Comments
 (0)