prettier-plugin-solidity 1.0.0 → 1.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.
Files changed (139) hide show
  1. package/README.md +45 -11
  2. package/dist/standalone.js +3 -0
  3. package/dist/standalone.js.LICENSE.txt +15 -0
  4. package/dist/standalone.js.map +1 -0
  5. package/package.json +36 -7
  6. package/src/common/util.js +59 -0
  7. package/src/nodes/HexLiteral.js +1 -1
  8. package/src/nodes/ImportDirective.js +5 -4
  9. package/src/nodes/InlineAssemblyStatement.js +1 -1
  10. package/src/nodes/SourceUnit.js +1 -1
  11. package/src/nodes/StringLiteral.js +1 -1
  12. package/src/parser.js +6 -5
  13. package/src/prettier-comments/language-js/comments.js +12 -10
  14. package/src/printer.js +3 -3
  15. package/.c8rc +0 -11
  16. package/.eslintignore +0 -5
  17. package/.eslintrc +0 -9
  18. package/.nvmrc +0 -1
  19. package/.prettierignore +0 -4
  20. package/.prettierrc +0 -4
  21. package/CODE_OF_CONDUCT.md +0 -76
  22. package/HOW_TO_PUBLISH.md +0 -39
  23. package/STYLEGUIDE.md +0 -382
  24. package/coverage/clover.xml +0 -1171
  25. package/coverage/coverage-final.json +0 -99
  26. package/coverage/lcov-report/base.css +0 -224
  27. package/coverage/lcov-report/block-navigation.js +0 -87
  28. package/coverage/lcov-report/favicon.png +0 -0
  29. package/coverage/lcov-report/index.html +0 -176
  30. package/coverage/lcov-report/prettify.css +0 -1
  31. package/coverage/lcov-report/prettify.js +0 -2
  32. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  33. package/coverage/lcov-report/sorter.js +0 -196
  34. package/coverage/lcov-report/src/binary-operator-printers/arithmetic.js.html +0 -244
  35. package/coverage/lcov-report/src/binary-operator-printers/assignment.js.html +0 -175
  36. package/coverage/lcov-report/src/binary-operator-printers/bit.js.html +0 -103
  37. package/coverage/lcov-report/src/binary-operator-printers/comparison.js.html +0 -199
  38. package/coverage/lcov-report/src/binary-operator-printers/exponentiation.js.html +0 -148
  39. package/coverage/lcov-report/src/binary-operator-printers/index.html +0 -221
  40. package/coverage/lcov-report/src/binary-operator-printers/index.js.html +0 -124
  41. package/coverage/lcov-report/src/binary-operator-printers/logical.js.html +0 -208
  42. package/coverage/lcov-report/src/binary-operator-printers/shift.js.html +0 -103
  43. package/coverage/lcov-report/src/clean.js.html +0 -109
  44. package/coverage/lcov-report/src/comments/handler.js.html +0 -373
  45. package/coverage/lcov-report/src/comments/handlers/handleContractDefinitionComments.js.html +0 -265
  46. package/coverage/lcov-report/src/comments/handlers/handleModifierInvocationComments.js.html +0 -292
  47. package/coverage/lcov-report/src/comments/handlers/index.html +0 -146
  48. package/coverage/lcov-report/src/comments/handlers/index.js.html +0 -103
  49. package/coverage/lcov-report/src/comments/ignore.js.html +0 -184
  50. package/coverage/lcov-report/src/comments/index.html +0 -161
  51. package/coverage/lcov-report/src/comments/index.js.html +0 -97
  52. package/coverage/lcov-report/src/comments/printer.js.html +0 -277
  53. package/coverage/lcov-report/src/index.html +0 -191
  54. package/coverage/lcov-report/src/index.js.html +0 -268
  55. package/coverage/lcov-report/src/loc.js.html +0 -133
  56. package/coverage/lcov-report/src/nodes/ArrayTypeName.js.html +0 -115
  57. package/coverage/lcov-report/src/nodes/AssemblyAssignment.js.html +0 -130
  58. package/coverage/lcov-report/src/nodes/AssemblyBlock.js.html +0 -160
  59. package/coverage/lcov-report/src/nodes/AssemblyCall.js.html +0 -133
  60. package/coverage/lcov-report/src/nodes/AssemblyCase.js.html +0 -112
  61. package/coverage/lcov-report/src/nodes/AssemblyFor.js.html +0 -139
  62. package/coverage/lcov-report/src/nodes/AssemblyFunctionDefinition.js.html +0 -184
  63. package/coverage/lcov-report/src/nodes/AssemblyIf.js.html +0 -115
  64. package/coverage/lcov-report/src/nodes/AssemblyLocalDefinition.js.html +0 -160
  65. package/coverage/lcov-report/src/nodes/AssemblyMemberAccess.js.html +0 -112
  66. package/coverage/lcov-report/src/nodes/AssemblyStackAssignment.js.html +0 -112
  67. package/coverage/lcov-report/src/nodes/AssemblySwitch.js.html +0 -133
  68. package/coverage/lcov-report/src/nodes/BinaryOperation.js.html +0 -127
  69. package/coverage/lcov-report/src/nodes/Block.js.html +0 -166
  70. package/coverage/lcov-report/src/nodes/BooleanLiteral.js.html +0 -100
  71. package/coverage/lcov-report/src/nodes/Break.js.html +0 -100
  72. package/coverage/lcov-report/src/nodes/BreakStatement.js.html +0 -100
  73. package/coverage/lcov-report/src/nodes/CatchClause.js.html +0 -148
  74. package/coverage/lcov-report/src/nodes/Conditional.js.html +0 -151
  75. package/coverage/lcov-report/src/nodes/Continue.js.html +0 -100
  76. package/coverage/lcov-report/src/nodes/ContinueStatement.js.html +0 -100
  77. package/coverage/lcov-report/src/nodes/ContractDefinition.js.html +0 -226
  78. package/coverage/lcov-report/src/nodes/CustomErrorDefinition.js.html +0 -139
  79. package/coverage/lcov-report/src/nodes/DecimalNumber.js.html +0 -100
  80. package/coverage/lcov-report/src/nodes/DoWhileStatement.js.html +0 -157
  81. package/coverage/lcov-report/src/nodes/ElementaryTypeName.js.html +0 -115
  82. package/coverage/lcov-report/src/nodes/EmitStatement.js.html +0 -100
  83. package/coverage/lcov-report/src/nodes/EnumDefinition.js.html +0 -151
  84. package/coverage/lcov-report/src/nodes/EnumValue.js.html +0 -100
  85. package/coverage/lcov-report/src/nodes/EventDefinition.js.html +0 -145
  86. package/coverage/lcov-report/src/nodes/ExpressionStatement.js.html +0 -181
  87. package/coverage/lcov-report/src/nodes/FileLevelConstant.js.html +0 -121
  88. package/coverage/lcov-report/src/nodes/ForStatement.js.html +0 -223
  89. package/coverage/lcov-report/src/nodes/FunctionCall.js.html +0 -280
  90. package/coverage/lcov-report/src/nodes/FunctionDefinition.js.html +0 -457
  91. package/coverage/lcov-report/src/nodes/FunctionTypeName.js.html +0 -217
  92. package/coverage/lcov-report/src/nodes/HexLiteral.js.html +0 -133
  93. package/coverage/lcov-report/src/nodes/HexNumber.js.html +0 -100
  94. package/coverage/lcov-report/src/nodes/Identifier.js.html +0 -100
  95. package/coverage/lcov-report/src/nodes/IfStatement.js.html +0 -265
  96. package/coverage/lcov-report/src/nodes/ImportDirective.js.html +0 -247
  97. package/coverage/lcov-report/src/nodes/IndexAccess.js.html +0 -199
  98. package/coverage/lcov-report/src/nodes/IndexRangeAccess.js.html +0 -121
  99. package/coverage/lcov-report/src/nodes/InheritanceSpecifier.js.html +0 -130
  100. package/coverage/lcov-report/src/nodes/InlineAssemblyStatement.js.html +0 -148
  101. package/coverage/lcov-report/src/nodes/LabelDefinition.js.html +0 -118
  102. package/coverage/lcov-report/src/nodes/Mapping.js.html +0 -118
  103. package/coverage/lcov-report/src/nodes/MemberAccess.js.html +0 -466
  104. package/coverage/lcov-report/src/nodes/ModifierDefinition.js.html +0 -259
  105. package/coverage/lcov-report/src/nodes/ModifierInvocation.js.html +0 -190
  106. package/coverage/lcov-report/src/nodes/NameValueExpression.js.html +0 -115
  107. package/coverage/lcov-report/src/nodes/NameValueList.js.html +0 -148
  108. package/coverage/lcov-report/src/nodes/NewExpression.js.html +0 -100
  109. package/coverage/lcov-report/src/nodes/NumberLiteral.js.html +0 -109
  110. package/coverage/lcov-report/src/nodes/PragmaDirective.js.html +0 -100
  111. package/coverage/lcov-report/src/nodes/ReturnStatement.js.html +0 -157
  112. package/coverage/lcov-report/src/nodes/RevertStatement.js.html +0 -100
  113. package/coverage/lcov-report/src/nodes/SourceUnit.js.html +0 -133
  114. package/coverage/lcov-report/src/nodes/StateVariableDeclaration.js.html +0 -166
  115. package/coverage/lcov-report/src/nodes/StringLiteral.js.html +0 -148
  116. package/coverage/lcov-report/src/nodes/StructDefinition.js.html +0 -154
  117. package/coverage/lcov-report/src/nodes/ThrowStatement.js.html +0 -100
  118. package/coverage/lcov-report/src/nodes/TryStatement.js.html +0 -214
  119. package/coverage/lcov-report/src/nodes/TupleExpression.js.html +0 -160
  120. package/coverage/lcov-report/src/nodes/TypeDefinition.js.html +0 -100
  121. package/coverage/lcov-report/src/nodes/UnaryOperation.js.html +0 -121
  122. package/coverage/lcov-report/src/nodes/UncheckedStatement.js.html +0 -118
  123. package/coverage/lcov-report/src/nodes/UserDefinedTypeName.js.html +0 -100
  124. package/coverage/lcov-report/src/nodes/UsingForDeclaration.js.html +0 -166
  125. package/coverage/lcov-report/src/nodes/VariableDeclaration.js.html +0 -253
  126. package/coverage/lcov-report/src/nodes/VariableDeclarationStatement.js.html +0 -217
  127. package/coverage/lcov-report/src/nodes/WhileStatement.js.html +0 -154
  128. package/coverage/lcov-report/src/nodes/index.html +0 -1256
  129. package/coverage/lcov-report/src/nodes/index.js.html +0 -319
  130. package/coverage/lcov-report/src/nodes/print-comments.js.html +0 -166
  131. package/coverage/lcov-report/src/nodes/print-preserving-empty-lines.js.html +0 -244
  132. package/coverage/lcov-report/src/nodes/print-separated-item.js.html +0 -148
  133. package/coverage/lcov-report/src/nodes/print-separated-list.js.html +0 -154
  134. package/coverage/lcov-report/src/options.js.html +0 -238
  135. package/coverage/lcov-report/src/parser.js.html +0 -643
  136. package/coverage/lcov-report/src/printer.js.html +0 -214
  137. package/coverage/lcov.info +0 -2672
  138. package/jest.config.js +0 -25
  139. package/src/prettier-comments/common/util.js +0 -684
