-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathContractDefinition.ts
More file actions
69 lines (56 loc) · 2.12 KB
/
ContractDefinition.ts
File metadata and controls
69 lines (56 loc) · 2.12 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
import { doc } from 'prettier';
import { satisfies } from 'semver';
import { NonterminalKind } from '@nomicfoundation/slang/cst';
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 { AstPath, Doc, ParserOptions } from 'prettier';
import type { AstNode } from './types.d.ts';
import type { PrintFunction } 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, options: ParserOptions<AstNode>) {
super(ast);
this.abstractKeyword = ast.abstractKeyword?.unparse();
this.name = new TerminalNode(ast.name);
this.specifiers = new ContractSpecifiers(ast.specifiers, options);
this.members = new ContractMembers(ast.members, options);
this.updateMetadata(this.specifiers, this.members);
this.cleanModifierInvocationArguments(options);
}
cleanModifierInvocationArguments(options: ParserOptions<AstNode>): void {
// Older versions of Solidity defined a constructor as a function having
// the same name as the contract.
if (!satisfies(options.compiler, '>=0.5.0')) {
for (const { variant } of this.members.items) {
if (
variant.kind === NonterminalKind.FunctionDefinition &&
variant.name.variant.value !== this.name.value
) {
variant.cleanModifierInvocationArguments();
}
}
}
}
print(path: AstPath<ContractDefinition>, print: PrintFunction): Doc {
return [
group([
this.abstractKeyword ? 'abstract ' : '',
'contract ',
path.call(print, 'name'),
path.call(print, 'specifiers'),
this.specifiers.items.length > 0 ? '' : line,
'{'
]),
path.call(print, 'members'),
'}'
];
}
}