-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathhandle-contract-definition-comments.ts
More file actions
68 lines (60 loc) · 2.11 KB
/
handle-contract-definition-comments.ts
File metadata and controls
68 lines (60 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { NonterminalKind } from '@nomicfoundation/slang/cst';
import { util } from 'prettier';
import { locEnd } from '../../slang-utils/loc.js';
import addCollectionNodeLastComment from './add-collection-node-last-comment.js';
import type { HandlerParams } from './types.d.ts';
const { addLeadingComment, addTrailingComment } = util;
export default function handleContractDefinitionComments({
text,
precedingNode,
enclosingNode,
followingNode,
comment
}: HandlerParams): boolean {
if (enclosingNode?.kind !== NonterminalKind.ContractDefinition) {
return false;
}
const nextCharacter = util.getNextNonSpaceNonCommentCharacter(
text,
locEnd(comment)
);
// Everything before the ContractSpecifiers is pushed onto the beginning of
// the ContractDefinition.
if (
followingNode?.kind === NonterminalKind.ContractSpecifiers ||
(followingNode?.kind === NonterminalKind.ContractMembers &&
nextCharacter !== '{')
) {
addLeadingComment(enclosingNode, comment);
return true;
}
// The comment is at the end of the body of the ContractDefinition.
if (precedingNode?.kind === NonterminalKind.ContractMembers) {
addCollectionNodeLastComment(precedingNode, comment);
return true;
}
// The last comments before the body.
if (nextCharacter === '{') {
if (precedingNode?.kind === NonterminalKind.ContractSpecifiers) {
if (precedingNode.items.length === 0) {
addTrailingComment(precedingNode, comment);
return true;
}
const lastContractSpecifier =
precedingNode.items[precedingNode.items.length - 1].variant;
// If the last ContractSpecifier's an InheritanceSpecifier, the comment
// is appended to the last InheritanceType.
if (lastContractSpecifier.kind === NonterminalKind.InheritanceSpecifier) {
addCollectionNodeLastComment(lastContractSpecifier.types, comment);
return true;
}
if (
lastContractSpecifier.kind === NonterminalKind.StorageLayoutSpecifier
) {
addTrailingComment(lastContractSpecifier.expression, comment);
return true;
}
}
}
return false;
}