package/jest.config.js DELETED
@@ -1,25 +0,0 @@
1
- const TEST_STANDALONE = Boolean(process.env.TEST_STANDALONE);
2
-
3
- module.exports = {
4
- moduleNameMapper: {
5
- '^prettier$': TEST_STANDALONE
6
- ? '<rootDir>/node_modules/prettier/standalone'
7
- : '<rootDir>/node_modules/prettier'
8
- },
9
- runner: 'jest-light-runner',
10
- setupFiles: ['<rootDir>/tests/config/setup.js'],
11
- snapshotSerializers: [
12
- 'jest-snapshot-serializer-raw',
13
- 'jest-snapshot-serializer-ansi'
14
- ],
15
- testEnvironment: 'node',
16
- testMatch: [
17
- '<rootDir>/tests/format/**/jsfmt.spec.js',
18
- '<rootDir>/tests/unit/**/*.js'
19
- ],
20
- transform: {},
21
- watchPlugins: [
22
- 'jest-watch-typeahead/filename',
23
- 'jest-watch-typeahead/testname'
24
- ]
25
- };
@@ -1,684 +0,0 @@
1
- "use strict";
2
-
3
- const stringWidth = require("string-width");
4
- const emojiRegex = require("emoji-regex")();
5
- const escapeStringRegexp = require("escape-string-regexp");
6
-
7
- // eslint-disable-next-line no-control-regex
8
- const notAsciiRegex = /[^\x20-\x7F]/;
9
-
10
- function isExportDeclaration(node) {
11
- if (node) {
12
- switch (node.type) {
13
- case "ExportDefaultDeclaration":
14
- case "ExportDefaultSpecifier":
15
- case "DeclareExportDeclaration":
16
- case "ExportNamedDeclaration":
17
- case "ExportAllDeclaration":
18
- return true;
19
- }
20
- }
21
-
22
- return false;
23
- }
24
-
25
- function getParentExportDeclaration(path) {
26
- const parentNode = path.getParentNode();
27
- if (path.getName() === "declaration" && isExportDeclaration(parentNode)) {
28
- return parentNode;
29
- }
30
-
31
- return null;
32
- }
33
-
34
- function getPenultimate(arr) {
35
- if (arr.length > 1) {
36
- return arr[arr.length - 2];
37
- }
38
- return null;
39
- }
40
-
41
- function getLast(arr) {
42
- if (arr.length > 0) {
43
- return arr[arr.length - 1];
44
- }
45
- return null;
46
- }
47
-
48
- function skip(chars) {
49
- return (text, index, opts) => {
50
- const backwards = opts && opts.backwards;
51
-
52
- // Allow `skip` functions to be threaded together without having
53
- // to check for failures (did someone say monads?).
54
- if (index === false) {
55
- return false;
56
- }
57
-
58
- const length = text.length;
59
- let cursor = index;
60
- while (cursor >= 0 && cursor < length) {
61
- const c = text.charAt(cursor);
62
- if (chars instanceof RegExp) {
63
- if (!chars.test(c)) {
64
- return cursor;
65
- }
66
- } else if (chars.indexOf(c) === -1) {
67
- return cursor;
68
- }
69
-
70
- backwards ? cursor-- : cursor++;
71
- }
72
-
73
- if (cursor === -1 || cursor === length) {
74
- // If we reached the beginning or end of the file, return the
75
- // out-of-bounds cursor. It's up to the caller to handle this
76
- // correctly. We don't want to indicate `false` though if it
77
- // actually skipped valid characters.
78
- return cursor;
79
- }
80
- return false;
81
- };
82
- }
83
-
84
- const skipWhitespace = skip(/\s/);
85
- const skipSpaces = skip(" \t");
86
- const skipToLineEnd = skip(",; \t");
87
- const skipEverythingButNewLine = skip(/[^\r\n]/);
88
-
89
- function skipInlineComment(text, index) {
90
- if (index === false) {
91
- return false;
92
- }
93
-
94
- if (text.charAt(index) === "/" && text.charAt(index + 1) === "*") {
95
- for (let i = index + 2; i < text.length; ++i) {
96
- if (text.charAt(i) === "*" && text.charAt(i + 1) === "/") {
97
- return i + 2;
98
- }
99
- }
100
- }
101
- return index;
102
- }
103
-
104
- function skipTrailingComment(text, index) {
105
- if (index === false) {
106
- return false;
107
- }
108
-
109
- if (text.charAt(index) === "/" && text.charAt(index + 1) === "/") {
110
- return skipEverythingButNewLine(text, index);
111
- }
112
- return index;
113
- }
114
-
115
- // This one doesn't use the above helper function because it wants to
116
- // test \r\n in order and `skip` doesn't support ordering and we only
117
- // want to skip one newline. It's simple to implement.
118
- function skipNewline(text, index, opts) {
119
- const backwards = opts && opts.backwards;
120
- if (index === false) {
121
- return false;
122
- }
123
-
124
- const atIndex = text.charAt(index);
125
- if (backwards) {
126
- if (text.charAt(index - 1) === "\r" && atIndex === "\n") {
127
- return index - 2;
128
- }
129
- if (
130
- atIndex === "\n" ||
131
- atIndex === "\r" ||
132
- atIndex === "\u2028" ||
133
- atIndex === "\u2029"
134
- ) {
135
- return index - 1;
136
- }
137
- } else {
138
- if (atIndex === "\r" && text.charAt(index + 1) === "\n") {
139
- return index + 2;
140
- }
141
- if (
142
- atIndex === "\n" ||
143
- atIndex === "\r" ||
144
- atIndex === "\u2028" ||
145
- atIndex === "\u2029"
146
- ) {
147
- return index + 1;
148
- }
149
- }
150
-
151
- return index;
152
- }
153
-
154
- function hasNewline(text, index, opts) {
155
- opts = opts || {};
156
- const idx = skipSpaces(text, opts.backwards ? index - 1 : index, opts);
157
- const idx2 = skipNewline(text, idx, opts);
158
- return idx !== idx2;
159
- }
160
-
161
- function hasNewlineInRange(text, start, end) {
162
- for (let i = start; i < end; ++i) {
163
- if (text.charAt(i) === "\n") {
164
- return true;
165
- }
166
- }
167
- return false;
168
- }
169
-
170
- // Note: this function doesn't ignore leading comments unlike isNextLineEmpty
171
- function isPreviousLineEmpty(text, node, locStart) {
172
- let idx = locStart(node) - 1;
173
- idx = skipSpaces(text, idx, { backwards: true });
174
- idx = skipNewline(text, idx, { backwards: true });
175
- idx = skipSpaces(text, idx, { backwards: true });
176
- const idx2 = skipNewline(text, idx, { backwards: true });
177
- return idx !== idx2;
178
- }
179
-
180
- function isNextLineEmptyAfterIndex(text, index) {
181
- let oldIdx = null;
182
- let idx = index;
183
- while (idx !== oldIdx) {
184
- // We need to skip all the potential trailing inline comments
185
- oldIdx = idx;
186
- idx = skipToLineEnd(text, idx);
187
- idx = skipInlineComment(text, idx);
188
- idx = skipSpaces(text, idx);
189
- }
190
- idx = skipTrailingComment(text, idx);
191
- idx = skipNewline(text, idx);
192
- return hasNewline(text, idx);
193
- }
194
-
195
- function isNextLineEmpty(text, node, locEnd) {
196
- return isNextLineEmptyAfterIndex(text, locEnd(node));
197
- }
198
-
199
- function getNextNonSpaceNonCommentCharacterIndex(text, node, locEnd) {
200
- let oldIdx = null;
201
- let idx = locEnd(node);
202
- while (idx !== oldIdx) {
203
- oldIdx = idx;
204
- idx = skipSpaces(text, idx);
205
- idx = skipInlineComment(text, idx);
206
- idx = skipTrailingComment(text, idx);
207
- idx = skipNewline(text, idx);
208
- }
209
- return idx;
210
- }
211
-
212
- function getNextNonSpaceNonCommentCharacter(text, node, locEnd) {
213
- return text.charAt(
214
- getNextNonSpaceNonCommentCharacterIndex(text, node, locEnd)
215
- );
216
- }
217
-
218
- function hasSpaces(text, index, opts) {
219
- opts = opts || {};
220
- const idx = skipSpaces(text, opts.backwards ? index - 1 : index, opts);
221
- return idx !== index;
222
- }
223
-
224
- function setLocStart(node, index) {
225
- if (node.range) {
226
- node.range[0] = index;
227
- } else {
228
- node.start = index;
229
- }
230
- }
231
-
232
- function setLocEnd(node, index) {
233
- if (node.range) {
234
- node.range[1] = index;
235
- } else {
236
- node.end = index;
237
- }
238
- }
239
-
240
- const PRECEDENCE = {};
241
- [
242
- ["|>"],
243
- ["||", "??"],
244
- ["&&"],
245
- ["|"],
246
- ["^"],
247
- ["&"],
248
- ["==", "===", "!=", "!=="],
249
- ["<", ">", "<=", ">=", "in", "instanceof"],
250
- [">>", "<<", ">>>"],
251
- ["+", "-"],
252
- ["*", "/", "%"],
253
- ["**"]
254
- ].forEach((tier, i) => {
255
- tier.forEach(op => {
256
- PRECEDENCE[op] = i;
257
- });
258
- });
259
-
260
- function getPrecedence(op) {
261
- return PRECEDENCE[op];
262
- }
263
-
264
- const equalityOperators = {
265
- "==": true,
266
- "!=": true,
267
- "===": true,
268
- "!==": true
269
- };
270
- const multiplicativeOperators = {
271
- "*": true,
272
- "/": true,
273
- "%": true
274
- };
275
- const bitshiftOperators = {
276
- ">>": true,
277
- ">>>": true,
278
- "<<": true
279
- };
280
-
281
- function shouldFlatten(parentOp, nodeOp) {
282
- if (getPrecedence(nodeOp) !== getPrecedence(parentOp)) {
283
- return false;
284
- }
285
-
286
- // ** is right-associative
287
- // x ** y ** z --> x ** (y ** z)
288
- if (parentOp === "**") {
289
- return false;
290
- }
291
-
292
- // x == y == z --> (x == y) == z
293
- if (equalityOperators[parentOp] && equalityOperators[nodeOp]) {
294
- return false;
295
- }
296
-
297
- // x * y % z --> (x * y) % z
298
- if (
299
- (nodeOp === "%" && multiplicativeOperators[parentOp]) ||
300
- (parentOp === "%" && multiplicativeOperators[nodeOp])
301
- ) {
302
- return false;
303
- }
304
-
305
- // x * y / z --> (x * y) / z
306
- // x / y * z --> (x / y) * z
307
- if (
308
- nodeOp !== parentOp &&
309
- multiplicativeOperators[nodeOp] &&
310
- multiplicativeOperators[parentOp]
311
- ) {
312
- return false;
313
- }
314
-
315
- // x << y << z --> (x << y) << z
316
- if (bitshiftOperators[parentOp] && bitshiftOperators[nodeOp]) {
317
- return false;
318
- }
319
-
320
- return true;
321
- }
322
-
323
- function isBitwiseOperator(operator) {
324
- return (
325
- !!bitshiftOperators[operator] ||
326
- operator === "|" ||
327
- operator === "^" ||
328
- operator === "&"
329
- );
330
- }
331
-
332
- // Tests if an expression starts with `{`, or (if forbidFunctionClassAndDoExpr
333
- // holds) `function`, `class`, or `do {}`. Will be overzealous if there's
334
- // already necessary grouping parentheses.
335
- function startsWithNoLookaheadToken(node, forbidFunctionClassAndDoExpr) {
336
- node = getLeftMost(node);
337
- switch (node.type) {
338
- case "FunctionExpression":
339
- case "ClassExpression":
340
- case "DoExpression":
341
- return forbidFunctionClassAndDoExpr;
342
- case "ObjectExpression":
343
- return true;
344
- case "MemberExpression":
345
- return startsWithNoLookaheadToken(
346
- node.object,
347
- forbidFunctionClassAndDoExpr
348
- );
349
- case "TaggedTemplateExpression":
350
- if (node.tag.type === "FunctionExpression") {
351
- // IIFEs are always already parenthesized
352
- return false;
353
- }
354
- return startsWithNoLookaheadToken(node.tag, forbidFunctionClassAndDoExpr);
355
- case "CallExpression":
356
- if (node.callee.type === "FunctionExpression") {
357
- // IIFEs are always already parenthesized
358
- return false;
359
- }
360
- return startsWithNoLookaheadToken(
361
- node.callee,
362
- forbidFunctionClassAndDoExpr
363
- );
364
- case "ConditionalExpression":
365
- return startsWithNoLookaheadToken(
366
- node.test,
367
- forbidFunctionClassAndDoExpr
368
- );
369
- case "UpdateExpression":
370
- return (
371
- !node.prefix &&
372
- startsWithNoLookaheadToken(node.argument, forbidFunctionClassAndDoExpr)
373
- );
374
- case "BindExpression":
375
- return (
376
- node.object &&
377
- startsWithNoLookaheadToken(node.object, forbidFunctionClassAndDoExpr)
378
- );
379
- case "SequenceExpression":
380
- return startsWithNoLookaheadToken(
381
- node.expressions[0],
382
- forbidFunctionClassAndDoExpr
383
- );
384
- case "TSAsExpression":
385
- return startsWithNoLookaheadToken(
386
- node.expression,
387
- forbidFunctionClassAndDoExpr
388
- );
389
- default:
390
- return false;
391
- }
392
- }
393
-
394
- function getLeftMost(node) {
395
- if (node.left) {
396
- return getLeftMost(node.left);
397
- }
398
- return node;
399
- }
400
-
401
- function getAlignmentSize(value, tabWidth, startIndex) {
402
- startIndex = startIndex || 0;
403
-
404
- let size = 0;
405
- for (let i = startIndex; i < value.length; ++i) {
406
- if (value[i] === "\t") {
407
- // Tabs behave in a way that they are aligned to the nearest
408
- // multiple of tabWidth:
409
- // 0 -> 4, 1 -> 4, 2 -> 4, 3 -> 4
410
- // 4 -> 8, 5 -> 8, 6 -> 8, 7 -> 8 ...
411
- size = size + tabWidth - (size % tabWidth);
412
- } else {
413
- size++;
414
- }
415
- }
416
-
417
- return size;
418
- }
419
-
420
- function getIndentSize(value, tabWidth) {
421
- const lastNewlineIndex = value.lastIndexOf("\n");
422
- if (lastNewlineIndex === -1) {
423
- return 0;
424
- }
425
-
426
- return getAlignmentSize(
427
- // All the leading whitespaces
428
- value.slice(lastNewlineIndex + 1).match(/^[ \t]*/)[0],
429
- tabWidth
430
- );
431
- }
432
-
433
- function printString(rawContent, options) {
434
- const double = { quote: '"', regex: /"/g };
435
- const single = { quote: "'", regex: /'/g };
436
-
437
- const preferred = options.singleQuote ? single : double;
438
- const alternate = preferred === single ? double : single;
439
-
440
- let shouldUseAlternateQuote = false;
441
- let canChangeDirectiveQuotes = false;
442
-
443
- // If `rawContent` contains at least one of the quote preferred for enclosing
444
- // the string, we might want to enclose with the alternate quote instead, to
445
- // minimize the number of escaped quotes.
446
- // Also check for the alternate quote, to determine if we're allowed to swap
447
- // the quotes on a DirectiveLiteral.
448
- if (
449
- rawContent.includes(preferred.quote) ||
450
- rawContent.includes(alternate.quote)
451
- ) {
452
- const numPreferredQuotes = (rawContent.match(preferred.regex) || []).length;
453
- const numAlternateQuotes = (rawContent.match(alternate.regex) || []).length;
454
-
455
- shouldUseAlternateQuote = numPreferredQuotes > numAlternateQuotes;
456
- } else {
457
- canChangeDirectiveQuotes = true;
458
- }
459
-
460
- const enclosingQuote =
461
- options.parser === "json"
462
- ? double.quote
463
- : shouldUseAlternateQuote
464
- ? alternate.quote
465
- : preferred.quote;
466
-
467
- // It might sound unnecessary to use `makeString` even if the string already
468
- // is enclosed with `enclosingQuote`, but it isn't. The string could contain
469
- // unnecessary escapes (such as in `"\'"`). Always using `makeString` makes
470
- // sure that we consistently output the minimum amount of escaped quotes.
471
- return makeString(
472
- rawContent,
473
- enclosingQuote,
474
- !(
475
- options.parser === "css" ||
476
- options.parser === "less" ||
477
- options.parser === "scss"
478
- )
479
- );
480
- }
481
-
482
- function makeString(rawContent, enclosingQuote, unescapeUnnecessaryEscapes) {
483
- const otherQuote = enclosingQuote === '"' ? "'" : '"';
484
-
485
- // Matches _any_ escape and unescaped quotes (both single and double).
486
- const regex = /\\([\s\S])|(['"])/g;
487
-
488
- // Escape and unescape single and double quotes as needed to be able to
489
- // enclose `rawContent` with `enclosingQuote`.
490
- const newContent = rawContent.replace(regex, (match, escaped, quote) => {
491
- // If we matched an escape, and the escaped character is a quote of the
492
- // other type than we intend to enclose the string with, there's no need for
493
- // it to be escaped, so return it _without_ the backslash.
494
- if (escaped === otherQuote) {
495
- return escaped;
496
- }
497
-
498
- // If we matched an unescaped quote and it is of the _same_ type as we
499
- // intend to enclose the string with, it must be escaped, so return it with
500
- // a backslash.
501
- if (quote === enclosingQuote) {
502
- return "\\" + quote;
503
- }
504
-
505
- if (quote) {
506
- return quote;
507
- }
508
-
509
- // Unescape any unnecessarily escaped character.
510
- // Adapted from https://github.com/eslint/eslint/blob/de0b4ad7bd820ade41b1f606008bea68683dc11a/lib/rules/no-useless-escape.js#L27
511
- return unescapeUnnecessaryEscapes &&
512
- /^[^\\nrvtbfux\r\n\u2028\u2029"'0-7]$/.test(escaped)
513
- ? escaped
514
- : "\\" + escaped;
515
- });
516
-
517
- return enclosingQuote + newContent + enclosingQuote;
518
- }
519
-
520
- function printNumber(rawNumber) {
521
- return (
522
- rawNumber
523
- .toLowerCase()
524
- // Remove unnecessary plus and zeroes from scientific notation.
525
- .replace(/^([+-]?[\d.]+e)(?:\+|(-))?0*(\d)/, "$1$2$3")
526
- // Remove unnecessary scientific notation (1e0).
527
- .replace(/^([+-]?[\d.]+)e[+-]?0+$/, "$1")
528
- // Make sure numbers always start with a digit.
529
- .replace(/^([+-])?\./, "$10.")
530
- // Remove extraneous trailing decimal zeroes.
531
- .replace(/(\.\d+?)0+(?=e|$)/, "$1")
532
- // Remove trailing dot.
533
- .replace(/\.(?=e|$)/, "")
534
- );
535
- }
536
-
537
- function getMaxContinuousCount(str, target) {
538
- const results = str.match(
539
- new RegExp(`(${escapeStringRegexp(target)})+`, "g")
540
- );
541
-
542
- if (results === null) {
543
- return 0;
544
- }
545
-
546
- return results.reduce(
547
- (maxCount, result) => Math.max(maxCount, result.length / target.length),
548
- 0
549
- );
550
- }
551
-
552
- function getStringWidth(text) {
553
- if (!text) {
554
- return 0;
555
- }
556
-
557
- // shortcut to avoid needless string `RegExp`s, replacements, and allocations within `string-width`
558
- if (!notAsciiRegex.test(text)) {
559
- return text.length;
560
- }
561
-
562
- // emojis are considered 2-char width for consistency
563
- // see https://github.com/sindresorhus/string-width/issues/11
564
- // for the reason why not implemented in `string-width`
565
- return stringWidth(text.replace(emojiRegex, " "));
566
- }
567
-
568
- function hasIgnoreComment(path) {
569
- const node = path.getValue();
570
- return hasNodeIgnoreComment(node);
571
- }
572
-
573
- function hasNodeIgnoreComment(node) {
574
- return (
575
- node &&
576
- node.comments &&
577
- node.comments.length > 0 &&
578
- node.comments.some(comment => comment.value.trim() === "prettier-ignore")
579
- );
580
- }
581
-
582
- function matchAncestorTypes(path, types, index) {
583
- index = index || 0;
584
- types = types.slice();
585
- while (types.length) {
586
- const parent = path.getParentNode(index);
587
- const type = types.shift();
588
- if (!parent || parent.type !== type) {
589
- return false;
590
- }
591
- index++;
592
- }
593
- return true;
594
- }
595
-
596
- function addCommentHelper(node, comment) {
597
- const comments = node.comments || (node.comments = []);
598
- comments.push(comment);
599
- comment.printed = false;
600
-
601
- // For some reason, TypeScript parses `// x` inside of JSXText as a comment
602
- // We already "print" it via the raw text, we don't need to re-print it as a
603
- // comment
604
- if (node.type === "JSXText") {
605
- comment.printed = true;
606
- }
607
- }
608
-
609
- function addLeadingComment(node, comment) {
610
- comment.leading = true;
611
- comment.trailing = false;
612
- addCommentHelper(node, comment);
613
- }
614
-
615
- function addDanglingComment(node, comment) {
616
- comment.leading = false;
617
- comment.trailing = false;
618
- addCommentHelper(node, comment);
619
- }
620
-
621
- function addTrailingComment(node, comment) {
622
- comment.leading = false;
623
- comment.trailing = true;
624
- addCommentHelper(node, comment);
625
- }
626
-
627
- function isWithinParentArrayProperty(path, propertyName) {
628
- const node = path.getValue();
629
- const parent = path.getParentNode();
630
-
631
- if (parent == null) {
632
- return false;
633
- }
634
-
635
- if (!Array.isArray(parent[propertyName])) {
636
- return false;
637
- }
638
-
639
- const key = path.getName();
640
- return parent[propertyName][key] === node;
641
- }
642
-
643
- module.exports = {
644
- getStringWidth,
645
- getMaxContinuousCount,
646
- getPrecedence,
647
- shouldFlatten,
648
- isBitwiseOperator,
649
- isExportDeclaration,
650
- getParentExportDeclaration,
651
- getPenultimate,
652
- getLast,
653
- getNextNonSpaceNonCommentCharacterIndex,
654
- getNextNonSpaceNonCommentCharacter,
655
- skip,
656
- skipWhitespace,
657
- skipSpaces,
658
- skipToLineEnd,
659
- skipEverythingButNewLine,
660
- skipInlineComment,
661
- skipTrailingComment,
662
- skipNewline,
663
- isNextLineEmptyAfterIndex,
664
- isNextLineEmpty,
665
- isPreviousLineEmpty,
666
- hasNewline,
667
- hasNewlineInRange,
668
- hasSpaces,
669
- setLocStart,
670
- setLocEnd,
671
- startsWithNoLookaheadToken,
672
- getAlignmentSize,
673
- getIndentSize,
674
- printString,
675
- printNumber,
676
- hasIgnoreComment,
677
- hasNodeIgnoreComment,
678
- makeString,
679
- matchAncestorTypes,
680
- addLeadingComment,
681
- addDanglingComment,
682
- addTrailingComment,
683
- isWithinParentArrayProperty
684
- };