prettier-plugin-solidity 2.0.0-beta.8 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/README.md +61 -57
  2. package/dist/comments/handlers/handleContractDefinitionComments.js +7 -2
  3. package/dist/comments/handlers/handleContractDefinitionComments.js.map +1 -1
  4. package/dist/ea6945b7193921081ced.wasm +0 -0
  5. package/dist/index.js +5 -5
  6. package/dist/index.js.map +1 -1
  7. package/dist/nodes/ContractDefinition.js +30 -10
  8. package/dist/nodes/ContractDefinition.js.map +1 -1
  9. package/dist/printer.js +1 -1
  10. package/dist/printer.js.map +1 -1
  11. package/dist/slang-comments/handlers/handle-contract-definition-comments.js +18 -8
  12. package/dist/slang-comments/handlers/handle-contract-definition-comments.js.map +1 -1
  13. package/dist/slang-comments/handlers/handle-contract-specifiers-comments.js +22 -0
  14. package/dist/slang-comments/handlers/handle-contract-specifiers-comments.js.map +1 -0
  15. package/dist/slang-comments/handlers/handle-storage-layout-specifier-comments.js +14 -0
  16. package/dist/slang-comments/handlers/handle-storage-layout-specifier-comments.js.map +1 -0
  17. package/dist/slang-comments/handlers/index.js +4 -0
  18. package/dist/slang-comments/handlers/index.js.map +1 -1
  19. package/dist/slang-nodes/ConditionalExpression.js +6 -12
  20. package/dist/slang-nodes/ConditionalExpression.js.map +1 -1
  21. package/dist/slang-nodes/ContractDefinition.js +5 -6
  22. package/dist/slang-nodes/ContractDefinition.js.map +1 -1
  23. package/dist/slang-nodes/ContractSpecifier.js +27 -0
  24. package/dist/slang-nodes/ContractSpecifier.js.map +1 -0
  25. package/dist/slang-nodes/ContractSpecifiers.js +31 -0
  26. package/dist/slang-nodes/ContractSpecifiers.js.map +1 -0
  27. package/dist/slang-nodes/FunctionCallExpression.js +3 -4
  28. package/dist/slang-nodes/FunctionCallExpression.js.map +1 -1
  29. package/dist/slang-nodes/IndexAccessExpression.js +3 -4
  30. package/dist/slang-nodes/IndexAccessExpression.js.map +1 -1
  31. package/dist/slang-nodes/ReturnStatement.js +3 -5
  32. package/dist/slang-nodes/ReturnStatement.js.map +1 -1
  33. package/dist/slang-nodes/StateVariableDefinition.js +3 -6
  34. package/dist/slang-nodes/StateVariableDefinition.js.map +1 -1
  35. package/dist/slang-nodes/StateVariableDefinitionValue.js +1 -2
  36. package/dist/slang-nodes/StateVariableDefinitionValue.js.map +1 -1
  37. package/dist/slang-nodes/StorageLayoutSpecifier.js +25 -0
  38. package/dist/slang-nodes/StorageLayoutSpecifier.js.map +1 -0
  39. package/dist/slang-nodes/TupleDeconstructionStatement.js +3 -4
  40. package/dist/slang-nodes/TupleDeconstructionStatement.js.map +1 -1
  41. package/dist/slang-nodes/VariableDeclarationStatement.js +3 -2
  42. package/dist/slang-nodes/VariableDeclarationStatement.js.map +1 -1
  43. package/dist/slang-utils/sort-contract-specifiers.js +14 -0
  44. package/dist/slang-utils/sort-contract-specifiers.js.map +1 -0
  45. package/dist/slangPrinter.js +2 -3
  46. package/dist/slangPrinter.js.map +1 -1
  47. package/dist/standalone.js +1 -1
  48. package/dist/standalone.js.map +1 -1
  49. package/package.json +14 -11
  50. package/src/comments/handlers/handleContractDefinitionComments.js +11 -2
  51. package/src/index.ts +5 -5
  52. package/src/nodes/ContractDefinition.js +31 -11
  53. package/src/printer.js +1 -1
  54. package/src/slang-comments/handlers/handle-contract-definition-comments.ts +21 -13
  55. package/src/slang-comments/handlers/handle-contract-specifiers-comments.ts +33 -0
  56. package/src/slang-comments/handlers/handle-storage-layout-specifier-comments.ts +23 -0
  57. package/src/slang-comments/handlers/index.ts +4 -0
  58. package/src/slang-nodes/ConditionalExpression.ts +4 -10
  59. package/src/slang-nodes/ContractDefinition.ts +6 -7
  60. package/src/slang-nodes/ContractSpecifier.ts +48 -0
  61. package/src/slang-nodes/ContractSpecifiers.ts +52 -0
  62. package/src/slang-nodes/FunctionCallExpression.ts +3 -4
  63. package/src/slang-nodes/IndexAccessExpression.ts +3 -4
  64. package/src/slang-nodes/ReturnStatement.ts +3 -5
  65. package/src/slang-nodes/StateVariableDefinition.ts +3 -6
  66. package/src/slang-nodes/StateVariableDefinitionValue.ts +1 -2
  67. package/src/slang-nodes/StorageLayoutSpecifier.ts +45 -0
  68. package/src/slang-nodes/TupleDeconstructionStatement.ts +3 -4
  69. package/src/slang-nodes/VariableDeclarationStatement.ts +3 -2
  70. package/src/slang-nodes/types.d.ts +6 -0
  71. package/src/slang-utils/sort-contract-specifiers.ts +25 -0
  72. package/src/slangPrinter.ts +2 -3
  73. package/src/types.d.ts +7 -219
  74. package/dist/78244c53725e2131398e.wasm +0 -0
  75. package/dist/slang-utils/prettier-version-satisfies.js +0 -6
  76. package/dist/slang-utils/prettier-version-satisfies.js.map +0 -1
  77. package/dist/slang-utils/print-warning.js +0 -12
  78. package/dist/slang-utils/print-warning.js.map +0 -1
  79. package/src/slang-utils/prettier-version-satisfies.ts +0 -8
  80. package/src/slang-utils/print-warning.ts +0 -12
