flow-parser 0.318.0 → 0.320.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/flow_parser.js +9498 -9488
- package/oxidized/FlowParser.js +249 -0
- package/oxidized/FlowParser.js.flow +457 -0
- package/oxidized/FlowParserDeserializer.js +209 -0
- package/oxidized/FlowParserDeserializer.js.flow +323 -0
- package/oxidized/FlowParserNodeDeserializers.js +1781 -0
- package/oxidized/FlowParserNodeDeserializers.js.flow +18 -0
- package/oxidized/FlowParserWASM.js +1 -0
- package/oxidized/FlowParserWASM.js.flow +119 -0
- package/oxidized/HermesAST.js.flow +57 -0
- package/oxidized/HermesParserDecodeUTF8String.js +45 -0
- package/oxidized/HermesParserDecodeUTF8String.js.flow +65 -0
- package/oxidized/ParserOptions.js +8 -0
- package/oxidized/ParserOptions.js.flow +55 -0
- package/oxidized/babel/TransformESTreeToBabel.js +1063 -0
- package/oxidized/babel/TransformESTreeToBabel.js.flow +1357 -0
- package/oxidized/estree/StripFlowTypes.js +158 -0
- package/oxidized/estree/StripFlowTypes.js.flow +158 -0
- package/oxidized/estree/StripFlowTypesForBabel.js +191 -0
- package/oxidized/estree/StripFlowTypesForBabel.js.flow +238 -0
- package/oxidized/estree/TransformComponentSyntax.js +764 -0
- package/oxidized/estree/TransformComponentSyntax.js.flow +882 -0
- package/oxidized/estree/TransformEnumSyntax.js +92 -0
- package/oxidized/estree/TransformEnumSyntax.js.flow +125 -0
- package/oxidized/estree/TransformMatchSyntax.js +990 -0
- package/oxidized/estree/TransformMatchSyntax.js.flow +991 -0
- package/oxidized/estree/TransformRecordSyntax.js +283 -0
- package/oxidized/estree/TransformRecordSyntax.js.flow +310 -0
- package/oxidized/generated/ESTreeVisitorKeys.js +234 -0
- package/oxidized/generated/ESTreeVisitorKeys.js.flow +22 -0
- package/oxidized/getModuleDocblock.js +87 -0
- package/oxidized/getModuleDocblock.js.flow +119 -0
- package/oxidized/index.js +220 -0
- package/oxidized/index.js.flow +183 -0
- package/oxidized/oxidized-src/FlowParser.js +249 -0
- package/oxidized/oxidized-src/FlowParserDeserializer.js +209 -0
- package/oxidized/oxidized-src/FlowParserNodeDeserializers.js +1781 -0
- package/oxidized/oxidized-src/HermesParserDecodeUTF8String.js +45 -0
- package/oxidized/oxidized-src/ParserOptions.js +8 -0
- package/oxidized/oxidized-src/babel/TransformESTreeToBabel.js +1063 -0
- package/oxidized/oxidized-src/estree/StripFlowTypes.js +158 -0
- package/oxidized/oxidized-src/estree/StripFlowTypesForBabel.js +191 -0
- package/oxidized/oxidized-src/estree/TransformComponentSyntax.js +764 -0
- package/oxidized/oxidized-src/estree/TransformEnumSyntax.js +92 -0
- package/oxidized/oxidized-src/estree/TransformMatchSyntax.js +990 -0
- package/oxidized/oxidized-src/estree/TransformRecordSyntax.js +283 -0
- package/oxidized/oxidized-src/generated/ESTreeVisitorKeys.js +234 -0
- package/oxidized/oxidized-src/getModuleDocblock.js +87 -0
- package/oxidized/oxidized-src/index.js +220 -0
- package/oxidized/oxidized-src/transform/SimpleTransform.js +95 -0
- package/oxidized/oxidized-src/transform/astArrayMutationHelpers.js +52 -0
- package/oxidized/oxidized-src/transform/astNodeMutationHelpers.js +142 -0
- package/oxidized/oxidized-src/transform/print/comments/comments.js +245 -0
- package/oxidized/oxidized-src/transform/print/comments/prettier/common/util.js +235 -0
- package/oxidized/oxidized-src/transform/print/comments/prettier/language-js/comments.js +663 -0
- package/oxidized/oxidized-src/transform/print/comments/prettier/language-js/loc.js +30 -0
- package/oxidized/oxidized-src/transform/print/comments/prettier/language-js/printer-estree.js +23 -0
- package/oxidized/oxidized-src/transform/print/comments/prettier/language-js/utils.js +89 -0
- package/oxidized/oxidized-src/transform/print/comments/prettier/main/comments.js +374 -0
- package/oxidized/oxidized-src/transform/print/comments/prettier/utils/get-last.js +5 -0
- package/oxidized/oxidized-src/transform/print/detachedNodeTypes.js +1 -0
- package/oxidized/oxidized-src/transform/print/print.js +68 -0
- package/oxidized/oxidized-src/traverse/SimpleTraverser.js +90 -0
- package/oxidized/oxidized-src/traverse/getVisitorKeys.js +29 -0
- package/oxidized/oxidized-src/utils/Builders.js +181 -0
- package/oxidized/oxidized-src/utils/GenID.js +37 -0
- package/oxidized/oxidized-src/utils/createSyntaxError.js +15 -0
- package/oxidized/oxidized-src/utils/isReservedWord.js +53 -0
- package/oxidized/oxidized-src/utils/mutateESTreeASTForPrettier.js +85 -0
- package/oxidized/transform/SimpleTransform.js +95 -0
- package/oxidized/transform/SimpleTransform.js.flow +168 -0
- package/oxidized/transform/astArrayMutationHelpers.js +52 -0
- package/oxidized/transform/astArrayMutationHelpers.js.flow +71 -0
- package/oxidized/transform/astNodeMutationHelpers.js +142 -0
- package/oxidized/transform/astNodeMutationHelpers.js.flow +252 -0
- package/oxidized/transform/print/comments/comments.js +245 -0
- package/oxidized/transform/print/comments/comments.js.flow +330 -0
- package/oxidized/transform/print/comments/prettier/common/util.js +235 -0
- package/oxidized/transform/print/comments/prettier/language-js/comments.js +663 -0
- package/oxidized/transform/print/comments/prettier/language-js/loc.js +30 -0
- package/oxidized/transform/print/comments/prettier/language-js/printer-estree.js +23 -0
- package/oxidized/transform/print/comments/prettier/language-js/utils.js +89 -0
- package/oxidized/transform/print/comments/prettier/main/comments.js +374 -0
- package/oxidized/transform/print/comments/prettier/utils/get-last.js +5 -0
- package/oxidized/transform/print/detachedNodeTypes.js +1 -0
- package/oxidized/transform/print/detachedNodeTypes.js.flow +24 -0
- package/oxidized/transform/print/print.js +68 -0
- package/oxidized/transform/print/print.js.flow +87 -0
- package/oxidized/traverse/SimpleTraverser.js +90 -0
- package/oxidized/traverse/SimpleTraverser.js.flow +133 -0
- package/oxidized/traverse/getVisitorKeys.js +29 -0
- package/oxidized/traverse/getVisitorKeys.js.flow +36 -0
- package/oxidized/utils/Builders.js +181 -0
- package/oxidized/utils/Builders.js.flow +218 -0
- package/oxidized/utils/GenID.js +37 -0
- package/oxidized/utils/GenID.js.flow +39 -0
- package/oxidized/utils/createSyntaxError.js +15 -0
- package/oxidized/utils/createSyntaxError.js.flow +24 -0
- package/oxidized/utils/isReservedWord.js +53 -0
- package/oxidized/utils/isReservedWord.js.flow +57 -0
- package/oxidized/utils/mutateESTreeASTForPrettier.js +85 -0
- package/oxidized/utils/mutateESTreeASTForPrettier.js.flow +130 -0
- package/package.json +6 -4
|
@@ -0,0 +1,663 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
getLast,
|
|
5
|
+
hasNewline,
|
|
6
|
+
addLeadingComment,
|
|
7
|
+
getNextNonSpaceNonCommentCharacterIndexWithStartIndex,
|
|
8
|
+
getNextNonSpaceNonCommentCharacterIndex,
|
|
9
|
+
hasNewlineInRange,
|
|
10
|
+
addTrailingComment,
|
|
11
|
+
addDanglingComment,
|
|
12
|
+
getNextNonSpaceNonCommentCharacter,
|
|
13
|
+
isNonEmptyArray
|
|
14
|
+
} = require('../common/util.js');
|
|
15
|
+
|
|
16
|
+
const {
|
|
17
|
+
isBlockComment,
|
|
18
|
+
getFunctionParameters,
|
|
19
|
+
isPrettierIgnoreComment,
|
|
20
|
+
isCallLikeExpression,
|
|
21
|
+
getCallArguments,
|
|
22
|
+
isCallExpression,
|
|
23
|
+
isMemberExpression,
|
|
24
|
+
isObjectProperty
|
|
25
|
+
} = require('./utils.js');
|
|
26
|
+
|
|
27
|
+
const {
|
|
28
|
+
locStart,
|
|
29
|
+
locEnd
|
|
30
|
+
} = require('./loc.js');
|
|
31
|
+
|
|
32
|
+
function handleOwnLineComment(context) {
|
|
33
|
+
return [handleIgnoreComments, handleLastFunctionArgComments, handleMemberExpressionComments, handleIfStatementComments, handleWhileComments, handleTryStatementComments, handleClassComments, handleImportSpecifierComments, handleForComments, handleUnionTypeComments, handleMatchOrPatternComments, handleOnlyComments, handleImportDeclarationComments, handleAssignmentPatternComments, handleMethodNameComments, handleLabeledStatementComments].some(fn => fn(context));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function handleEndOfLineComment(context) {
|
|
37
|
+
return [handleClosureTypeCastComments, handleLastFunctionArgComments, handleConditionalExpressionComments, handleImportSpecifierComments, handleIfStatementComments, handleWhileComments, handleTryStatementComments, handleClassComments, handleLabeledStatementComments, handleCallExpressionComments, handlePropertyComments, handleOnlyComments, handleTypeAliasComments, handleVariableDeclaratorComments].some(fn => fn(context));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function handleRemainingComment(context) {
|
|
41
|
+
return [handleIgnoreComments, handleIfStatementComments, handleWhileComments, handleObjectPropertyAssignment, handleCommentInEmptyParens, handleMethodNameComments, handleOnlyComments, handleCommentAfterArrowParams, handleFunctionNameComments, handleTSMappedTypeComments, handleBreakAndContinueStatementComments, handleTSFunctionTrailingComments].some(fn => fn(context));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function addBlockStatementFirstComment(node, comment) {
|
|
45
|
+
const firstNonEmptyNode = (node.body || node.properties).find(({
|
|
46
|
+
type
|
|
47
|
+
}) => type !== 'EmptyStatement');
|
|
48
|
+
|
|
49
|
+
if (firstNonEmptyNode) {
|
|
50
|
+
addLeadingComment(firstNonEmptyNode, comment);
|
|
51
|
+
} else {
|
|
52
|
+
addDanglingComment(node, comment);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function addBlockOrNotComment(node, comment) {
|
|
57
|
+
if (node.type === 'BlockStatement') {
|
|
58
|
+
addBlockStatementFirstComment(node, comment);
|
|
59
|
+
} else {
|
|
60
|
+
addLeadingComment(node, comment);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function handleClosureTypeCastComments({
|
|
65
|
+
comment,
|
|
66
|
+
followingNode
|
|
67
|
+
}) {
|
|
68
|
+
if (followingNode && isTypeCastComment(comment)) {
|
|
69
|
+
addLeadingComment(followingNode, comment);
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function handleIfStatementComments({
|
|
77
|
+
comment,
|
|
78
|
+
precedingNode,
|
|
79
|
+
enclosingNode,
|
|
80
|
+
followingNode,
|
|
81
|
+
text
|
|
82
|
+
}) {
|
|
83
|
+
if (!enclosingNode || enclosingNode.type !== 'IfStatement' || !followingNode) {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const nextCharacter = getNextNonSpaceNonCommentCharacter(text, comment, locEnd);
|
|
88
|
+
|
|
89
|
+
if (nextCharacter === ')') {
|
|
90
|
+
addTrailingComment(precedingNode, comment);
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (precedingNode === enclosingNode.consequent && followingNode === enclosingNode.alternate) {
|
|
95
|
+
if (precedingNode.type === 'BlockStatement') {
|
|
96
|
+
addTrailingComment(precedingNode, comment);
|
|
97
|
+
} else {
|
|
98
|
+
addDanglingComment(enclosingNode, comment);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (followingNode.type === 'BlockStatement') {
|
|
105
|
+
addBlockStatementFirstComment(followingNode, comment);
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (followingNode.type === 'IfStatement') {
|
|
110
|
+
addBlockOrNotComment(followingNode.consequent, comment);
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (enclosingNode.consequent === followingNode) {
|
|
115
|
+
addLeadingComment(followingNode, comment);
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function handleWhileComments({
|
|
123
|
+
comment,
|
|
124
|
+
precedingNode,
|
|
125
|
+
enclosingNode,
|
|
126
|
+
followingNode,
|
|
127
|
+
text
|
|
128
|
+
}) {
|
|
129
|
+
if (!enclosingNode || enclosingNode.type !== 'WhileStatement' || !followingNode) {
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const nextCharacter = getNextNonSpaceNonCommentCharacter(text, comment, locEnd);
|
|
134
|
+
|
|
135
|
+
if (nextCharacter === ')') {
|
|
136
|
+
addTrailingComment(precedingNode, comment);
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (followingNode.type === 'BlockStatement') {
|
|
141
|
+
addBlockStatementFirstComment(followingNode, comment);
|
|
142
|
+
return true;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (enclosingNode.body === followingNode) {
|
|
146
|
+
addLeadingComment(followingNode, comment);
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function handleTryStatementComments({
|
|
154
|
+
comment,
|
|
155
|
+
precedingNode,
|
|
156
|
+
enclosingNode,
|
|
157
|
+
followingNode
|
|
158
|
+
}) {
|
|
159
|
+
if (!enclosingNode || enclosingNode.type !== 'TryStatement' && enclosingNode.type !== 'CatchClause' || !followingNode) {
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (enclosingNode.type === 'CatchClause' && precedingNode) {
|
|
164
|
+
addTrailingComment(precedingNode, comment);
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (followingNode.type === 'BlockStatement') {
|
|
169
|
+
addBlockStatementFirstComment(followingNode, comment);
|
|
170
|
+
return true;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (followingNode.type === 'TryStatement') {
|
|
174
|
+
addBlockOrNotComment(followingNode.finalizer, comment);
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (followingNode.type === 'CatchClause') {
|
|
179
|
+
addBlockOrNotComment(followingNode.body, comment);
|
|
180
|
+
return true;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function handleMemberExpressionComments({
|
|
187
|
+
comment,
|
|
188
|
+
enclosingNode,
|
|
189
|
+
followingNode
|
|
190
|
+
}) {
|
|
191
|
+
if (isMemberExpression(enclosingNode) && followingNode && followingNode.type === 'Identifier') {
|
|
192
|
+
addLeadingComment(enclosingNode, comment);
|
|
193
|
+
return true;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function handleConditionalExpressionComments({
|
|
200
|
+
comment,
|
|
201
|
+
precedingNode,
|
|
202
|
+
enclosingNode,
|
|
203
|
+
followingNode,
|
|
204
|
+
text
|
|
205
|
+
}) {
|
|
206
|
+
const isSameLineAsPrecedingNode = precedingNode && !hasNewlineInRange(text, locEnd(precedingNode), locStart(comment));
|
|
207
|
+
|
|
208
|
+
if ((!precedingNode || !isSameLineAsPrecedingNode) && enclosingNode && (enclosingNode.type === 'ConditionalExpression' || enclosingNode.type === 'TSConditionalType') && followingNode) {
|
|
209
|
+
addLeadingComment(followingNode, comment);
|
|
210
|
+
return true;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function handleObjectPropertyAssignment({
|
|
217
|
+
comment,
|
|
218
|
+
precedingNode,
|
|
219
|
+
enclosingNode
|
|
220
|
+
}) {
|
|
221
|
+
if (isObjectProperty(enclosingNode) && enclosingNode.shorthand && enclosingNode.key === precedingNode && enclosingNode.value.type === 'AssignmentPattern') {
|
|
222
|
+
addTrailingComment(enclosingNode.value.left, comment);
|
|
223
|
+
return true;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return false;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function handleClassComments({
|
|
230
|
+
comment,
|
|
231
|
+
precedingNode,
|
|
232
|
+
enclosingNode,
|
|
233
|
+
followingNode
|
|
234
|
+
}) {
|
|
235
|
+
if (enclosingNode && (enclosingNode.type === 'ClassDeclaration' || enclosingNode.type === 'ClassExpression' || enclosingNode.type === 'DeclareClass' || enclosingNode.type === 'DeclareInterface' || enclosingNode.type === 'InterfaceDeclaration' || enclosingNode.type === 'TSInterfaceDeclaration')) {
|
|
236
|
+
if (isNonEmptyArray(enclosingNode.decorators) && !(followingNode && followingNode.type === 'Decorator')) {
|
|
237
|
+
addTrailingComment(getLast(enclosingNode.decorators), comment);
|
|
238
|
+
return true;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
if (enclosingNode.body && followingNode === enclosingNode.body) {
|
|
242
|
+
addBlockStatementFirstComment(enclosingNode.body, comment);
|
|
243
|
+
return true;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
if (followingNode) {
|
|
247
|
+
for (const prop of ['implements', 'extends', 'mixins']) {
|
|
248
|
+
if (enclosingNode[prop] && followingNode === enclosingNode[prop][0]) {
|
|
249
|
+
if (precedingNode && (precedingNode === enclosingNode.id || precedingNode === enclosingNode.typeParameters || precedingNode === enclosingNode.superClass)) {
|
|
250
|
+
addTrailingComment(precedingNode, comment);
|
|
251
|
+
} else {
|
|
252
|
+
addDanglingComment(enclosingNode, comment, prop);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
return true;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
return false;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function handleMethodNameComments({
|
|
265
|
+
comment,
|
|
266
|
+
precedingNode,
|
|
267
|
+
enclosingNode,
|
|
268
|
+
text
|
|
269
|
+
}) {
|
|
270
|
+
if (enclosingNode && precedingNode && (enclosingNode.type === 'Property' || enclosingNode.type === 'TSDeclareMethod' || enclosingNode.type === 'TSAbstractMethodDefinition') && precedingNode.type === 'Identifier' && enclosingNode.key === precedingNode && getNextNonSpaceNonCommentCharacter(text, precedingNode, locEnd) !== ':') {
|
|
271
|
+
addTrailingComment(precedingNode, comment);
|
|
272
|
+
return true;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
if (precedingNode && enclosingNode && precedingNode.type === 'Decorator' && (enclosingNode.type === 'ClassMethod' || enclosingNode.type === 'ClassProperty' || enclosingNode.type === 'PropertyDefinition' || enclosingNode.type === 'TSAbstractPropertyDefinition' || enclosingNode.type === 'TSAbstractMethodDefinition' || enclosingNode.type === 'TSDeclareMethod' || enclosingNode.type === 'MethodDefinition')) {
|
|
276
|
+
addTrailingComment(precedingNode, comment);
|
|
277
|
+
return true;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
return false;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function handleFunctionNameComments({
|
|
284
|
+
comment,
|
|
285
|
+
precedingNode,
|
|
286
|
+
enclosingNode,
|
|
287
|
+
text
|
|
288
|
+
}) {
|
|
289
|
+
if (getNextNonSpaceNonCommentCharacter(text, comment, locEnd) !== '(') {
|
|
290
|
+
return false;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
if (precedingNode && enclosingNode && (enclosingNode.type === 'FunctionDeclaration' || enclosingNode.type === 'FunctionExpression' || enclosingNode.type === 'ClassMethod' || enclosingNode.type === 'MethodDefinition' || enclosingNode.type === 'ObjectMethod')) {
|
|
294
|
+
addTrailingComment(precedingNode, comment);
|
|
295
|
+
return true;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
return false;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function handleCommentAfterArrowParams({
|
|
302
|
+
comment,
|
|
303
|
+
enclosingNode,
|
|
304
|
+
text
|
|
305
|
+
}) {
|
|
306
|
+
if (!(enclosingNode && enclosingNode.type === 'ArrowFunctionExpression')) {
|
|
307
|
+
return false;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
const index = getNextNonSpaceNonCommentCharacterIndex(text, comment, locEnd);
|
|
311
|
+
|
|
312
|
+
if (index !== false && text.slice(index, index + 2) === '=>') {
|
|
313
|
+
addDanglingComment(enclosingNode, comment);
|
|
314
|
+
return true;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
return false;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
function handleCommentInEmptyParens({
|
|
321
|
+
comment,
|
|
322
|
+
enclosingNode,
|
|
323
|
+
text
|
|
324
|
+
}) {
|
|
325
|
+
if (getNextNonSpaceNonCommentCharacter(text, comment, locEnd) !== ')') {
|
|
326
|
+
return false;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
if (enclosingNode && (isRealFunctionLikeNode(enclosingNode) && getFunctionParameters(enclosingNode).length === 0 || isCallLikeExpression(enclosingNode) && getCallArguments(enclosingNode).length === 0)) {
|
|
330
|
+
addDanglingComment(enclosingNode, comment);
|
|
331
|
+
return true;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
if (enclosingNode && (enclosingNode.type === 'MethodDefinition' || enclosingNode.type === 'TSAbstractMethodDefinition') && getFunctionParameters(enclosingNode.value).length === 0) {
|
|
335
|
+
addDanglingComment(enclosingNode.value, comment);
|
|
336
|
+
return true;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
return false;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
function handleLastFunctionArgComments({
|
|
343
|
+
comment,
|
|
344
|
+
precedingNode,
|
|
345
|
+
enclosingNode,
|
|
346
|
+
followingNode,
|
|
347
|
+
text
|
|
348
|
+
}) {
|
|
349
|
+
if (precedingNode && precedingNode.type === 'FunctionTypeParam' && enclosingNode && enclosingNode.type === 'FunctionTypeAnnotation' && followingNode && followingNode.type !== 'FunctionTypeParam') {
|
|
350
|
+
addTrailingComment(precedingNode, comment);
|
|
351
|
+
return true;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
if (precedingNode && (precedingNode.type === 'Identifier' || precedingNode.type === 'AssignmentPattern') && enclosingNode && isRealFunctionLikeNode(enclosingNode) && getNextNonSpaceNonCommentCharacter(text, comment, locEnd) === ')') {
|
|
355
|
+
addTrailingComment(precedingNode, comment);
|
|
356
|
+
return true;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
if (enclosingNode && enclosingNode.type === 'FunctionDeclaration' && followingNode && followingNode.type === 'BlockStatement') {
|
|
360
|
+
const functionParamRightParenIndex = (() => {
|
|
361
|
+
const parameters = getFunctionParameters(enclosingNode);
|
|
362
|
+
|
|
363
|
+
if (parameters.length > 0) {
|
|
364
|
+
return getNextNonSpaceNonCommentCharacterIndexWithStartIndex(text, locEnd(getLast(parameters)));
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
const functionParamLeftParenIndex = getNextNonSpaceNonCommentCharacterIndexWithStartIndex(text, locEnd(enclosingNode.id));
|
|
368
|
+
return functionParamLeftParenIndex !== false && getNextNonSpaceNonCommentCharacterIndexWithStartIndex(text, functionParamLeftParenIndex + 1);
|
|
369
|
+
})();
|
|
370
|
+
|
|
371
|
+
if (locStart(comment) > functionParamRightParenIndex) {
|
|
372
|
+
addBlockStatementFirstComment(followingNode, comment);
|
|
373
|
+
return true;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
return false;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
function handleImportSpecifierComments({
|
|
381
|
+
comment,
|
|
382
|
+
enclosingNode
|
|
383
|
+
}) {
|
|
384
|
+
if (enclosingNode && enclosingNode.type === 'ImportSpecifier') {
|
|
385
|
+
addLeadingComment(enclosingNode, comment);
|
|
386
|
+
return true;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
return false;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
function handleLabeledStatementComments({
|
|
393
|
+
comment,
|
|
394
|
+
enclosingNode
|
|
395
|
+
}) {
|
|
396
|
+
if (enclosingNode && enclosingNode.type === 'LabeledStatement') {
|
|
397
|
+
addLeadingComment(enclosingNode, comment);
|
|
398
|
+
return true;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
return false;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
function handleBreakAndContinueStatementComments({
|
|
405
|
+
comment,
|
|
406
|
+
enclosingNode
|
|
407
|
+
}) {
|
|
408
|
+
if (enclosingNode && (enclosingNode.type === 'ContinueStatement' || enclosingNode.type === 'BreakStatement') && !enclosingNode.label) {
|
|
409
|
+
addTrailingComment(enclosingNode, comment);
|
|
410
|
+
return true;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
return false;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
function handleCallExpressionComments({
|
|
417
|
+
comment,
|
|
418
|
+
precedingNode,
|
|
419
|
+
enclosingNode
|
|
420
|
+
}) {
|
|
421
|
+
if (isCallExpression(enclosingNode) && precedingNode && enclosingNode.callee === precedingNode && enclosingNode.arguments.length > 0) {
|
|
422
|
+
addLeadingComment(enclosingNode.arguments[0], comment);
|
|
423
|
+
return true;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
return false;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
function handleUnionTypeComments({
|
|
430
|
+
comment,
|
|
431
|
+
precedingNode,
|
|
432
|
+
enclosingNode,
|
|
433
|
+
followingNode
|
|
434
|
+
}) {
|
|
435
|
+
if (enclosingNode && (enclosingNode.type === 'UnionTypeAnnotation' || enclosingNode.type === 'TSUnionType')) {
|
|
436
|
+
if (isPrettierIgnoreComment(comment)) {
|
|
437
|
+
followingNode.prettierIgnore = true;
|
|
438
|
+
comment.unignore = true;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
if (precedingNode) {
|
|
442
|
+
addTrailingComment(precedingNode, comment);
|
|
443
|
+
return true;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
return false;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
if (followingNode && (followingNode.type === 'UnionTypeAnnotation' || followingNode.type === 'TSUnionType') && isPrettierIgnoreComment(comment)) {
|
|
450
|
+
followingNode.types[0].prettierIgnore = true;
|
|
451
|
+
comment.unignore = true;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
return false;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
function handleMatchOrPatternComments({
|
|
458
|
+
comment,
|
|
459
|
+
precedingNode,
|
|
460
|
+
enclosingNode,
|
|
461
|
+
followingNode
|
|
462
|
+
}) {
|
|
463
|
+
if (enclosingNode && enclosingNode.type === 'MatchOrPattern') {
|
|
464
|
+
if (isPrettierIgnoreComment(comment)) {
|
|
465
|
+
followingNode.prettierIgnore = true;
|
|
466
|
+
comment.unignore = true;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
if (precedingNode) {
|
|
470
|
+
addTrailingComment(precedingNode, comment);
|
|
471
|
+
return true;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
return false;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
if (followingNode && followingNode.type === 'MatchOrPattern' && isPrettierIgnoreComment(comment)) {
|
|
478
|
+
followingNode.types[0].prettierIgnore = true;
|
|
479
|
+
comment.unignore = true;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
return false;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
function handlePropertyComments({
|
|
486
|
+
comment,
|
|
487
|
+
enclosingNode
|
|
488
|
+
}) {
|
|
489
|
+
if (isObjectProperty(enclosingNode)) {
|
|
490
|
+
addLeadingComment(enclosingNode, comment);
|
|
491
|
+
return true;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
return false;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
function handleOnlyComments({
|
|
498
|
+
comment,
|
|
499
|
+
enclosingNode,
|
|
500
|
+
followingNode,
|
|
501
|
+
ast,
|
|
502
|
+
isLastComment
|
|
503
|
+
}) {
|
|
504
|
+
if (ast && ast.body && ast.body.length === 0) {
|
|
505
|
+
if (isLastComment) {
|
|
506
|
+
addDanglingComment(ast, comment);
|
|
507
|
+
} else {
|
|
508
|
+
addLeadingComment(ast, comment);
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
return true;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
if (enclosingNode && enclosingNode.type === 'Program' && enclosingNode.body.length === 0 && !isNonEmptyArray(enclosingNode.directives)) {
|
|
515
|
+
if (isLastComment) {
|
|
516
|
+
addDanglingComment(enclosingNode, comment);
|
|
517
|
+
} else {
|
|
518
|
+
addLeadingComment(enclosingNode, comment);
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
return true;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
if (followingNode && followingNode.type === 'Program' && followingNode.body.length === 0 && enclosingNode && enclosingNode.type === 'ModuleExpression') {
|
|
525
|
+
addDanglingComment(followingNode, comment);
|
|
526
|
+
return true;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
return false;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
function handleForComments({
|
|
533
|
+
comment,
|
|
534
|
+
enclosingNode
|
|
535
|
+
}) {
|
|
536
|
+
if (enclosingNode && (enclosingNode.type === 'ForInStatement' || enclosingNode.type === 'ForOfStatement')) {
|
|
537
|
+
addLeadingComment(enclosingNode, comment);
|
|
538
|
+
return true;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
return false;
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
function handleImportDeclarationComments({
|
|
545
|
+
comment,
|
|
546
|
+
precedingNode,
|
|
547
|
+
enclosingNode,
|
|
548
|
+
text
|
|
549
|
+
}) {
|
|
550
|
+
if (precedingNode && precedingNode.type === 'ImportSpecifier' && enclosingNode && enclosingNode.type === 'ImportDeclaration' && hasNewline(text, locEnd(comment))) {
|
|
551
|
+
addTrailingComment(precedingNode, comment);
|
|
552
|
+
return true;
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
return false;
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
function handleAssignmentPatternComments({
|
|
559
|
+
comment,
|
|
560
|
+
enclosingNode
|
|
561
|
+
}) {
|
|
562
|
+
if (enclosingNode && enclosingNode.type === 'AssignmentPattern') {
|
|
563
|
+
addLeadingComment(enclosingNode, comment);
|
|
564
|
+
return true;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
return false;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
function handleTypeAliasComments({
|
|
571
|
+
comment,
|
|
572
|
+
enclosingNode
|
|
573
|
+
}) {
|
|
574
|
+
if (enclosingNode && enclosingNode.type === 'TypeAlias') {
|
|
575
|
+
addLeadingComment(enclosingNode, comment);
|
|
576
|
+
return true;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
return false;
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
function handleVariableDeclaratorComments({
|
|
583
|
+
comment,
|
|
584
|
+
enclosingNode,
|
|
585
|
+
followingNode
|
|
586
|
+
}) {
|
|
587
|
+
if (enclosingNode && (enclosingNode.type === 'VariableDeclarator' || enclosingNode.type === 'AssignmentExpression') && followingNode && (followingNode.type === 'ObjectExpression' || followingNode.type === 'ArrayExpression' || followingNode.type === 'TemplateLiteral' || followingNode.type === 'TaggedTemplateExpression' || isBlockComment(comment))) {
|
|
588
|
+
addLeadingComment(followingNode, comment);
|
|
589
|
+
return true;
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
return false;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
function handleTSFunctionTrailingComments({
|
|
596
|
+
comment,
|
|
597
|
+
enclosingNode,
|
|
598
|
+
followingNode,
|
|
599
|
+
text
|
|
600
|
+
}) {
|
|
601
|
+
if (!followingNode && enclosingNode && (enclosingNode.type === 'TSMethodSignature' || enclosingNode.type === 'TSDeclareFunction' || enclosingNode.type === 'TSAbstractMethodDefinition') && getNextNonSpaceNonCommentCharacter(text, comment, locEnd) === ';') {
|
|
602
|
+
addTrailingComment(enclosingNode, comment);
|
|
603
|
+
return true;
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
return false;
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
function handleIgnoreComments({
|
|
610
|
+
comment,
|
|
611
|
+
enclosingNode,
|
|
612
|
+
followingNode
|
|
613
|
+
}) {
|
|
614
|
+
if (isPrettierIgnoreComment(comment) && enclosingNode && enclosingNode.type === 'TSMappedType' && followingNode && followingNode.type === 'TSTypeParameter' && followingNode.constraint) {
|
|
615
|
+
enclosingNode.prettierIgnore = true;
|
|
616
|
+
comment.unignore = true;
|
|
617
|
+
return true;
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
function handleTSMappedTypeComments({
|
|
622
|
+
comment,
|
|
623
|
+
precedingNode,
|
|
624
|
+
enclosingNode,
|
|
625
|
+
followingNode
|
|
626
|
+
}) {
|
|
627
|
+
if (!enclosingNode || enclosingNode.type !== 'TSMappedType') {
|
|
628
|
+
return false;
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
if (followingNode && followingNode.type === 'TSTypeParameter' && followingNode.name) {
|
|
632
|
+
addLeadingComment(followingNode.name, comment);
|
|
633
|
+
return true;
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
if (precedingNode && precedingNode.type === 'TSTypeParameter' && precedingNode.constraint) {
|
|
637
|
+
addTrailingComment(precedingNode.constraint, comment);
|
|
638
|
+
return true;
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
return false;
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
function isRealFunctionLikeNode(node) {
|
|
645
|
+
return node.type === 'ArrowFunctionExpression' || node.type === 'FunctionExpression' || node.type === 'FunctionDeclaration' || node.type === 'ObjectMethod' || node.type === 'ClassMethod' || node.type === 'TSDeclareFunction' || node.type === 'TSCallSignatureDeclaration' || node.type === 'TSConstructSignatureDeclaration' || node.type === 'TSMethodSignature' || node.type === 'TSConstructorType' || node.type === 'TSFunctionType' || node.type === 'TSDeclareMethod';
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
function getCommentChildNodes(node, options) {
|
|
649
|
+
if ((options.parser === 'typescript' || options.parser === 'flow' || options.parser === 'espree' || options.parser === 'meriyah' || options.parser === '__babel_estree') && node.type === 'MethodDefinition' && node.value && node.value.type === 'FunctionExpression' && getFunctionParameters(node.value).length === 0 && !node.value.returnType && !isNonEmptyArray(node.value.typeParameters) && node.value.body) {
|
|
650
|
+
return [...(node.decorators || []), node.key, node.value.body];
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
function isTypeCastComment(comment) {
|
|
655
|
+
return isBlockComment(comment) && comment.value[0] === '*' && /@type\b/.test(comment.value);
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
module.exports = {
|
|
659
|
+
handleOwnLineComment,
|
|
660
|
+
handleEndOfLineComment,
|
|
661
|
+
handleRemainingComment,
|
|
662
|
+
getCommentChildNodes
|
|
663
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
isNonEmptyArray
|
|
5
|
+
} = require('../common/util.js');
|
|
6
|
+
|
|
7
|
+
function locStart(node, opts) {
|
|
8
|
+
const {
|
|
9
|
+
ignoreDecorators
|
|
10
|
+
} = opts || {};
|
|
11
|
+
|
|
12
|
+
if (!ignoreDecorators) {
|
|
13
|
+
const decorators = node.declaration && node.declaration.decorators || node.decorators;
|
|
14
|
+
|
|
15
|
+
if (isNonEmptyArray(decorators)) {
|
|
16
|
+
return locStart(decorators[0]);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return node.range ? node.range[0] : node.start;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function locEnd(node) {
|
|
24
|
+
return node.range ? node.range[1] : node.end;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
module.exports = {
|
|
28
|
+
locStart,
|
|
29
|
+
locEnd
|
|
30
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const handleComments = require('./comments.js');
|
|
4
|
+
|
|
5
|
+
const {
|
|
6
|
+
isBlockComment,
|
|
7
|
+
isLineComment
|
|
8
|
+
} = require('./utils.js');
|
|
9
|
+
|
|
10
|
+
function canAttachComment(node) {
|
|
11
|
+
return node.type && !isBlockComment(node) && !isLineComment(node) && node.type !== 'EmptyStatement' && node.type !== 'TemplateElement' && node.type !== 'Import' && node.type !== 'TSEmptyBodyFunctionExpression';
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
module.exports = {
|
|
15
|
+
canAttachComment,
|
|
16
|
+
handleComments: {
|
|
17
|
+
avoidAstMutation: true,
|
|
18
|
+
ownLine: handleComments.handleOwnLineComment,
|
|
19
|
+
endOfLine: handleComments.handleEndOfLineComment,
|
|
20
|
+
remaining: handleComments.handleRemainingComment
|
|
21
|
+
},
|
|
22
|
+
getCommentChildNodes: handleComments.getCommentChildNodes
|
|
23
|
+
};
|