-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathContractSpecifiers.ts
More file actions
52 lines (38 loc) · 1.71 KB
/
ContractSpecifiers.ts
File metadata and controls
52 lines (38 loc) · 1.71 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
import { doc } from 'prettier';
import { NonterminalKind } from '@nomicfoundation/slang/cst';
import { sortContractSpecifiers } from '../slang-utils/sort-contract-specifiers.js';
import { printSeparatedList } from '../slang-printers/print-separated-list.js';
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
import { ContractSpecifier } from './ContractSpecifier.js';
import type * as ast from '@nomicfoundation/slang/ast';
import type { AstPath, Doc, ParserOptions } from 'prettier';
import type { AstNode } from './types.js';
import type { PrintFunction, SlangNode } from '../types.js';
const { group, ifBreak, line, softline } = doc.builders;
export class ContractSpecifiers implements SlangNode {
readonly kind = NonterminalKind.ContractSpecifiers;
comments;
loc;
items: ContractSpecifier[];
constructor(ast: ast.ContractSpecifiers, options: ParserOptions<AstNode>) {
let metadata = getNodeMetadata(ast, true);
this.items = ast.items.map((item) => new ContractSpecifier(item, options));
metadata = updateMetadata(metadata, [this.items]);
this.comments = metadata.comments;
this.loc = metadata.loc;
this.items = this.items.sort(sortContractSpecifiers);
}
print(path: AstPath<ContractSpecifiers>, print: PrintFunction): Doc {
if (this.items.length === 0) return '';
const [specifier1, specifier2] = path.map(print, 'items');
if (typeof specifier2 === 'undefined') return [' ', specifier1];
const groupId = Symbol('Slang.ContractSpecifiers.inheritance');
return printSeparatedList(
[group(specifier1, { id: groupId }), specifier2],
{
firstSeparator: line,
separator: ifBreak('', softline, { groupId })
}
);
}
}