Skip to content

Commit 8ca298a

Browse files
committed
Identifiers can have associated comments so we had to add an edge case for this particular TerminalNode
1 parent 843fd3f commit 8ca298a

42 files changed

Lines changed: 402 additions & 199 deletions

Some content is hidden

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

src/slang-nodes/ABICoderPragma.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
22
import { getNodeMetadata } from '../slang-utils/metadata.js';
3+
import { Identifier } from './Identifier.js';
34

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

89
export class ABICoderPragma implements SlangNode {
910
readonly kind = NonterminalKind.ABICoderPragma;
@@ -12,18 +13,19 @@ export class ABICoderPragma implements SlangNode {
1213

1314
loc;
1415

15-
version: string;
16+
version: Identifier;
1617

1718
constructor(ast: ast.ABICoderPragma, offset: number) {
1819
const metadata = getNodeMetadata(ast, offset);
20+
const { offsets } = metadata;
1921

20-
this.version = ast.version.text;
22+
this.version = new Identifier(ast.version, offsets[0]);
2123

2224
this.comments = metadata.comments;
2325
this.loc = metadata.loc;
2426
}
2527

26-
print(): Doc {
27-
return `abicoder ${this.version}`;
28+
print(path: AstPath<ABICoderPragma>, print: PrintFunction): Doc {
29+
return ['abicoder ', path.call(print, 'version')];
2830
}
2931
}

src/slang-nodes/CatchClauseError.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { doc } from 'prettier';
22
import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
33
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
4+
import { Identifier } from './Identifier.js';
45
import { ParametersDeclaration } from './ParametersDeclaration.js';
56

67
import type * as ast from '@nomicfoundation/slang/ast';
@@ -17,7 +18,7 @@ export class CatchClauseError implements SlangNode {
1718

1819
loc;
1920

20-
name?: string;
21+
name?: Identifier;
2122

2223
parameters: ParametersDeclaration;
2324

@@ -29,10 +30,14 @@ export class CatchClauseError implements SlangNode {
2930
let metadata = getNodeMetadata(ast, offset);
3031
const { offsets } = metadata;
3132

32-
this.name = ast.name?.text;
33+
let i = 0;
34+
if (ast.name) {
35+
this.name = new Identifier(ast.name, offsets[i]);
36+
i += 1;
37+
}
3338
this.parameters = new ParametersDeclaration(
3439
ast.parameters,
35-
offsets[0],
40+
offsets[i],
3641
options
3742
);
3843

@@ -44,7 +49,7 @@ export class CatchClauseError implements SlangNode {
4449

4550
print(path: AstPath<CatchClauseError>, print: PrintFunction): Doc {
4651
return [
47-
this.name ? this.name : '',
52+
this.name ? path.call(print, 'name') : '',
4853
group(path.call(print, 'parameters')),
4954
' '
5055
];

src/slang-nodes/ConstantDefinition.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
22
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
33
import { TypeName } from './TypeName.js';
4+
import { Identifier } from './Identifier.js';
45
import { Expression } from './Expression.js';
56

67
import type * as ast from '@nomicfoundation/slang/ast';
@@ -17,7 +18,7 @@ export class ConstantDefinition implements SlangNode {
1718

1819
typeName: TypeName;
1920

20-
name: string;
21+
name: Identifier;
2122

2223
value: Expression;
2324

@@ -30,8 +31,8 @@ export class ConstantDefinition implements SlangNode {
3031
const { offsets } = metadata;
3132

3233
this.typeName = new TypeName(ast.typeName, offsets[0], options);
33-
this.name = ast.name.text;
34-
this.value = new Expression(ast.value, offsets[1], options);
34+
this.name = new Identifier(ast.name, offsets[1]);
35+
this.value = new Expression(ast.value, offsets[2], options);
3536

3637
metadata = updateMetadata(metadata, [this.typeName, this.value]);
3738

@@ -42,7 +43,9 @@ export class ConstantDefinition implements SlangNode {
4243
print(path: AstPath<ConstantDefinition>, print: PrintFunction): Doc {
4344
return [
4445
path.call(print, 'typeName'),
45-
` constant ${this.name} = `,
46+
' constant ',
47+
path.call(print, 'name'),
48+
' = ',
4649
path.call(print, 'value'),
4750
';'
4851
];

src/slang-nodes/ContractDefinition.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import coerce from 'semver/functions/coerce.js';
33
import satisfies from 'semver/functions/satisfies.js';
44
import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
55
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
6+
import { Identifier } from './Identifier.js';
67
import { InheritanceSpecifier } from './InheritanceSpecifier.js';
78
import { ContractMembers } from './ContractMembers.js';
89

@@ -22,7 +23,7 @@ export class ContractDefinition implements SlangNode {
2223

2324
abstractKeyword?: string;
2425

25-
name: string;
26+
name: Identifier;
2627

2728
inheritance?: InheritanceSpecifier;
2829

@@ -37,8 +38,8 @@ export class ContractDefinition implements SlangNode {
3738
const { offsets } = metadata;
3839

3940
this.abstractKeyword = ast.abstractKeyword?.text;
40-
this.name = ast.name.text;
41-
let i = 0;
41+
this.name = new Identifier(ast.name, offsets[0]);
42+
let i = 1;
4243
if (ast.inheritance) {
4344
this.inheritance = new InheritanceSpecifier(
4445
ast.inheritance,
@@ -65,7 +66,7 @@ export class ContractDefinition implements SlangNode {
6566
for (const member of this.members.items) {
6667
if (
6768
member.variant.kind === NonterminalKind.FunctionDefinition &&
68-
member.variant.name.variant !== this.name
69+
member.variant.name.variant.value !== this.name.value
6970
) {
7071
member.variant.cleanModifierInvocationArguments();
7172
}
@@ -77,7 +78,8 @@ export class ContractDefinition implements SlangNode {
7778
return [
7879
group([
7980
this.abstractKeyword ? 'abstract ' : '',
80-
`contract ${this.name}`,
81+
'contract ',
82+
path.call(print, 'name'),
8183
this.inheritance ? [' ', path.call(print, 'inheritance')] : line,
8284
'{'
8385
]),

src/slang-nodes/EnumDefinition.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
22
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
3+
import { Identifier } from './Identifier.js';
34
import { EnumMembers } from './EnumMembers.js';
45

56
import type * as ast from '@nomicfoundation/slang/ast';
@@ -13,16 +14,16 @@ export class EnumDefinition implements SlangNode {
1314

1415
loc;
1516

16-
name: string;
17+
name: Identifier;
1718

1819
members: EnumMembers;
1920

2021
constructor(ast: ast.EnumDefinition, offset: number) {
2122
let metadata = getNodeMetadata(ast, offset);
2223
const { offsets } = metadata;
2324

24-
this.name = ast.name.text;
25-
this.members = new EnumMembers(ast.members, offsets[0]);
25+
this.name = new Identifier(ast.name, offsets[0]);
26+
this.members = new EnumMembers(ast.members, offsets[1]);
2627

2728
metadata = updateMetadata(metadata, [this.members]);
2829

@@ -31,6 +32,12 @@ export class EnumDefinition implements SlangNode {
3132
}
3233

3334
print(path: AstPath<EnumDefinition>, print: PrintFunction): Doc {
34-
return [`enum ${this.name} {`, path.call(print, 'members'), '}'];
35+
return [
36+
'enum ',
37+
path.call(print, 'name'),
38+
' {',
39+
path.call(print, 'members'),
40+
'}'
41+
];
3542
}
3643
}

src/slang-nodes/EnumMembers.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import { doc } from 'prettier';
22
import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
33
import { printSeparatedList } from '../slang-printers/print-separated-list.js';
44
import { getNodeMetadata } from '../slang-utils/metadata.js';
5+
import { Identifier } from './Identifier.js';
56

67
import type * as ast from '@nomicfoundation/slang/ast';
7-
import type { Doc } from 'prettier';
8-
import type { SlangNode } from '../types';
8+
import type { AstPath, Doc } from 'prettier';
9+
import type { PrintFunction, SlangNode } from '../types';
910

1011
const { hardline } = doc.builders;
1112

@@ -16,21 +17,26 @@ export class EnumMembers implements SlangNode {
1617

1718
loc;
1819

19-
items: string[];
20+
items: Identifier[];
2021

2122
separators: string[];
2223

2324
constructor(ast: ast.EnumMembers, offset: number) {
2425
const metadata = getNodeMetadata(ast, offset);
26+
const { offsets } = metadata;
2527

26-
this.items = ast.items.map((item) => item.text);
28+
this.items = ast.items.map(
29+
(item, index) => new Identifier(item, offsets[index])
30+
);
2731
this.separators = ast.separators.map((separator) => separator.text);
2832

2933
this.comments = metadata.comments;
3034
this.loc = metadata.loc;
3135
}
3236

33-
print(): Doc {
34-
return printSeparatedList(this.items, { firstSeparator: hardline });
37+
print(path: AstPath<EnumMembers>, print: PrintFunction): Doc {
38+
return printSeparatedList(path.map(print, 'items'), {
39+
firstSeparator: hardline
40+
});
3541
}
3642
}

src/slang-nodes/ErrorDefinition.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
22
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
3+
import { Identifier } from './Identifier.js';
34
import { ErrorParametersDeclaration } from './ErrorParametersDeclaration.js';
45

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

1516
loc;
1617

17-
name: string;
18+
name: Identifier;
1819

1920
members: ErrorParametersDeclaration;
2021

@@ -26,10 +27,10 @@ export class ErrorDefinition implements SlangNode {
2627
let metadata = getNodeMetadata(ast, offset);
2728
const { offsets } = metadata;
2829

29-
this.name = ast.name.text;
30+
this.name = new Identifier(ast.name, offsets[0]);
3031
this.members = new ErrorParametersDeclaration(
3132
ast.members,
32-
offsets[0],
33+
offsets[1],
3334
options
3435
);
3536

@@ -40,6 +41,11 @@ export class ErrorDefinition implements SlangNode {
4041
}
4142

4243
print(path: AstPath<ErrorDefinition>, print: PrintFunction): Doc {
43-
return [`error ${this.name}`, path.call(print, 'members'), ';'];
44+
return [
45+
'error ',
46+
path.call(print, 'name'),
47+
path.call(print, 'members'),
48+
';'
49+
];
4450
}
4551
}

src/slang-nodes/ErrorParameter.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
22
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
33
import { TypeName } from './TypeName.js';
4+
import { Identifier } from './Identifier.js';
45

56
import type * as ast from '@nomicfoundation/slang/ast';
67
import type { AstPath, Doc, ParserOptions } from 'prettier';
@@ -16,7 +17,7 @@ export class ErrorParameter implements SlangNode {
1617

1718
typeName: TypeName;
1819

19-
name?: string;
20+
name?: Identifier;
2021

2122
constructor(
2223
ast: ast.ErrorParameter,
@@ -27,7 +28,9 @@ export class ErrorParameter implements SlangNode {
2728
const { offsets } = metadata;
2829

2930
this.typeName = new TypeName(ast.typeName, offsets[0], options);
30-
this.name = ast.name?.text;
31+
if (ast.name) {
32+
this.name = new Identifier(ast.name, offsets[1]);
33+
}
3134

3235
metadata = updateMetadata(metadata, [this.typeName]);
3336

@@ -36,6 +39,9 @@ export class ErrorParameter implements SlangNode {
3639
}
3740

3841
print(path: AstPath<ErrorParameter>, print: PrintFunction): Doc {
39-
return [path.call(print, 'typeName'), this.name ? ` ${this.name}` : ''];
42+
return [
43+
path.call(print, 'typeName'),
44+
this.name ? [' ', path.call(print, 'name')] : ''
45+
];
4046
}
4147
}

src/slang-nodes/EventDefinition.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
22
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
33
import { EventParametersDeclaration } from './EventParametersDeclaration.js';
4+
import { Identifier } from './Identifier.js';
45

56
import type * as ast from '@nomicfoundation/slang/ast';
67
import type { AstPath, Doc, ParserOptions } from 'prettier';
@@ -14,7 +15,7 @@ export class EventDefinition implements SlangNode {
1415

1516
loc;
1617

17-
name: string;
18+
name: Identifier;
1819

1920
parameters: EventParametersDeclaration;
2021

@@ -28,10 +29,10 @@ export class EventDefinition implements SlangNode {
2829
let metadata = getNodeMetadata(ast, offset);
2930
const { offsets } = metadata;
3031

31-
this.name = ast.name.text;
32+
this.name = new Identifier(ast.name, offsets[0]);
3233
this.parameters = new EventParametersDeclaration(
3334
ast.parameters,
34-
offsets[0],
35+
offsets[1],
3536
options
3637
);
3738
this.anonymousKeyword = ast.anonymousKeyword?.text;
@@ -44,7 +45,8 @@ export class EventDefinition implements SlangNode {
4445

4546
print(path: AstPath<EventDefinition>, print: PrintFunction): Doc {
4647
return [
47-
`event ${this.name}`,
48+
'event ',
49+
path.call(print, 'name'),
4850
path.call(print, 'parameters'),
4951
this.anonymousKeyword ? ' anonymous' : '',
5052
';'

0 commit comments

Comments
 (0)