Skip to content

Commit 530d187

Browse files
authored
Using node instead of getNode() (#1167)
* use the node and grandparent getters instead of getNode() and getNode(2) * small refactor * using destructuring when possible * `i` should start at 2 instead of 0 because it's always used as `i + 2` * small optimisations when we only need the destructured `node` * variable is only used within the for loop * more accurate type to avoid type assertion and destructure * access the variant directly in the for..of loops * since using the constant `groupId` there is no need for the group to be stored in a variable and can go directly in the resulting document * other destructuring and variable caching that where missing * using existing helper * reordering import * cleaning up extra check for existence of variable * missing types annotations * fixing some file extensions * `offset` by definition equals to `initialOffset + parent.textLength.utf16` after having collected the `textLength.utf16` of all the `children` * only check once for the value of a boolean * using an actual `for...in` loop instead of storing the keys in an array and then using a `for...of` loop * null check is not needed * undo the destructuring of single variable that did not improve the filesize
1 parent 90fa4c0 commit 530d187

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+223
-261
lines changed

src/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,8 @@ const parsers = {
5151
[antlrParserId]: antlrParser
5252
};
5353

54-
const antlrCanAttachComment = (node: { type: string }): boolean =>
55-
typeof node.type === 'string' &&
56-
node.type !== 'BlockComment' &&
57-
node.type !== 'LineComment';
54+
const antlrCanAttachComment = ({ type }: { type: string }): boolean =>
55+
typeof type === 'string' && type !== 'BlockComment' && type !== 'LineComment';
5856
const canAttachComment = (node: AstNode): boolean =>
5957
typeof node !== 'string' &&
6058
typeof node !== 'undefined' &&

src/slang-comments/handlers/add-collection-node-first-comment.ts renamed to src/slang-comments/handlers/add-collection-first-comment.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { util } from 'prettier';
22

3-
import type { Comment, CollectionNode } from '../../slang-nodes/types.d.ts';
3+
import type { Comment, Collection } from '../../slang-nodes/types.d.ts';
44

55
const { addDanglingComment, addLeadingComment } = util;
66

7-
export default function addCollectionNodeFirstComment(
8-
node: CollectionNode,
7+
export default function addCollectionFirstComment(
8+
node: Collection,
99
comment: Comment
1010
): void {
1111
if (node.items.length === 0) {

src/slang-comments/handlers/add-collection-node-last-comment.ts renamed to src/slang-comments/handlers/add-collection-last-comment.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { util } from 'prettier';
22

3-
import type { Comment, CollectionNode } from '../../slang-nodes/types.d.ts';
3+
import type { Comment, Collection } from '../../slang-nodes/types.d.ts';
44

55
const { addDanglingComment, addTrailingComment } = util;
66

7-
export default function addCollectionNodeLastComment(
8-
node: CollectionNode,
7+
export default function addCollectionLastComment(
8+
node: Collection,
99
comment: Comment
1010
): void {
1111
if (node.items.length === 0) {

src/slang-comments/handlers/handle-block-comments.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { NonterminalKind } from '@nomicfoundation/slang/cst';
2-
import addCollectionNodeFirstComment from './add-collection-node-first-comment.js';
3-
import addCollectionNodeLastComment from './add-collection-node-last-comment.js';
2+
import addCollectionFirstComment from './add-collection-first-comment.js';
3+
import addCollectionLastComment from './add-collection-last-comment.js';
44

55
import type { HandlerParams } from './types.d.ts';
66

@@ -15,12 +15,12 @@ export default function handleBlockComments({
1515
}
1616

1717
if (precedingNode?.kind === NonterminalKind.Statements) {
18-
addCollectionNodeLastComment(precedingNode, comment);
18+
addCollectionLastComment(precedingNode, comment);
1919
return true;
2020
}
2121

2222
if (followingNode?.kind === NonterminalKind.Statements) {
23-
addCollectionNodeFirstComment(followingNode, comment);
23+
addCollectionFirstComment(followingNode, comment);
2424
return true;
2525
}
2626

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { NonterminalKind } from '@nomicfoundation/slang/cst';
22
import { util } from 'prettier';
33
import { locEnd } from '../../slang-utils/loc.js';
4-
import addCollectionNodeLastComment from './add-collection-node-last-comment.js';
4+
import addCollectionLastComment from './add-collection-last-comment.js';
55

66
import type { HandlerParams } from './types.d.ts';
77

@@ -36,7 +36,7 @@ export default function handleContractDefinitionComments({
3636

3737
// The comment is at the end of the body of the ContractDefinition.
3838
if (precedingNode?.kind === NonterminalKind.ContractMembers) {
39-
addCollectionNodeLastComment(precedingNode, comment);
39+
addCollectionLastComment(precedingNode, comment);
4040
return true;
4141
}
4242

@@ -52,7 +52,7 @@ export default function handleContractDefinitionComments({
5252
// If the last ContractSpecifier's an InheritanceSpecifier, the comment
5353
// is appended to the last InheritanceType.
5454
if (lastContractSpecifier.kind === NonterminalKind.InheritanceSpecifier) {
55-
addCollectionNodeLastComment(lastContractSpecifier.types, comment);
55+
addCollectionLastComment(lastContractSpecifier.types, comment);
5656
return true;
5757
}
5858
if (

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { NonterminalKind } from '@nomicfoundation/slang/cst';
22
import { util } from 'prettier';
3-
import addCollectionNodeLastComment from './add-collection-node-last-comment.js';
3+
import addCollectionLastComment from './add-collection-last-comment.js';
44

55
import type { HandlerParams } from './types.d.ts';
66

@@ -20,7 +20,7 @@ export default function handleContractSpecifiersComments({
2020
precedingNode.kind === NonterminalKind.ContractSpecifier
2121
) {
2222
if (precedingNode.variant.kind === NonterminalKind.InheritanceSpecifier) {
23-
addCollectionNodeLastComment(precedingNode.variant.types, comment);
23+
addCollectionLastComment(precedingNode.variant.types, comment);
2424
return true;
2525
}
2626
if (precedingNode.variant.kind === NonterminalKind.StorageLayoutSpecifier) {

src/slang-comments/handlers/handle-else-branch-comments.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { NonterminalKind } from '@nomicfoundation/slang/cst';
22
import { util } from 'prettier';
3-
import addCollectionNodeFirstComment from './add-collection-node-first-comment.js';
3+
import addCollectionFirstComment from './add-collection-first-comment.js';
44

55
import type { HandlerParams } from './types.d.ts';
66

@@ -20,7 +20,7 @@ export default function handleElseBranchComments({
2020
followingNode.variant.kind === NonterminalKind.IfStatement
2121
) {
2222
if (followingNode.variant.body.variant.kind === NonterminalKind.Block) {
23-
addCollectionNodeFirstComment(
23+
addCollectionFirstComment(
2424
followingNode.variant.body.variant.statements,
2525
comment
2626
);

src/slang-comments/handlers/handle-if-statement-comments.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { NonterminalKind } from '@nomicfoundation/slang/cst';
22
import { util } from 'prettier';
33
import { locEnd } from '../../slang-utils/loc.js';
4-
import addCollectionNodeFirstComment from './add-collection-node-first-comment.js';
4+
import addCollectionFirstComment from './add-collection-first-comment.js';
55

66
import type { HandlerParams } from './types.d.ts';
77

@@ -45,10 +45,7 @@ export default function handleIfStatementComments({
4545

4646
if (followingNode.kind === NonterminalKind.IfStatement) {
4747
if (followingNode.body.variant.kind === NonterminalKind.Block) {
48-
addCollectionNodeFirstComment(
49-
followingNode.body.variant.statements,
50-
comment
51-
);
48+
addCollectionFirstComment(followingNode.body.variant.statements, comment);
5249
} else {
5350
addLeadingComment(followingNode.body.variant, comment);
5451
}
@@ -60,7 +57,7 @@ export default function handleIfStatementComments({
6057
// if (a) /* comment */ true
6158
if (enclosingNode.body === followingNode) {
6259
if (followingNode.variant.kind === NonterminalKind.Block) {
63-
addCollectionNodeFirstComment(followingNode.variant.statements, comment);
60+
addCollectionFirstComment(followingNode.variant.statements, comment);
6461
} else {
6562
addLeadingComment(followingNode, comment);
6663
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { NonterminalKind } from '@nomicfoundation/slang/cst';
22
import { util } from 'prettier';
33
import { locEnd } from '../../slang-utils/loc.js';
4-
import addCollectionNodeFirstComment from './add-collection-node-first-comment.js';
5-
import addCollectionNodeLastComment from './add-collection-node-last-comment.js';
4+
import addCollectionFirstComment from './add-collection-first-comment.js';
5+
import addCollectionLastComment from './add-collection-last-comment.js';
66

77
import type { HandlerParams } from './types.d.ts';
88

@@ -24,7 +24,7 @@ export default function handleInterfaceDefinitionComments({
2424

2525
// The comment is at the end of the body of the InterfaceDefinition.
2626
if (precedingNode?.kind === NonterminalKind.InterfaceMembers) {
27-
addCollectionNodeLastComment(precedingNode, comment);
27+
addCollectionLastComment(precedingNode, comment);
2828
return true;
2929
}
3030

@@ -33,7 +33,7 @@ export default function handleInterfaceDefinitionComments({
3333
nextCharacter === '{' &&
3434
followingNode?.kind === NonterminalKind.InterfaceMembers
3535
) {
36-
addCollectionNodeFirstComment(followingNode, comment);
36+
addCollectionFirstComment(followingNode, comment);
3737
return true;
3838
}
3939

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { NonterminalKind } from '@nomicfoundation/slang/cst';
22
import { util } from 'prettier';
33
import { locEnd } from '../../slang-utils/loc.js';
4-
import addCollectionNodeFirstComment from './add-collection-node-first-comment.js';
5-
import addCollectionNodeLastComment from './add-collection-node-last-comment.js';
4+
import addCollectionFirstComment from './add-collection-first-comment.js';
5+
import addCollectionLastComment from './add-collection-last-comment.js';
66

77
import type { HandlerParams } from './types.d.ts';
88

@@ -24,7 +24,7 @@ export default function handleLibraryDefinitionComments({
2424

2525
// The comment is at the end of the body of the ContractDefinition.
2626
if (precedingNode?.kind === NonterminalKind.LibraryMembers) {
27-
addCollectionNodeLastComment(precedingNode, comment);
27+
addCollectionLastComment(precedingNode, comment);
2828
return true;
2929
}
3030

@@ -33,7 +33,7 @@ export default function handleLibraryDefinitionComments({
3333
nextCharacter === '{' &&
3434
followingNode?.kind === NonterminalKind.LibraryMembers
3535
) {
36-
addCollectionNodeFirstComment(followingNode, comment);
36+
addCollectionFirstComment(followingNode, comment);
3737
return true;
3838
}
3939

0 commit comments

Comments
 (0)