@@ -57,6 +57,7 @@ export class VariableDeclarationStatement implements SlangNode {
57
57
  path: AstPath<VariableDeclarationStatement>,
58
58
  print: PrintFunction
59
59
  ): Doc {
60
+ const groupId = Symbol('Slang.VariableDeclarationStatement.variables');
60
61
  const declarationDoc = group(
61
62
  [
62
63
  path.call(print, 'variableType'),
@@ -68,12 +69,12 @@ export class VariableDeclarationStatement implements SlangNode {
68
69
  path.call(print, 'name')
69
70
  ])
70
71
  ],
71
- { id: Symbol('Slang.VariableDeclarationStatement.variables') }
72
+ { id: groupId }
72
73
  );
73
74
 
74
75
  return [
75
76
  declarationDoc,
76
- indentIfBreak(path.call(print, 'value'), { groupId: declarationDoc.id! }),
77
+ indentIfBreak(path.call(print, 'value'), { groupId }),
77
78
  ';'
78
79
  ];
79
80
  }
@@ -29,6 +29,8 @@ import type { ContinueStatement } from './ContinueStatement.ts';
29
29
  import type { ContractDefinition } from './ContractDefinition.ts';
30
30
  import type { ContractMember } from './ContractMember.ts';
31
31
  import type { ContractMembers } from './ContractMembers.ts';
32
+ import type { ContractSpecifier } from './ContractSpecifier.ts';
33
+ import type { ContractSpecifiers } from './ContractSpecifiers.ts';
32
34
  import type { DecimalNumberExpression } from './DecimalNumberExpression.ts';
33
35
  import type { DoWhileStatement } from './DoWhileStatement.ts';
34
36
  import type { ElementaryType } from './ElementaryType.ts';
