prettier-plugin-solidity 2.0.0 → 2.1.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/LICENSE +1 -1
- package/README.md +2 -2
- package/dist/3620299963220ca08ee1.wasm +0 -0
- package/dist/binary-operator-printers/addition.js +11 -0
- package/dist/binary-operator-printers/addition.js.map +1 -0
- package/dist/binary-operator-printers/bit.js +5 -2
- package/dist/binary-operator-printers/bit.js.map +1 -1
- package/dist/binary-operator-printers/equality.js +7 -0
- package/dist/binary-operator-printers/equality.js.map +1 -0
- package/dist/binary-operator-printers/exponentiation.js +20 -12
- package/dist/binary-operator-printers/exponentiation.js.map +1 -1
- package/dist/binary-operator-printers/index.js +4 -2
- package/dist/binary-operator-printers/index.js.map +1 -1
- package/dist/binary-operator-printers/inequality.js +8 -0
- package/dist/binary-operator-printers/inequality.js.map +1 -0
- package/dist/binary-operator-printers/logical.js +9 -24
- package/dist/binary-operator-printers/logical.js.map +1 -1
- package/dist/binary-operator-printers/multiplication.js +19 -0
- package/dist/binary-operator-printers/multiplication.js.map +1 -0
- package/dist/binary-operator-printers/printers/binary-operation-printer.js +5 -0
- package/dist/binary-operator-printers/printers/binary-operation-printer.js.map +1 -0
- package/dist/binary-operator-printers/printers/create-binary-operation-printer.js +24 -0
- package/dist/binary-operator-printers/printers/create-binary-operation-printer.js.map +1 -0
- package/dist/binary-operator-printers/printers/create-group-if-necessary-builder.js +10 -0
- package/dist/binary-operator-printers/printers/create-group-if-necessary-builder.js.map +1 -0
- package/dist/binary-operator-printers/printers/create-indent-if-necessary-builder.js +22 -0
- package/dist/binary-operator-printers/printers/create-indent-if-necessary-builder.js.map +1 -0
- package/dist/binary-operator-printers/shift.js +6 -2
- package/dist/binary-operator-printers/shift.js.map +1 -1
- package/dist/binary-operator-printers/utils/should-group-or-indent.js +3 -0
- package/dist/binary-operator-printers/utils/should-group-or-indent.js.map +1 -0
- package/dist/comments/handlers/handleContractDefinitionComments.js +2 -3
- package/dist/comments/handlers/handleContractDefinitionComments.js.map +1 -1
- package/dist/comments/handlers/handleModifierInvocationComments.js +2 -3
- package/dist/comments/handlers/handleModifierInvocationComments.js.map +1 -1
- package/dist/common/printer-helpers.js +4 -11
- package/dist/common/printer-helpers.js.map +1 -1
- package/dist/index.js +1 -3
- package/dist/index.js.map +1 -1
- package/dist/nodes/Conditional.js +2 -2
- package/dist/nodes/Conditional.js.map +1 -1
- package/dist/nodes/ExpressionStatement.js +1 -1
- package/dist/nodes/ExpressionStatement.js.map +1 -1
- package/dist/nodes/FunctionDefinition.js +3 -3
- package/dist/nodes/FunctionDefinition.js.map +1 -1
- package/dist/nodes/ImportDirective.js +3 -1
- package/dist/nodes/ImportDirective.js.map +1 -1
- package/dist/options.js +16 -0
- package/dist/options.js.map +1 -1
- package/dist/parser.js +4 -0
- package/dist/parser.js.map +1 -1
- package/dist/prettier-comments/language-js/comments.js +8 -9
- package/dist/prettier-comments/language-js/comments.js.map +1 -1
- package/dist/printer.js +1 -1
- package/dist/printer.js.map +1 -1
- package/dist/slang-comments/handlers/{add-collection-node-first-comment.js → add-collection-first-comment.js} +2 -2
- package/dist/slang-comments/handlers/add-collection-first-comment.js.map +1 -0
- package/dist/slang-comments/handlers/{add-collection-node-last-comment.js → add-collection-last-comment.js} +2 -2
- package/dist/slang-comments/handlers/add-collection-last-comment.js.map +1 -0
- package/dist/slang-comments/handlers/handle-block-comments.js +4 -4
- package/dist/slang-comments/handlers/handle-block-comments.js.map +1 -1
- package/dist/slang-comments/handlers/handle-contract-definition-comments.js +3 -3
- package/dist/slang-comments/handlers/handle-contract-definition-comments.js.map +1 -1
- package/dist/slang-comments/handlers/handle-contract-specifiers-comments.js +2 -2
- package/dist/slang-comments/handlers/handle-contract-specifiers-comments.js.map +1 -1
- package/dist/slang-comments/handlers/handle-else-branch-comments.js +2 -2
- package/dist/slang-comments/handlers/handle-else-branch-comments.js.map +1 -1
- package/dist/slang-comments/handlers/handle-if-statement-comments.js +3 -3
- package/dist/slang-comments/handlers/handle-if-statement-comments.js.map +1 -1
- package/dist/slang-comments/handlers/handle-interface-definition-comments.js +4 -4
- package/dist/slang-comments/handlers/handle-interface-definition-comments.js.map +1 -1
- package/dist/slang-comments/handlers/handle-library-definition-comments.js +4 -4
- package/dist/slang-comments/handlers/handle-library-definition-comments.js.map +1 -1
- package/dist/slang-comments/handlers/handle-modifier-invocation-comments.js +2 -2
- package/dist/slang-comments/handlers/handle-modifier-invocation-comments.js.map +1 -1
- package/dist/slang-comments/handlers/handle-parameters-declaration-comments.js +4 -4
- package/dist/slang-comments/handlers/handle-parameters-declaration-comments.js.map +1 -1
- package/dist/slang-comments/handlers/handle-positional-arguments-declaration-comments.js +4 -4
- package/dist/slang-comments/handlers/handle-positional-arguments-declaration-comments.js.map +1 -1
- package/dist/slang-comments/handlers/handle-struct-comments.js +22 -0
- package/dist/slang-comments/handlers/handle-struct-comments.js.map +1 -0
- package/dist/slang-comments/handlers/handle-while-statement-comments.js +2 -2
- package/dist/slang-comments/handlers/handle-while-statement-comments.js.map +1 -1
- package/dist/slang-comments/handlers/index.js +2 -0
- package/dist/slang-comments/handlers/index.js.map +1 -1
- package/dist/slang-comments/printer.js +1 -2
- package/dist/slang-comments/printer.js.map +1 -1
- package/dist/slang-nodes/AdditiveExpression.js +12 -1
- package/dist/slang-nodes/AdditiveExpression.js.map +1 -1
- package/dist/slang-nodes/BitwiseAndExpression.js +8 -1
- package/dist/slang-nodes/BitwiseAndExpression.js.map +1 -1
- package/dist/slang-nodes/BitwiseOrExpression.js +8 -1
- package/dist/slang-nodes/BitwiseOrExpression.js.map +1 -1
- package/dist/slang-nodes/BitwiseXorExpression.js +8 -1
- package/dist/slang-nodes/BitwiseXorExpression.js.map +1 -1
- package/dist/slang-nodes/ConditionalExpression.js +11 -10
- package/dist/slang-nodes/ConditionalExpression.js.map +1 -1
- package/dist/slang-nodes/ContractDefinition.js +4 -4
- package/dist/slang-nodes/ContractDefinition.js.map +1 -1
- package/dist/slang-nodes/EqualityExpression.js +10 -2
- package/dist/slang-nodes/EqualityExpression.js.map +1 -1
- package/dist/slang-nodes/ExponentiationExpression.js +16 -2
- package/dist/slang-nodes/ExponentiationExpression.js.map +1 -1
- package/dist/slang-nodes/FallbackFunctionDefinition.js +4 -4
- package/dist/slang-nodes/FallbackFunctionDefinition.js.map +1 -1
- package/dist/slang-nodes/FunctionCallExpression.js +6 -5
- package/dist/slang-nodes/FunctionCallExpression.js.map +1 -1
- package/dist/slang-nodes/FunctionDefinition.js +4 -4
- package/dist/slang-nodes/FunctionDefinition.js.map +1 -1
- package/dist/slang-nodes/IfStatement.js +5 -4
- package/dist/slang-nodes/IfStatement.js.map +1 -1
- package/dist/slang-nodes/ImportDeconstructionSymbols.js +1 -1
- package/dist/slang-nodes/ImportDeconstructionSymbols.js.map +1 -1
- package/dist/slang-nodes/IndexAccessExpression.js +10 -9
- package/dist/slang-nodes/IndexAccessExpression.js.map +1 -1
- package/dist/slang-nodes/InequalityExpression.js +8 -2
- package/dist/slang-nodes/InequalityExpression.js.map +1 -1
- package/dist/slang-nodes/MemberAccessExpression.js +11 -13
- package/dist/slang-nodes/MemberAccessExpression.js.map +1 -1
- package/dist/slang-nodes/MultiplicativeExpression.js +13 -1
- package/dist/slang-nodes/MultiplicativeExpression.js.map +1 -1
- package/dist/slang-nodes/ReceiveFunctionDefinition.js +4 -4
- package/dist/slang-nodes/ReceiveFunctionDefinition.js.map +1 -1
- package/dist/slang-nodes/ReturnStatement.js +5 -3
- package/dist/slang-nodes/ReturnStatement.js.map +1 -1
- package/dist/slang-nodes/ShiftExpression.js +11 -1
- package/dist/slang-nodes/ShiftExpression.js.map +1 -1
- package/dist/slang-nodes/StateVariableDefinition.js +1 -4
- package/dist/slang-nodes/StateVariableDefinition.js.map +1 -1
- package/dist/slang-nodes/TupleDeconstructionStatement.js +1 -2
- package/dist/slang-nodes/TupleDeconstructionStatement.js.map +1 -1
- package/dist/slang-nodes/UnnamedFunctionDefinition.js +3 -3
- package/dist/slang-nodes/UnnamedFunctionDefinition.js.map +1 -1
- package/dist/slang-nodes/VariableDeclarationStatement.js +10 -11
- package/dist/slang-nodes/VariableDeclarationStatement.js.map +1 -1
- package/dist/slang-printers/create-binary-operation-printer.js +14 -9
- package/dist/slang-printers/create-binary-operation-printer.js.map +1 -1
- package/dist/slang-printers/print-binary-operation.js +24 -25
- package/dist/slang-printers/print-binary-operation.js.map +1 -1
- package/dist/slang-printers/print-comments.js +1 -1
- package/dist/slang-printers/print-comments.js.map +1 -1
- package/dist/slang-printers/print-logical-operation.js +5 -13
- package/dist/slang-printers/print-logical-operation.js.map +1 -1
- package/dist/slang-printers/print-preserving-empty-lines.js +1 -1
- package/dist/slang-printers/print-preserving-empty-lines.js.map +1 -1
- package/dist/slang-utils/create-hug-function.js +5 -4
- package/dist/slang-utils/create-hug-function.js.map +1 -1
- package/dist/slang-utils/create-parser.js +31 -86
- package/dist/slang-utils/create-parser.js.map +1 -1
- package/dist/slang-utils/metadata.js +31 -35
- package/dist/slang-utils/metadata.js.map +1 -1
- package/dist/slang-utils/sort-contract-specifiers.js +5 -7
- package/dist/slang-utils/sort-contract-specifiers.js.map +1 -1
- package/dist/slang-utils/sort-function-attributes.js +1 -3
- package/dist/slang-utils/sort-function-attributes.js.map +1 -1
- package/dist/slangPrinter.js +9 -16
- package/dist/slangPrinter.js.map +1 -1
- package/dist/slangSolidityParser.js +6 -9
- package/dist/slangSolidityParser.js.map +1 -1
- package/dist/standalone.js +1 -1
- package/dist/standalone.js.map +1 -1
- package/package.json +19 -19
- package/src/binary-operator-printers/addition.js +11 -0
- package/src/binary-operator-printers/bit.js +5 -2
- package/src/binary-operator-printers/equality.js +7 -0
- package/src/binary-operator-printers/exponentiation.js +22 -13
- package/src/binary-operator-printers/index.js +4 -2
- package/src/binary-operator-printers/inequality.js +8 -0
- package/src/binary-operator-printers/logical.js +12 -26
- package/src/binary-operator-printers/multiplication.js +19 -0
- package/src/binary-operator-printers/printers/binary-operation-printer.js +9 -0
- package/src/binary-operator-printers/printers/create-binary-operation-printer.js +31 -0
- package/src/binary-operator-printers/printers/create-group-if-necessary-builder.js +12 -0
- package/src/binary-operator-printers/printers/create-indent-if-necessary-builder.js +23 -0
- package/src/binary-operator-printers/shift.js +6 -2
- package/src/binary-operator-printers/utils/should-group-or-indent.js +3 -0
- package/src/comments/handlers/handleContractDefinitionComments.js +7 -4
- package/src/comments/handlers/handleModifierInvocationComments.js +7 -4
- package/src/common/printer-helpers.js +4 -15
- package/src/index.ts +2 -4
- package/src/nodes/Conditional.js +2 -2
- package/src/nodes/ExpressionStatement.js +1 -1
- package/src/nodes/FunctionDefinition.js +3 -4
- package/src/nodes/ImportDirective.js +5 -1
- package/src/options.ts +16 -0
- package/src/parser.js +4 -0
- package/src/prettier-comments/language-js/comments.js +10 -16
- package/src/printer.js +1 -4
- package/src/slang-comments/handlers/{add-collection-node-first-comment.ts → add-collection-first-comment.ts} +3 -3
- package/src/slang-comments/handlers/{add-collection-node-last-comment.ts → add-collection-last-comment.ts} +3 -3
- package/src/slang-comments/handlers/handle-block-comments.ts +4 -4
- package/src/slang-comments/handlers/handle-contract-definition-comments.ts +3 -3
- package/src/slang-comments/handlers/handle-contract-specifiers-comments.ts +2 -2
- package/src/slang-comments/handlers/handle-else-branch-comments.ts +2 -2
- package/src/slang-comments/handlers/handle-if-statement-comments.ts +3 -6
- package/src/slang-comments/handlers/handle-interface-definition-comments.ts +4 -4
- package/src/slang-comments/handlers/handle-library-definition-comments.ts +4 -4
- package/src/slang-comments/handlers/handle-modifier-invocation-comments.ts +2 -2
- package/src/slang-comments/handlers/handle-parameters-declaration-comments.ts +4 -4
- package/src/slang-comments/handlers/handle-positional-arguments-declaration-comments.ts +4 -4
- package/src/slang-comments/handlers/handle-struct-comments.ts +39 -0
- package/src/slang-comments/handlers/handle-while-statement-comments.ts +2 -2
- package/src/slang-comments/handlers/index.ts +2 -0
- package/src/slang-comments/printer.ts +1 -3
- package/src/slang-nodes/AdditiveExpression.ts +15 -1
- package/src/slang-nodes/BitwiseAndExpression.ts +11 -1
- package/src/slang-nodes/BitwiseOrExpression.ts +14 -4
- package/src/slang-nodes/BitwiseXorExpression.ts +11 -1
- package/src/slang-nodes/ConditionalExpression.ts +10 -10
- package/src/slang-nodes/ContractDefinition.ts +4 -4
- package/src/slang-nodes/ContractSpecifier.ts +2 -2
- package/src/slang-nodes/ContractSpecifiers.ts +2 -2
- package/src/slang-nodes/EqualityExpression.ts +15 -2
- package/src/slang-nodes/ExponentiationExpression.ts +17 -4
- package/src/slang-nodes/FallbackFunctionDefinition.ts +4 -4
- package/src/slang-nodes/FunctionCallExpression.ts +6 -5
- package/src/slang-nodes/FunctionDefinition.ts +4 -4
- package/src/slang-nodes/IfStatement.ts +5 -4
- package/src/slang-nodes/ImportDeconstructionSymbols.ts +1 -1
- package/src/slang-nodes/IndexAccessExpression.ts +10 -10
- package/src/slang-nodes/InequalityExpression.ts +11 -2
- package/src/slang-nodes/MemberAccessExpression.ts +13 -17
- package/src/slang-nodes/MultiplicativeExpression.ts +16 -1
- package/src/slang-nodes/PositionalArguments.ts +2 -2
- package/src/slang-nodes/ReceiveFunctionDefinition.ts +4 -4
- package/src/slang-nodes/ReturnStatement.ts +4 -3
- package/src/slang-nodes/ShiftExpression.ts +14 -1
- package/src/slang-nodes/StateVariableDefinition.ts +1 -5
- package/src/slang-nodes/StorageLayoutSpecifier.ts +2 -2
- package/src/slang-nodes/StringLiteral.ts +1 -1
- package/src/slang-nodes/TupleDeconstructionStatement.ts +4 -6
- package/src/slang-nodes/UnnamedFunctionDefinition.ts +3 -3
- package/src/slang-nodes/VariableDeclarationStatement.ts +13 -15
- package/src/slang-nodes/types.d.ts +9 -1
- package/src/slang-printers/create-binary-operation-printer.ts +16 -11
- package/src/slang-printers/print-binary-operation.ts +48 -39
- package/src/slang-printers/print-comments.ts +1 -1
- package/src/slang-printers/print-logical-operation.ts +8 -17
- package/src/slang-printers/print-preserving-empty-lines.ts +3 -7
- package/src/slang-utils/create-hug-function.ts +5 -5
- package/src/slang-utils/create-parser.ts +56 -119
- package/src/slang-utils/metadata.ts +33 -37
- package/src/slang-utils/sort-contract-specifiers.ts +6 -9
- package/src/slang-utils/sort-function-attributes.ts +2 -5
- package/src/slangPrinter.ts +11 -21
- package/src/slangSolidityParser.ts +9 -12
- package/dist/binary-operator-printers/arithmetic.js +0 -44
- package/dist/binary-operator-printers/arithmetic.js.map +0 -1
- package/dist/binary-operator-printers/comparison.js +0 -38
- package/dist/binary-operator-printers/comparison.js.map +0 -1
- package/dist/common/backward-compatibility.js +0 -27
- package/dist/common/backward-compatibility.js.map +0 -1
- package/dist/ea6945b7193921081ced.wasm +0 -0
- package/dist/slang-comments/handlers/add-collection-node-first-comment.js.map +0 -1
- package/dist/slang-comments/handlers/add-collection-node-last-comment.js.map +0 -1
- package/dist/slang-printers/print-comparison-operation.js +0 -35
- package/dist/slang-printers/print-comparison-operation.js.map +0 -1
- package/src/binary-operator-printers/arithmetic.js +0 -51
- package/src/binary-operator-printers/comparison.js +0 -36
- package/src/common/backward-compatibility.js +0 -34
- package/src/slang-printers/print-comparison-operation.ts +0 -45
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { doc } from 'prettier';
|
|
2
2
|
import { NonterminalKind } from '@nomicfoundation/slang/cst';
|
|
3
3
|
import { createBinaryOperationPrinter } from '../slang-printers/create-binary-operation-printer.js';
|
|
4
|
+
import { binaryIndentRulesBuilder } from '../slang-printers/print-binary-operation.js';
|
|
4
5
|
import { createHugFunction } from '../slang-utils/create-hug-function.js';
|
|
6
|
+
import { createKindCheckFunction } from '../slang-utils/create-kind-check-function.js';
|
|
5
7
|
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
|
|
6
8
|
import { Expression } from './Expression.js';
|
|
7
9
|
|
|
@@ -10,17 +12,28 @@ import type { AstPath, Doc, ParserOptions } from 'prettier';
|
|
|
10
12
|
import type { AstNode } from './types.d.ts';
|
|
11
13
|
import type { PrintFunction, SlangNode } from '../types.d.ts';
|
|
12
14
|
|
|
13
|
-
const { group
|
|
15
|
+
const { group } = doc.builders;
|
|
14
16
|
|
|
15
17
|
const tryToHug = createHugFunction(['**']);
|
|
16
18
|
|
|
19
|
+
const shouldIndent = createKindCheckFunction([
|
|
20
|
+
NonterminalKind.MultiplicativeExpression,
|
|
21
|
+
NonterminalKind.AdditiveExpression,
|
|
22
|
+
NonterminalKind.ShiftExpression,
|
|
23
|
+
NonterminalKind.BitwiseAndExpression,
|
|
24
|
+
NonterminalKind.BitwiseOrExpression,
|
|
25
|
+
NonterminalKind.BitwiseXorExpression,
|
|
26
|
+
NonterminalKind.InequalityExpression,
|
|
27
|
+
NonterminalKind.EqualityExpression,
|
|
28
|
+
NonterminalKind.AndExpression,
|
|
29
|
+
NonterminalKind.OrExpression
|
|
30
|
+
]);
|
|
31
|
+
|
|
17
32
|
const printExponentiationExpression = createBinaryOperationPrinter(
|
|
18
33
|
() =>
|
|
19
34
|
(document: Doc): Doc =>
|
|
20
35
|
group(document), // always group
|
|
21
|
-
()
|
|
22
|
-
(document: Doc): Doc =>
|
|
23
|
-
indent(document) // always indent
|
|
36
|
+
binaryIndentRulesBuilder(shouldIndent) // indent as a binary operation with some exceptions
|
|
24
37
|
);
|
|
25
38
|
|
|
26
39
|
export class ExponentiationExpression implements SlangNode {
|
|
@@ -53,12 +53,12 @@ export class FallbackFunctionDefinition implements SlangNode {
|
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
cleanModifierInvocationArguments(): void {
|
|
56
|
-
for (const attribute of this.attributes.items) {
|
|
56
|
+
for (const { variant: attribute } of this.attributes.items) {
|
|
57
57
|
if (
|
|
58
|
-
typeof attribute
|
|
59
|
-
attribute.
|
|
58
|
+
typeof attribute !== 'string' &&
|
|
59
|
+
attribute.kind === NonterminalKind.ModifierInvocation
|
|
60
60
|
) {
|
|
61
|
-
attribute.
|
|
61
|
+
attribute.cleanModifierInvocationArguments();
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
64
|
}
|
|
@@ -39,18 +39,19 @@ export class FunctionCallExpression implements SlangNode {
|
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
print(path: AstPath<FunctionCallExpression>, print: PrintFunction): Doc {
|
|
42
|
-
|
|
43
|
-
|
|
42
|
+
const operandDoc = path.call(print, 'operand');
|
|
43
|
+
const argumentsDoc = path.call(print, 'arguments');
|
|
44
44
|
|
|
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
48
|
const groupId = Symbol('Slang.FunctionCallExpression.operand');
|
|
49
|
-
operandDoc = group(operandDoc.contents, { id: groupId });
|
|
50
|
-
argumentsDoc = indentIfBreak(argumentsDoc, { groupId });
|
|
51
49
|
// We wrap the expression in a label in case there is an IndexAccess or
|
|
52
50
|
// a FunctionCall following this IndexAccess.
|
|
53
|
-
return label('MemberAccessChain', [
|
|
51
|
+
return label('MemberAccessChain', [
|
|
52
|
+
group(operandDoc.contents, { id: groupId }),
|
|
53
|
+
indentIfBreak(argumentsDoc, { groupId })
|
|
54
|
+
]);
|
|
54
55
|
}
|
|
55
56
|
|
|
56
57
|
return [operandDoc, argumentsDoc].flat();
|
|
@@ -61,12 +61,12 @@ export class FunctionDefinition implements SlangNode {
|
|
|
61
61
|
}
|
|
62
62
|
|
|
63
63
|
cleanModifierInvocationArguments(): void {
|
|
64
|
-
for (const attribute of this.attributes.items) {
|
|
64
|
+
for (const { variant: attribute } of this.attributes.items) {
|
|
65
65
|
if (
|
|
66
|
-
typeof attribute
|
|
67
|
-
attribute.
|
|
66
|
+
typeof attribute !== 'string' &&
|
|
67
|
+
attribute.kind === NonterminalKind.ModifierInvocation
|
|
68
68
|
) {
|
|
69
|
-
attribute.
|
|
69
|
+
attribute.cleanModifierInvocationArguments();
|
|
70
70
|
}
|
|
71
71
|
}
|
|
72
72
|
}
|
|
@@ -47,19 +47,20 @@ export class IfStatement implements SlangNode {
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
print(path: AstPath<IfStatement>, print: PrintFunction): Doc {
|
|
50
|
+
const { kind: bodyKind, comments: bodyComments } = this.body.variant;
|
|
50
51
|
return [
|
|
51
52
|
'if (',
|
|
52
53
|
printSeparatedItem(path.call(print, 'condition')),
|
|
53
54
|
')',
|
|
54
|
-
|
|
55
|
+
bodyKind === NonterminalKind.Block
|
|
55
56
|
? [' ', path.call(print, 'body')]
|
|
56
57
|
: group(indent([line, path.call(print, 'body')]), {
|
|
57
|
-
shouldBreak:
|
|
58
|
+
shouldBreak: bodyKind === NonterminalKind.IfStatement // `if` within `if`
|
|
58
59
|
}),
|
|
59
60
|
this.elseBranch
|
|
60
61
|
? [
|
|
61
|
-
|
|
62
|
-
|
|
62
|
+
bodyKind !== NonterminalKind.Block || // else on a new line if body is not a block
|
|
63
|
+
bodyComments.some(
|
|
63
64
|
(comment) =>
|
|
64
65
|
!isBlockComment(comment) || comment.placement === 'ownLine'
|
|
65
66
|
) // or if body has trailing single line comments or a block comment on a new line
|
|
@@ -43,7 +43,7 @@ export class ImportDeconstructionSymbols implements SlangNode {
|
|
|
43
43
|
const compiler = coerce(options.compiler);
|
|
44
44
|
return printSeparatedList(
|
|
45
45
|
path.map(print, 'items'),
|
|
46
|
-
compiler && satisfies(compiler, '>=0.7.4')
|
|
46
|
+
compiler && satisfies(compiler, '>=0.7.4') && this.items.length > 1
|
|
47
47
|
? {
|
|
48
48
|
// if the compiler exists and is greater than or equal to 0.7.4 we will
|
|
49
49
|
// split the ImportDirective.
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { doc } from 'prettier';
|
|
2
2
|
import { NonterminalKind } from '@nomicfoundation/slang/cst';
|
|
3
|
+
import { printSeparatedItem } from '../slang-printers/print-separated-item.js';
|
|
3
4
|
import { isLabel } from '../slang-utils/is-label.js';
|
|
4
5
|
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
|
|
5
6
|
import { Expression } from './Expression.js';
|
|
@@ -10,7 +11,7 @@ import type { AstPath, Doc, ParserOptions } from 'prettier';
|
|
|
10
11
|
import type { AstNode } from './types.d.ts';
|
|
11
12
|
import type { PrintFunction, SlangNode } from '../types.d.ts';
|
|
12
13
|
|
|
13
|
-
const { group,
|
|
14
|
+
const { group, indentIfBreak, label } = doc.builders;
|
|
14
15
|
|
|
15
16
|
export class IndexAccessExpression implements SlangNode {
|
|
16
17
|
readonly kind = NonterminalKind.IndexAccessExpression;
|
|
@@ -43,24 +44,23 @@ export class IndexAccessExpression implements SlangNode {
|
|
|
43
44
|
}
|
|
44
45
|
|
|
45
46
|
print(path: AstPath<IndexAccessExpression>, print: PrintFunction): Doc {
|
|
46
|
-
|
|
47
|
-
|
|
47
|
+
const operandDoc = path.call(print, 'operand');
|
|
48
|
+
const indexDoc = [
|
|
48
49
|
'[',
|
|
49
|
-
|
|
50
|
-
softline,
|
|
50
|
+
printSeparatedItem([path.call(print, 'start'), path.call(print, 'end')]),
|
|
51
51
|
']'
|
|
52
|
-
]
|
|
52
|
+
];
|
|
53
53
|
|
|
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
57
|
const groupId = Symbol('Slang.IndexAccessExpression.operand');
|
|
58
|
-
operandDoc = group(operandDoc.contents, { id: groupId });
|
|
59
|
-
|
|
60
|
-
indexDoc = indentIfBreak(indexDoc, { groupId });
|
|
61
58
|
// We wrap the expression in a label in case there is an IndexAccess or
|
|
62
59
|
// a FunctionCall following this IndexAccess.
|
|
63
|
-
return label('MemberAccessChain', [
|
|
60
|
+
return label('MemberAccessChain', [
|
|
61
|
+
group(operandDoc.contents, { id: groupId }),
|
|
62
|
+
indentIfBreak(indexDoc, { groupId })
|
|
63
|
+
]);
|
|
64
64
|
}
|
|
65
65
|
|
|
66
66
|
return [operandDoc, indexDoc].flat();
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { NonterminalKind } from '@nomicfoundation/slang/cst';
|
|
2
|
-
import {
|
|
2
|
+
import { createKindCheckFunction } from '../slang-utils/create-kind-check-function.js';
|
|
3
|
+
import { printBinaryOperation } from '../slang-printers/print-binary-operation.js';
|
|
3
4
|
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
|
|
4
5
|
import { Expression } from './Expression.js';
|
|
5
6
|
|
|
@@ -8,6 +9,14 @@ import type { AstPath, Doc, ParserOptions } from 'prettier';
|
|
|
8
9
|
import type { AstNode } from './types.d.ts';
|
|
9
10
|
import type { PrintFunction, SlangNode } from '../types.d.ts';
|
|
10
11
|
|
|
12
|
+
const printComparisonExpression = printBinaryOperation(
|
|
13
|
+
createKindCheckFunction([
|
|
14
|
+
NonterminalKind.EqualityExpression,
|
|
15
|
+
NonterminalKind.AndExpression,
|
|
16
|
+
NonterminalKind.OrExpression
|
|
17
|
+
])
|
|
18
|
+
);
|
|
19
|
+
|
|
11
20
|
export class InequalityExpression implements SlangNode {
|
|
12
21
|
readonly kind = NonterminalKind.InequalityExpression;
|
|
13
22
|
|
|
@@ -39,6 +48,6 @@ export class InequalityExpression implements SlangNode {
|
|
|
39
48
|
print: PrintFunction,
|
|
40
49
|
options: ParserOptions<AstNode>
|
|
41
50
|
): Doc {
|
|
42
|
-
return
|
|
51
|
+
return printComparisonExpression(this, path, print, options);
|
|
43
52
|
}
|
|
44
53
|
}
|
|
@@ -24,30 +24,26 @@ function isEndOfChain(
|
|
|
24
24
|
path: AstPath<StrictAstNode>
|
|
25
25
|
): boolean {
|
|
26
26
|
for (
|
|
27
|
-
let i =
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
isChainableExpression(grandparentNode);
|
|
31
|
-
i += 2,
|
|
32
|
-
currentNode = grandparentNode,
|
|
33
|
-
grandparentNode = path.getNode(i + 2)!
|
|
27
|
+
let i = 2, current: StrictAstNode = node, grandparent = path.getNode(i)!;
|
|
28
|
+
isChainableExpression(grandparent);
|
|
29
|
+
i += 2, current = grandparent, grandparent = path.getNode(i)!
|
|
34
30
|
) {
|
|
35
|
-
switch (
|
|
31
|
+
switch (grandparent.kind) {
|
|
36
32
|
case NonterminalKind.MemberAccessExpression:
|
|
37
|
-
// If
|
|
38
|
-
// chain.
|
|
33
|
+
// If `grandparent` is a MemberAccessExpression we are not at the end
|
|
34
|
+
// of the chain.
|
|
39
35
|
return false;
|
|
40
36
|
case NonterminalKind.IndexAccessExpression:
|
|
41
|
-
// If
|
|
42
|
-
// operand then it must be the start or the end in which case it is
|
|
43
|
-
// end of the chain.
|
|
44
|
-
if (
|
|
37
|
+
// If `grandparent` is an IndexAccessExpression and `current` is not
|
|
38
|
+
// the operand then it must be the start or the end in which case it is
|
|
39
|
+
// the end of the chain.
|
|
40
|
+
if (current !== grandparent.operand.variant) return true;
|
|
45
41
|
break;
|
|
46
42
|
case NonterminalKind.FunctionCallExpression:
|
|
47
|
-
// If
|
|
48
|
-
// operand then it must be and argument in which case it is the end
|
|
43
|
+
// If `grandparent` is a FunctionCallExpression and `current` is not
|
|
44
|
+
// the operand then it must be and argument in which case it is the end
|
|
49
45
|
// of the chain.
|
|
50
|
-
if (
|
|
46
|
+
if (current !== grandparent.operand.variant) return true;
|
|
51
47
|
break;
|
|
52
48
|
}
|
|
53
49
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { NonterminalKind } from '@nomicfoundation/slang/cst';
|
|
2
2
|
import { printBinaryOperation } from '../slang-printers/print-binary-operation.js';
|
|
3
3
|
import { createHugFunction } from '../slang-utils/create-hug-function.js';
|
|
4
|
+
import { createKindCheckFunction } from '../slang-utils/create-kind-check-function.js';
|
|
4
5
|
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
|
|
5
6
|
import { Expression } from './Expression.js';
|
|
6
7
|
|
|
@@ -13,6 +14,20 @@ const multiplicationTryToHug = createHugFunction(['/', '%']);
|
|
|
13
14
|
const divisionTryToHug = createHugFunction(['*', '%']);
|
|
14
15
|
const moduloTryToHug = createHugFunction(['*', '/', '%']);
|
|
15
16
|
|
|
17
|
+
export const printMultiplicativeExpression = printBinaryOperation(
|
|
18
|
+
createKindCheckFunction([
|
|
19
|
+
NonterminalKind.AdditiveExpression,
|
|
20
|
+
NonterminalKind.ShiftExpression,
|
|
21
|
+
NonterminalKind.BitwiseAndExpression,
|
|
22
|
+
NonterminalKind.BitwiseOrExpression,
|
|
23
|
+
NonterminalKind.BitwiseXorExpression,
|
|
24
|
+
NonterminalKind.InequalityExpression,
|
|
25
|
+
NonterminalKind.EqualityExpression,
|
|
26
|
+
NonterminalKind.AndExpression,
|
|
27
|
+
NonterminalKind.OrExpression
|
|
28
|
+
])
|
|
29
|
+
);
|
|
30
|
+
|
|
16
31
|
export class MultiplicativeExpression implements SlangNode {
|
|
17
32
|
readonly kind = NonterminalKind.MultiplicativeExpression;
|
|
18
33
|
|
|
@@ -61,6 +76,6 @@ export class MultiplicativeExpression implements SlangNode {
|
|
|
61
76
|
print: PrintFunction,
|
|
62
77
|
options: ParserOptions<AstNode>
|
|
63
78
|
): Doc {
|
|
64
|
-
return
|
|
79
|
+
return printMultiplicativeExpression(this, path, print, options);
|
|
65
80
|
}
|
|
66
81
|
}
|
|
@@ -17,9 +17,9 @@ export class PositionalArguments implements SlangNode {
|
|
|
17
17
|
|
|
18
18
|
loc;
|
|
19
19
|
|
|
20
|
-
items;
|
|
20
|
+
items: Expression[];
|
|
21
21
|
|
|
22
|
-
separators;
|
|
22
|
+
separators: string[];
|
|
23
23
|
|
|
24
24
|
constructor(ast: ast.PositionalArguments, options: ParserOptions<AstNode>) {
|
|
25
25
|
let metadata = getNodeMetadata(ast, true);
|
|
@@ -46,12 +46,12 @@ export class ReceiveFunctionDefinition implements SlangNode {
|
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
cleanModifierInvocationArguments(): void {
|
|
49
|
-
for (const attribute of this.attributes.items) {
|
|
49
|
+
for (const { variant: attribute } of this.attributes.items) {
|
|
50
50
|
if (
|
|
51
|
-
typeof attribute
|
|
52
|
-
attribute.
|
|
51
|
+
typeof attribute !== 'string' &&
|
|
52
|
+
attribute.kind === NonterminalKind.ModifierInvocation
|
|
53
53
|
) {
|
|
54
|
-
attribute.
|
|
54
|
+
attribute.cleanModifierInvocationArguments();
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
}
|
|
@@ -16,10 +16,11 @@ function printExpression(
|
|
|
16
16
|
print: PrintFunction,
|
|
17
17
|
options: ParserOptions<AstNode>
|
|
18
18
|
): Doc {
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
const expressionVariant = node.expression?.variant;
|
|
20
|
+
if (expressionVariant) {
|
|
21
|
+
return expressionVariant.kind === NonterminalKind.TupleExpression ||
|
|
21
22
|
(options.experimentalTernaries &&
|
|
22
|
-
|
|
23
|
+
expressionVariant.kind === NonterminalKind.ConditionalExpression)
|
|
23
24
|
? [' ', path.call(print, 'expression')]
|
|
24
25
|
: group(indent([line, path.call(print, 'expression')]));
|
|
25
26
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { NonterminalKind } from '@nomicfoundation/slang/cst';
|
|
2
2
|
import { printBinaryOperation } from '../slang-printers/print-binary-operation.js';
|
|
3
3
|
import { createHugFunction } from '../slang-utils/create-hug-function.js';
|
|
4
|
+
import { createKindCheckFunction } from '../slang-utils/create-kind-check-function.js';
|
|
4
5
|
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
|
|
5
6
|
import { Expression } from './Expression.js';
|
|
6
7
|
|
|
@@ -20,6 +21,18 @@ const tryToHugLeftOperand = createHugFunction([
|
|
|
20
21
|
]);
|
|
21
22
|
const tryToHugRightOperand = createHugFunction(['+', '-', '*', '/', '**']);
|
|
22
23
|
|
|
24
|
+
const printShiftExpression = printBinaryOperation(
|
|
25
|
+
createKindCheckFunction([
|
|
26
|
+
NonterminalKind.BitwiseAndExpression,
|
|
27
|
+
NonterminalKind.BitwiseOrExpression,
|
|
28
|
+
NonterminalKind.BitwiseXorExpression,
|
|
29
|
+
NonterminalKind.InequalityExpression,
|
|
30
|
+
NonterminalKind.EqualityExpression,
|
|
31
|
+
NonterminalKind.AndExpression,
|
|
32
|
+
NonterminalKind.OrExpression
|
|
33
|
+
])
|
|
34
|
+
);
|
|
35
|
+
|
|
23
36
|
export class ShiftExpression implements SlangNode {
|
|
24
37
|
readonly kind = NonterminalKind.ShiftExpression;
|
|
25
38
|
|
|
@@ -54,6 +67,6 @@ export class ShiftExpression implements SlangNode {
|
|
|
54
67
|
print: PrintFunction,
|
|
55
68
|
options: ParserOptions<AstNode>
|
|
56
69
|
): Doc {
|
|
57
|
-
return
|
|
70
|
+
return printShiftExpression(this, path, print, options);
|
|
58
71
|
}
|
|
59
72
|
}
|
|
@@ -53,13 +53,9 @@ export class StateVariableDefinition implements SlangNode {
|
|
|
53
53
|
|
|
54
54
|
print(path: AstPath<StateVariableDefinition>, print: PrintFunction): Doc {
|
|
55
55
|
const groupId = Symbol('Slang.StateVariableDefinition.attributes');
|
|
56
|
-
const attributesDoc = group(indent(path.call(print, 'attributes')), {
|
|
57
|
-
id: groupId
|
|
58
|
-
});
|
|
59
|
-
|
|
60
56
|
return [
|
|
61
57
|
path.call(print, 'typeName'),
|
|
62
|
-
|
|
58
|
+
group(indent(path.call(print, 'attributes')), { id: groupId }),
|
|
63
59
|
' ',
|
|
64
60
|
path.call(print, 'name'),
|
|
65
61
|
this.value ? indentIfBreak(path.call(print, 'value'), { groupId }) : '',
|
|
@@ -6,8 +6,8 @@ import { Expression } from './Expression.js';
|
|
|
6
6
|
|
|
7
7
|
import type * as ast from '@nomicfoundation/slang/ast';
|
|
8
8
|
import type { AstPath, Doc, ParserOptions } from 'prettier';
|
|
9
|
-
import type { AstNode } from './types.
|
|
10
|
-
import type { PrintFunction, SlangNode } from '../types.
|
|
9
|
+
import type { AstNode } from './types.d.ts';
|
|
10
|
+
import type { PrintFunction, SlangNode } from '../types.d.ts';
|
|
11
11
|
|
|
12
12
|
const { line } = doc.builders;
|
|
13
13
|
|
|
@@ -45,13 +45,11 @@ export class TupleDeconstructionStatement implements SlangNode {
|
|
|
45
45
|
print: PrintFunction
|
|
46
46
|
): Doc {
|
|
47
47
|
const groupId = Symbol('Slang.VariableDeclarationStatement.variables');
|
|
48
|
-
const declarationDoc = group(
|
|
49
|
-
[this.varKeyword ? 'var (' : '(', path.call(print, 'elements'), ')'],
|
|
50
|
-
{ id: groupId }
|
|
51
|
-
);
|
|
52
|
-
|
|
53
48
|
return [
|
|
54
|
-
|
|
49
|
+
group(
|
|
50
|
+
[this.varKeyword ? 'var (' : '(', path.call(print, 'elements'), ')'],
|
|
51
|
+
{ id: groupId }
|
|
52
|
+
),
|
|
55
53
|
indentIfBreak([' = ', path.call(print, 'expression'), ';'], { groupId })
|
|
56
54
|
];
|
|
57
55
|
}
|
|
@@ -46,9 +46,9 @@ export class UnnamedFunctionDefinition implements SlangNode {
|
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
cleanModifierInvocationArguments(): void {
|
|
49
|
-
for (const attribute of this.attributes.items) {
|
|
50
|
-
if (typeof attribute
|
|
51
|
-
attribute.
|
|
49
|
+
for (const { variant: attribute } of this.attributes.items) {
|
|
50
|
+
if (typeof attribute !== 'string') {
|
|
51
|
+
attribute.cleanModifierInvocationArguments();
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
54
|
}
|
|
@@ -58,22 +58,20 @@ export class VariableDeclarationStatement implements SlangNode {
|
|
|
58
58
|
print: PrintFunction
|
|
59
59
|
): Doc {
|
|
60
60
|
const groupId = Symbol('Slang.VariableDeclarationStatement.variables');
|
|
61
|
-
const declarationDoc = group(
|
|
62
|
-
[
|
|
63
|
-
path.call(print, 'variableType'),
|
|
64
|
-
indent([
|
|
65
|
-
this.storageLocation
|
|
66
|
-
? [line, path.call(print, 'storageLocation')]
|
|
67
|
-
: '',
|
|
68
|
-
' ',
|
|
69
|
-
path.call(print, 'name')
|
|
70
|
-
])
|
|
71
|
-
],
|
|
72
|
-
{ id: groupId }
|
|
73
|
-
);
|
|
74
|
-
|
|
75
61
|
return [
|
|
76
|
-
|
|
62
|
+
group(
|
|
63
|
+
[
|
|
64
|
+
path.call(print, 'variableType'),
|
|
65
|
+
indent([
|
|
66
|
+
this.storageLocation
|
|
67
|
+
? [line, path.call(print, 'storageLocation')]
|
|
68
|
+
: '',
|
|
69
|
+
' ',
|
|
70
|
+
path.call(print, 'name')
|
|
71
|
+
])
|
|
72
|
+
],
|
|
73
|
+
{ id: groupId }
|
|
74
|
+
),
|
|
77
75
|
indentIfBreak(path.call(print, 'value'), { groupId }),
|
|
78
76
|
';'
|
|
79
77
|
];
|
|
@@ -460,7 +460,15 @@ export type StrictAstNode =
|
|
|
460
460
|
| YulPaths
|
|
461
461
|
| YulPath;
|
|
462
462
|
|
|
463
|
-
export type
|
|
463
|
+
export type NodeCollection = Extract<
|
|
464
|
+
StrictAstNode,
|
|
465
|
+
{ items: StrictAstNode[] | Identifier[] | YulIdentifier[] }
|
|
466
|
+
>;
|
|
467
|
+
|
|
468
|
+
export type Collection = Extract<
|
|
469
|
+
StrictAstNode,
|
|
470
|
+
{ items: StrictAstNode[] | Identifier[] | YulIdentifier[] | string[] }
|
|
471
|
+
>;
|
|
464
472
|
|
|
465
473
|
export type BinaryOperation = Extract<
|
|
466
474
|
StrictAstNode,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { TerminalKind } from '@nomicfoundation/slang/cst';
|
|
1
|
+
import { NonterminalKind, TerminalKind } from '@nomicfoundation/slang/cst';
|
|
2
2
|
import { doc } from 'prettier';
|
|
3
3
|
import { isBinaryOperation } from '../slang-utils/is-binary-operation.js';
|
|
4
4
|
|
|
@@ -15,17 +15,23 @@ const { group, line } = doc.builders;
|
|
|
15
15
|
function rightOperandPrint(
|
|
16
16
|
node: BinaryOperation,
|
|
17
17
|
path: AstPath<BinaryOperation>,
|
|
18
|
-
print: PrintFunction
|
|
18
|
+
print: PrintFunction,
|
|
19
|
+
options: ParserOptions<AstNode>
|
|
19
20
|
): Doc {
|
|
20
|
-
const rightOperand =
|
|
21
|
+
const rightOperand =
|
|
22
|
+
options.experimentalOperatorPosition === 'end'
|
|
23
|
+
? [` ${node.operator}`, line, path.call(print, 'rightOperand')]
|
|
24
|
+
: [line, `${node.operator} `, path.call(print, 'rightOperand')];
|
|
21
25
|
|
|
22
|
-
// If
|
|
23
|
-
//
|
|
26
|
+
// If there's only a single binary expression, we want to create a group in
|
|
27
|
+
// order to avoid having a small right part like -1 be on its own line.
|
|
28
|
+
const leftOperand = node.leftOperand.variant;
|
|
29
|
+
const grandparentNode = path.grandparent as StrictAstNode;
|
|
24
30
|
const shouldGroup =
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
31
|
+
(leftOperand.kind === TerminalKind.Identifier ||
|
|
32
|
+
!isBinaryOperation(leftOperand)) &&
|
|
33
|
+
(!isBinaryOperation(grandparentNode) ||
|
|
34
|
+
grandparentNode.kind === NonterminalKind.AssignmentExpression);
|
|
29
35
|
|
|
30
36
|
return shouldGroup ? group(rightOperand) : rightOperand;
|
|
31
37
|
}
|
|
@@ -51,7 +57,6 @@ export const createBinaryOperationPrinter =
|
|
|
51
57
|
|
|
52
58
|
return groupRules([
|
|
53
59
|
path.call(print, 'leftOperand'),
|
|
54
|
-
|
|
55
|
-
indentRules(rightOperandPrint(node, path, print))
|
|
60
|
+
indentRules(rightOperandPrint(node, path, print, options))
|
|
56
61
|
]);
|
|
57
62
|
};
|