prettier-plugin-solidity 1.0.0-beta.6 → 1.0.0-dev.21

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 (359) hide show
  1. package/.eslintignore +5 -0
  2. package/.github/FUNDING.yml +1 -0
  3. package/.github/dependabot.yml +23 -0
  4. package/.github/workflows/CI.yml +83 -0
  5. package/.nvmrc +1 -1
  6. package/.prettierignore +4 -1
  7. package/CODE_OF_CONDUCT.md +1 -1
  8. package/Foo.sol +404 -0
  9. package/HOW_TO_PUBLISH.md +2 -2
  10. package/LICENSE +1 -1
  11. package/README.md +220 -28
  12. package/jest.config.js +16 -4
  13. package/package.json +34 -22
  14. package/scripts/makeData.js +2 -1
  15. package/src/binary-operator-printers/arithmetic.js +9 -11
  16. package/src/binary-operator-printers/assignment.js +10 -11
  17. package/src/binary-operator-printers/bit.js +1 -1
  18. package/src/binary-operator-printers/comparison.js +8 -10
  19. package/src/binary-operator-printers/exponentiation.js +8 -10
  20. package/src/binary-operator-printers/index.js +8 -8
  21. package/src/binary-operator-printers/logical.js +8 -10
  22. package/src/binary-operator-printers/shift.js +1 -1
  23. package/src/clean.js +1 -1
  24. package/src/comments/handlers/ContractDefinition.js +8 -6
  25. package/src/comments/printer.js +6 -6
  26. package/src/index.js +8 -3
  27. package/src/loc.js +3 -3
  28. package/src/nodes/ArrayTypeName.js +6 -13
  29. package/src/nodes/AssemblyAssignment.js +1 -1
  30. package/src/nodes/AssemblyBlock.js +13 -14
  31. package/src/nodes/AssemblyCall.js +2 -8
  32. package/src/nodes/AssemblyCase.js +5 -12
  33. package/src/nodes/AssemblyFor.js +1 -1
  34. package/src/nodes/AssemblyFunctionDefinition.js +24 -25
  35. package/src/nodes/AssemblyIf.js +6 -13
  36. package/src/nodes/AssemblyLocalDefinition.js +3 -3
  37. package/src/nodes/AssemblyMemberAccess.js +5 -12
  38. package/src/nodes/AssemblySwitch.js +8 -14
  39. package/src/nodes/Block.js +10 -12
  40. package/src/nodes/Break.js +5 -0
  41. package/src/nodes/CatchClause.js +7 -10
  42. package/src/nodes/Conditional.js +12 -16
  43. package/src/nodes/ContractDefinition.js +17 -20
  44. package/src/nodes/CustomErrorDefinition.js +18 -0
  45. package/src/nodes/DoWhileStatement.js +9 -16
  46. package/src/nodes/ElementaryTypeName.js +2 -8
  47. package/src/nodes/EmitStatement.js +1 -8
  48. package/src/nodes/EnumDefinition.js +12 -14
  49. package/src/nodes/EventDefinition.js +9 -16
  50. package/src/nodes/ExpressionStatement.js +3 -3
  51. package/src/nodes/FileLevelConstant.js +8 -15
  52. package/src/nodes/ForStatement.js +24 -25
  53. package/src/nodes/FunctionCall.js +51 -32
  54. package/src/nodes/FunctionDefinition.js +70 -47
  55. package/src/nodes/FunctionTypeName.js +18 -21
  56. package/src/nodes/HexLiteral.js +1 -1
  57. package/src/nodes/IfStatement.js +10 -12
  58. package/src/nodes/ImportDirective.js +35 -12
  59. package/src/nodes/IndexAccess.js +29 -13
  60. package/src/nodes/IndexRangeAccess.js +8 -15
  61. package/src/nodes/InheritanceSpecifier.js +5 -9
  62. package/src/nodes/InlineAssemblyStatement.js +1 -7
  63. package/src/nodes/LabelDefinition.js +3 -3
  64. package/src/nodes/Mapping.js +7 -14
  65. package/src/nodes/MemberAccess.js +111 -14
  66. package/src/nodes/ModifierDefinition.js +29 -32
  67. package/src/nodes/ModifierInvocation.js +7 -9
  68. package/src/nodes/NameValueExpression.js +6 -13
  69. package/src/nodes/NameValueList.js +3 -3
  70. package/src/nodes/NewExpression.js +1 -7
  71. package/src/nodes/NumberLiteral.js +1 -7
  72. package/src/nodes/PragmaDirective.js +1 -20
  73. package/src/nodes/ReturnStatement.js +9 -6
  74. package/src/nodes/RevertStatement.js +5 -0
  75. package/src/nodes/SourceUnit.js +6 -4
  76. package/src/nodes/StateVariableDeclaration.js +9 -12
  77. package/src/nodes/StringLiteral.js +6 -8
  78. package/src/nodes/StructDefinition.js +13 -14
  79. package/src/nodes/TryStatement.js +5 -5
  80. package/src/nodes/TupleExpression.js +7 -9
  81. package/src/nodes/TypeDefinition.js +5 -0
  82. package/src/nodes/UnaryOperation.js +3 -9
  83. package/src/nodes/UncheckedStatement.js +3 -3
  84. package/src/nodes/UsingForDeclaration.js +7 -14
  85. package/src/nodes/VariableDeclaration.js +19 -23
  86. package/src/nodes/VariableDeclarationStatement.js +21 -15
  87. package/src/nodes/WhileStatement.js +8 -15
  88. package/src/nodes/index.js +72 -68
  89. package/src/nodes/print-comments.js +6 -3
  90. package/src/nodes/print-preserving-empty-lines.js +10 -4
  91. package/src/nodes/print-separated-item.js +3 -3
  92. package/src/nodes/print-separated-list.js +4 -11
  93. package/src/options.js +47 -3
  94. package/src/parser.js +70 -4
  95. package/src/prettier-comments/common/util.js +3 -4
  96. package/src/prettier-comments/language-js/comments.js +1 -1
  97. package/src/printer.js +18 -2
  98. package/tests/config/.prettierrc +5 -0
  99. package/tests/config/format-test.js +458 -0
  100. package/tests/config/require-prettier.js +5 -0
  101. package/tests/config/require-standalone.js +25 -0
  102. package/tests/config/setup.js +7 -0
  103. package/tests/config/utils/check-parsers.js +207 -0
  104. package/tests/config/utils/compile-contract.js +36 -0
  105. package/tests/config/utils/consistent-end-of-line.js +13 -0
  106. package/tests/config/utils/create-snapshot.js +98 -0
  107. package/tests/config/utils/stringify-options-for-title.js +15 -0
  108. package/tests/config/utils/visualize-end-of-line.js +18 -0
  109. package/tests/config/utils/visualize-range.js +40 -0
  110. package/tests/{AddressPayable → format/AddressPayable}/AddressPayable.sol +0 -0
  111. package/tests/{AddressPayable → format/AddressPayable}/__snapshots__/jsfmt.spec.js.snap +9 -2
  112. package/tests/format/AddressPayable/jsfmt.spec.js +1 -0
  113. package/tests/{AllSolidityFeatures → format/AllSolidityFeatures}/AllSolidityFeatures.sol +10 -0
  114. package/tests/{AllSolidityFeatures → format/AllSolidityFeatures}/__snapshots__/jsfmt.spec.js.snap +49 -6
  115. package/tests/format/AllSolidityFeatures/jsfmt.spec.js +1 -0
  116. package/tests/{Arrays → format/Arrays}/Arrays.sol +8 -0
  117. package/tests/{Arrays → format/Arrays}/__snapshots__/jsfmt.spec.js.snap +29 -4
  118. package/tests/format/Arrays/jsfmt.spec.js +1 -0
  119. package/tests/{Assembly → format/Assembly}/Assembly.sol +9 -0
  120. package/tests/{Assembly → format/Assembly}/__snapshots__/jsfmt.spec.js.snap +42 -18
  121. package/tests/format/Assembly/jsfmt.spec.js +1 -0
  122. package/tests/{BasicIterator → format/BasicIterator}/BasicIterator.sol +0 -0
  123. package/tests/{BasicIterator → format/BasicIterator}/__snapshots__/jsfmt.spec.js.snap +9 -2
  124. package/tests/format/BasicIterator/jsfmt.spec.js +1 -0
  125. package/tests/{BinaryOperators → format/BinaryOperators}/BinaryOperators.sol +0 -0
  126. package/tests/{BinaryOperators → format/BinaryOperators}/Parentheses.sol +0 -0
  127. package/tests/{BinaryOperators → format/BinaryOperators}/__snapshots__/jsfmt.spec.js.snap +19 -5
  128. package/tests/format/BinaryOperators/jsfmt.spec.js +1 -0
  129. package/tests/format/BreakingChangesV0.7.4/BreakingChangesV0.7.4.sol +9 -0
  130. package/tests/format/BreakingChangesV0.7.4/__snapshots__/jsfmt.spec.js.snap +198 -0
  131. package/tests/format/BreakingChangesV0.7.4/jsfmt.spec.js +14 -0
  132. package/tests/format/BreakingChangesV0.8.0/BreakingChangesV0.8.0.sol +43 -0
  133. package/tests/format/BreakingChangesV0.8.0/__snapshots__/jsfmt.spec.js.snap +815 -0
  134. package/tests/format/BreakingChangesV0.8.0/jsfmt.spec.js +26 -0
  135. package/tests/{Comments → format/Comments}/Comments.sol +19 -0
  136. package/tests/{Comments → format/Comments}/__snapshots__/jsfmt.spec.js.snap +49 -2
  137. package/tests/format/Comments/jsfmt.spec.js +1 -0
  138. package/tests/{Conditional → format/Conditional}/Conditional.sol +0 -0
  139. package/tests/{Conditional → format/Conditional}/__snapshots__/jsfmt.spec.js.snap +12 -4
  140. package/tests/format/Conditional/jsfmt.spec.js +1 -0
  141. package/tests/{Constructors → format/Constructors}/Constructors.sol +0 -0
  142. package/tests/{Constructors → format/Constructors}/__snapshots__/jsfmt.spec.js.snap +9 -2
  143. package/tests/format/Constructors/jsfmt.spec.js +1 -0
  144. package/tests/{ContractDefinitions → format/ContractDefinitions}/ContractDefinitions.sol +0 -0
  145. package/tests/format/ContractDefinitions/__snapshots__/jsfmt.spec.js.snap +22 -0
  146. package/tests/format/ContractDefinitions/jsfmt.spec.js +1 -0
  147. package/tests/format/CustomErrors/CustomErrors.sol +20 -0
  148. package/tests/format/CustomErrors/__snapshots__/jsfmt.spec.js.snap +50 -0
  149. package/tests/format/CustomErrors/jsfmt.spec.js +1 -0
  150. package/tests/{EnumDefinitions → format/EnumDefinitions}/EnumDefinitions.sol +0 -0
  151. package/tests/format/EnumDefinitions/__snapshots__/jsfmt.spec.js.snap +72 -0
  152. package/tests/format/EnumDefinitions/jsfmt.spec.js +2 -0
  153. package/tests/{Etc → format/Etc}/Etc.sol +0 -0
  154. package/tests/{Etc → format/Etc}/__snapshots__/jsfmt.spec.js.snap +9 -2
  155. package/tests/format/Etc/jsfmt.spec.js +1 -0
  156. package/tests/format/ExplicitVariableTypes/ExplicitVariableTypes.sol +36 -0
  157. package/tests/format/ExplicitVariableTypes/__snapshots__/jsfmt.spec.js.snap +258 -0
  158. package/tests/format/ExplicitVariableTypes/jsfmt.spec.js +9 -0
  159. package/tests/{ForStatements → format/ForStatements}/ForStatements.sol +0 -0
  160. package/tests/{ForStatements → format/ForStatements}/__snapshots__/jsfmt.spec.js.snap +9 -2
  161. package/tests/format/ForStatements/jsfmt.spec.js +1 -0
  162. package/tests/format/FunctionCalls/FunctionCalls.sol +40 -0
  163. package/tests/format/FunctionCalls/__snapshots__/jsfmt.spec.js.snap +310 -0
  164. package/tests/format/FunctionCalls/jsfmt.spec.js +2 -0
  165. package/tests/{FunctionDefinitions → format/FunctionDefinitions}/FunctionDefinitions.sol +5 -2
  166. package/tests/{FunctionDefinitions → format/FunctionDefinitions}/__snapshots__/jsfmt.spec.js.snap +18 -4
  167. package/tests/format/FunctionDefinitions/jsfmt.spec.js +1 -0
  168. package/tests/format/HexLiteral/HexLiteral.sol +7 -0
  169. package/tests/format/HexLiteral/__snapshots__/jsfmt.spec.js.snap +57 -0
  170. package/tests/format/HexLiteral/jsfmt.spec.js +8 -0
  171. package/tests/{IfStatements → format/IfStatements}/IfStatements.sol +0 -0
  172. package/tests/{IfStatements → format/IfStatements}/__snapshots__/jsfmt.spec.js.snap +9 -2
  173. package/tests/format/IfStatements/jsfmt.spec.js +1 -0
  174. package/tests/{Immutable → format/Immutable}/Immutable.sol +0 -0
  175. package/tests/{Immutable → format/Immutable}/__snapshots__/jsfmt.spec.js.snap +9 -2
  176. package/tests/format/Immutable/jsfmt.spec.js +1 -0
  177. package/tests/{ImportDirective → format/ImportDirective}/ImportDirectives.sol +0 -0
  178. package/tests/format/ImportDirective/__snapshots__/jsfmt.spec.js.snap +46 -0
  179. package/tests/format/ImportDirective/jsfmt.spec.js +2 -0
  180. package/tests/{Inbox → format/Inbox}/Inbox.sol +0 -0
  181. package/tests/{Inbox → format/Inbox}/__snapshots__/jsfmt.spec.js.snap +9 -2
  182. package/tests/format/Inbox/jsfmt.spec.js +1 -0
  183. package/tests/{IndexOf → format/IndexOf}/IndexOf.sol +0 -0
  184. package/tests/{IndexOf → format/IndexOf}/__snapshots__/jsfmt.spec.js.snap +9 -2
  185. package/tests/format/IndexOf/jsfmt.spec.js +1 -0
  186. package/tests/{IndexRangeAccess → format/IndexRangeAccess}/IndexRangeAccess.sol +0 -0
  187. package/tests/{IndexRangeAccess → format/IndexRangeAccess}/__snapshots__/jsfmt.spec.js.snap +9 -2
  188. package/tests/format/IndexRangeAccess/jsfmt.spec.js +1 -0
  189. package/tests/{InheritanceSpecifier → format/InheritanceSpecifier}/InheritanceSpecifier.sol +0 -0
  190. package/tests/{InheritanceSpecifier → format/InheritanceSpecifier}/__snapshots__/jsfmt.spec.js.snap +9 -2
  191. package/tests/format/InheritanceSpecifier/jsfmt.spec.js +1 -0
  192. package/tests/{Issues → format/Issues}/Issue205.sol +0 -0
  193. package/tests/{Issues → format/Issues}/Issue289.sol +0 -0
  194. package/tests/{Issues → format/Issues}/Issue355.sol +0 -0
  195. package/tests/{Issues → format/Issues}/Issue385.sol +0 -0
  196. package/tests/format/Issues/Issue564.sol +9 -0
  197. package/tests/format/Issues/__snapshots__/jsfmt.spec.js.snap +174 -0
  198. package/tests/format/Issues/jsfmt.spec.js +1 -0
  199. package/tests/format/MemberAccess/MemberAccess.sol +102 -0
  200. package/tests/format/MemberAccess/__snapshots__/jsfmt.spec.js.snap +243 -0
  201. package/tests/format/MemberAccess/jsfmt.spec.js +1 -0
  202. package/tests/{ModifierDefinitions → format/ModifierDefinitions}/ModifierDefinitions.sol +0 -0
  203. package/tests/{ModifierDefinitions → format/ModifierDefinitions}/__snapshots__/jsfmt.spec.js.snap +9 -2
  204. package/tests/format/ModifierDefinitions/jsfmt.spec.js +1 -0
  205. package/tests/format/ModifierInvocations/ModifierInvocations.sol +17 -0
  206. package/tests/format/ModifierInvocations/__snapshots__/jsfmt.spec.js.snap +51 -0
  207. package/tests/format/ModifierInvocations/jsfmt.spec.js +1 -0
  208. package/tests/{MultipartStrings → format/MultipartStrings}/MultipartStrings.sol +0 -0
  209. package/tests/{MultipartStrings → format/MultipartStrings}/__snapshots__/jsfmt.spec.js.snap +12 -3
  210. package/tests/format/MultipartStrings/jsfmt.spec.js +1 -0
  211. package/tests/{NameValueExpression → format/NameValueExpression}/NameValueExpression.sol +0 -0
  212. package/tests/{NameValueExpression → format/NameValueExpression}/__snapshots__/jsfmt.spec.js.snap +21 -6
  213. package/tests/format/NameValueExpression/jsfmt.spec.js +2 -0
  214. package/tests/{Ownable → format/Ownable}/Ownable.sol +0 -0
  215. package/tests/{Ownable → format/Ownable}/__snapshots__/jsfmt.spec.js.snap +9 -2
  216. package/tests/format/Ownable/jsfmt.spec.js +1 -0
  217. package/tests/format/Parentheses/AddNoParentheses.sol +92 -0
  218. package/tests/format/Parentheses/BitAndNoParentheses.sol +92 -0
  219. package/tests/format/Parentheses/BitOrNoParentheses.sol +92 -0
  220. package/tests/format/Parentheses/BitXorNoParentheses.sol +92 -0
  221. package/tests/format/Parentheses/DivNoParentheses.sol +92 -0
  222. package/tests/format/Parentheses/ExpNoParentheses.sol +92 -0
  223. package/tests/format/Parentheses/LogicNoParentheses.sol +20 -0
  224. package/tests/format/Parentheses/ModNoParentheses.sol +92 -0
  225. package/tests/format/Parentheses/MulNoParentheses.sol +92 -0
  226. package/tests/format/Parentheses/ShiftLNoParentheses.sol +92 -0
  227. package/tests/format/Parentheses/ShiftRNoParentheses.sol +92 -0
  228. package/tests/format/Parentheses/SubNoParentheses.sol +92 -0
  229. package/tests/format/Parentheses/__snapshots__/jsfmt.spec.js.snap +2237 -0
  230. package/tests/format/Parentheses/jsfmt.spec.js +1 -0
  231. package/tests/{Pragma → format/Pragma}/Pragma.sol +1 -0
  232. package/tests/{Pragma → format/Pragma}/__snapshots__/jsfmt.spec.js.snap +11 -2
  233. package/tests/format/Pragma/jsfmt.spec.js +1 -0
  234. package/tests/{PrettierIgnore → format/PrettierIgnore}/PrettierIgnore.sol +0 -0
  235. package/tests/{PrettierIgnore → format/PrettierIgnore}/__snapshots__/jsfmt.spec.js.snap +9 -2
  236. package/tests/format/PrettierIgnore/jsfmt.spec.js +1 -0
  237. package/tests/{Proxy → format/Proxy}/Proxy.sol +0 -0
  238. package/tests/{Proxy → format/Proxy}/__snapshots__/jsfmt.spec.js.snap +16 -9
  239. package/tests/format/Proxy/jsfmt.spec.js +1 -0
  240. package/tests/format/RespectDefaultOptions/__snapshots__/jsfmt.spec.js.snap +19 -0
  241. package/tests/format/RespectDefaultOptions/jsfmt.spec.js +5 -0
  242. package/tests/format/RespectDefaultOptions/respect-default-options.js +3 -0
  243. package/tests/{SampleCrowdsale → format/SampleCrowdsale}/SampleCrowdsale.sol +0 -0
  244. package/tests/{SampleCrowdsale → format/SampleCrowdsale}/__snapshots__/jsfmt.spec.js.snap +9 -2
  245. package/tests/format/SampleCrowdsale/jsfmt.spec.js +1 -0
  246. package/tests/{SimpleAuction → format/SimpleAuction}/SimpleAuction.sol +0 -0
  247. package/tests/{SimpleAuction → format/SimpleAuction}/__snapshots__/jsfmt.spec.js.snap +9 -2
  248. package/tests/format/SimpleAuction/jsfmt.spec.js +1 -0
  249. package/tests/{SimpleStorage → format/SimpleStorage}/SimpleStorage.sol +0 -0
  250. package/tests/{SimpleStorage → format/SimpleStorage}/__snapshots__/jsfmt.spec.js.snap +9 -2
  251. package/tests/format/SimpleStorage/jsfmt.spec.js +1 -0
  252. package/tests/{SplittableCommodity → format/SplittableCommodity}/SplittableCommodity.sol +0 -0
  253. package/tests/{SplittableCommodity → format/SplittableCommodity}/__snapshots__/jsfmt.spec.js.snap +19 -13
  254. package/tests/format/SplittableCommodity/jsfmt.spec.js +1 -0
  255. package/tests/{StateVariableDeclarations → format/StateVariableDeclarations}/StateVariableDeclarations.sol +0 -0
  256. package/tests/format/StateVariableDeclarations/__snapshots__/jsfmt.spec.js.snap +20 -0
  257. package/tests/format/StateVariableDeclarations/jsfmt.spec.js +1 -0
  258. package/tests/format/StringLiteral/StringLiteral.sol +11 -0
  259. package/tests/format/StringLiteral/__snapshots__/jsfmt.spec.js.snap +36 -0
  260. package/tests/format/StringLiteral/jsfmt.spec.js +1 -0
  261. package/tests/{StyleGuide → format/StyleGuide}/BlankLines.sol +0 -0
  262. package/tests/{StyleGuide → format/StyleGuide}/ControlStructures.sol +0 -0
  263. package/tests/{StyleGuide → format/StyleGuide}/FunctionDeclaration.sol +0 -0
  264. package/tests/{StyleGuide → format/StyleGuide}/Mappings.sol +0 -0
  265. package/tests/{StyleGuide → format/StyleGuide}/MaximumLineLength.sol +0 -0
  266. package/tests/{StyleGuide → format/StyleGuide}/OtherRecommendations.sol +0 -0
  267. package/tests/{StyleGuide → format/StyleGuide}/VariableDeclarations.sol +0 -0
  268. package/tests/{StyleGuide → format/StyleGuide}/WhitespaceInExpressions.sol +0 -0
  269. package/tests/{StyleGuide → format/StyleGuide}/__snapshots__/jsfmt.spec.js.snap +72 -16
  270. package/tests/format/StyleGuide/jsfmt.spec.js +1 -0
  271. package/tests/{TryCatch → format/TryCatch}/TryCatch.sol +0 -0
  272. package/tests/{TryCatch → format/TryCatch}/__snapshots__/jsfmt.spec.js.snap +9 -2
  273. package/tests/format/TryCatch/jsfmt.spec.js +1 -0
  274. package/tests/{Tupples → format/Tupples}/Tupples.sol +0 -0
  275. package/tests/{Tupples → format/Tupples}/__snapshots__/jsfmt.spec.js.snap +9 -2
  276. package/tests/format/Tupples/jsfmt.spec.js +1 -0
  277. package/tests/format/TypeDefinition/TypeDefinition.sol +12 -0
  278. package/tests/format/TypeDefinition/__snapshots__/jsfmt.spec.js.snap +74 -0
  279. package/tests/format/TypeDefinition/jsfmt.spec.js +1 -0
  280. package/tests/{WhileStatements → format/WhileStatements}/WhileStatements.sol +0 -0
  281. package/tests/{WhileStatements → format/WhileStatements}/__snapshots__/jsfmt.spec.js.snap +9 -2
  282. package/tests/format/WhileStatements/jsfmt.spec.js +1 -0
  283. package/tests/format/WrongCompiler/WrongCompiler.sol +3 -0
  284. package/tests/format/WrongCompiler/__snapshots__/jsfmt.spec.js.snap +55 -0
  285. package/tests/format/WrongCompiler/jsfmt.spec.js +10 -0
  286. package/tests/{quotes → format/quotes}/Quotes.sol +2 -2
  287. package/tests/{quotes → format/quotes}/__snapshots__/jsfmt.spec.js.snap +62 -34
  288. package/tests/format/quotes/jsfmt.spec.js +2 -0
  289. package/tests/{strings → format/strings}/__snapshots__/jsfmt.spec.js.snap +11 -5
  290. package/tests/format/strings/jsfmt.spec.js +1 -0
  291. package/tests/{strings → format/strings}/strings.sol +0 -0
  292. package/{__tests__ → tests/unit}/binary-operator-printers/__snapshots__/index.test.js.snap +0 -0
  293. package/{__tests__ → tests/unit}/binary-operator-printers/index.test.js +1 -1
  294. package/{__tests__ → tests/unit}/comments/printer.test.js +1 -1
  295. package/tests/unit/prettier-version.test.js +26 -0
  296. package/{__tests__ → tests/unit}/printer.test.js +1 -1
  297. package/.travis.yml +0 -10
  298. package/__tests__/nodes/__snapshots__/index.test.js.snap +0 -73
  299. package/__tests__/nodes/index.test.js +0 -5
  300. package/codecov.yml +0 -2
  301. package/scripts/__snapshots__/makeData.test.js.snap +0 -17
  302. package/scripts/makeData.test.js +0 -10
  303. package/tests/AddressPayable/jsfmt.spec.js +0 -1
  304. package/tests/AllSolidityFeatures/jsfmt.spec.js +0 -1
  305. package/tests/Arrays/jsfmt.spec.js +0 -1
  306. package/tests/Assembly/jsfmt.spec.js +0 -1
  307. package/tests/BasicIterator/jsfmt.spec.js +0 -1
  308. package/tests/BinaryOperators/jsfmt.spec.js +0 -1
  309. package/tests/Comments/jsfmt.spec.js +0 -1
  310. package/tests/Conditional/jsfmt.spec.js +0 -1
  311. package/tests/Constructors/jsfmt.spec.js +0 -1
  312. package/tests/ContractDefinitions/__snapshots__/jsfmt.spec.js.snap +0 -15
  313. package/tests/ContractDefinitions/jsfmt.spec.js +0 -1
  314. package/tests/EnumDefinitions/__snapshots__/jsfmt.spec.js.snap +0 -47
  315. package/tests/EnumDefinitions/jsfmt.spec.js +0 -2
  316. package/tests/Etc/jsfmt.spec.js +0 -1
  317. package/tests/ExplicitVariableTypes/ExplicitVariableTypes.sol +0 -30
  318. package/tests/ExplicitVariableTypes/__snapshots__/jsfmt.spec.js.snap +0 -242
  319. package/tests/ExplicitVariableTypes/jsfmt.spec.js +0 -3
  320. package/tests/ForStatements/jsfmt.spec.js +0 -1
  321. package/tests/FunctionCalls/FunctionCalls.sol +0 -19
  322. package/tests/FunctionCalls/__snapshots__/jsfmt.spec.js.snap +0 -77
  323. package/tests/FunctionCalls/jsfmt.spec.js +0 -2
  324. package/tests/FunctionDefinitions/jsfmt.spec.js +0 -1
  325. package/tests/IfStatements/jsfmt.spec.js +0 -1
  326. package/tests/Immutable/jsfmt.spec.js +0 -1
  327. package/tests/ImportDirective/__snapshots__/jsfmt.spec.js.snap +0 -41
  328. package/tests/ImportDirective/jsfmt.spec.js +0 -2
  329. package/tests/Inbox/jsfmt.spec.js +0 -1
  330. package/tests/IndexOf/jsfmt.spec.js +0 -1
  331. package/tests/IndexRangeAccess/jsfmt.spec.js +0 -1
  332. package/tests/InheritanceSpecifier/jsfmt.spec.js +0 -1
  333. package/tests/Issues/__snapshots__/jsfmt.spec.js.snap +0 -112
  334. package/tests/Issues/jsfmt.spec.js +0 -1
  335. package/tests/MemberAccess/MemberAccess.sol +0 -15
  336. package/tests/MemberAccess/__snapshots__/jsfmt.spec.js.snap +0 -56
  337. package/tests/MemberAccess/jsfmt.spec.js +0 -1
  338. package/tests/ModifierDefinitions/jsfmt.spec.js +0 -1
  339. package/tests/MultipartStrings/jsfmt.spec.js +0 -1
  340. package/tests/NameValueExpression/jsfmt.spec.js +0 -2
  341. package/tests/Ownable/jsfmt.spec.js +0 -1
  342. package/tests/Pragma/jsfmt.spec.js +0 -1
  343. package/tests/PrettierIgnore/jsfmt.spec.js +0 -1
  344. package/tests/Proxy/jsfmt.spec.js +0 -1
  345. package/tests/SampleCrowdsale/jsfmt.spec.js +0 -1
  346. package/tests/SimpleAuction/jsfmt.spec.js +0 -1
  347. package/tests/SimpleStorage/jsfmt.spec.js +0 -1
  348. package/tests/SplittableCommodity/jsfmt.spec.js +0 -1
  349. package/tests/StateVariableDeclarations/__snapshots__/jsfmt.spec.js.snap +0 -13
  350. package/tests/StateVariableDeclarations/jsfmt.spec.js +0 -1
  351. package/tests/StyleGuide/jsfmt.spec.js +0 -1
  352. package/tests/TryCatch/jsfmt.spec.js +0 -1
  353. package/tests/Tupples/jsfmt.spec.js +0 -1
  354. package/tests/WhileStatements/jsfmt.spec.js +0 -1
  355. package/tests/quotes/jsfmt.spec.js +0 -2
  356. package/tests/strings/jsfmt.spec.js +0 -1
  357. package/tests_config/helpers.js +0 -44
  358. package/tests_config/raw-serializer.js +0 -15
  359. package/tests_config/run_spec.js +0 -79