@@ -139,6 +141,7 @@ import type { StateVariableAttribute } from './StateVariableAttribute.ts';
139
141
  import type { StateVariableAttributes } from './StateVariableAttributes.ts';
140
142
  import type { StateVariableDefinition } from './StateVariableDefinition.ts';
141
143
  import type { StateVariableDefinitionValue } from './StateVariableDefinitionValue.ts';
144
+ import type { StorageLayoutSpecifier } from './StorageLayoutSpecifier.ts';
142
145
  import type { StorageLocation } from './StorageLocation.ts';
143
146
  import type { StringExpression } from './StringExpression.ts';
144
147
  import type { StringLiteral } from './StringLiteral.ts';
@@ -257,6 +260,7 @@ export type StrictAstNode =
257
260
  | ContractDefinition
258
261
  | InheritanceSpecifier
259
262
  | InheritanceType
263
+ | StorageLayoutSpecifier
260
264
  | InterfaceDefinition
261
265
  | LibraryDefinition
262
266
  | StructDefinition
@@ -411,6 +415,8 @@ export type StrictAstNode =
411
415
  | YulLiteral
412
416
  | SourceUnitMembers
413
417
  | VersionExpressionSet
418
+ | ContractSpecifier
419
+ | ContractSpecifiers
414
420
  | ContractMembers
415
421
  | InterfaceMembers
416
422
  | LibraryMembers
@@ -0,0 +1,25 @@
1
+ import { NonterminalKind } from '@nomicfoundation/slang/cst';
2
+
3
+ import type { ContractSpecifier } from '../slang-nodes/ContractSpecifier.js';
4
+
5
+ export function sortContractSpecifiers(
6
+ a: ContractSpecifier,
7
+ b: ContractSpecifier
8
+ ): number {
9
+ const aVariant = a.variant;
10
+ const bVariant = b.variant;
11
+
12
+ // OverrideSpecifiers before ModifierInvocation
13
+ if (
14
+ aVariant.kind === NonterminalKind.InheritanceSpecifier &&
15
+ bVariant.kind === NonterminalKind.StorageLayoutSpecifier
16
+ )
17
+ return -1;
18
+ if (
19
+ bVariant.kind === NonterminalKind.InheritanceSpecifier &&
20
+ aVariant.kind === NonterminalKind.StorageLayoutSpecifier
21
+ )
22
+ return 1;
23
+
24
+ return 0;
25
+ }
@@ -20,10 +20,9 @@ function hasNodeIgnoreComment(node: StrictAstNode): boolean {
20
20
  }
21
21
 
