prettier-plugin-solidity 2.0.0-beta.8 → 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 +63 -59
- 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 +9 -5
- 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 +5 -7
- 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/ContractDefinition.js +30 -10
- package/dist/nodes/ContractDefinition.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 +2 -2
- 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 +20 -10
- 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-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-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/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 +6 -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 +17 -22
- package/dist/slang-nodes/ConditionalExpression.js.map +1 -1
- package/dist/slang-nodes/ContractDefinition.js +9 -10
- 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/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 +7 -7
- 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 +11 -11
- 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 +6 -6
- 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 +3 -9
- 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 -5
- 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 +12 -12
- 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 +12 -0
- package/dist/slang-utils/sort-contract-specifiers.js.map +1 -0
- 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 +10 -18
- 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 +23 -20
- 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 +18 -6
- package/src/comments/handlers/handleModifierInvocationComments.js +7 -4
- package/src/common/printer-helpers.js +4 -15
- package/src/index.ts +6 -8
- package/src/nodes/Conditional.js +2 -2
- package/src/nodes/ContractDefinition.js +31 -11
- 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 +2 -5
- 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 +23 -15
- package/src/slang-comments/handlers/handle-contract-specifiers-comments.ts +33 -0
- 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-storage-layout-specifier-comments.ts +23 -0
- 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 +6 -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 +14 -20
- package/src/slang-nodes/ContractDefinition.ts +10 -11
- package/src/slang-nodes/ContractSpecifier.ts +48 -0
- package/src/slang-nodes/ContractSpecifiers.ts +52 -0
- 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 +7 -7
- 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 +11 -12
- 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 +5 -6
- package/src/slang-nodes/ShiftExpression.ts +14 -1
- package/src/slang-nodes/StateVariableDefinition.ts +3 -10
- package/src/slang-nodes/StateVariableDefinitionValue.ts +1 -2
- package/src/slang-nodes/StorageLayoutSpecifier.ts +45 -0
- package/src/slang-nodes/StringLiteral.ts +1 -1
- package/src/slang-nodes/TupleDeconstructionStatement.ts +6 -9
- package/src/slang-nodes/UnnamedFunctionDefinition.ts +3 -3
- package/src/slang-nodes/VariableDeclarationStatement.ts +15 -16
- package/src/slang-nodes/types.d.ts +15 -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 +22 -0
- package/src/slang-utils/sort-function-attributes.ts +2 -5
- package/src/slangPrinter.ts +12 -23
- package/src/slangSolidityParser.ts +9 -12
- package/src/types.d.ts +7 -219
- package/dist/78244c53725e2131398e.wasm +0 -0
- 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/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/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/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
- package/src/slang-utils/prettier-version-satisfies.ts +0 -8
- package/src/slang-utils/print-warning.ts +0 -12
|
@@ -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
|
|
|
@@ -21,6 +22,15 @@ const tryToHug = createHugFunction([
|
|
|
21
22
|
'^'
|
|
22
23
|
]);
|
|
23
24
|
|
|
25
|
+
const printBitwiseOrExpression = printBinaryOperation(
|
|
26
|
+
createKindCheckFunction([
|
|
27
|
+
NonterminalKind.InequalityExpression,
|
|
28
|
+
NonterminalKind.EqualityExpression,
|
|
29
|
+
NonterminalKind.AndExpression,
|
|
30
|
+
NonterminalKind.OrExpression
|
|
31
|
+
])
|
|
32
|
+
);
|
|
33
|
+
|
|
24
34
|
export class BitwiseOrExpression implements SlangNode {
|
|
25
35
|
readonly kind = NonterminalKind.BitwiseOrExpression;
|
|
26
36
|
|
|
@@ -28,11 +38,11 @@ export class BitwiseOrExpression implements SlangNode {
|
|
|
28
38
|
|
|
29
39
|
loc;
|
|
30
40
|
|
|
31
|
-
leftOperand;
|
|
41
|
+
leftOperand: Expression;
|
|
32
42
|
|
|
33
|
-
operator;
|
|
43
|
+
operator: string;
|
|
34
44
|
|
|
35
|
-
rightOperand;
|
|
45
|
+
rightOperand: Expression;
|
|
36
46
|
|
|
37
47
|
constructor(ast: ast.BitwiseOrExpression, options: ParserOptions<AstNode>) {
|
|
38
48
|
let metadata = getNodeMetadata(ast);
|
|
@@ -55,6 +65,6 @@ export class BitwiseOrExpression implements SlangNode {
|
|
|
55
65
|
print: PrintFunction,
|
|
56
66
|
options: ParserOptions<AstNode>
|
|
57
67
|
): Doc {
|
|
58
|
-
return
|
|
68
|
+
return printBitwiseOrExpression(this, path, print, options);
|
|
59
69
|
}
|
|
60
70
|
}
|
|
@@ -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
|
|
|
@@ -11,6 +12,15 @@ import type { PrintFunction, SlangNode } from '../types.d.ts';
|
|
|
11
12
|
|
|
12
13
|
const tryToHug = createHugFunction(['+', '-', '*', '/', '**', '<<', '>>', '&']);
|
|
13
14
|
|
|
15
|
+
const printBitwiseXorExpression = printBinaryOperation(
|
|
16
|
+
createKindCheckFunction([
|
|
17
|
+
NonterminalKind.InequalityExpression,
|
|
18
|
+
NonterminalKind.EqualityExpression,
|
|
19
|
+
NonterminalKind.AndExpression,
|
|
20
|
+
NonterminalKind.OrExpression
|
|
21
|
+
])
|
|
22
|
+
);
|
|
23
|
+
|
|
14
24
|
export class BitwiseXorExpression implements SlangNode {
|
|
15
25
|
readonly kind = NonterminalKind.BitwiseXorExpression;
|
|
16
26
|
|
|
@@ -45,6 +55,6 @@ export class BitwiseXorExpression implements SlangNode {
|
|
|
45
55
|
print: PrintFunction,
|
|
46
56
|
options: ParserOptions<AstNode>
|
|
47
57
|
): Doc {
|
|
48
|
-
return
|
|
58
|
+
return printBitwiseXorExpression(this, path, print, options);
|
|
49
59
|
}
|
|
50
60
|
}
|
|
@@ -11,11 +11,11 @@ import type { PrintFunction, SlangNode } from '../types.d.ts';
|
|
|
11
11
|
|
|
12
12
|
const { group, hardline, ifBreak, indent, line, softline } = doc.builders;
|
|
13
13
|
|
|
14
|
-
function fillTab(
|
|
15
|
-
if (
|
|
14
|
+
function fillTab({ useTabs, tabWidth }: ParserOptions<AstNode>): Doc {
|
|
15
|
+
if (useTabs) return '\t';
|
|
16
16
|
// For the odd case of `tabWidth` of 1 or 0 we initiate `fillTab` as a single
|
|
17
17
|
// space.
|
|
18
|
-
return
|
|
18
|
+
return tabWidth > 2 ? ' '.repeat(tabWidth - 1) : ' ';
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
function experimentalTernaries(
|
|
@@ -24,21 +24,18 @@ function experimentalTernaries(
|
|
|
24
24
|
print: PrintFunction,
|
|
25
25
|
options: ParserOptions<AstNode>
|
|
26
26
|
): Doc {
|
|
27
|
-
const grandparent = path.
|
|
27
|
+
const grandparent = path.grandparent as StrictAstNode;
|
|
28
28
|
const isNested = grandparent.kind === NonterminalKind.ConditionalExpression;
|
|
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),
|
|
@@ -53,9 +50,10 @@ function experimentalTernaries(
|
|
|
53
50
|
path.call(print, 'trueExpression')
|
|
54
51
|
]);
|
|
55
52
|
|
|
53
|
+
const groupId = Symbol('Slang.ConditionalExpression.trueExpression');
|
|
56
54
|
const conditionAndTrueExpressionGroup = group(
|
|
57
55
|
[operandDoc, trueExpressionDoc],
|
|
58
|
-
{ id:
|
|
56
|
+
{ id: groupId }
|
|
59
57
|
);
|
|
60
58
|
|
|
61
59
|
const falseExpression = path.call(print, 'falseExpression');
|
|
@@ -67,10 +65,9 @@ function experimentalTernaries(
|
|
|
67
65
|
: ifBreak(
|
|
68
66
|
[fillTab(options), indent(falseExpression)],
|
|
69
67
|
[' ', falseExpression],
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
}
|
|
68
|
+
// We only add `fillTab` if we are sure the trueExpression is
|
|
69
|
+
// indented.
|
|
70
|
+
{ groupId }
|
|
74
71
|
)
|
|
75
72
|
];
|
|
76
73
|
|
|
@@ -90,7 +87,7 @@ function traditionalTernaries(
|
|
|
90
87
|
indent([
|
|
91
88
|
// Nested trueExpression and falseExpression are always printed in a new
|
|
92
89
|
// line
|
|
93
|
-
(path.
|
|
90
|
+
(path.grandparent as StrictAstNode).kind ===
|
|
94
91
|
NonterminalKind.ConditionalExpression
|
|
95
92
|
? hardline
|
|
96
93
|
: line,
|
|
@@ -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;
|
|
@@ -51,12 +49,12 @@ export class ContractDefinition implements SlangNode {
|
|
|
51
49
|
// the same name as the contract.
|
|
52
50
|
const compiler = coerce(options.compiler);
|
|
53
51
|
if (compiler && !satisfies(compiler, '>=0.5.0')) {
|
|
54
|
-
for (const member of this.members.items) {
|
|
52
|
+
for (const { variant: member } of this.members.items) {
|
|
55
53
|
if (
|
|
56
|
-
member.
|
|
57
|
-
member.
|
|
54
|
+
member.kind === NonterminalKind.FunctionDefinition &&
|
|
55
|
+
member.name.variant.value !== this.name.value
|
|
58
56
|
) {
|
|
59
|
-
member.
|
|
57
|
+
member.cleanModifierInvocationArguments();
|
|
60
58
|
}
|
|
61
59
|
}
|
|
62
60
|
}
|
|
@@ -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.d.ts';
|
|
9
|
+
import type { PrintFunction, SlangNode } from '../types.d.ts';
|
|
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.d.ts';
|
|
11
|
+
import type { PrintFunction, SlangNode } from '../types.d.ts';
|
|
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
|
+
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { NonterminalKind } from '@nomicfoundation/slang/cst';
|
|
2
|
-
import {
|
|
2
|
+
import { createHugFunction } from '../slang-utils/create-hug-function.js';
|
|
3
|
+
import { createKindCheckFunction } from '../slang-utils/create-kind-check-function.js';
|
|
4
|
+
import { printBinaryOperation } from '../slang-printers/print-binary-operation.js';
|
|
3
5
|
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
|
|
4
6
|
import { Expression } from './Expression.js';
|
|
5
7
|
|
|
@@ -8,6 +10,15 @@ import type { AstPath, Doc, ParserOptions } from 'prettier';
|
|
|
8
10
|
import type { AstNode } from './types.d.ts';
|
|
9
11
|
import type { PrintFunction, SlangNode } from '../types.d.ts';
|
|
10
12
|
|
|
13
|
+
const tryToHug = createHugFunction(['==', '!=']);
|
|
14
|
+
|
|
15
|
+
const printEqualityExpression = printBinaryOperation(
|
|
16
|
+
createKindCheckFunction([
|
|
17
|
+
NonterminalKind.AndExpression,
|
|
18
|
+
NonterminalKind.OrExpression
|
|
19
|
+
])
|
|
20
|
+
);
|
|
21
|
+
|
|
11
22
|
export class EqualityExpression implements SlangNode {
|
|
12
23
|
readonly kind = NonterminalKind.EqualityExpression;
|
|
13
24
|
|
|
@@ -32,6 +43,8 @@ export class EqualityExpression implements SlangNode {
|
|
|
32
43
|
|
|
33
44
|
this.comments = metadata.comments;
|
|
34
45
|
this.loc = metadata.loc;
|
|
46
|
+
|
|
47
|
+
this.leftOperand = tryToHug(this.leftOperand);
|
|
35
48
|
}
|
|
36
49
|
|
|
37
50
|
print(
|
|
@@ -39,6 +52,6 @@ export class EqualityExpression implements SlangNode {
|
|
|
39
52
|
print: PrintFunction,
|
|
40
53
|
options: ParserOptions<AstNode>
|
|
41
54
|
): Doc {
|
|
42
|
-
return
|
|
55
|
+
return printEqualityExpression(this, path, print, options);
|
|
43
56
|
}
|
|
44
57
|
}
|
|
@@ -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,19 +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
|
-
|
|
49
|
-
id: Symbol('FunctionCallExpression.operand')
|
|
50
|
-
});
|
|
51
|
-
argumentsDoc = indentIfBreak(argumentsDoc, { groupId: operandDoc.id! });
|
|
48
|
+
const groupId = Symbol('Slang.FunctionCallExpression.operand');
|
|
52
49
|
// We wrap the expression in a label in case there is an IndexAccess or
|
|
53
50
|
// a FunctionCall following this IndexAccess.
|
|
54
|
-
return label('MemberAccessChain', [
|
|
51
|
+
return label('MemberAccessChain', [
|
|
52
|
+
group(operandDoc.contents, { id: groupId }),
|
|
53
|
+
indentIfBreak(argumentsDoc, { groupId })
|
|
54
|
+
]);
|
|
55
55
|
}
|
|
56
56
|
|
|
57
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,25 +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
|
-
|
|
58
|
-
id: Symbol('IndexAccessExpression.operand')
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
indexDoc = indentIfBreak(indexDoc, { groupId: operandDoc.id! });
|
|
57
|
+
const groupId = Symbol('Slang.IndexAccessExpression.operand');
|
|
62
58
|
// We wrap the expression in a label in case there is an IndexAccess or
|
|
63
59
|
// a FunctionCall following this IndexAccess.
|
|
64
|
-
return label('MemberAccessChain', [
|
|
60
|
+
return label('MemberAccessChain', [
|
|
61
|
+
group(operandDoc.contents, { id: groupId }),
|
|
62
|
+
indentIfBreak(indexDoc, { groupId })
|
|
63
|
+
]);
|
|
65
64
|
}
|
|
66
65
|
|
|
67
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
|
}
|