@@ -0,0 +1,458 @@
1
+ "use strict";
2
+
3
+ const { TEST_STANDALONE } = process.env;
4
+
5
+ const fs = require("fs");
6
+ const path = require("path");
7
+ const prettier = !TEST_STANDALONE
8
+ ? require("./require-prettier")
9
+ : require("./require-standalone");
10
+ const checkParsers = require("./utils/check-parsers");
11
+ const createSnapshot = require("./utils/create-snapshot");
12
+ const visualizeEndOfLine = require("./utils/visualize-end-of-line");
13
+ const consistentEndOfLine = require("./utils/consistent-end-of-line");
14
+ const stringifyOptionsForTitle = require("./utils/stringify-options-for-title");
15
+
16
+ const { FULL_TEST } = process.env;
17
+ const BOM = "\uFEFF";
18
+
19
+ const CURSOR_PLACEHOLDER = "<|>";
20
+ const RANGE_START_PLACEHOLDER = "<<<PRETTIER_RANGE_START>>>";
21
+ const RANGE_END_PLACEHOLDER = "<<<PRETTIER_RANGE_END>>>";
22
+
23
+ // Here we add files that will not be the same when formatting a second time.
24
+ const unstableTests = new Map(
25
+ [].map((fixture) => {
26
+ const [file, isUnstable = () => true] = Array.isArray(fixture)
27
+ ? fixture
28
+ : [fixture];
29
+ return [path.join(__dirname, "../format/", file), isUnstable];
30
+ })
31
+ );
32
+
33
+ // Here we add files that will not have the same AST after being formatted.
34
+ const unstableAstTests = new Map(
35
+ [].map((fixture) => {
36
+ const [file, isAstUnstable = () => true] = Array.isArray(fixture)
37
+ ? fixture
38
+ : [fixture];
39
+ return [path.join(__dirname, "../format/", file), isAstUnstable];
40
+ })
41
+ );
42
+
43
+ const testsWithAstChanges = new Map(
44
+ [
45
+ [
46
+ "ExplicitVariableTypes/ExplicitVariableTypes.sol",
47
+ (options) =>
48
+ options.explicitTypes === undefined ||
49
+ options.explicitTypes !== "preserve",
50
+ ],
51
+ "Parentheses/AddNoParentheses.sol",
52
+ "Parentheses/SubNoParentheses.sol",
53
+ "Parentheses/MulNoParentheses.sol",
54
+ "Parentheses/DivNoParentheses.sol",
55
+ "Parentheses/ModNoParentheses.sol",
56
+ "Parentheses/ExpNoParentheses.sol",
57
+ "Parentheses/ShiftLNoParentheses.sol",
58
+ "Parentheses/ShiftRNoParentheses.sol",
59
+ "Parentheses/BitAndNoParentheses.sol",
60
+ "Parentheses/BitOrNoParentheses.sol",
61
+ "Parentheses/BitXorNoParentheses.sol",
62
+ "Parentheses/LogicNoParentheses.sol",
63
+ "HexLiteral/HexLiteral.sol",
64
+ "ModifierInvocations/ModifierInvocations.sol",
65
+ ].map((fixture) => {
66
+ const [file, compareBytecode = () => true] = Array.isArray(fixture)
67
+ ? fixture
68
+ : [fixture];
69
+ return [path.join(__dirname, "../format/", file), compareBytecode];
70
+ })
71
+ );
72
+
73
+ const isUnstable = (filename, options) => {
74
+ const testFunction = unstableTests.get(filename);
75
+
76
+ if (!testFunction) {
77
+ return false;
78
+ }
79
+
80
+ return testFunction(options);
81
+ };
82
+
83
+ const isAstUnstable = (filename, options) => {
84
+ const testFunction = unstableAstTests.get(filename);
85
+
86
+ if (!testFunction) {
87
+ return false;
88
+ }
89
+
90
+ return testFunction(options);
91
+ };
92
+
93
+ const shouldCompareBytecode = (filename, options) => {
94
+ const testFunction = testsWithAstChanges.get(filename);
95
+
96
+ if (!testFunction) {
97
+ return false;
98
+ }
99
+
100
+ return testFunction(options);
101
+ };
102
+
103
+ const shouldThrowOnFormat = (filename, options) => {
104
+ const { errors = {} } = options;
105
+ if (errors === true) {
106
+ return true;
107
+ }
108
+
109
+ const files = errors[options.parser];
110
+
111
+ if (files === true || (Array.isArray(files) && files.includes(filename))) {
112
+ return true;
113
+ }
114
+
115
+ return false;
116
+ };
117
+
118
+ const isTestDirectory = (dirname, name) =>
119
+ (dirname + path.sep).startsWith(
120
+ path.join(__dirname, "../format", name) + path.sep
121
+ );
122
+
123
+ function runSpec(fixtures, parsers, options) {
124
+ let { dirname, snippets = [] } =
125
+ typeof fixtures === "string" ? { dirname: fixtures } : fixtures;
126
+
127
+ // `IS_PARSER_INFERENCE_TESTS` mean to test `inferParser` on `standalone`
128
+ const IS_PARSER_INFERENCE_TESTS = isTestDirectory(
129
+ dirname,
130
+ "misc/parser-inference"
131
+ );
132
+
133
+ // `IS_ERROR_TESTS` mean to watch errors like:
134
+ // - syntax parser hasn't supported yet
135
+ // - syntax errors that should throws
136
+ const IS_ERROR_TESTS = isTestDirectory(dirname, "misc/errors");
137
+ if (IS_ERROR_TESTS) {
138
+ options = { errors: true, ...options };
139
+ }
140
+
141
+ if (IS_PARSER_INFERENCE_TESTS) {
142
+ parsers = [undefined];
143
+ }
144
+
145
+ snippets = snippets.map((test, index) => {
146
+ test = typeof test === "string" ? { code: test } : test;
147
+ return {
148
+ ...test,
149
+ name: `snippet: ${test.name || `#${index}`}`,
150
+ };
151
+ });
152
+
153
+ const files = fs
154
+ .readdirSync(dirname, { withFileTypes: true })
155
+ .map((file) => {
156
+ const basename = file.name;
157
+ const filename = path.join(dirname, basename);
158
+ if (
159
+ path.extname(basename) === ".snap" ||
160
+ !file.isFile() ||
161
+ basename[0] === "." ||
162
+ basename === "jsfmt.spec.js" ||
163
+ // VSCode creates this file sometime https://github.com/microsoft/vscode/issues/105191
164
+ basename === "debug.log"
165
+ ) {
166
+ return;
167
+ }
168
+
169
+ const text = fs.readFileSync(filename, "utf8");
170
+
171
+ return {
172
+ name: basename,
173
+ filename,
174
+ code: text,
175
+ };
176
+ })
177
+ .filter(Boolean);
178
+
179
+ // Make sure tests are in correct location
180
+ if (process.env.CHECK_TEST_PARSERS) {
181
+ if (!Array.isArray(parsers) || parsers.length === 0) {
182
+ throw new Error(`No parsers were specified for ${dirname}`);
183
+ }
184
+ checkParsers({ dirname, files }, parsers);
185
+ }
186
+
187
+ const [parser] = parsers;
188
+ const allParsers = [...parsers];
189
+
190
+ const stringifiedOptions = stringifyOptionsForTitle(options);
191
+
192
+ for (const { name, filename, code, output } of [...files, ...snippets]) {
193
+ const title = `${name}${
194
+ stringifiedOptions ? ` - ${stringifiedOptions}` : ""
195
+ }`;
196
+
197
+ describe(title, () => {
198
+ const formatOptions = {
199
+ plugins: [path.join(__dirname, "../..")],
200
+ printWidth: 80,
201
+ ...options,
202
+ filepath: filename,
203
+ parser,
204
+ };
205
+ const mainParserFormatResult = shouldThrowOnFormat(name, formatOptions)
206
+ ? { options: formatOptions, error: true }
207
+ : format(code, formatOptions);
208
+
209
+ for (const currentParser of allParsers) {
210
+ runTest({
211
+ parsers,
212
+ name,
213
+ filename,
214
+ code,
215
+ output,
216
+ parser: currentParser,
217
+ mainParserFormatResult,
218
+ mainParserFormatOptions: formatOptions,
219
+ });
220
+ }
221
+ });
222
+ }
223
+ }
224
+
225
+ function runTest({
226
+ parsers,
227
+ name,
228
+ filename,
229
+ code,
230
+ output,
231
+ parser,
232
+ mainParserFormatResult,
233
+ mainParserFormatOptions,
234
+ }) {
235
+ let formatOptions = mainParserFormatOptions;
236
+ let formatResult = mainParserFormatResult;
237
+ let formatTestTitle = "format";
238
+
239
+ // Verify parsers or error tests
240
+ if (
241
+ mainParserFormatResult.error ||
242
+ mainParserFormatOptions.parser !== parser
243
+ ) {
244
+ formatTestTitle = `[${parser}] format`;
245
+ formatOptions = { ...mainParserFormatResult.options, parser };
246
+ const runFormat = () => format(code, formatOptions);
247
+
248
+ if (shouldThrowOnFormat(name, formatOptions)) {
249
+ test(formatTestTitle, () => {
250
+ expect(runFormat).toThrowErrorMatchingSnapshot();
251
+ });
252
+ return;
253
+ }
254
+
255
+ // Verify parsers format result should be the same as main parser
256
+ output = mainParserFormatResult.outputWithCursor;
257
+ formatResult = runFormat();
258
+ }
259
+
260
+ test(formatTestTitle, () => {
261
+ // Make sure output has consistent EOL
262
+ expect(formatResult.eolVisualizedOutput).toEqual(
263
+ visualizeEndOfLine(consistentEndOfLine(formatResult.outputWithCursor))
264
+ );
265
+
266
+ // The result is assert to equals to `output`
267
+ if (typeof output === "string") {
268
+ expect(formatResult.eolVisualizedOutput).toEqual(
269
+ visualizeEndOfLine(output)
270
+ );
271
+ return;
272
+ }
273
+
274
+ // All parsers have the same result, only snapshot the result from main parser
275
+ expect(
276
+ createSnapshot(formatResult, {
277
+ parsers,
278
+ formatOptions,
279
+ CURSOR_PLACEHOLDER,
280
+ })
281
+ ).toMatchSnapshot();
282
+ });
283
+
284
+ if (!FULL_TEST) {
285
+ return;
286
+ }
287
+
288
+ const isUnstableTest = isUnstable(filename, formatOptions);
289
+ if (
290
+ (formatResult.changed || isUnstableTest) &&
291
+ // No range and cursor
292
+ formatResult.input === code
293
+ ) {
294
+ test(`[${parser}] second format`, () => {
295
+ const { eolVisualizedOutput: firstOutput, output } = formatResult;
296
+ const { eolVisualizedOutput: secondOutput } = format(
297
+ output,
298
+ formatOptions
299
+ );
300
+ if (isUnstableTest) {
301
+ // To keep eye on failed tests, this assert never supposed to pass,
302
+ // if it fails, just remove the file from `unstableTests`
303
+ expect(secondOutput).not.toEqual(firstOutput);
304
+ } else {
305
+ expect(secondOutput).toEqual(firstOutput);
306
+ }
307
+ });
308
+ }
309
+
310
+ const isAstUnstableTest = isAstUnstable(filename, formatOptions);
311
+ // Some parsers skip parsing empty files
312
+ if (formatResult.changed && code.trim()) {
313
+ test(`[${parser}] compare AST`, () => {
314
+ const { input, output } = formatResult;
315
+ const originalAst = parse(input, formatOptions);
316
+ const formattedAst = parse(output, formatOptions);
317
+ if (isAstUnstableTest) {
318
+ expect(formattedAst).not.toEqual(originalAst);
319
+ } else {
320
+ expect(formattedAst).toEqual(originalAst);
321
+ }
322
+ });
323
+ }
324
+
325
+ if (!shouldSkipEolTest(code, formatResult.options)) {
326
+ for (const eol of ["\r\n", "\r"]) {
327
+ test(`[${parser}] EOL ${JSON.stringify(eol)}`, () => {
328
+ const output = format(
329
+ code.replace(/\n/g, eol),
330
+ formatOptions
331
+ ).eolVisualizedOutput;
332
+ // Only if `endOfLine: "auto"` the result will be different
333
+ const expected =
334
+ formatOptions.endOfLine === "auto"
335
+ ? visualizeEndOfLine(
336
+ // All `code` use `LF`, so the `eol` of result is always `LF`
337
+ formatResult.outputWithCursor.replace(/\n/g, eol)
338
+ )
339
+ : formatResult.eolVisualizedOutput;
340
+ expect(output).toEqual(expected);
341
+ });
342
+ }
343
+ }
344
+
345
+ if (code.charAt(0) !== BOM) {
346
+ test(`[${parser}] BOM`, () => {
347
+ const output = format(BOM + code, formatOptions).eolVisualizedOutput;
348
+ const expected = BOM + formatResult.eolVisualizedOutput;
349
+ expect(output).toEqual(expected);
350
+ });
351
+ }
352
+ if (shouldCompareBytecode(filename, formatOptions)) {
353
+ test(`[${parser}] compare Bytecode`, () => {
354
+ // We require the compiler here as it makes all tests slow when added at
355
+ // the top of the file.
356
+ // TODO investigate this warning
357
+ // - A worker process has failed to exit gracefully and has been force
358
+ // exited. This is likely caused by tests leaking due to improper
359
+ // teardown. Try running with --detectOpenHandles to find leaks.
360
+ const compileContract = require("./utils/compile-contract");
361
+ const output = compileContract(filename, formatResult.output);
362
+ const expected = compileContract(filename, formatResult.input);
363
+ expect(output).toEqual(expected);
364
+ });
365
+ }
366
+ }
367
+
368
+ function shouldSkipEolTest(code, options) {
369
+ if (code.includes("\r")) {
370
+ return true;
371
+ }
372
+ const { requirePragma, rangeStart, rangeEnd } = options;
373
+ if (requirePragma) {
374
+ return true;
375
+ }
376
+
377
+ if (
378
+ typeof rangeStart === "number" &&
379
+ typeof rangeEnd === "number" &&
380
+ rangeStart >= rangeEnd
381
+ ) {
382
+ return true;
383
+ }
384
+ return false;
385
+ }
386
+
387
+ function parse(source, options) {
388
+ return prettier.__debug.parse(source, options, /* massage */ true).ast;
389
+ }
390
+
391
+ const indexProperties = [
392
+ {
393
+ property: "cursorOffset",
394
+ placeholder: CURSOR_PLACEHOLDER,
395
+ },
396
+ {
397
+ property: "rangeStart",
398
+ placeholder: RANGE_START_PLACEHOLDER,
399
+ },
400
+ {
401
+ property: "rangeEnd",
402
+ placeholder: RANGE_END_PLACEHOLDER,
403
+ },
404
+ ];
405
+ function replacePlaceholders(originalText, originalOptions) {
406
+ const indexes = indexProperties
407
+ .map(({ property, placeholder }) => {
408
+ const value = originalText.indexOf(placeholder);
409
+ return value === -1 ? undefined : { property, value, placeholder };
410
+ })
411
+ .filter(Boolean)
412
+ .sort((a, b) => a.value - b.value);
413
+
414
+ const options = { ...originalOptions };
415
+ let text = originalText;
416
+ let offset = 0;
417
+ for (const { property, value, placeholder } of indexes) {
418
+ text = text.replace(placeholder, "");
419
+ options[property] = value + offset;
420
+ offset -= placeholder.length;
421
+ }
422
+ return { text, options };
423
+ }
424
+
425
+ const insertCursor = (text, cursorOffset) =>
426
+ cursorOffset >= 0
427
+ ? text.slice(0, cursorOffset) +
428
+ CURSOR_PLACEHOLDER +
429
+ text.slice(cursorOffset)
430
+ : text;
431
+ function format(originalText, originalOptions) {
432
+ const { text: input, options } = replacePlaceholders(
433
+ originalText,
434
+ originalOptions
435
+ );
436
+ const inputWithCursor = insertCursor(input, options.cursorOffset);
437
+
438
+ const { formatted: output, cursorOffset } = prettier.formatWithCursor(
439
+ input,
440
+ options
441
+ );
442
+ const outputWithCursor = insertCursor(output, cursorOffset);
443
+ const eolVisualizedOutput = visualizeEndOfLine(outputWithCursor);
444
+
445
+ const changed = outputWithCursor !== inputWithCursor;
446
+
447
+ return {
448
+ changed,
449
+ options,
450
+ input,
451
+ inputWithCursor,
452
+ output,
453
+ outputWithCursor,
454
+ eolVisualizedOutput,
455
+ };
456
+ }
457
+
458
+ module.exports = runSpec;
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+
3
+ const prettier = require("prettier");
4
+
5
+ module.exports = prettier;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+
3
+ const prettier = require("prettier/standalone");
4
+ const babelPlugin = require("prettier/parser-babel");
5
+ const solidityPlugin = require("../../src/index");
6
+
7
+ module.exports = {
8
+ formatWithCursor(input, options) {
9
+ const $$$options = {
10
+ ...options,
11
+ plugins: [babelPlugin, solidityPlugin, ...(options.plugins || [])],
12
+ };
13
+ return prettier.formatWithCursor(input, $$$options);
14
+ },
15
+
16
+ __debug: {
17
+ parse(input, options, massage) {
18
+ const $$$options = {
19
+ ...options,
20
+ plugins: [babelPlugin, solidityPlugin, ...(options.plugins || [])],
21
+ };
22
+ return prettier.__debug.parse(input, $$$options, massage);
23
+ },
24
+ },
25
+ };
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(global, "run_spec", {
4
+ get() {
5
+ return require("./format-test");
6
+ },
7
+ });
@@ -0,0 +1,207 @@
1
+ "use strict";
2
+
3
+ const path = require("path");
4
+ const { outdent } = require("outdent");
5
+
6
+ const TESTS_ROOT = path.join(__dirname, "../../format");
7
+
8
+ const getCategory = (dirname) =>
9
+ path.relative(TESTS_ROOT, dirname).split(path.sep).shift();
10
+
11
+ const categoryParsers = new Map([
12
+ [
13
+ "angular",
14
+ {
15
+ parsers: ["angular", "__ng_interpolation", "__ng_action"],
16
+ verifyParsers: [],
17
+ extensions: [".html", ".ng"],
18
+ },
19
+ ],
20
+ [
21
+ "css",
22
+ { parsers: ["css"], verifyParsers: ["less", "scss"], extensions: [".css"] },
23
+ ],
24
+ [
25
+ "flow",
26
+ {
27
+ parsers: ["flow", "babel-flow"],
28
+ verifyParsers: ["babel", "flow", "babel-flow", "typescript", "babel-ts"],
29
+ extensions: [".js"],
30
+ },
31
+ ],
32
+ [
33
+ "flow-repo",
34
+ {
35
+ parsers: ["flow", "babel-flow"],
36
+ verifyParsers: ["babel", "flow", "babel-flow", "typescript", "babel-ts"],
37
+ extensions: [".js"],
38
+ },
39
+ ],
40
+ [
41
+ "graphql",
42
+ { parsers: ["graphql"], verifyParsers: [], extensions: [".graphql"] },
43
+ ],
44
+ [
45
+ "handlebars",
46
+ { parsers: ["glimmer"], verifyParsers: [], extensions: [".hbs"] },
47
+ ],
48
+ ["html", { parsers: ["html"], verifyParsers: [], extensions: [".html"] }],
49
+ ["mjml", { parsers: ["html"], verifyParsers: [], extensions: [".mjml"] }],
50
+ [
51
+ "js",
52
+ {
53
+ parsers: ["babel", "meriyah", "espree"],
54
+ verifyParsers: [
55
+ "babel",
56
+ "meriyah",
57
+ "espree",
58
+ "flow",
59
+ "babel-flow",
60
+ "typescript",
61
+ "babel-ts",
62
+ ],
63
+ extensions: [".js"],
64
+ },
65
+ ],
66
+ [
67
+ "json",
68
+ {
69
+ parsers: ["json", "json5", "json-stringify"],
70
+ verifyParsers: ["json", "json5", "json-stringify"],
71
+ extensions: [".json"],
72
+ },
73
+ ],
74
+ [
75
+ "jsx",
76
+ {
77
+ parsers: ["babel", "flow", "babel-flow", "typescript", "babel-ts"],
78
+ verifyParsers: ["babel", "flow", "babel-flow", "typescript", "babel-ts"],
79
+ extensions: [".js"],
80
+ },
81
+ ],
82
+ [
83
+ "less",
84
+ {
85
+ parsers: ["less"],
86
+ verifyParsers: ["css", "scss"],
87
+ extensions: [".less"],
88
+ },
89
+ ],
90
+ ["lwc", { parsers: ["lwc"], verifyParsers: [], extensions: [".html"] }],
91
+ [
92
+ "markdown",
93
+ { parsers: ["markdown"], verifyParsers: [], extensions: [".md"] },
94
+ ],
95
+ ["mdx", { parsers: ["mdx"], verifyParsers: [], extensions: [".mdx"] }],
96
+ [
97
+ "scss",
98
+ {
99
+ parsers: ["scss"],
100
+ verifyParsers: ["css", "less"],
101
+ extensions: [".scss"],
102
+ },
103
+ ],
104
+ [
105
+ "stylefmt-repo",
106
+ { parsers: ["css", "scss"], verifyParsers: [], extensions: [".css"] },
107
+ ],
108
+ [
109
+ "typescript",
110
+ {
111
+ parsers: ["typescript", "babel-ts"],
112
+ verifyParsers: ["babel", "flow", "babel-flow", "typescript", "babel-ts"],
113
+ extensions: [".ts", ".tsx"],
114
+ },
115
+ ],
116
+ [
117
+ "vue",
118
+ { parsers: ["vue"], verifyParsers: [], extensions: [".vue", ".html"] },
119
+ ],
120
+ ["yaml", { parsers: ["yaml"], verifyParsers: [], extensions: [".yml"] }],
121
+ ]);
122
+
123
+ const getParserCategories = (parser) => {
124
+ const categories = [];
125
+ for (const [category, { parsers }] of categoryParsers) {
126
+ if (parsers.includes(parser)) {
127
+ categories.push(category);
128
+ }
129
+ }
130
+
131
+ return categories;
132
+ };
133
+
134
+ const checkParser = ({ dirname, files }, parsers = []) => {
135
+ const category = getCategory(dirname);
136
+ const categoryAllowedParsers = categoryParsers.get(category);
137
+
138
+ if (!categoryAllowedParsers) {
139
+ return;
140
+ }
141
+
142
+ const {
143
+ parsers: allowedParsers = [],
144
+ verifyParsers: allowedVerifyParsers = [],
145
+ extensions = [],
146
+ } = categoryAllowedParsers;
147
+
148
+ const [parser, ...verifyParsers] = parsers;
149
+
150
+ if (verifyParsers.includes(parser)) {
151
+ throw new Error(
152
+ `verifyParsers ${JSON.stringify(
153
+ verifyParsers
154
+ )} should not include parser "${parser}".`
155
+ );
156
+ }
157
+
158
+ if (allowedParsers && !allowedParsers.includes(parser)) {
159
+ const suggestCategories = getParserCategories(parser);
160
+
161
+ const suggestion =
162
+ suggestCategories.length === 0
163
+ ? ""
164
+ : outdent`
165
+ Suggest move your tests to:
166
+ ${suggestCategories
167
+ .map((category) => `- ${path.join(TESTS_ROOT, category)}`)
168
+ .join("\n")}
169
+
170
+ Or config to allow use this parser in "${__filename}".
171
+ `;
172
+
173
+ throw new Error(
174
+ `Parser "${parser}" should not used in "${dirname}".${
175
+ suggestion ? `\n\n${suggestion}` : ""
176
+ }`
177
+ );
178
+ }
179
+
180
+ if (allowedVerifyParsers) {
181
+ for (const verifyParser of verifyParsers) {
182
+ if (!allowedVerifyParsers.includes(verifyParser)) {
183
+ throw new Error(
184
+ outdent`
185
+ Parser "${verifyParser}" should not used to verify in "${dirname}".
186
+ Please remove it or config to allow use this parser in "${__filename}".
187
+ `
188
+ );
189
+ }
190
+ }
191
+ }
192
+
193
+ for (const { name, filename } of files) {
194
+ const ext = path.extname(filename);
195
+ if (!extensions.includes(ext)) {
196
+ throw new Error(
197
+ outdent`
198
+ File "${name}" should not tested in "${dirname}".
199
+ Allowed extensions: ${extensions.join(",")}.
200
+ Please rename it or config to allow test "${ext}" file in "${__filename}".
201
+ `
202
+ );
203
+ }
204
+ }
205
+ };
206
+
207
+ module.exports = checkParser;