-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathVariableDeclarationStatement.ts
More file actions
81 lines (66 loc) · 2.21 KB
/
VariableDeclarationStatement.ts
File metadata and controls
81 lines (66 loc) · 2.21 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
75
76
77
78
79
80
81
import { NonterminalKind } from '@nomicfoundation/slang/cst';
import { doc } from 'prettier';
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
import { VariableDeclarationType } from './VariableDeclarationType.js';
import { StorageLocation } from './StorageLocation.js';
import { Identifier } from './Identifier.js';
import { VariableDeclarationValue } from './VariableDeclarationValue.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, SlangNode } from '../types.d.ts';
const { group, indent, indentIfBreak, line } = doc.builders;
export class VariableDeclarationStatement implements SlangNode {
readonly kind = NonterminalKind.VariableDeclarationStatement;
comments;
loc;
variableType: VariableDeclarationType;
storageLocation?: StorageLocation;
name: Identifier;
value?: VariableDeclarationValue;
constructor(
ast: ast.VariableDeclarationStatement,
options: ParserOptions<AstNode>
) {
let metadata = getNodeMetadata(ast);
this.variableType = new VariableDeclarationType(ast.variableType, options);
if (ast.storageLocation) {
this.storageLocation = new StorageLocation(ast.storageLocation);
}
this.name = new Identifier(ast.name);
if (ast.value) {
this.value = new VariableDeclarationValue(ast.value, options);
}
metadata = updateMetadata(metadata, [
this.variableType,
this.storageLocation,
this.value
]);
this.comments = metadata.comments;
this.loc = metadata.loc;
}
print(
path: AstPath<VariableDeclarationStatement>,
print: PrintFunction
): Doc {
const groupId = Symbol('Slang.VariableDeclarationStatement.variables');
const declarationDoc = group(
[
path.call(print, 'variableType'),
indent([
this.storageLocation
? [line, path.call(print, 'storageLocation')]
: '',
' ',
path.call(print, 'name')
])
],
{ id: groupId }
);
return [
declarationDoc,
indentIfBreak(path.call(print, 'value'), { groupId }),
';'
];
}
}