Skip to content

Commit 22edc8e

Browse files
committed
Comments running closer to prettier for edge cases
1 parent d3f7141 commit 22edc8e

File tree

9 files changed

+59
-61
lines changed

9 files changed

+59
-61
lines changed

src/slang-comments/handlers/handle-contract-definition-comments.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
22
import { util } from 'prettier';
33
import { getNextNonSpaceNonCommentCharacter } from '../../slang-utils/backward-compatibility.js';
4-
import addCollectionNodeFirstComment from './add-collection-node-first-comment.js';
54
import addCollectionNodeLastComment from './add-collection-node-last-comment.js';
65

76
import type { HandlerParams } from './types';
@@ -52,13 +51,6 @@ export default function handleContractDefinitionComments({
5251
);
5352
return true;
5453
}
55-
56-
// If there's no InheritanceSpecifier, the comment before the body is
57-
// assumed to be intended at the beginning of the body.
58-
if (followingNode?.kind === NonterminalKind.ContractMembers) {
59-
addCollectionNodeFirstComment(followingNode, comment);
60-
return true;
61-
}
6254
}
6355

6456
return false;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
2+
import { util } from 'prettier';
3+
import { getNextNonSpaceNonCommentCharacter } from '../../slang-utils/backward-compatibility.js';
4+
import addCollectionNodeFirstComment from './add-collection-node-first-comment.js';
5+
6+
import type { HandlerParams } from './types';
7+
8+
const { addTrailingComment } = util;
9+
10+
export default function handleModifierInvocationComments({
11+
text,
12+
precedingNode,
13+
enclosingNode,
14+
followingNode,
15+
comment
16+
}: HandlerParams): boolean {
17+
if (enclosingNode?.kind !== NonterminalKind.ModifierInvocation) {
18+
return false;
19+
}
20+
21+
const nextCharacter = getNextNonSpaceNonCommentCharacter(text, comment);
22+
23+
// The last comments before the body.
24+
if (
25+
precedingNode?.kind === NonterminalKind.IdentifierPath &&
26+
nextCharacter === '(' &&
27+
followingNode?.kind === NonterminalKind.ArgumentsDeclaration &&
28+
followingNode.variant.kind ===
29+
NonterminalKind.PositionalArgumentsDeclaration
30+
) {
31+
if (followingNode.variant.arguments.items.length === 0) {
32+
addTrailingComment(enclosingNode, comment);
33+
} else {
34+
addCollectionNodeFirstComment(followingNode.variant.arguments, comment);
35+
}
36+
return true;
37+
}
38+
39+
return false;
40+
}

src/slang-comments/handlers/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import handleElseBranchComments from './handle-else-branch-comments.js';
44
import handleIfStatementComments from './handle-if-statement-comments.js';
55
import handleInterfaceDefinitionComments from './handle-interface-definition-comments.js';
66
import handleLibraryDefinitionComments from './handle-library-definition-comments.js';
7+
import handleModifierInvocationComments from './handle-modifier-invocation-comments.js';
78
import handleParametersDeclarationComments from './handle-parameters-declaration-comments.js';
89
import handlePositionalArgumentsDeclarationComments from './handle-positional-arguments-declaration-comments.js';
910
import handleWhileStatementComments from './handle-while-statement-comments.js';
@@ -16,6 +17,7 @@ export default [
1617
handleIfStatementComments,
1718
handleInterfaceDefinitionComments,
1819
handleLibraryDefinitionComments,
20+
handleModifierInvocationComments,
1921
handleParametersDeclarationComments,
2022
handlePositionalArgumentsDeclarationComments,
2123
handleWhileStatementComments,

src/slang-nodes/ModifierInvocation.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
2-
import { isComment } from '../slang-utils/is-comment.js';
2+
import { isBlockComment } from '../slang-utils/is-comment.js';
33
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
44
import { IdentifierPath } from './IdentifierPath.js';
55
import { ArgumentsDeclaration } from './ArgumentsDeclaration.js';
@@ -50,7 +50,9 @@ export class ModifierInvocation implements SlangNode {
5050
this.arguments.variant.kind ===
5151
NonterminalKind.PositionalArgumentsDeclaration &&
5252
this.arguments.variant.arguments.items.length === 0 && // no arguments
53-
!ast.arguments!.variant.cst.children().some((child) => isComment(child)) // no comments, at this point we need to check the CST
53+
!ast
54+
.arguments!.variant.cst.children()
55+
.some((child) => isBlockComment(child)) // no comments, at this point we need to check the CST
5456
) {
5557
delete this.arguments;
5658
}

src/slang-nodes/SourceUnit.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ export class SourceUnit implements SlangNode {
3131

3232
metadata = updateMetadata(metadata, [this.members]);
3333

34-
this.comments = metadata.comments;
34+
// Because of comments being extracted like a russian doll, the order needs
35+
// to be fixed at the end.
36+
this.comments = metadata.comments.sort((a, b) => a.loc.start - b.loc.start);
3537
this.loc = metadata.loc;
3638
}
3739

src/slang-utils/is-comment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { AstNode, BlockComment, Comment } from '../slang-nodes';
77
export const isBlockComment = createKindCheckFunction([
88
TerminalKind.MultiLineComment,
99
TerminalKind.MultiLineNatSpecComment
10-
]) as (node: AstNode) => node is BlockComment;
10+
]) as (node: AstNode | Node) => node is BlockComment;
1111

