prettier-plugin-solidity 2.0.0-beta.8 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +61 -57
- package/dist/comments/handlers/handleContractDefinitionComments.js +7 -2
- package/dist/comments/handlers/handleContractDefinitionComments.js.map +1 -1
- package/dist/ea6945b7193921081ced.wasm +0 -0
- package/dist/index.js +5 -5
- package/dist/index.js.map +1 -1
- package/dist/nodes/ContractDefinition.js +30 -10
- package/dist/nodes/ContractDefinition.js.map +1 -1
- package/dist/printer.js +1 -1
- package/dist/printer.js.map +1 -1
- package/dist/slang-comments/handlers/handle-contract-definition-comments.js +18 -8
- package/dist/slang-comments/handlers/handle-contract-definition-comments.js.map +1 -1
- package/dist/slang-comments/handlers/handle-contract-specifiers-comments.js +22 -0
- package/dist/slang-comments/handlers/handle-contract-specifiers-comments.js.map +1 -0
- package/dist/slang-comments/handlers/handle-storage-layout-specifier-comments.js +14 -0
- package/dist/slang-comments/handlers/handle-storage-layout-specifier-comments.js.map +1 -0
- package/dist/slang-comments/handlers/index.js +4 -0
- package/dist/slang-comments/handlers/index.js.map +1 -1
- package/dist/slang-nodes/ConditionalExpression.js +6 -12
- package/dist/slang-nodes/ConditionalExpression.js.map +1 -1
- package/dist/slang-nodes/ContractDefinition.js +5 -6
- package/dist/slang-nodes/ContractDefinition.js.map +1 -1
- package/dist/slang-nodes/ContractSpecifier.js +27 -0
- package/dist/slang-nodes/ContractSpecifier.js.map +1 -0
- package/dist/slang-nodes/ContractSpecifiers.js +31 -0
- package/dist/slang-nodes/ContractSpecifiers.js.map +1 -0
- package/dist/slang-nodes/FunctionCallExpression.js +3 -4
- package/dist/slang-nodes/FunctionCallExpression.js.map +1 -1
- package/dist/slang-nodes/IndexAccessExpression.js +3 -4
- package/dist/slang-nodes/IndexAccessExpression.js.map +1 -1
- package/dist/slang-nodes/ReturnStatement.js +3 -5
- package/dist/slang-nodes/ReturnStatement.js.map +1 -1
- package/dist/slang-nodes/StateVariableDefinition.js +3 -6
- package/dist/slang-nodes/StateVariableDefinition.js.map +1 -1
- package/dist/slang-nodes/StateVariableDefinitionValue.js +1 -2
- package/dist/slang-nodes/StateVariableDefinitionValue.js.map +1 -1
- package/dist/slang-nodes/StorageLayoutSpecifier.js +25 -0
- package/dist/slang-nodes/StorageLayoutSpecifier.js.map +1 -0
- package/dist/slang-nodes/TupleDeconstructionStatement.js +3 -4
- package/dist/slang-nodes/TupleDeconstructionStatement.js.map +1 -1
- package/dist/slang-nodes/VariableDeclarationStatement.js +3 -2
- package/dist/slang-nodes/VariableDeclarationStatement.js.map +1 -1
- package/dist/slang-utils/sort-contract-specifiers.js +14 -0
- package/dist/slang-utils/sort-contract-specifiers.js.map +1 -0
- package/dist/slangPrinter.js +2 -3
- package/dist/slangPrinter.js.map +1 -1
- package/dist/standalone.js +1 -1
- package/dist/standalone.js.map +1 -1
- package/package.json +14 -11
- package/src/comments/handlers/handleContractDefinitionComments.js +11 -2
- package/src/index.ts +5 -5
- package/src/nodes/ContractDefinition.js +31 -11
- package/src/printer.js +1 -1
- package/src/slang-comments/handlers/handle-contract-definition-comments.ts +21 -13
- package/src/slang-comments/handlers/handle-contract-specifiers-comments.ts +33 -0
- package/src/slang-comments/handlers/handle-storage-layout-specifier-comments.ts +23 -0
- package/src/slang-comments/handlers/index.ts +4 -0
- package/src/slang-nodes/ConditionalExpression.ts +4 -10
- package/src/slang-nodes/ContractDefinition.ts +6 -7
- package/src/slang-nodes/ContractSpecifier.ts +48 -0
- package/src/slang-nodes/ContractSpecifiers.ts +52 -0
- package/src/slang-nodes/FunctionCallExpression.ts +3 -4
- package/src/slang-nodes/IndexAccessExpression.ts +3 -4
- package/src/slang-nodes/ReturnStatement.ts +3 -5
- package/src/slang-nodes/StateVariableDefinition.ts +3 -6
- package/src/slang-nodes/StateVariableDefinitionValue.ts +1 -2
- package/src/slang-nodes/StorageLayoutSpecifier.ts +45 -0
- package/src/slang-nodes/TupleDeconstructionStatement.ts +3 -4
- package/src/slang-nodes/VariableDeclarationStatement.ts +3 -2
- package/src/slang-nodes/types.d.ts +6 -0
- package/src/slang-utils/sort-contract-specifiers.ts +25 -0
- package/src/slangPrinter.ts +2 -3
- package/src/types.d.ts +7 -219
- package/dist/78244c53725e2131398e.wasm +0 -0
- package/dist/slang-utils/prettier-version-satisfies.js +0 -6
- package/dist/slang-utils/prettier-version-satisfies.js.map +0 -1
- package/dist/slang-utils/print-warning.js +0 -12
- package/dist/slang-utils/print-warning.js.map +0 -1
- package/src/slang-utils/prettier-version-satisfies.ts +0 -8
- package/src/slang-utils/print-warning.ts +0 -12
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prettier-plugin-solidity",
|
|
3
|
-
"version": "2.0.0
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "A Prettier Plugin for automatically formatting your Solidity code.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -84,17 +84,20 @@
|
|
|
84
84
|
},
|
|
85
85
|
"devDependencies": {
|
|
86
86
|
"@babel/code-frame": "^7.26.2",
|
|
87
|
+
"@eslint/eslintrc": "^3.3.1",
|
|
88
|
+
"@eslint/js": "^9.25.1",
|
|
87
89
|
"@types/jest": "^29.5.14",
|
|
88
|
-
"@types/semver": "^7.
|
|
89
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
90
|
-
"@typescript-eslint/parser": "^8.
|
|
90
|
+
"@types/semver": "^7.7.0",
|
|
91
|
+
"@typescript-eslint/eslint-plugin": "^8.31.0",
|
|
92
|
+
"@typescript-eslint/parser": "^8.31.0",
|
|
91
93
|
"c8": "^10.1.3",
|
|
92
94
|
"cross-env": "^7.0.3",
|
|
93
|
-
"eslint": "^9.
|
|
94
|
-
"eslint-config-prettier": "10.1.
|
|
95
|
+
"eslint": "^9.25.1",
|
|
96
|
+
"eslint-config-prettier": "^10.1.2",
|
|
95
97
|
"esm-utils": "^4.3.0",
|
|
98
|
+
"globals": "^16.0.0",
|
|
96
99
|
"jest": "^29.7.0",
|
|
97
|
-
"jest-light-runner": "^0.
|
|
100
|
+
"jest-light-runner": "^0.7.8",
|
|
98
101
|
"jest-snapshot-serializer-ansi": "^2.2.1",
|
|
99
102
|
"jest-snapshot-serializer-raw": "^2.0.0",
|
|
100
103
|
"jest-watch-typeahead": "^2.2.2",
|
|
@@ -103,13 +106,13 @@
|
|
|
103
106
|
"solc": "^0.8.29",
|
|
104
107
|
"ts-loader": "^9.5.2",
|
|
105
108
|
"ts-node": "^10.9.2",
|
|
106
|
-
"typescript": "^5.8.
|
|
107
|
-
"webpack": "^5.
|
|
109
|
+
"typescript": "^5.8.3",
|
|
110
|
+
"webpack": "^5.99.7",
|
|
108
111
|
"webpack-cli": "^6.0.1"
|
|
109
112
|
},
|
|
110
113
|
"dependencies": {
|
|
111
|
-
"@nomicfoundation/slang": "1.
|
|
112
|
-
"@solidity-parser/parser": "^0.
|
|
114
|
+
"@nomicfoundation/slang": "1.1.0",
|
|
115
|
+
"@solidity-parser/parser": "^0.20.1",
|
|
113
116
|
"semver": "^7.7.1"
|
|
114
117
|
},
|
|
115
118
|
"peerDependencies": {
|
|
@@ -27,10 +27,19 @@ function handleContractDefinitionComments({
|
|
|
27
27
|
);
|
|
28
28
|
|
|
29
29
|
// The comment is behind the start of the Block `{}` or behind a base contract
|
|
30
|
-
if (
|
|
30
|
+
if (
|
|
31
|
+
(followingNode &&
|
|
32
|
+
(followingNode.type === 'InheritanceSpecifier' ||
|
|
33
|
+
followingNode === enclosingNode.storageLayout)) ||
|
|
34
|
+
nextCharacter === '{'
|
|
35
|
+
) {
|
|
31
36
|
// In this scenario the comment belongs to a base contract.
|
|
32
37
|
// contract A is B, /* comment for B */ C /* comment for C */ {}
|
|
33
|
-
if (
|
|
38
|
+
if (
|
|
39
|
+
precedingNode &&
|
|
40
|
+
(precedingNode.type === 'InheritanceSpecifier' ||
|
|
41
|
+
precedingNode === enclosingNode.storageLayout)
|
|
42
|
+
) {
|
|
34
43
|
addTrailingComment(precedingNode, comment);
|
|
35
44
|
return true;
|
|
36
45
|
}
|
package/src/index.ts
CHANGED
|
@@ -18,10 +18,10 @@ import type {
|
|
|
18
18
|
} from 'prettier';
|
|
19
19
|
import type { AstNode } from './slang-nodes/types.d.ts';
|
|
20
20
|
|
|
21
|
-
const slangParserId = 'slang
|
|
22
|
-
const antlrParserId = '
|
|
21
|
+
const slangParserId = 'slang';
|
|
22
|
+
const antlrParserId = 'antlr';
|
|
23
23
|
const slangAstId = 'slang-ast';
|
|
24
|
-
const antlrAstId = '
|
|
24
|
+
const antlrAstId = 'antlr-ast';
|
|
25
25
|
|
|
26
26
|
// https://prettier.io/docs/en/plugins.html#languages
|
|
27
27
|
// https://github.com/github-linguist/linguist/blob/master/lib/linguist/languages.yml
|
|
@@ -51,7 +51,7 @@ const parsers = {
|
|
|
51
51
|
[antlrParserId]: antlrParser
|
|
52
52
|
};
|
|
53
53
|
|
|
54
|
-
const
|
|
54
|
+
const antlrCanAttachComment = (node: { type: string }): boolean =>
|
|
55
55
|
typeof node.type === 'string' &&
|
|
56
56
|
node.type !== 'BlockComment' &&
|
|
57
57
|
node.type !== 'LineComment';
|
|
@@ -63,7 +63,7 @@ const canAttachComment = (node: AstNode): boolean =>
|
|
|
63
63
|
|
|
64
64
|
// https://prettier.io/docs/en/plugins.html#printers
|
|
65
65
|
const antlrPrinter = {
|
|
66
|
-
canAttachComment:
|
|
66
|
+
canAttachComment: antlrCanAttachComment,
|
|
67
67
|
handleComments: {
|
|
68
68
|
ownLine: comments.solidityHandleOwnLineComment,
|
|
69
69
|
endOfLine: comments.solidityHandleEndOfLineComment,
|
|
@@ -6,17 +6,37 @@ import {
|
|
|
6
6
|
printSeparatedList
|
|
7
7
|
} from '../common/printer-helpers.js';
|
|
8
8
|
|
|
9
|
-
const { group, line,
|
|
9
|
+
const { group, hardline, ifBreak, line, softline } = doc.builders;
|
|
10
10
|
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
11
|
+
const specifiers = (node, path, print) => {
|
|
12
|
+
const document = [];
|
|
13
|
+
if (node.baseContracts.length > 0) {
|
|
14
|
+
document.push([
|
|
15
|
+
'is',
|
|
16
|
+
printSeparatedList(path.map(print, 'baseContracts'), {
|
|
17
|
+
firstSeparator: line
|
|
18
|
+
})
|
|
19
|
+
]);
|
|
20
|
+
}
|
|
21
|
+
if (node.storageLayout) {
|
|
22
|
+
document.push([
|
|
23
|
+
'layout at',
|
|
24
|
+
printSeparatedItem(path.call(print, 'storageLayout'), {
|
|
25
|
+
firstSeparator: line
|
|
26
|
+
})
|
|
27
|
+
]);
|
|
28
|
+
}
|
|
29
|
+
if (document.length === 0) return line;
|
|
30
|
+
if (document.length === 1) return [' ', document];
|
|
31
|
+
const groupId = Symbol('ContractSpecifiers.inheritance');
|
|
32
|
+
return printSeparatedList(
|
|
33
|
+
[group(document[0], { id: groupId }), document[1]],
|
|
34
|
+
{
|
|
35
|
+
firstSeparator: line,
|
|
36
|
+
separator: ifBreak('', softline, { groupId })
|
|
37
|
+
}
|
|
38
|
+
);
|
|
39
|
+
};
|
|
20
40
|
|
|
21
41
|
const body = (node, path, options, print) => {
|
|
22
42
|
const comments = printComments(node, path, options);
|
|
@@ -34,7 +54,7 @@ export const ContractDefinition = {
|
|
|
34
54
|
node.kind === 'abstract' ? 'abstract contract' : node.kind,
|
|
35
55
|
' ',
|
|
36
56
|
node.name,
|
|
37
|
-
|
|
57
|
+
specifiers(node, path, print),
|
|
38
58
|
'{'
|
|
39
59
|
]),
|
|
40
60
|
body(node, path, options, print),
|
package/src/printer.js
CHANGED
|
@@ -23,10 +23,10 @@ export default function handleContractDefinitionComments({
|
|
|
23
23
|
locEnd(comment)
|
|
24
24
|
);
|
|
25
25
|
|
|
26
|
-
// Everything before the
|
|
26
|
+
// Everything before the ContractSpecifiers is pushed onto the beginning of
|
|
27
27
|
// the ContractDefinition.
|
|
28
28
|
if (
|
|
29
|
-
followingNode?.kind === NonterminalKind.
|
|
29
|
+
followingNode?.kind === NonterminalKind.ContractSpecifiers ||
|
|
30
30
|
(followingNode?.kind === NonterminalKind.ContractMembers &&
|
|
31
31
|
nextCharacter !== '{')
|
|
32
32
|
) {
|
|
@@ -42,17 +42,25 @@ export default function handleContractDefinitionComments({
|
|
|
42
42
|
|
|
43
43
|
// The last comments before the body.
|
|
44
44
|
if (nextCharacter === '{') {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
)
|
|
55
|
-
|
|
45
|
+
if (precedingNode?.kind === NonterminalKind.ContractSpecifiers) {
|
|
46
|
+
if (precedingNode.items.length === 0) {
|
|
47
|
+
addTrailingComment(precedingNode, comment);
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
const lastContractSpecifier =
|
|
51
|
+
precedingNode.items[precedingNode.items.length - 1].variant;
|
|
52
|
+
// If the last ContractSpecifier's an InheritanceSpecifier, the comment
|
|
53
|
+
// is appended to the last InheritanceType.
|
|
54
|
+
if (lastContractSpecifier.kind === NonterminalKind.InheritanceSpecifier) {
|
|
55
|
+
addCollectionNodeLastComment(lastContractSpecifier.types, comment);
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
if (
|
|
59
|
+
lastContractSpecifier.kind === NonterminalKind.StorageLayoutSpecifier
|
|
60
|
+
) {
|
|
61
|
+
addTrailingComment(lastContractSpecifier.expression, comment);
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
56
64
|
}
|
|
57
65
|
}
|
|
58
66
|
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { NonterminalKind } from '@nomicfoundation/slang/cst';
|
|
2
|
+
import { util } from 'prettier';
|
|
3
|
+
import addCollectionNodeLastComment from './add-collection-node-last-comment.js';
|
|
4
|
+
|
|
5
|
+
import type { HandlerParams } from './types.d.ts';
|
|
6
|
+
|
|
7
|
+
const { addTrailingComment } = util;
|
|
8
|
+
|
|
9
|
+
export default function handleContractSpecifiersComments({
|
|
10
|
+
precedingNode,
|
|
11
|
+
enclosingNode,
|
|
12
|
+
comment
|
|
13
|
+
}: HandlerParams): boolean {
|
|
14
|
+
if (enclosingNode?.kind !== NonterminalKind.ContractSpecifiers) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (
|
|
19
|
+
precedingNode &&
|
|
20
|
+
precedingNode.kind === NonterminalKind.ContractSpecifier
|
|
21
|
+
) {
|
|
22
|
+
if (precedingNode.variant.kind === NonterminalKind.InheritanceSpecifier) {
|
|
23
|
+
addCollectionNodeLastComment(precedingNode.variant.types, comment);
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
if (precedingNode.variant.kind === NonterminalKind.StorageLayoutSpecifier) {
|
|
27
|
+
addTrailingComment(precedingNode.variant.expression, comment);
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { NonterminalKind } from '@nomicfoundation/slang/cst';
|
|
2
|
+
import { util } from 'prettier';
|
|
3
|
+
|
|
4
|
+
import type { HandlerParams } from './types.d.ts';
|
|
5
|
+
|
|
6
|
+
const { addLeadingComment } = util;
|
|
7
|
+
|
|
8
|
+
export default function handleStorageLayoutSpecifierComments({
|
|
9
|
+
enclosingNode,
|
|
10
|
+
followingNode,
|
|
11
|
+
comment
|
|
12
|
+
}: HandlerParams): boolean {
|
|
13
|
+
if (enclosingNode?.kind !== NonterminalKind.StorageLayoutSpecifier) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (followingNode?.kind === NonterminalKind.Expression) {
|
|
18
|
+
addLeadingComment(followingNode, comment);
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import handleBlockComments from './handle-block-comments.js';
|
|
2
2
|
import handleContractDefinitionComments from './handle-contract-definition-comments.js';
|
|
3
|
+
import handleContractSpecifiersComments from './handle-contract-specifiers-comments.js';
|
|
3
4
|
import handleElseBranchComments from './handle-else-branch-comments.js';
|
|
4
5
|
import handleIfStatementComments from './handle-if-statement-comments.js';
|
|
5
6
|
import handleInterfaceDefinitionComments from './handle-interface-definition-comments.js';
|
|
@@ -7,12 +8,14 @@ import handleLibraryDefinitionComments from './handle-library-definition-comment
|
|
|
7
8
|
import handleModifierInvocationComments from './handle-modifier-invocation-comments.js';
|
|
8
9
|
import handleParametersDeclarationComments from './handle-parameters-declaration-comments.js';
|
|
9
10
|
import handlePositionalArgumentsDeclarationComments from './handle-positional-arguments-declaration-comments.js';
|
|
11
|
+
import handleStorageLayoutSpecifierComments from './handle-storage-layout-specifier-comments.js';
|
|
10
12
|
import handleWhileStatementComments from './handle-while-statement-comments.js';
|
|
11
13
|
import handleYulBlockComments from './handle-yul-block-comments.js';
|
|
12
14
|
|
|
13
15
|
export default [
|
|
14
16
|
handleBlockComments,
|
|
15
17
|
handleContractDefinitionComments,
|
|
18
|
+
handleContractSpecifiersComments,
|
|
16
19
|
handleElseBranchComments,
|
|
17
20
|
handleIfStatementComments,
|
|
18
21
|
handleInterfaceDefinitionComments,
|
|
@@ -20,6 +23,7 @@ export default [
|
|
|
20
23
|
handleModifierInvocationComments,
|
|
21
24
|
handleParametersDeclarationComments,
|
|
22
25
|
handlePositionalArgumentsDeclarationComments,
|
|
26
|
+
handleStorageLayoutSpecifierComments,
|
|
23
27
|
handleWhileStatementComments,
|
|
24
28
|
handleYulBlockComments
|
|
25
29
|
];
|
|
@@ -29,16 +29,13 @@ function experimentalTernaries(
|
|
|
29
29
|
const isNestedAsTrueExpression =
|
|
30
30
|
isNested && grandparent.trueExpression.variant === node;
|
|
31
31
|
const falseExpressionInSameLine =
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
node.falseExpression.variant.kind ===
|
|
35
|
-
NonterminalKind.ConditionalExpression);
|
|
32
|
+
node.falseExpression.variant.kind === NonterminalKind.TupleExpression ||
|
|
33
|
+
node.falseExpression.variant.kind === NonterminalKind.ConditionalExpression;
|
|
36
34
|
|
|
37
35
|
// If the `condition` breaks into multiple lines, we add parentheses,
|
|
38
36
|
// unless it already is a `TupleExpression`.
|
|
39
37
|
const operand = path.call(print, 'operand');
|
|
40
38
|
const operandDoc = group([
|
|
41
|
-
typeof node.operand.variant !== 'string' &&
|
|
42
39
|
node.operand.variant.kind === NonterminalKind.TupleExpression
|
|
43
40
|
? operand
|
|
44
41
|
: ifBreak(['(', printSeparatedItem(operand), ')'], operand),
|
|
@@ -137,13 +134,10 @@ export class ConditionalExpression implements SlangNode {
|
|
|
137
134
|
// `condition` must be a single `bool` value.
|
|
138
135
|
const operandLoc = this.operand.loc;
|
|
139
136
|
while (
|
|
140
|
-
typeof this.operand.variant !== 'string' &&
|
|
141
137
|
this.operand.variant.kind === NonterminalKind.TupleExpression &&
|
|
142
138
|
this.operand.variant.items.items.length === 1 &&
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
this.operand.variant.items.items[0].expression!.variant.kind !==
|
|
146
|
-
NonterminalKind.ConditionalExpression)
|
|
139
|
+
this.operand.variant.items.items[0].expression!.variant.kind !==
|
|
140
|
+
NonterminalKind.ConditionalExpression
|
|
147
141
|
) {
|
|
148
142
|
this.operand = this.operand.variant.items.items[0].expression!;
|
|
149
143
|
}
|
|
@@ -3,7 +3,7 @@ import { coerce, satisfies } from 'semver';
|
|
|
3
3
|
import { NonterminalKind } from '@nomicfoundation/slang/cst';
|
|
4
4
|
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
|
|
5
5
|
import { Identifier } from './Identifier.js';
|
|
6
|
-
import {
|
|
6
|
+
import { ContractSpecifiers } from './ContractSpecifiers.js';
|
|
7
7
|
import { ContractMembers } from './ContractMembers.js';
|
|
8
8
|
|
|
9
9
|
import type * as ast from '@nomicfoundation/slang/ast';
|
|
@@ -24,7 +24,7 @@ export class ContractDefinition implements SlangNode {
|
|
|
24
24
|
|
|
25
25
|
name: Identifier;
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
specifiers: ContractSpecifiers;
|
|
28
28
|
|
|
29
29
|
members: ContractMembers;
|
|
30
30
|
|
|
@@ -33,12 +33,10 @@ export class ContractDefinition implements SlangNode {
|
|
|
33
33
|
|
|
34
34
|
this.abstractKeyword = ast.abstractKeyword?.unparse();
|
|
35
35
|
this.name = new Identifier(ast.name);
|
|
36
|
-
|
|
37
|
-
this.inheritance = new InheritanceSpecifier(ast.inheritance, options);
|
|
38
|
-
}
|
|
36
|
+
this.specifiers = new ContractSpecifiers(ast.specifiers, options);
|
|
39
37
|
this.members = new ContractMembers(ast.members, options);
|
|
40
38
|
|
|
41
|
-
metadata = updateMetadata(metadata, [this.
|
|
39
|
+
metadata = updateMetadata(metadata, [this.specifiers, this.members]);
|
|
42
40
|
|
|
43
41
|
this.comments = metadata.comments;
|
|
44
42
|
this.loc = metadata.loc;
|
|
@@ -68,7 +66,8 @@ export class ContractDefinition implements SlangNode {
|
|
|
68
66
|
this.abstractKeyword ? 'abstract ' : '',
|
|
69
67
|
'contract ',
|
|
70
68
|
path.call(print, 'name'),
|
|
71
|
-
|
|
69
|
+
path.call(print, 'specifiers'),
|
|
70
|
+
this.specifiers.items.length > 0 ? '' : line,
|
|
72
71
|
'{'
|
|
73
72
|
]),
|
|
74
73
|
path.call(print, 'members'),
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { NonterminalKind } from '@nomicfoundation/slang/cst';
|
|
2
|
+
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
|
|
3
|
+
import { InheritanceSpecifier } from './InheritanceSpecifier.js';
|
|
4
|
+
import { StorageLayoutSpecifier } from './StorageLayoutSpecifier.js';
|
|
5
|
+
|
|
6
|
+
import type * as ast from '@nomicfoundation/slang/ast';
|
|
7
|
+
import type { AstPath, Doc, ParserOptions } from 'prettier';
|
|
8
|
+
import type { AstNode } from './types.js';
|
|
9
|
+
import type { PrintFunction, SlangNode } from '../types.js';
|
|
10
|
+
|
|
11
|
+
export class ContractSpecifier implements SlangNode {
|
|
12
|
+
readonly kind = NonterminalKind.ContractSpecifier;
|
|
13
|
+
|
|
14
|
+
comments;
|
|
15
|
+
|
|
16
|
+
loc;
|
|
17
|
+
|
|
18
|
+
variant: InheritanceSpecifier | StorageLayoutSpecifier;
|
|
19
|
+
|
|
20
|
+
constructor(ast: ast.ContractSpecifier, options: ParserOptions<AstNode>) {
|
|
21
|
+
let metadata = getNodeMetadata(ast);
|
|
22
|
+
|
|
23
|
+
switch (ast.variant.cst.kind) {
|
|
24
|
+
case NonterminalKind.InheritanceSpecifier:
|
|
25
|
+
this.variant = new InheritanceSpecifier(
|
|
26
|
+
ast.variant as ast.InheritanceSpecifier,
|
|
27
|
+
options
|
|
28
|
+
);
|
|
29
|
+
break;
|
|
30
|
+
case NonterminalKind.StorageLayoutSpecifier:
|
|
31
|
+
this.variant = new StorageLayoutSpecifier(
|
|
32
|
+
ast.variant as ast.StorageLayoutSpecifier,
|
|
33
|
+
options
|
|
34
|
+
);
|
|
35
|
+
break;
|
|
36
|
+
default:
|
|
37
|
+
throw new Error(`Unexpected variant: ${ast.variant.cst.kind}`);
|
|
38
|
+
}
|
|
39
|
+
metadata = updateMetadata(metadata, [this.variant]);
|
|
40
|
+
|
|
41
|
+
this.comments = metadata.comments;
|
|
42
|
+
this.loc = metadata.loc;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
print(path: AstPath<ContractSpecifier>, print: PrintFunction): Doc {
|
|
46
|
+
return path.call(print, 'variant');
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { doc } from 'prettier';
|
|
2
|
+
import { NonterminalKind } from '@nomicfoundation/slang/cst';
|
|
3
|
+
import { sortContractSpecifiers } from '../slang-utils/sort-contract-specifiers.js';
|
|
4
|
+
import { printSeparatedList } from '../slang-printers/print-separated-list.js';
|
|
5
|
+
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
|
|
6
|
+
import { ContractSpecifier } from './ContractSpecifier.js';
|
|
7
|
+
|
|
8
|
+
import type * as ast from '@nomicfoundation/slang/ast';
|
|
9
|
+
import type { AstPath, Doc, ParserOptions } from 'prettier';
|
|
10
|
+
import type { AstNode } from './types.js';
|
|
11
|
+
import type { PrintFunction, SlangNode } from '../types.js';
|
|
12
|
+
|
|
13
|
+
const { group, ifBreak, line, softline } = doc.builders;
|
|
14
|
+
|
|
15
|
+
export class ContractSpecifiers implements SlangNode {
|
|
16
|
+
readonly kind = NonterminalKind.ContractSpecifiers;
|
|
17
|
+
|
|
18
|
+
comments;
|
|
19
|
+
|
|
20
|
+
loc;
|
|
21
|
+
|
|
22
|
+
items: ContractSpecifier[];
|
|
23
|
+
|
|
24
|
+
constructor(ast: ast.ContractSpecifiers, options: ParserOptions<AstNode>) {
|
|
25
|
+
let metadata = getNodeMetadata(ast, true);
|
|
26
|
+
|
|
27
|
+
this.items = ast.items.map((item) => new ContractSpecifier(item, options));
|
|
28
|
+
|
|
29
|
+
metadata = updateMetadata(metadata, [this.items]);
|
|
30
|
+
|
|
31
|
+
this.comments = metadata.comments;
|
|
32
|
+
this.loc = metadata.loc;
|
|
33
|
+
|
|
34
|
+
this.items = this.items.sort(sortContractSpecifiers);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
print(path: AstPath<ContractSpecifiers>, print: PrintFunction): Doc {
|
|
38
|
+
if (this.items.length === 0) return '';
|
|
39
|
+
|
|
40
|
+
const [specifier1, specifier2] = path.map(print, 'items');
|
|
41
|
+
if (typeof specifier2 === 'undefined') return [' ', specifier1];
|
|
42
|
+
|
|
43
|
+
const groupId = Symbol('Slang.ContractSpecifiers.inheritance');
|
|
44
|
+
return printSeparatedList(
|
|
45
|
+
[group(specifier1, { id: groupId }), specifier2],
|
|
46
|
+
{
|
|
47
|
+
firstSeparator: line,
|
|
48
|
+
separator: ifBreak('', softline, { groupId })
|
|
49
|
+
}
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -45,10 +45,9 @@ export class FunctionCallExpression implements SlangNode {
|
|
|
45
45
|
// If we are at the end of a MemberAccessChain we should indent the
|
|
46
46
|
// arguments accordingly.
|
|
47
47
|
if (isLabel(operandDoc) && operandDoc.label === 'MemberAccessChain') {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
});
|
|
51
|
-
argumentsDoc = indentIfBreak(argumentsDoc, { groupId: operandDoc.id! });
|
|
48
|
+
const groupId = Symbol('Slang.FunctionCallExpression.operand');
|
|
49
|
+
operandDoc = group(operandDoc.contents, { id: groupId });
|
|
50
|
+
argumentsDoc = indentIfBreak(argumentsDoc, { groupId });
|
|
52
51
|
// We wrap the expression in a label in case there is an IndexAccess or
|
|
53
52
|
// a FunctionCall following this IndexAccess.
|
|
54
53
|
return label('MemberAccessChain', [operandDoc, argumentsDoc]);
|
|
@@ -54,11 +54,10 @@ export class IndexAccessExpression implements SlangNode {
|
|
|
54
54
|
// If we are at the end of a MemberAccessChain we should indent the
|
|
55
55
|
// arguments accordingly.
|
|
56
56
|
if (isLabel(operandDoc) && operandDoc.label === 'MemberAccessChain') {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
});
|
|
57
|
+
const groupId = Symbol('Slang.IndexAccessExpression.operand');
|
|
58
|
+
operandDoc = group(operandDoc.contents, { id: groupId });
|
|
60
59
|
|
|
61
|
-
indexDoc = indentIfBreak(indexDoc, { groupId
|
|
60
|
+
indexDoc = indentIfBreak(indexDoc, { groupId });
|
|
62
61
|
// We wrap the expression in a label in case there is an IndexAccess or
|
|
63
62
|
// a FunctionCall following this IndexAccess.
|
|
64
63
|
return label('MemberAccessChain', [operandDoc, indexDoc]);
|
|
@@ -17,11 +17,9 @@ function printExpression(
|
|
|
17
17
|
options: ParserOptions<AstNode>
|
|
18
18
|
): Doc {
|
|
19
19
|
if (node.expression) {
|
|
20
|
-
return
|
|
21
|
-
(
|
|
22
|
-
|
|
23
|
-
node.expression.variant.kind ===
|
|
24
|
-
NonterminalKind.ConditionalExpression))
|
|
20
|
+
return node.expression.variant.kind === NonterminalKind.TupleExpression ||
|
|
21
|
+
(options.experimentalTernaries &&
|
|
22
|
+
node.expression.variant.kind === NonterminalKind.ConditionalExpression)
|
|
25
23
|
? [' ', path.call(print, 'expression')]
|
|
26
24
|
: group(indent([line, path.call(print, 'expression')]));
|
|
27
25
|
}
|
|
@@ -52,8 +52,9 @@ export class StateVariableDefinition implements SlangNode {
|
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
print(path: AstPath<StateVariableDefinition>, print: PrintFunction): Doc {
|
|
55
|
+
const groupId = Symbol('Slang.StateVariableDefinition.attributes');
|
|
55
56
|
const attributesDoc = group(indent(path.call(print, 'attributes')), {
|
|
56
|
-
id:
|
|
57
|
+
id: groupId
|
|
57
58
|
});
|
|
58
59
|
|
|
59
60
|
return [
|
|
@@ -61,11 +62,7 @@ export class StateVariableDefinition implements SlangNode {
|
|
|
61
62
|
attributesDoc,
|
|
62
63
|
' ',
|
|
63
64
|
path.call(print, 'name'),
|
|
64
|
-
this.value
|
|
65
|
-
? indentIfBreak(path.call(print, 'value'), {
|
|
66
|
-
groupId: attributesDoc.id!
|
|
67
|
-
})
|
|
68
|
-
: '',
|
|
65
|
+
this.value ? indentIfBreak(path.call(print, 'value'), { groupId }) : '',
|
|
69
66
|
';'
|
|
70
67
|
];
|
|
71
68
|
}
|
|
@@ -37,8 +37,7 @@ export class StateVariableDefinitionValue implements SlangNode {
|
|
|
37
37
|
path: AstPath<StateVariableDefinitionValue>,
|
|
38
38
|
print: PrintFunction
|
|
39
39
|
): Doc {
|
|
40
|
-
return
|
|
41
|
-
this.value.variant.kind === NonterminalKind.ArrayExpression
|
|
40
|
+
return this.value.variant.kind === NonterminalKind.ArrayExpression
|
|
42
41
|
? [' = ', path.call(print, 'value')]
|
|
43
42
|
: group([' =', indent([line, path.call(print, 'value')])]);
|
|
44
43
|
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { doc } from 'prettier';
|
|
2
|
+
import { NonterminalKind } from '@nomicfoundation/slang/cst';
|
|
3
|
+
import { printSeparatedItem } from '../slang-printers/print-separated-item.js';
|
|
4
|
+
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
|
|
5
|
+
import { Expression } from './Expression.js';
|
|
6
|
+
|
|
7
|
+
import type * as ast from '@nomicfoundation/slang/ast';
|
|
8
|
+
import type { AstPath, Doc, ParserOptions } from 'prettier';
|
|
9
|
+
import type { AstNode } from './types.js';
|
|
10
|
+
import type { PrintFunction, SlangNode } from '../types.js';
|
|
11
|
+
|
|
12
|
+
const { line } = doc.builders;
|
|
13
|
+
|
|
14
|
+
export class StorageLayoutSpecifier implements SlangNode {
|
|
15
|
+
readonly kind = NonterminalKind.StorageLayoutSpecifier;
|
|
16
|
+
|
|
17
|
+
comments;
|
|
18
|
+
|
|
19
|
+
loc;
|
|
20
|
+
|
|
21
|
+
expression: Expression;
|
|
22
|
+
|
|
23
|
+
constructor(
|
|
24
|
+
ast: ast.StorageLayoutSpecifier,
|
|
25
|
+
options: ParserOptions<AstNode>
|
|
26
|
+
) {
|
|
27
|
+
let metadata = getNodeMetadata(ast);
|
|
28
|
+
|
|
29
|
+
this.expression = new Expression(ast.expression, options);
|
|
30
|
+
|
|
31
|
+
metadata = updateMetadata(metadata, [this.expression]);
|
|
32
|
+
|
|
33
|
+
this.comments = metadata.comments;
|
|
34
|
+
this.loc = metadata.loc;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
print(path: AstPath<StorageLayoutSpecifier>, print: PrintFunction): Doc {
|
|
38
|
+
return [
|
|
39
|
+
'layout at',
|
|
40
|
+
printSeparatedItem(path.call(print, 'expression'), {
|
|
41
|
+
firstSeparator: line
|
|
42
|
+
})
|
|
43
|
+
];
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -44,16 +44,15 @@ export class TupleDeconstructionStatement implements SlangNode {
|
|
|
44
44
|
path: AstPath<TupleDeconstructionStatement>,
|
|
45
45
|
print: PrintFunction
|
|
46
46
|
): Doc {
|
|
47
|
+
const groupId = Symbol('Slang.VariableDeclarationStatement.variables');
|
|
47
48
|
const declarationDoc = group(
|
|
48
49
|
[this.varKeyword ? 'var (' : '(', path.call(print, 'elements'), ')'],
|
|
49
|
-
{ id:
|
|
50
|
+
{ id: groupId }
|
|
50
51
|
);
|
|
51
52
|
|
|
52
53
|
return [
|
|
53
54
|
declarationDoc,
|
|
54
|
-
indentIfBreak([' = ', path.call(print, 'expression'), ';'], {
|
|
55
|
-
groupId: declarationDoc.id!
|
|
56
|
-
})
|
|
55
|
+
indentIfBreak([' = ', path.call(print, 'expression'), ';'], { groupId })
|
|
57
56
|
];
|
|
58
57
|
}
|
|
59
58
|
}
|