Skip to content

Commit a300316

Browse files
committed
delegating printing nodes with prettier-ignore to prettier
1 parent f860d65 commit a300316

3 files changed

Lines changed: 23 additions & 59 deletions

File tree

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import slangParse from './slangSolidityParser.js';
99
import slangPrint from './slangPrinter.js';
1010
import { isBlockComment, isComment } from './slang-utils/is-comment.js';
1111
import { locEnd, locStart } from './slang-utils/loc.js';
12+
import { hasPrettierIgnore } from './slang-utils/has-prettier-ignore.js';
1213

1314
import type {
1415
Parser,
@@ -78,6 +79,7 @@ const slangPrinter: Printer<AstNode> = {
7879
isBlockComment,
7980
massageAstNode,
8081
print: slangPrint,
82+
hasPrettierIgnore,
8183
printComment
8284
};
8385

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { isBlockComment, isComment } from './is-comment.js';
2+
3+
import type { AstPath } from 'prettier';
4+
import type { AstNode } from '../slang-nodes/types.js';
5+
6+
export function hasPrettierIgnore({ node }: AstPath<AstNode>): boolean {
7+
if (typeof node === 'string' || node === undefined || isComment(node))
8+
return false;
9+
10+
// Prettier sets SourceUnit's comments to undefined after assigning comments
11+
// to each node.
12+
return Boolean(
13+
node.comments?.some(
14+
(comment) =>
15+
comment.value
16+
.slice(2, isBlockComment(comment) ? -2 : undefined)
17+
.trim() === 'prettier-ignore'
18+
)
19+
);
20+
}

src/slangPrinter.ts

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import { isBlockComment } from './slang-utils/is-comment.js';
2-
import { locEnd, locStart } from './slang-utils/loc.js';
3-
41
import type { AstPath, Doc, ParserOptions } from 'prettier';
52
import type {
63
AstNode,
@@ -9,72 +6,17 @@ import type {
96
} from './slang-nodes/types.d.ts';
107
import type { PrintFunction } from './types.d.ts';
118

12-
function hasNodeIgnoreComment({ comments }: StrictAstNode): boolean {
13-
// Prettier sets SourceUnit's comments to undefined after assigning comments
14-
// to each node.
15-
return Boolean(
16-
comments?.some(
17-
(comment) =>
18-
comment.value
19-
.slice(2, isBlockComment(comment) ? -2 : undefined)
20-
.trim() === 'prettier-ignore'
21-
)
22-
);
23-
}
24-
25-
function ignoreComments(path: AstPath<StrictAstNode>): void {
26-
const node = path.node;
27-
// We ignore anything that is not an object
28-
if (node === null || typeof node !== 'object') return;
29-
30-
let key: keyof StrictAstNode;
31-
for (key in node) {
32-
switch (key) {
33-
// We ignore `kind` and `loc` since these are added by the parser.
34-
// `updateMetadata` is an internal function.
35-
case 'kind':
36-
case 'loc':
37-
case 'updateMetadata':
38-
break;
39-
// The key `comments` will contain every comment for this node.
40-
case 'comments':
41-
if (node.comments !== undefined) {
42-
path.each(({ node }) => (node.printed = true), key);
43-
}
44-
break;
45-
default:
46-
// If the value for that key is an Array or an Object we go deeper.
47-
const childNode = node[key];
48-
if (typeof childNode === 'object') {
49-
if (Array.isArray(childNode)) {
50-
path.each(ignoreComments, key);
51-
break;
52-
}
53-
path.call(ignoreComments, key);
54-
}
55-
}
56-
}
57-
}
58-
599
// Nodes take care of undefined and string properties so we can restrict path
6010
// to AstPath<StrictAstNode>
6111
function genericPrint(
6212
path: AstPath<Exclude<StrictAstNode, StrictPolymorphicNode>>,
6313
options: ParserOptions<AstNode>,
6414
print: PrintFunction
6515
): Doc {
66-
const node = path.node;
67-
68-
if (hasNodeIgnoreComment(node)) {
69-
ignoreComments(path);
70-
71-
return options.originalText.slice(locStart(node), locEnd(node));
72-
}
73-
7416
// Since each node has a print function with a specific AstPath, the union of
7517
// all nodes into AstNode creates a print function with an AstPath of the
7618
// intersection of all nodes. This forces us to cast this with a never type.
77-
return node.print(path as AstPath<never>, print, options);
19+
return path.node.print(path as AstPath<never>, print, options);
7820
}
7921

8022
export default genericPrint;

0 commit comments

Comments
 (0)