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
|
@@ -2,59 +2,68 @@ import { NonterminalKind } from '@nomicfoundation/slang/cst';
|
|
|
2
2
|
import { doc } from 'prettier';
|
|
3
3
|
import { createBinaryOperationPrinter } from './create-binary-operation-printer.js';
|
|
4
4
|
import { createKindCheckFunction } from '../slang-utils/create-kind-check-function.js';
|
|
5
|
+
import { isBinaryOperation } from '../slang-utils/is-binary-operation.js';
|
|
5
6
|
|
|
6
|
-
import type { AstPath, Doc } from 'prettier';
|
|
7
|
-
import type {
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
import type { AstPath, Doc, ParserOptions } from 'prettier';
|
|
8
|
+
import type {
|
|
9
|
+
AstNode,
|
|
10
|
+
BinaryOperation,
|
|
11
|
+
StrictAstNode
|
|
12
|
+
} from '../slang-nodes/types.d.ts';
|
|
13
|
+
import type { PrintFunction } from '../types.d.ts';
|
|
10
14
|
|
|
11
15
|
const { group, indent } = doc.builders;
|
|
12
16
|
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
NonterminalKind.MultiplicativeExpression,
|
|
16
|
-
NonterminalKind.ExponentiationExpression,
|
|
17
|
-
NonterminalKind.AssignmentExpression,
|
|
18
|
-
NonterminalKind.BitwiseAndExpression,
|
|
19
|
-
NonterminalKind.BitwiseOrExpression,
|
|
20
|
-
NonterminalKind.BitwiseXorExpression,
|
|
21
|
-
NonterminalKind.AndExpression,
|
|
22
|
-
NonterminalKind.OrExpression,
|
|
23
|
-
NonterminalKind.ShiftExpression
|
|
24
|
-
]) as (
|
|
25
|
-
node: StrictAstNode
|
|
26
|
-
) => node is Exclude<
|
|
27
|
-
BinaryOperation,
|
|
28
|
-
EqualityExpression | InequalityExpression
|
|
29
|
-
>;
|
|
30
|
-
|
|
31
|
-
const binaryGroupRulesBuilder =
|
|
17
|
+
export const binaryGroupRulesBuilder =
|
|
18
|
+
(shouldGroup: (node: BinaryOperation) => boolean) =>
|
|
32
19
|
(path: AstPath<BinaryOperation>) =>
|
|
33
20
|
(document: Doc): Doc => {
|
|
34
|
-
const grandparentNode = path.
|
|
35
|
-
if (
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
return group(document);
|
|
21
|
+
const grandparentNode = path.grandparent as StrictAstNode;
|
|
22
|
+
if (!isBinaryOperation(grandparentNode)) return group(document);
|
|
23
|
+
if (shouldGroup(grandparentNode)) return group(document);
|
|
24
|
+
return document;
|
|
39
25
|
};
|
|
40
26
|
|
|
41
|
-
const
|
|
27
|
+
const isStatementWithoutIndentedOperation = createKindCheckFunction([
|
|
28
|
+
NonterminalKind.ReturnStatement,
|
|
29
|
+
NonterminalKind.IfStatement,
|
|
30
|
+
NonterminalKind.WhileStatement
|
|
31
|
+
]);
|
|
32
|
+
|
|
33
|
+
export const shouldNotIndent = (
|
|
34
|
+
node: StrictAstNode,
|
|
35
|
+
path: AstPath<BinaryOperation>,
|
|
36
|
+
index: number
|
|
37
|
+
): boolean =>
|
|
38
|
+
isStatementWithoutIndentedOperation(node) ||
|
|
39
|
+
(node.kind === NonterminalKind.ExpressionStatement &&
|
|
40
|
+
(path.getNode(index + 1) as StrictAstNode).kind ===
|
|
41
|
+
NonterminalKind.ForStatementCondition);
|
|
42
|
+
|
|
43
|
+
export const binaryIndentRulesBuilder =
|
|
44
|
+
(shouldIndent: (node: BinaryOperation) => boolean) =>
|
|
42
45
|
(path: AstPath<BinaryOperation>) =>
|
|
43
46
|
(document: Doc): Doc => {
|
|
44
|
-
let node = path.
|
|
45
|
-
for (let i = 2; ; i += 2) {
|
|
47
|
+
for (let i = 2, node = path.node; ; i += 2) {
|
|
46
48
|
const grandparentNode = path.getNode(i) as StrictAstNode;
|
|
47
|
-
if (grandparentNode
|
|
48
|
-
if (!
|
|
49
|
-
|
|
50
|
-
}
|
|
49
|
+
if (shouldNotIndent(grandparentNode, path, i)) break;
|
|
50
|
+
if (!isBinaryOperation(grandparentNode)) return indent(document);
|
|
51
|
+
if (shouldIndent(grandparentNode)) return indent(document);
|
|
51
52
|
if (node === grandparentNode.rightOperand.variant) break;
|
|
52
53
|
node = grandparentNode;
|
|
53
54
|
}
|
|
54
55
|
return document;
|
|
55
56
|
};
|
|
56
57
|
|
|
57
|
-
export const printBinaryOperation =
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
58
|
+
export const printBinaryOperation = (
|
|
59
|
+
shouldGroupAndIndent: (node: StrictAstNode) => boolean
|
|
60
|
+
): ((
|
|
61
|
+
node: BinaryOperation,
|
|
62
|
+
path: AstPath<BinaryOperation>,
|
|
63
|
+
print: PrintFunction,
|
|
64
|
+
options: ParserOptions<AstNode>
|
|
65
|
+
) => Doc) =>
|
|
66
|
+
createBinaryOperationPrinter(
|
|
67
|
+
binaryGroupRulesBuilder(shouldGroupAndIndent),
|
|
68
|
+
binaryIndentRulesBuilder(shouldGroupAndIndent)
|
|
69
|
+
);
|
|
@@ -12,7 +12,7 @@ export function printComments(path: AstPath<AstNode>): Doc[] {
|
|
|
12
12
|
const document = joinExisting(
|
|
13
13
|
line,
|
|
14
14
|
path.map((commentPath) => {
|
|
15
|
-
const comment = commentPath.
|
|
15
|
+
const comment = commentPath.node;
|
|
16
16
|
if (comment.trailing || comment.leading || comment.printed) {
|
|
17
17
|
return '';
|
|
18
18
|
}
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { NonterminalKind } from '@nomicfoundation/slang/cst';
|
|
2
2
|
import { doc } from 'prettier';
|
|
3
|
-
import { createKindCheckFunction } from '../slang-utils/create-kind-check-function.js';
|
|
4
3
|
import { isBinaryOperation } from '../slang-utils/is-binary-operation.js';
|
|
5
4
|
import { createBinaryOperationPrinter } from './create-binary-operation-printer.js';
|
|
5
|
+
import {
|
|
6
|
+
binaryGroupRulesBuilder,
|
|
7
|
+
shouldNotIndent
|
|
8
|
+
} from './print-binary-operation.js';
|
|
6
9
|
|
|
7
10
|
import type { AstPath, Doc, ParserOptions } from 'prettier';
|
|
8
11
|
import type {
|
|
@@ -11,28 +14,16 @@ import type {
|
|
|
11
14
|
StrictAstNode
|
|
12
15
|
} from '../slang-nodes/types.d.ts';
|
|
13
16
|
|
|
14
|
-
const {
|
|
17
|
+
const { indent } = doc.builders;
|
|
15
18
|
|
|
16
|
-
const
|
|
17
|
-
NonterminalKind.ReturnStatement,
|
|
18
|
-
NonterminalKind.IfStatement,
|
|
19
|
-
NonterminalKind.WhileStatement
|
|
20
|
-
]);
|
|
21
|
-
|
|
22
|
-
const logicalGroupRulesBuilder =
|
|
23
|
-
(path: AstPath<BinaryOperation>) =>
|
|
24
|
-
(document: Doc): Doc =>
|
|
25
|
-
isBinaryOperation(path.getNode(2) as StrictAstNode)
|
|
26
|
-
? document
|
|
27
|
-
: group(document);
|
|
19
|
+
const logicalGroupRulesBuilder = binaryGroupRulesBuilder(() => false);
|
|
28
20
|
|
|
29
21
|
const logicalIndentRulesBuilder =
|
|
30
22
|
(path: AstPath<BinaryOperation>, options: ParserOptions<AstNode>) =>
|
|
31
23
|
(document: Doc): Doc => {
|
|
32
|
-
let node = path.
|
|
33
|
-
for (let i = 2; ; i += 2) {
|
|
24
|
+
for (let i = 2, node = path.node; ; i += 2) {
|
|
34
25
|
const grandparentNode = path.getNode(i) as StrictAstNode;
|
|
35
|
-
if (
|
|
26
|
+
if (shouldNotIndent(grandparentNode, path, i)) break;
|
|
36
27
|
if (
|
|
37
28
|
options.experimentalTernaries &&
|
|
38
29
|
grandparentNode.kind === NonterminalKind.ConditionalExpression &&
|
|
@@ -3,22 +3,18 @@ import { doc, util } from 'prettier';
|
|
|
3
3
|
import { locEnd } from '../slang-utils/loc.js';
|
|
4
4
|
|
|
5
5
|
import type { AstPath, Doc, ParserOptions } from 'prettier';
|
|
6
|
-
import type {
|
|
7
|
-
AstNode,
|
|
8
|
-
CollectionNode,
|
|
9
|
-
StrictAstNode
|
|
10
|
-
} from '../slang-nodes/types.d.ts';
|
|
6
|
+
import type { AstNode, NodeCollection } from '../slang-nodes/types.d.ts';
|
|
11
7
|
import type { PrintFunction } from '../types.d.ts';
|
|
12
8
|
|
|
13
9
|
const { hardline } = doc.builders;
|
|
14
10
|
|
|
15
11
|
export function printPreservingEmptyLines(
|
|
16
|
-
path: AstPath<
|
|
12
|
+
path: AstPath<NodeCollection>,
|
|
17
13
|
print: PrintFunction,
|
|
18
14
|
options: ParserOptions<AstNode>
|
|
19
15
|
): Doc {
|
|
20
16
|
return path.map((childPath) => {
|
|
21
|
-
const node = childPath.
|
|
17
|
+
const node = childPath.node;
|
|
22
18
|
|
|
23
19
|
return [
|
|
24
20
|
// Only attempt to prepend an empty line if `node` is not the first item
|
|
@@ -10,13 +10,13 @@ export function createHugFunction(
|
|
|
10
10
|
): (node: Expression) => Expression {
|
|
11
11
|
const operators = new Set(huggableOperators);
|
|
12
12
|
return (node: Expression): Expression => {
|
|
13
|
+
const variant = node.variant;
|
|
13
14
|
if (
|
|
14
|
-
|
|
15
|
-
isBinaryOperation(
|
|
16
|
-
operators.has(
|
|
15
|
+
variant.kind !== TerminalKind.Identifier &&
|
|
16
|
+
isBinaryOperation(variant) &&
|
|
17
|
+
operators.has(variant.operator)
|
|
17
18
|
) {
|
|
18
|
-
const
|
|
19
|
-
|
|
19
|
+
const loc = node.loc;
|
|
20
20
|
return Object.assign(Object.create(Expression.prototype) as Expression, {
|
|
21
21
|
kind: NonterminalKind.Expression,
|
|
22
22
|
loc: { ...loc },
|
|
@@ -1,145 +1,82 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { NonterminalKind, Query } from '@nomicfoundation/slang/cst';
|
|
1
|
+
import { NonterminalKind } from '@nomicfoundation/slang/cst';
|
|
3
2
|
import { Parser } from '@nomicfoundation/slang/parser';
|
|
4
3
|
import { LanguageFacts } from '@nomicfoundation/slang/utils';
|
|
5
|
-
import {
|
|
6
|
-
maxSatisfying,
|
|
7
|
-
minSatisfying,
|
|
8
|
-
minor,
|
|
9
|
-
major,
|
|
10
|
-
satisfies,
|
|
11
|
-
validRange
|
|
12
|
-
} from 'semver';
|
|
13
|
-
import { VersionExpressionSets } from '../slang-nodes/VersionExpressionSets.js';
|
|
4
|
+
import { minSatisfying } from 'semver';
|
|
14
5
|
|
|
15
6
|
import type { ParseOutput } from '@nomicfoundation/slang/parser';
|
|
16
7
|
import type { ParserOptions } from 'prettier';
|
|
17
|
-
import type { AstNode } from '../slang-nodes/types.
|
|
8
|
+
import type { AstNode } from '../slang-nodes/types.d.ts';
|
|
18
9
|
|
|
19
10
|
const supportedVersions = LanguageFacts.allVersions();
|
|
11
|
+
const supportedLength = supportedVersions.length;
|
|
20
12
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
return versions;
|
|
32
|
-
}, []);
|
|
33
|
-
|
|
34
|
-
const queries = [
|
|
35
|
-
Query.create('[VersionPragma @versionRanges [VersionExpressionSets]]')
|
|
36
|
-
];
|
|
13
|
+
function parserAndOutput(
|
|
14
|
+
text: string,
|
|
15
|
+
version: string
|
|
16
|
+
): { parser: Parser; parseOutput: ParseOutput } {
|
|
17
|
+
const parser = Parser.create(version);
|
|
18
|
+
return {
|
|
19
|
+
parser,
|
|
20
|
+
parseOutput: parser.parseNonterminal(NonterminalKind.SourceUnit, text)
|
|
21
|
+
};
|
|
22
|
+
}
|
|
37
23
|
|
|
38
|
-
|
|
24
|
+
function createError(
|
|
25
|
+
result: { parseOutput: ParseOutput },
|
|
26
|
+
reason: string
|
|
27
|
+
): Error {
|
|
28
|
+
return new Error(
|
|
29
|
+
`We encountered the following syntax error:\n\n\t${result.parseOutput.errors()[0].message}\n\n${reason}`
|
|
30
|
+
);
|
|
31
|
+
}
|
|
39
32
|
|
|
40
33
|
export function createParser(
|
|
41
34
|
text: string,
|
|
42
35
|
options: ParserOptions<AstNode>
|
|
43
|
-
):
|
|
44
|
-
const compiler =
|
|
36
|
+
): { parser: Parser; parseOutput: ParseOutput } {
|
|
37
|
+
const compiler = minSatisfying(supportedVersions, options.compiler);
|
|
45
38
|
if (compiler) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
39
|
+
const result = parserAndOutput(text, compiler);
|
|
40
|
+
|
|
41
|
+
if (!result.parseOutput.isValid())
|
|
42
|
+
throw createError(
|
|
43
|
+
result,
|
|
44
|
+
`Based on the compiler option provided, we inferred your code to be using Solidity version ${
|
|
45
|
+
result.parser.languageVersion
|
|
46
|
+
}. If you would like to change that, specify a different version in your \`.prettierrc\` file.`
|
|
47
|
+
);
|
|
51
48
|
|
|
52
|
-
|
|
53
|
-
if (parser) {
|
|
54
|
-
isCachedParser = true;
|
|
55
|
-
} else {
|
|
56
|
-
parser = Parser.create(milestoneVersions[0]);
|
|
49
|
+
return result;
|
|
57
50
|
}
|
|
58
51
|
|
|
59
|
-
|
|
60
|
-
|
|
52
|
+
const inferredRanges: string[] = LanguageFacts.inferLanguageVersions(text);
|
|
53
|
+
const inferredLength = inferredRanges.length;
|
|
61
54
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
let i = isCachedParser ? 0 : 1;
|
|
68
|
-
i <= milestoneVersions.length;
|
|
69
|
-
i += 1
|
|
70
|
-
) {
|
|
71
|
-
try {
|
|
72
|
-
const version = milestoneVersions[i];
|
|
73
|
-
parser = Parser.create(version);
|
|
74
|
-
parseOutput = parser.parseNonterminal(NonterminalKind.SourceUnit, text);
|
|
75
|
-
inferredRanges = tryToCollectPragmas(parseOutput, parser);
|
|
76
|
-
break;
|
|
77
|
-
} catch {
|
|
78
|
-
continue;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
55
|
+
if (inferredLength === 0 || inferredLength === supportedLength) {
|
|
56
|
+
const result = parserAndOutput(
|
|
57
|
+
text,
|
|
58
|
+
supportedVersions[supportedLength - 1]
|
|
59
|
+
);
|
|
82
60
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
90
|
-
return versions.filter((supportedVersion) =>
|
|
91
|
-
satisfies(supportedVersion, inferredRange)
|
|
61
|
+
if (!result.parseOutput.isValid())
|
|
62
|
+
throw createError(
|
|
63
|
+
result,
|
|
64
|
+
`We couldn't infer a Solidity version based on the pragma statements in your code so we defaulted to ${
|
|
65
|
+
result.parser.languageVersion
|
|
66
|
+
}. You might be attempting to use a syntax not yet supported by Slang or you might want to specify a version in your \`.prettierrc\` file.`
|
|
92
67
|
);
|
|
93
|
-
|
|
94
|
-
supportedVersions
|
|
95
|
-
);
|
|
96
|
-
|
|
97
|
-
const inferredVersion =
|
|
98
|
-
satisfyingVersions.length > 0
|
|
99
|
-
? satisfyingVersions[satisfyingVersions.length - 1]
|
|
100
|
-
: supportedVersions[supportedVersions.length - 1];
|
|
101
|
-
|
|
102
|
-
if (inferredVersion !== parser.languageVersion) {
|
|
103
|
-
parser = Parser.create(inferredVersion);
|
|
104
|
-
parseOutput = parser.parseNonterminal(NonterminalKind.SourceUnit, text);
|
|
68
|
+
return result;
|
|
105
69
|
}
|
|
106
70
|
|
|
107
|
-
|
|
108
|
-
return [parser, parseOutput!];
|
|
109
|
-
}
|
|
71
|
+
const result = parserAndOutput(text, inferredRanges[0]);
|
|
110
72
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
const ranges: string[] = [];
|
|
118
|
-
|
|
119
|
-
let match;
|
|
120
|
-
while ((match = matches.next())) {
|
|
121
|
-
const versionRange = new SlangVersionExpressionSets(
|
|
122
|
-
match.captures.versionRanges[0].node.asNonterminalNode()!
|
|
123
|
-
);
|
|
124
|
-
ranges.push(
|
|
125
|
-
// Replace all comments that could be in the expression with whitespace
|
|
126
|
-
new VersionExpressionSets(versionRange).comments.reduce(
|
|
127
|
-
(range, comment) => range.replace(comment.value, ' '),
|
|
128
|
-
versionRange.cst.unparse()
|
|
129
|
-
)
|
|
73
|
+
if (!result.parseOutput.isValid())
|
|
74
|
+
throw createError(
|
|
75
|
+
result,
|
|
76
|
+
`Based on the pragma statements, we inferred your code to be using Solidity version ${
|
|
77
|
+
result.parser.languageVersion
|
|
78
|
+
}. If you would like to change that, update the pragmas in your source file, or specify a version in your \`.prettierrc\` file.`
|
|
130
79
|
);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
if (ranges.length === 0) {
|
|
134
|
-
// If we didn't find pragmas but succeeded parsing the source we keep it.
|
|
135
|
-
if (!isCachedParser && parseOutput.isValid()) {
|
|
136
|
-
return [parser.languageVersion];
|
|
137
|
-
}
|
|
138
|
-
// Otherwise we throw.
|
|
139
|
-
throw new Error(
|
|
140
|
-
`Using version ${parser.languageVersion} did not find any pragma statement and does not parse without errors.`
|
|
141
|
-
);
|
|
142
|
-
}
|
|
143
80
|
|
|
144
|
-
return
|
|
81
|
+
return result;
|
|
145
82
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { TerminalKind, TerminalNode } from '@nomicfoundation/slang/cst';
|
|
2
2
|
import { createKindCheckFunction } from './create-kind-check-function.js';
|
|
3
|
+
import { isComment } from './is-comment.js';
|
|
3
4
|
import { MultiLineComment } from '../slang-nodes/MultiLineComment.js';
|
|
4
5
|
import { MultiLineNatSpecComment } from '../slang-nodes/MultiLineNatSpecComment.js';
|
|
5
6
|
import { SingleLineComment } from '../slang-nodes/SingleLineComment.js';
|
|
@@ -8,7 +9,6 @@ import { SingleLineNatSpecComment } from '../slang-nodes/SingleLineNatSpecCommen
|
|
|
8
9
|
import type { Node } from '@nomicfoundation/slang/cst';
|
|
9
10
|
import type { Comment, StrictAstNode } from '../slang-nodes/types.d.ts';
|
|
10
11
|
import type { Metadata, SlangAstNode } from '../types.d.ts';
|
|
11
|
-
import { isComment } from './is-comment.js';
|
|
12
12
|
|
|
13
13
|
const isCommentOrWhiteSpace = createKindCheckFunction([
|
|
14
14
|
TerminalKind.MultiLineComment,
|
|
@@ -53,37 +53,37 @@ export function getNodeMetadata(
|
|
|
53
53
|
}
|
|
54
54
|
};
|
|
55
55
|
}
|
|
56
|
+
const { cst: parent } = ast;
|
|
57
|
+
const children = parent.children().map(({ node }) => node);
|
|
56
58
|
|
|
57
|
-
const
|
|
58
|
-
return child.node;
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
const initialOffset = offsets.get(ast.cst.id) || 0;
|
|
59
|
+
const initialOffset = offsets.get(parent.id) || 0;
|
|
62
60
|
let offset = initialOffset;
|
|
61
|
+
const comments: Comment[] = [];
|
|
63
62
|
|
|
64
|
-
const
|
|
63
|
+
for (const child of children) {
|
|
64
|
+
const { id, kind, textLength } = child;
|
|
65
65
|
if (child.isNonterminalNode()) {
|
|
66
|
-
offsets.set(
|
|
66
|
+
offsets.set(id, offset);
|
|
67
67
|
} else {
|
|
68
68
|
if (isComment(child)) {
|
|
69
|
-
offsets.set(
|
|
69
|
+
offsets.set(id, offset);
|
|
70
70
|
}
|
|
71
|
-
switch (
|
|
71
|
+
switch (kind) {
|
|
72
72
|
// Since the fetching the comments and calculating offsets are both done
|
|
73
73
|
// as we iterate over the children and the comment also depends on the
|
|
74
74
|
// offset, it's hard to separate these responsibilities into different
|
|
75
75
|
// functions without doing the iteration twice.
|
|
76
76
|
case TerminalKind.MultiLineComment:
|
|
77
|
-
|
|
77
|
+
comments.push(new MultiLineComment(child));
|
|
78
78
|
break;
|
|
79
79
|
case TerminalKind.MultiLineNatSpecComment:
|
|
80
|
-
|
|
80
|
+
comments.push(new MultiLineNatSpecComment(child));
|
|
81
81
|
break;
|
|
82
82
|
case TerminalKind.SingleLineComment:
|
|
83
|
-
|
|
83
|
+
comments.push(new SingleLineComment(child));
|
|
84
84
|
break;
|
|
85
85
|
case TerminalKind.SingleLineNatSpecComment:
|
|
86
|
-
|
|
86
|
+
comments.push(new SingleLineNatSpecComment(child));
|
|
87
87
|
break;
|
|
88
88
|
case TerminalKind.Identifier:
|
|
89
89
|
case TerminalKind.YulIdentifier:
|
|
@@ -91,24 +91,21 @@ export function getNodeMetadata(
|
|
|
91
91
|
// functions, etc...
|
|
92
92
|
// Since a user can add comments to this section of the code as well,
|
|
93
93
|
// we need to track the offsets.
|
|
94
|
-
offsets.set(
|
|
94
|
+
offsets.set(id, offset);
|
|
95
95
|
break;
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
-
offset +=
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
const trailingOffset = enclosePeripheralComments
|
|
107
|
-
? 0
|
|
108
|
-
: getLeadingOffset(children.reverse());
|
|
99
|
+
offset += textLength.utf16;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const [leadingOffset, trailingOffset] = enclosePeripheralComments
|
|
103
|
+
? [0, 0]
|
|
104
|
+
: [getLeadingOffset(children), getLeadingOffset(children.reverse())];
|
|
105
|
+
|
|
109
106
|
const loc = {
|
|
110
107
|
start: initialOffset + leadingOffset,
|
|
111
|
-
end:
|
|
108
|
+
end: offset - trailingOffset,
|
|
112
109
|
leadingOffset,
|
|
113
110
|
trailingOffset
|
|
114
111
|
};
|
|
@@ -132,23 +129,22 @@ function collectComments(
|
|
|
132
129
|
}
|
|
133
130
|
|
|
134
131
|
export function updateMetadata(
|
|
135
|
-
|
|
132
|
+
{ comments, loc }: Metadata,
|
|
136
133
|
childNodes: (StrictAstNode | StrictAstNode[] | undefined)[]
|
|
137
134
|
): Metadata {
|
|
138
135
|
// Collect comments
|
|
139
|
-
|
|
136
|
+
comments = childNodes.reduce(collectComments, comments);
|
|
140
137
|
|
|
141
138
|
// calculate correct loc object
|
|
142
|
-
const { loc } = metadata;
|
|
143
139
|
if (loc.leadingOffset === 0) {
|
|
144
140
|
for (const childNode of childNodes) {
|
|
145
141
|
if (typeof childNode === 'undefined' || Array.isArray(childNode))
|
|
146
142
|
continue;
|
|
147
|
-
const
|
|
143
|
+
const { leadingOffset, start } = childNode.loc;
|
|
148
144
|
|
|
149
|
-
if (
|
|
150
|
-
loc.leadingOffset =
|
|
151
|
-
loc.start +=
|
|
145
|
+
if (start - leadingOffset === loc.start) {
|
|
146
|
+
loc.leadingOffset = leadingOffset;
|
|
147
|
+
loc.start += leadingOffset;
|
|
152
148
|
break;
|
|
153
149
|
}
|
|
154
150
|
}
|
|
@@ -158,11 +154,11 @@ export function updateMetadata(
|
|
|
158
154
|
for (const childNode of childNodes.reverse()) {
|
|
159
155
|
if (typeof childNode === 'undefined' || Array.isArray(childNode))
|
|
160
156
|
continue;
|
|
161
|
-
const
|
|
157
|
+
const { trailingOffset, end } = childNode.loc;
|
|
162
158
|
|
|
163
|
-
if (
|
|
164
|
-
loc.trailingOffset =
|
|
165
|
-
loc.end -=
|
|
159
|
+
if (end + trailingOffset === loc.end) {
|
|
160
|
+
loc.trailingOffset = trailingOffset;
|
|
161
|
+
loc.end -= trailingOffset;
|
|
166
162
|
break;
|
|
167
163
|
}
|
|
168
164
|
}
|
|
@@ -3,21 +3,18 @@ import { NonterminalKind } from '@nomicfoundation/slang/cst';
|
|
|
3
3
|
import type { ContractSpecifier } from '../slang-nodes/ContractSpecifier.js';
|
|
4
4
|
|
|
5
5
|
export function sortContractSpecifiers(
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
{ variant: { kind: aKind } }: ContractSpecifier,
|
|
7
|
+
{ variant: { kind: bKind } }: ContractSpecifier
|
|
8
8
|
): number {
|
|
9
|
-
const aVariant = a.variant;
|
|
10
|
-
const bVariant = b.variant;
|
|
11
|
-
|
|
12
9
|
// OverrideSpecifiers before ModifierInvocation
|
|
13
10
|
if (
|
|
14
|
-
|
|
15
|
-
|
|
11
|
+
aKind === NonterminalKind.InheritanceSpecifier &&
|
|
12
|
+
bKind === NonterminalKind.StorageLayoutSpecifier
|
|
16
13
|
)
|
|
17
14
|
return -1;
|
|
18
15
|
if (
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
bKind === NonterminalKind.InheritanceSpecifier &&
|
|
17
|
+
aKind === NonterminalKind.StorageLayoutSpecifier
|
|
21
18
|
)
|
|
22
19
|
return 1;
|
|
23
20
|
|
|
@@ -12,12 +12,9 @@ const visibilityKeyWords = new Set([
|
|
|
12
12
|
const mutabilityKeyWords = new Set(['pure', 'constant', 'payable', 'view']);
|
|
13
13
|
|
|
14
14
|
export function sortFunctionAttributes(
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
{ variant: aVariant }: SortableAttribute,
|
|
16
|
+
{ variant: bVariant }: SortableAttribute
|
|
17
17
|
): number {
|
|
18
|
-
const aVariant = a.variant;
|
|
19
|
-
const bVariant = b.variant;
|
|
20
|
-
|
|
21
18
|
const aIsString = typeof aVariant === 'string';
|
|
22
19
|
const bIsString = typeof bVariant === 'string';
|
|
23
20
|
|
package/src/slangPrinter.ts
CHANGED
|
@@ -5,27 +5,24 @@ import type { AstPath, Doc, ParserOptions } from 'prettier';
|
|
|
5
5
|
import type { AstNode, StrictAstNode } from './slang-nodes/types.d.ts';
|
|
6
6
|
import type { PrintFunction } from './types.d.ts';
|
|
7
7
|
|
|
8
|
-
function hasNodeIgnoreComment(
|
|
8
|
+
function hasNodeIgnoreComment({ comments }: StrictAstNode): boolean {
|
|
9
9
|
// Prettier sets SourceUnit's comments to undefined after assigning comments
|
|
10
10
|
// to each node.
|
|
11
|
-
return (
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
.slice(2, isBlockComment(comment) ? -2 : undefined)
|
|
17
|
-
.trim() === 'prettier-ignore'
|
|
18
|
-
)
|
|
11
|
+
return comments?.some(
|
|
12
|
+
(comment) =>
|
|
13
|
+
comment.value
|
|
14
|
+
.slice(2, isBlockComment(comment) ? -2 : undefined)
|
|
15
|
+
.trim() === 'prettier-ignore'
|
|
19
16
|
);
|
|
20
17
|
}
|
|
21
18
|
|
|
22
19
|
function ignoreComments(path: AstPath<AstNode>): void {
|
|
23
|
-
const node = path.
|
|
20
|
+
const node = path.node;
|
|
24
21
|
// We ignore anything that is not an object
|
|
25
22
|
if (node === null || typeof node !== 'object') return;
|
|
26
23
|
|
|
27
|
-
|
|
28
|
-
for (
|
|
24
|
+
let key: keyof StrictAstNode;
|
|
25
|
+
for (key in node) {
|
|
29
26
|
switch (key) {
|
|
30
27
|
// We ignore `kind`, `loc`, and comments since these are added by the
|
|
31
28
|
// parser
|
|
@@ -35,10 +32,7 @@ function ignoreComments(path: AstPath<AstNode>): void {
|
|
|
35
32
|
break;
|
|
36
33
|
// The key `comments` will contain every comment for this node
|
|
37
34
|
case 'comments':
|
|
38
|
-
path.each((commentPath) =>
|
|
39
|
-
const comment = commentPath.getNode()!;
|
|
40
|
-
comment.printed = true;
|
|
41
|
-
}, 'comments');
|
|
35
|
+
path.each((commentPath) => (commentPath.node.printed = true), key);
|
|
42
36
|
break;
|
|
43
37
|
default:
|
|
44
38
|
// If the value for that key is an Array or an Object we go deeper.
|
|
@@ -61,11 +55,7 @@ function genericPrint(
|
|
|
61
55
|
options: ParserOptions<AstNode>,
|
|
62
56
|
print: PrintFunction
|
|
63
57
|
): Doc {
|
|
64
|
-
const node = path.
|
|
65
|
-
|
|
66
|
-
if (node === null) {
|
|
67
|
-
return '';
|
|
68
|
-
}
|
|
58
|
+
const node = path.node;
|
|
69
59
|
|
|
70
60
|
if (hasNodeIgnoreComment(node)) {
|
|
71
61
|
ignoreComments(path);
|