Skip to content

Commit 4f394dc

Browse files
committed
All features printed.
1 parent ef897e5 commit 4f394dc

22 files changed

Lines changed: 761 additions & 611 deletions

src/nodes/ModifierDefinition.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { doc } from 'prettier';
22
import { printSeparatedList } from '../common/printer-helpers.js';
33

4-
const { group, indent, line } = doc.builders;
4+
const { dedent, group, indent, line } = doc.builders;
55

66
const modifierParameters = (node, path, print) => {
77
if (node.parameters?.length > 0) {
@@ -32,16 +32,22 @@ const override = (node, path, print) => {
3232

3333
const body = (node, path, print) => {
3434
if (!node.body) return ';';
35-
if (node.isVirtual) return group([' ', path.call(print, 'body')]);
36-
return [' ', path.call(print, 'body')];
35+
if (node.isVirtual) return group(path.call(print, 'body'));
36+
return [path.call(print, 'body')];
3737
};
3838

3939
export const ModifierDefinition = {
4040
print: ({ node, path, print }) => [
4141
'modifier ',
4242
node.name,
4343
modifierParameters(node, path, print),
44-
group(indent([virtual(node), override(node, path, print)])),
44+
group(
45+
indent([
46+
virtual(node),
47+
override(node, path, print),
48+
node.body ? dedent(line) : ''
49+
])
50+
),
4551
body(node, path, print)
4652
]
4753
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
2+
import { getNextNonSpaceNonCommentCharacter } from '../../slang-utils/backward-compatibility.js';
3+
import addHubNodeFirstComment from './add-hub-node-first-comment.js';
4+
import addHubNodeLastComment from './add-hub-node-last-comment.js';
5+
6+
import type { HandlerParams } from './types';
7+
8+
export default function handleInterfaceDefinitionComments({
9+
text,
10+
precedingNode,
11+
enclosingNode,
12+
followingNode,
13+
comment
14+
}: HandlerParams): boolean {
15+
if (enclosingNode?.kind !== NonterminalKind.InterfaceDefinition) {
16+
return false;
17+
}
18+
19+
const nextCharacter = getNextNonSpaceNonCommentCharacter(text, comment);
20+
21+
// The comment is at the end of the body of the InterfaceDefinition.
22+
if (precedingNode?.kind === NonterminalKind.InterfaceMembers) {
23+
addHubNodeLastComment(precedingNode, comment);
24+
return true;
25+
}
26+
27+
// The last comments before the body.
28+
if (
29+
nextCharacter === '{' &&
30+
followingNode?.kind === NonterminalKind.InterfaceMembers
31+
) {
32+
addHubNodeFirstComment(followingNode, comment);
33+
return true;
34+
}
35+
36+
return false;
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
2+
import { getNextNonSpaceNonCommentCharacter } from '../../slang-utils/backward-compatibility.js';
3+
import addHubNodeFirstComment from './add-hub-node-first-comment.js';
4+
import addHubNodeLastComment from './add-hub-node-last-comment.js';
5+
6+
import type { HandlerParams } from './types';
7+
8+
export default function handleLibraryDefinitionComments({
9+
text,
10+
precedingNode,
11+
enclosingNode,
12+
followingNode,
13+
comment
14+
}: HandlerParams): boolean {
15+
if (enclosingNode?.kind !== NonterminalKind.LibraryDefinition) {
16+
return false;
17+
}
18+
19+
const nextCharacter = getNextNonSpaceNonCommentCharacter(text, comment);
20+
21+
// The comment is at the end of the body of the ContractDefinition.
22+
if (precedingNode?.kind === NonterminalKind.LibraryMembers) {
23+
addHubNodeLastComment(precedingNode, comment);
24+
return true;
25+
}
26+
27+
// The last comments before the body.
28+
if (
29+
nextCharacter === '{' &&
30+
followingNode?.kind === NonterminalKind.LibraryMembers
31+
) {
32+
addHubNodeFirstComment(followingNode, comment);
33+
return true;
34+
}
35+
36+
return false;
37+
}

src/slang-comments/handlers/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import handleBlockComments from './handle-block-comments.js';
22
import handleContractDefinitionComments from './handle-contract-definition-comments.js';
33
import handleElseBranchComments from './handle-else-branch-comments.js';
44
import handleIfStatementComments from './handle-if-statement-comments.js';
5+
import handleInterfaceDefinitionComments from './handle-interface-definition-comments.js';
6+
import handleLibraryDefinitionComments from './handle-library-definition-comments.js';
57
import handleParametersDeclarationComments from './handle-parameters-declaration-comments.js';
68
import handlePositionalArgumentsDeclarationComments from './handle-positional-arguments-declaration-comments.js';
79
import handleWhileStatementComments from './handle-while-statement-comments.js';
@@ -12,6 +14,8 @@ export default [
1214
handleContractDefinitionComments,
1315
handleElseBranchComments,
1416
handleIfStatementComments,
17+
handleInterfaceDefinitionComments,
18+
handleLibraryDefinitionComments,
1519
handleParametersDeclarationComments,
1620
handlePositionalArgumentsDeclarationComments,
1721
handleWhileStatementComments,

src/slang-nodes/ConstantDefinition.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { TypeName } from './TypeName.js';
44
import { Expression } from './Expression.js';
55

66
import type * as ast from '@nomicfoundation/slang/ast';
7-
import type { /*AstPath,*/ Doc, ParserOptions } from 'prettier';
7+
import type { AstPath, Doc, ParserOptions } from 'prettier';
88
import type { AstNode } from '../slang-nodes';
9-
import type { SlangNode } from '../types';
9+
import type { PrintFunction, SlangNode } from '../types';
1010

1111
export class ConstantDefinition implements SlangNode {
1212
readonly kind = NonterminalKind.ConstantDefinition;
@@ -39,12 +39,12 @@ export class ConstantDefinition implements SlangNode {
3939
this.loc = metadata.loc;
4040
}
4141

42-
// TODO: implement print
43-
print(/*
44-
path: AstPath<ConstantDefinition>,
45-
print: PrintFunction,
46-
options: ParserOptions<AstNode>
47-
*/): Doc {
48-
return ['TODO: ConstantDefinition'];
42+
print(path: AstPath<ConstantDefinition>, print: PrintFunction): Doc {
43+
return [
44+
path.call(print, 'typeName'),
45+
` constant ${this.name} = `,
46+
path.call(print, 'value'),
47+
';'
48+
];
4949
}
5050
}

src/slang-nodes/OverridePaths.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
33
import { IdentifierPath } from './IdentifierPath.js';
44

55
import type * as ast from '@nomicfoundation/slang/ast';
6-
import type { /*AstPath,*/ Doc /*, ParserOptions*/ } from 'prettier';
7-
import type { SlangNode } from '../types';
6+
import type { AstPath, Doc } from 'prettier';
7+
import type { PrintFunction, SlangNode } from '../types';
8+
import { printSeparatedList } from '../slang-printers/print-separated-list.js';
89

910
export class OverridePaths implements SlangNode {
1011
readonly kind = NonterminalKind.OverridePaths;
@@ -32,12 +33,7 @@ export class OverridePaths implements SlangNode {
3233
this.loc = metadata.loc;
3334
}
3435

35-
// TODO: implement print
36-
print(/*
37-
path: AstPath<OverridePaths>,
38-
print: PrintFunction,
39-
options: ParserOptions<AstNode>
40-
*/): Doc {
41-
return ['TODO: OverridePaths'];
36+
print(path: AstPath<OverridePaths>, print: PrintFunction): Doc {
37+
return printSeparatedList(path.map(print, 'items'));
4238
}
4339
}

src/slang-nodes/OverridePathsDeclaration.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
33
import { OverridePaths } from './OverridePaths.js';
44

55
import type * as ast from '@nomicfoundation/slang/ast';
6-
import type { /*AstPath,*/ Doc /*, ParserOptions*/ } from 'prettier';
7-
import type { SlangNode } from '../types';
6+
import type { AstPath, Doc } from 'prettier';
7+
import type { PrintFunction, SlangNode } from '../types';
88

99
export class OverridePathsDeclaration implements SlangNode {
1010
readonly kind = NonterminalKind.OverridePathsDeclaration;
@@ -27,12 +27,7 @@ export class OverridePathsDeclaration implements SlangNode {
2727
this.loc = metadata.loc;
2828
}
2929

30-
// TODO: implement print
31-
print(/*
32-
path: AstPath<OverridePathsDeclaration>,
33-
print: PrintFunction,
34-
options: ParserOptions<AstNode>
35-
*/): Doc {
36-
return ['TODO: OverridePathsDeclaration'];
30+
print(path: AstPath<OverridePathsDeclaration>, print: PrintFunction): Doc {
31+
return ['(', path.call(print, 'paths'), ')'];
3732
}
3833
}

src/slang-nodes/PrefixExpression.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ export class PrefixExpression implements SlangNode {
3333

3434
this.comments = metadata.comments;
3535
this.loc = metadata.loc;
36+
37+
if (this.operator === 'delete') {
38+
this.operator = `${this.operator} `;
39+
}
3640
}
3741

3842
print(path: AstPath<PrefixExpression>, print: PrintFunction): Doc {

src/slang-nodes/StateVariableAttributes.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
2+
import { doc } from 'prettier';
23
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
4+
import { sortFunctionAttributes } from '../slang-utils/sort-function-attributes.js';
35
import { StateVariableAttribute } from './StateVariableAttribute.js';
46

57
import type * as ast from '@nomicfoundation/slang/ast';
68
import type { AstPath, Doc } from 'prettier';
79
import type { PrintFunction, SlangNode } from '../types';
810

11+
const { line } = doc.builders;
12+
913
export class StateVariableAttributes implements SlangNode {
1014
readonly kind = NonterminalKind.StateVariableAttributes;
1115

@@ -27,11 +31,13 @@ export class StateVariableAttributes implements SlangNode {
2731

2832
this.comments = metadata.comments;
2933
this.loc = metadata.loc;
34+
35+
this.items = this.items.sort(sortFunctionAttributes);
3036
}
3137

3238
print(path: AstPath<StateVariableAttributes>, print: PrintFunction): Doc {
3339
return this.items.length
34-
? path.map(print, 'items').map((item) => [' ', item])
40+
? path.map(print, 'items').map((item) => [line, item])
3541
: '';
3642
}
3743
}

src/slang-nodes/StateVariableDefinition.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
2+
import { doc } from 'prettier';
23
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
34
import { TypeName } from './TypeName.js';
45
import { StateVariableAttributes } from './StateVariableAttributes.js';
@@ -9,6 +10,8 @@ import type { AstPath, Doc, ParserOptions } from 'prettier';
910
import type { AstNode } from '../slang-nodes';
1011
import type { PrintFunction, SlangNode } from '../types';
1112

13+
const { group, indent, indentIfBreak } = doc.builders;
14+
1215
export class StateVariableDefinition implements SlangNode {
1316
readonly kind = NonterminalKind.StateVariableDefinition;
1417

@@ -50,11 +53,19 @@ export class StateVariableDefinition implements SlangNode {
5053
}
5154

5255
print(path: AstPath<StateVariableDefinition>, print: PrintFunction): Doc {
56+
const attributesDoc = group(indent(path.call(print, 'attributes')), {
57+
id: Symbol('Slang.StateVariableDefinition.attributes')
58+
});
59+
5360
return [
5461
path.call(print, 'typeName'),
55-
path.call(print, 'attributes'),
62+
attributesDoc,
5663
` ${this.name}`,
57-
this.value ? path.call(print, 'value') : '',
64+
this.value
65+
? indentIfBreak(path.call(print, 'value'), {
66+
groupId: attributesDoc.id!
67+
})
68+
: '',
5869
';'
5970
];
6071
}

0 commit comments

Comments
 (0)