1212
export const isComment = createKindCheckFunction([
1313
TerminalKind.MultiLineComment,

tests/config/run-format-test.js

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,6 @@ const unstableAstTests = new Map(
4040
}),
4141
);
4242

43-
const testsWithSlang = new Map(
44-
[
45-
// "Comments/Comments.sol", // TODO: finish Comments
46-
].map((fixture) => {
47-
const [file, testSlang = () => true] = Array.isArray(fixture)
48-
? fixture
49-
: [fixture];
50-
return [path.join(__dirname, "../format/", file), testSlang];
51-
}),
52-
);
53-
5443
const testsWithAstChanges = new Map(
5544
[
5645
"Parentheses/AddNoParentheses.sol",
@@ -95,16 +84,6 @@ const isAstUnstable = (filename, options) => {
9584
return testFunction(options);
9685
};
9786

98-
const shouldTestSlang = (filename, options) => {
99-
const testFunction = testsWithSlang.get(filename);
100-
101-
if (!testFunction) {
102-
return false;
103-
}
104-
105-
return testFunction(options);
106-
};
107-
10887
const shouldCompareBytecode = (filename, options) => {
10988
const testFunction = testsWithAstChanges.get(filename);
11089

@@ -309,22 +288,6 @@ async function runTest({
309288
}),
310289
).toMatchSnapshot();
311290

312-
if (shouldTestSlang(filename, formatOptions)) {
313-
const { input, output } = formatResult;
314-
const slangOptions = {
315-
...formatOptions,
316-
parser: "slang",
317-
};
318-
console.log(filename);
319-
const prettier = await getPrettier();
320-
const slangOutput = await prettier.format(input, slangOptions);
321-
322-
// const slangOutput2 = await prettier.format(output, slangOptions);
323-
324-
expect(slangOutput).toEqual(output);
325-
// expect(slangOutput2).toEqual(output);
326-
}
327-
328291
if (!FULL_TEST) {
329292
return;
330293
}

tests/format/Comments/__snapshots__/format.test.js.snap

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
exports[`Comments.sol - {"compiler":"0.4.24"} format 1`] = `
44
====================================options=====================================
55
compiler: "0.4.24"
6-
parsers: ["solidity-parse"]
6+
parsers: ["slang-solidity"]
77
printWidth: 80
88
| printWidth
99
=====================================input======================================
@@ -207,8 +207,7 @@ contract Comments3 is
207207
{
208208
// solhint-disable-previous-line no-empty-blocks
209209
210-
function someFunction() {} /*1*/
211-
/*2
210+
function someFunction() {} /*1*/ /*2
212211
*/
213212
}
214213
@@ -223,7 +222,7 @@ contract Comments4 is
223222
// solhint-disable-previous-line no-empty-blocks
224223
}
225224
226-
/*nice name*/ contract Comments5 {
225+
contract Comments5 /*nice name*/ {
227226
// solhint-disable-previous-line no-empty-blocks
228227
}
229228
@@ -289,7 +288,7 @@ interface Comments10 {
289288
// the first value
290289
// the second value
291290
// the lats value
292-
)/* comment outside the parameters */ external;
291+
) /* comment outside the parameters */ external;
293292
294293
function someOtherFunction(/* checking for Block comment */) external;
295294
}
@@ -319,14 +318,12 @@ contract Comments13 {
319318
function commentInModifierInvocation()
320319
external
321320
// comment 1
322-
// comment 2
323-
// comment 3
324-
modifier1 // comment 4
321+
modifier1 // comment 2
322+
// comment 3 // comment 4
325323
// comment 5
326-
// comment 6
327-
modifier2(/* comment 7 */) // comment 8
328-
// comment 9
324+
modifier2(/* comment 7 */) // comment 6 // comment 8
329325
modifier3(
326+
// comment 9
330327
// comment 10
331328
param1 // comment 11
332329
// comment 12
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
runFormatTest(import.meta, ['solidity-parse'], { compiler: '0.4.24' });
1+
runFormatTest(import.meta, ['slang-solidity'], { compiler: '0.4.24' });

0 commit comments

Comments
 (0)