-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathContractDefinition.ts
More file actions
74 lines (62 loc) · 2.14 KB
/
ContractDefinition.ts
File metadata and controls
74 lines (62 loc) · 2.14 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
69
70
71
72
73
74
import { NonterminalKind } from '@nomicfoundation/slang/cst';
import { doc } from 'prettier';
import { satisfies } from 'semver';
import { SlangNode } from './SlangNode.js';
import { TerminalNode } from './TerminalNode.js';
import { ContractSpecifiers } from './ContractSpecifiers.js';
import { ContractMembers } from './ContractMembers.js';
import type * as ast from '@nomicfoundation/slang/ast';
import type { Doc, ParserOptions } from 'prettier';
import type { CollectedMetadata, PrintFunction } from '../types.d.ts';
import type { PrintableNode } from './types.d.ts';
const { group, line } = doc.builders;
export class ContractDefinition extends SlangNode {
readonly kind = NonterminalKind.ContractDefinition;
abstractKeyword?: string;
name: TerminalNode;
specifiers: ContractSpecifiers;
members: ContractMembers;
constructor(
ast: ast.ContractDefinition,
collected: CollectedMetadata,
options: ParserOptions<PrintableNode>
) {
super(ast, collected);
this.abstractKeyword = ast.abstractKeyword?.unparse();
this.name = new TerminalNode(ast.name, collected);
this.specifiers = new ContractSpecifiers(
ast.specifiers,
collected,
options
);
this.members = new ContractMembers(ast.members, collected, options);
this.updateMetadata(this.specifiers, this.members);
// Older versions of Solidity defined a constructor as a function having
// the same name as the contract.
// So we delegate to the parents the responsibility of cleaning the
// arguments of modifier invocations.
if (!satisfies(options.compiler, '>=0.5.0')) {
for (const member of this.members.items) {
if (
member.kind === NonterminalKind.FunctionDefinition &&
member.name.value !== this.name.value
) {
member.cleanModifierInvocationArguments();
}
}
}
}
print(print: PrintFunction): Doc {
return [
`${this.abstractKeyword ? 'abstract ' : ''}contract `,
group([
print('name'),
print('specifiers'),
this.specifiers.items.length > 0 ? '' : line,
'{'
]),
print('members'),
'}'
];
}
}