22
22
  function ignoreComments(path: AstPath<AstNode>): void {
23
- // TODO: remove undefined once we stop supporting prettier 2
24
23
  const node = path.getNode();
25
24
  // We ignore anything that is not an object
26
- if (node === null || node === undefined || typeof node !== 'object') return;
25
+ if (node === null || typeof node !== 'object') return;
27
26
 
28
27
  const keys = Object.keys(node) as (keyof StrictAstNode)[];
29
28
  for (const key of keys) {
@@ -64,7 +63,7 @@ function genericPrint(
64
63
  ): Doc {
65
64
  const node = path.getNode();
66
65
 
67
- if (typeof node === 'undefined' || node === null) {
66
+ if (node === null) {
68
67
  return '';
69
68
  }
70
69
 
package/src/types.d.ts CHANGED
@@ -7,7 +7,6 @@ import type { AstNode, Comment, StrictAstNode } from './slang-nodes/types.d.ts';
7
7
  declare module 'prettier' {
8
8
  interface ParserOptions {
9
9
  compiler: string;
10
- experimentalTernaries: boolean;
11
10
  }
12
11
  }
13
12
 
@@ -40,7 +39,7 @@ interface Metadata {
40
39
 
41
40
  interface SlangNode {
42
41
  kind:
43
- | keyof typeof NonterminalKind
42
+ | NonterminalKind
44
43
  | typeof TerminalKind.Identifier
45
44
  | typeof TerminalKind.YulIdentifier
46
45
  | typeof TerminalKind.MultiLineComment
@@ -58,220 +57,9 @@ interface SlangNode {
58
57
 
59
58
  type PrintFunction = (path: AstPath<AstNode>) => Doc;
60
59
 
61
- export type SlangAstNode =
62
- | ast.SourceUnit
63
- | ast.PragmaDirective
64
- | ast.AbicoderPragma
65
- | ast.ExperimentalPragma
66
- | ast.VersionPragma
67
- | ast.VersionRange
68
- | ast.VersionTerm
69
- | ast.ImportDirective
70
- | ast.PathImport
71
- | ast.NamedImport
72
- | ast.ImportDeconstruction
73
- | ast.ImportDeconstructionSymbol
74
- | ast.ImportAlias
75
- | ast.UsingDirective
76
- | ast.UsingDeconstruction
77
- | ast.UsingDeconstructionSymbol
78
- | ast.UsingAlias
79
- | ast.ContractDefinition
80
- | ast.InheritanceSpecifier
81
- | ast.InheritanceType
82
- | ast.InterfaceDefinition
83
- | ast.LibraryDefinition
84
- | ast.StructDefinition
85
- | ast.StructMember
86
- | ast.EnumDefinition
87
- | ast.ConstantDefinition
88
- | ast.StateVariableDefinition
89
- | ast.StateVariableDefinitionValue
90
- | ast.FunctionDefinition
91
- | ast.ParametersDeclaration
92
- | ast.Parameter
93
- | ast.OverrideSpecifier
94
- | ast.OverridePathsDeclaration
95
- | ast.ReturnsDeclaration
96
- | ast.ConstructorDefinition
97
- | ast.UnnamedFunctionDefinition
98
- | ast.FallbackFunctionDefinition
99
- | ast.ReceiveFunctionDefinition
100
- | ast.ModifierDefinition
101
- | ast.ModifierInvocation
102
- | ast.EventDefinition
103
- | ast.EventParametersDeclaration
104
- | ast.EventParameter
105
- | ast.UserDefinedValueTypeDefinition
106
- | ast.ErrorDefinition
107
- | ast.ErrorParametersDeclaration
108
- | ast.ErrorParameter
109
- | ast.ArrayTypeName
110
- | ast.FunctionType
111
- | ast.MappingType
112
- | ast.MappingKey
113
- | ast.MappingValue
114
- | ast.AddressType
115
- | ast.Block
116
- | ast.UncheckedBlock
117
- | ast.ExpressionStatement
118
- | ast.AssemblyStatement
119
- | ast.AssemblyFlagsDeclaration
120
- | ast.TupleDeconstructionStatement
121
- | ast.TupleDeconstructionElement
122
- | ast.TypedTupleMember
123
- | ast.UntypedTupleMember
124
- | ast.VariableDeclarationStatement
125
- | ast.VariableDeclarationValue
126
- | ast.IfStatement
127
- | ast.ElseBranch
128
- | ast.ForStatement
129
- | ast.WhileStatement
130
- | ast.DoWhileStatement
131
- | ast.ContinueStatement
132
- | ast.BreakStatement
133
- | ast.ReturnStatement
134
- | ast.EmitStatement
135
- | ast.TryStatement
136
- | ast.CatchClause
137
- | ast.CatchClauseError
138
- | ast.RevertStatement
139
- | ast.ThrowStatement
140
- | ast.AssignmentExpression
141
- | ast.ConditionalExpression
142
- | ast.OrExpression
143
- | ast.AndExpression
144
- | ast.EqualityExpression
145
- | ast.InequalityExpression
146
- | ast.BitwiseOrExpression
147
- | ast.BitwiseXorExpression
148
- | ast.BitwiseAndExpression
149
- | ast.ShiftExpression
150
- | ast.AdditiveExpression
151
- | ast.MultiplicativeExpression
152
- | ast.ExponentiationExpression
153
- | ast.PostfixExpression
154
- | ast.PrefixExpression
155
- | ast.FunctionCallExpression
156
- | ast.CallOptionsExpression
157
- | ast.MemberAccessExpression
158
- | ast.IndexAccessExpression
159
- | ast.IndexAccessEnd
160
- | ast.PositionalArgumentsDeclaration
161
- | ast.NamedArgumentsDeclaration
162
- | ast.NamedArgumentGroup
163
- | ast.NamedArgument
164
- | ast.TypeExpression
165
- | ast.NewExpression
166
- | ast.TupleExpression
167
- | ast.TupleValue
168
- | ast.ArrayExpression
169
- | ast.HexNumberExpression
170
- | ast.DecimalNumberExpression
171
- | ast.YulBlock
172
- | ast.YulFunctionDefinition
173
- | ast.YulParametersDeclaration
174
- | ast.YulReturnsDeclaration
175
- | ast.YulVariableDeclarationStatement
176
- | ast.YulVariableDeclarationValue
177
- | ast.YulVariableAssignmentStatement
178
- | ast.YulStackAssignmentStatement
179
- | ast.YulStackAssignmentOperator
180
- | ast.YulColonAndEqual
181
- | ast.YulEqualAndColon
182
- | ast.YulIfStatement
183
- | ast.YulForStatement
184
- | ast.YulSwitchStatement
185
- | ast.YulDefaultCase
186
- | ast.YulValueCase
187
- | ast.YulLeaveStatement
188
- | ast.YulBreakStatement
189
- | ast.YulContinueStatement
190
- | ast.YulLabel
191
- | ast.YulFunctionCallExpression
192
- | ast.SourceUnitMember
193
- | ast.Pragma
194
- | ast.ExperimentalFeature
195
- | ast.VersionExpression
196
- | ast.VersionOperator
197
- | ast.VersionLiteral
198
- | ast.ImportClause
199
- | ast.UsingClause
200
- | ast.UsingOperator
201
- | ast.UsingTarget
202
- | ast.ContractMember
203
- | ast.StateVariableAttribute
204
- | ast.FunctionName
205
- | ast.FunctionAttribute
206
- | ast.FunctionBody
207
- | ast.ConstructorAttribute
208
- | ast.UnnamedFunctionAttribute
209
- | ast.FallbackFunctionAttribute
210
- | ast.ReceiveFunctionAttribute
211
- | ast.ModifierAttribute
212
- | ast.TypeName
213
- | ast.FunctionTypeAttribute
214
- | ast.MappingKeyType
215
- | ast.ElementaryType
216
- | ast.Statement
217
- | ast.TupleMember
218
- | ast.VariableDeclarationType
219
- | ast.StorageLocation
220
- | ast.ForStatementInitialization
221
- | ast.ForStatementCondition
222
- | ast.Expression
223
- | ast.ArgumentsDeclaration
224
- | ast.NumberUnit
225
- | ast.StringExpression
226
- | ast.StringLiteral
227
- | ast.HexStringLiteral
228
- | ast.UnicodeStringLiteral
229
- | ast.YulStatement
230
- | ast.YulAssignmentOperator
231
- | ast.YulSwitchCase
232
- | ast.YulExpression
233
- | ast.YulLiteral
234
- | ast.SourceUnitMembers
235
- | ast.VersionExpressionSet
236
- | ast.ContractMembers
237
- | ast.InterfaceMembers
238
- | ast.LibraryMembers
239
- | ast.StructMembers
240
- | ast.StateVariableAttributes
241
- | ast.FunctionAttributes
242
- | ast.ConstructorAttributes
243
- | ast.UnnamedFunctionAttributes
244
- | ast.FallbackFunctionAttributes
245
- | ast.ReceiveFunctionAttributes
246
- | ast.ModifierAttributes
247
- | ast.FunctionTypeAttributes
248
- | ast.Statements
249
- | ast.CatchClauses
250
- | ast.StringLiterals
251
- | ast.HexStringLiterals
252
- | ast.UnicodeStringLiterals
253
- | ast.YulStatements
254
- | ast.YulSwitchCases
255
- | ast.VersionExpressionSets
256
- | ast.SimpleVersionLiteral
257
- | ast.ImportDeconstructionSymbols
258
- | ast.UsingDeconstructionSymbols
259
- | ast.InheritanceTypes
260
- | ast.EnumMembers
261
- | ast.Parameters
262
- | ast.OverridePaths
263
- | ast.EventParameters
264
- | ast.ErrorParameters
265
- | ast.AssemblyFlags
266
- | ast.TupleDeconstructionElements
267
- | ast.PositionalArguments
268
- | ast.NamedArguments
269
- | ast.CallOptions
270
- | ast.TupleValues
271
- | ast.ArrayValues
272
- | ast.IdentifierPath
273
- | ast.YulParameters
274
- | ast.YulVariableNames
275
- | ast.YulArguments
276
- | ast.YulPaths
277
- | ast.YulPath;
60
+ // This the union of all the types in the namespace `ast`.
61
+ type TypeOfAst = typeof ast;
62
+ type KeyOfAst = keyof TypeOfAst;
63
+ type ValuesOf<E> = E[keyof E];
64
+
65
+ type SlangAstNode = { [k in KeyOfAst]: ValuesOf<TypeOfAst[k]> }[KeyOfAst];
Binary file
@@ -1,6 +0,0 @@
1
- import { version } from 'prettier';
2
- import satisfies from 'semver/functions/satisfies.js';
3
- export function prettierVersionSatisfies(range) {
4
- return satisfies(version, range);
5
- }
6
- //# sourceMappingURL=prettier-version-satisfies.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"prettier-version-satisfies.js","sourceRoot":"","sources":["../../src/slang-utils/prettier-version-satisfies.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AACnC,OAAO,SAAS,MAAM,+BAA+B,CAAC;AAItD,MAAM,UAAU,wBAAwB,CAAC,KAAqB;IAC5D,OAAO,SAAS,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;AACnC,CAAC"}
@@ -1,12 +0,0 @@
1
- export function printWarning(message) {
2
- // Prettier prints some temporary messages while formatting, and this warning
3
- // can mess with that output. We clear the line and move the cursor to the
4
- // beginning of the line to avoid this.
5
- //
6
- // \x1b: Escape character
7
- // [2K: Escape code to clear the entire line
8
- // \r: Carriage return
9
- const clearLine = '\x1b[2K\r';
10
- console.warn(`${clearLine}[prettier-solidity] ${message}`);
11
- }
12
- //# sourceMappingURL=print-warning.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"print-warning.js","sourceRoot":"","sources":["../../src/slang-utils/print-warning.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,YAAY,CAAC,OAAe;IAC1C,6EAA6E;IAC7E,0EAA0E;IAC1E,uCAAuC;IACvC,EAAE;IACF,yBAAyB;IACzB,6CAA6C;IAC7C,wBAAwB;IACxB,MAAM,SAAS,GAAG,WAAW,CAAC;IAE9B,OAAO,CAAC,IAAI,CAAC,GAAG,SAAS,uBAAuB,OAAO,EAAE,CAAC,CAAC;AAC7D,CAAC"}
@@ -1,8 +0,0 @@
1
- import { version } from 'prettier';
2
- import satisfies from 'semver/functions/satisfies.js';
3
-
4
- import type { Range } from 'semver';
5
-
6
- export function prettierVersionSatisfies(range: string | Range): boolean {
7
- return satisfies(version, range);
8
- }
@@ -1,12 +0,0 @@
1
- export function printWarning(message: string): void {
2
- // Prettier prints some temporary messages while formatting, and this warning
3
- // can mess with that output. We clear the line and move the cursor to the
4
- // beginning of the line to avoid this.
5
- //
6
- // \x1b: Escape character
7
- // [2K: Escape code to clear the entire line
8
- // \r: Carriage return
9
- const clearLine = '\x1b[2K\r';
10
-
11
- console.warn(`${clearLine}[prettier-solidity] ${message}`);
12
- }