Skip to content

Commit a7e0dcb

Browse files
committed
avoid using the ast more than once since we can't ensure the pointer is preserved.
1 parent 209064d commit a7e0dcb

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

src/clean.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const ignoredProperties = new Set([
55
'comments',
66
// this function is defined at constructor time so it won't pass AST
77
// comparisons.
8-
'cleanModifierInvocationArguments'
8+
'isEmpty'
99
]);
1010
// eslint-disable-next-line @typescript-eslint/no-empty-function
1111
function clean(/* ast, newObj, parent */): void {}

src/slang-nodes/ModifierInvocation.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { NonterminalKind } from '@nomicfoundation/slang/cst';
2-
import { isBlockComment } from '../slang-utils/is-comment.js';
32
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
43
import { IdentifierPath } from './IdentifierPath.js';
54
import { ArgumentsDeclaration } from './ArgumentsDeclaration.js';
@@ -16,8 +15,6 @@ export class ModifierInvocation implements SlangNode {
1615

1716
loc;
1817

19-
cleanModifierInvocationArguments;
20-
2118
name: IdentifierPath;
2219

2320
arguments?: ArgumentsDeclaration;
@@ -43,20 +40,17 @@ export class ModifierInvocation implements SlangNode {
4340

4441
this.comments = metadata.comments;
4542
this.loc = metadata.loc;
43+
}
4644

47-
this.cleanModifierInvocationArguments = (): void => {
48-
if (
49-
this.arguments &&
50-
this.arguments.variant.kind ===
51-
NonterminalKind.PositionalArgumentsDeclaration &&
52-
this.arguments.variant.arguments.items.length === 0 && // no arguments
53-
!ast.arguments!.variant.cst.children.some((child) =>
54-
isBlockComment(child.node)
55-
) // no comments, at this point we need to check the CST
56-
) {
57-
delete this.arguments;
58-
}
59-
};
45+
cleanModifierInvocationArguments(): void {
46+
if (
47+
this.arguments &&
48+
this.arguments.variant.kind ===
49+
NonterminalKind.PositionalArgumentsDeclaration &&
50+
this.arguments.variant.isEmpty()
51+
) {
52+
delete this.arguments;
53+
}
6054
}
6155

6256
print(path: AstPath<ModifierInvocation>, print: PrintFunction): Doc {

src/slang-nodes/PositionalArgumentsDeclaration.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { NonterminalKind } from '@nomicfoundation/slang/cst';
22
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
3+
import { isBlockComment } from '../slang-utils/is-comment.js';
34
import { PositionalArguments } from './PositionalArguments.js';
45

56
import type * as ast from '@nomicfoundation/slang/ast';
@@ -14,6 +15,8 @@ export class PositionalArgumentsDeclaration implements SlangNode {
1415

1516
loc;
1617

18+
isEmpty;
19+
1720
arguments: PositionalArguments;
1821

1922
constructor(
@@ -34,6 +37,14 @@ export class PositionalArgumentsDeclaration implements SlangNode {
3437

3538
this.comments = metadata.comments;
3639
this.loc = metadata.loc;
40+
41+
// We need to check the comments at this point because they will be removed
42+
// from this node into the root node.
43+
const empty =
44+
this.arguments.items.length === 0 && // no arguments
45+
!this.comments.some((comment) => isBlockComment(comment)); // no block comments
46+
47+
this.isEmpty = (): boolean => empty;
3748
}
3849

3950
print(

0 commit comments

Comments
 (0)