flow-parser 0.317.0 → 0.319.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/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 +1767 -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 +1055 -0
- package/oxidized/babel/TransformESTreeToBabel.js.flow +1349 -0
- package/oxidized/estree/StripFlowTypes.js +158 -0
- package/oxidized/estree/StripFlowTypes.js.flow +158 -0
- package/oxidized/estree/StripFlowTypesForBabel.js +176 -0
- package/oxidized/estree/StripFlowTypesForBabel.js.flow +216 -0
- package/oxidized/estree/TransformComponentSyntax.js +751 -0
- package/oxidized/estree/TransformComponentSyntax.js.flow +864 -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 +1767 -0
- package/oxidized/oxidized-src/HermesParserDecodeUTF8String.js +45 -0
- package/oxidized/oxidized-src/ParserOptions.js +8 -0
- package/oxidized/oxidized-src/babel/TransformESTreeToBabel.js +1055 -0
- package/oxidized/oxidized-src/estree/StripFlowTypes.js +158 -0
- package/oxidized/oxidized-src/estree/StripFlowTypesForBabel.js +176 -0
- package/oxidized/oxidized-src/estree/TransformComponentSyntax.js +751 -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 +16 -6
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function isBlockComment(comment) {
|
|
4
|
+
return comment.type === 'Block' || comment.type === 'CommentBlock' || comment.type === 'MultiLine';
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function isLineComment(comment) {
|
|
8
|
+
return comment.type === 'Line' || comment.type === 'CommentLine' || comment.type === 'SingleLine' || comment.type === 'HashbangComment' || comment.type === 'HTMLOpen' || comment.type === 'HTMLClose';
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function isCallExpression(node) {
|
|
12
|
+
return node && (node.type === 'CallExpression' || node.type === 'OptionalCallExpression');
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function isMemberExpression(node) {
|
|
16
|
+
return node && (node.type === 'MemberExpression' || node.type === 'OptionalMemberExpression');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const functionParametersCache = new WeakMap();
|
|
20
|
+
|
|
21
|
+
function getFunctionParameters(node) {
|
|
22
|
+
if (functionParametersCache.has(node)) {
|
|
23
|
+
return functionParametersCache.get(node);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const parameters = [];
|
|
27
|
+
|
|
28
|
+
if (node.this) {
|
|
29
|
+
parameters.push(node.this);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (Array.isArray(node.parameters)) {
|
|
33
|
+
parameters.push(...node.parameters);
|
|
34
|
+
} else if (Array.isArray(node.params)) {
|
|
35
|
+
parameters.push(...node.params);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (node.rest) {
|
|
39
|
+
parameters.push(node.rest);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
functionParametersCache.set(node, parameters);
|
|
43
|
+
return parameters;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const callArgumentsCache = new WeakMap();
|
|
47
|
+
|
|
48
|
+
function getCallArguments(node) {
|
|
49
|
+
if (callArgumentsCache.has(node)) {
|
|
50
|
+
return callArgumentsCache.get(node);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
let args = node.arguments;
|
|
54
|
+
|
|
55
|
+
if (node.type === 'ImportExpression') {
|
|
56
|
+
args = [node.source];
|
|
57
|
+
|
|
58
|
+
if (node.attributes) {
|
|
59
|
+
args.push(node.attributes);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
callArgumentsCache.set(node, args);
|
|
64
|
+
return args;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function isPrettierIgnoreComment(comment) {
|
|
68
|
+
return comment.value.trim() === 'prettier-ignore' && !comment.unignore;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function isCallLikeExpression(node) {
|
|
72
|
+
return isCallExpression(node) || node.type === 'NewExpression' || node.type === 'ImportExpression';
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function isObjectProperty(node) {
|
|
76
|
+
return node && (node.type === 'ObjectProperty' || node.type === 'Property' && !node.method && node.kind === 'init');
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
module.exports = {
|
|
80
|
+
getFunctionParameters,
|
|
81
|
+
getCallArguments,
|
|
82
|
+
isBlockComment,
|
|
83
|
+
isCallLikeExpression,
|
|
84
|
+
isLineComment,
|
|
85
|
+
isPrettierIgnoreComment,
|
|
86
|
+
isCallExpression,
|
|
87
|
+
isMemberExpression,
|
|
88
|
+
isObjectProperty
|
|
89
|
+
};
|
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const assert = require('assert');
|
|
4
|
+
|
|
5
|
+
const {
|
|
6
|
+
hasNewline,
|
|
7
|
+
addLeadingComment,
|
|
8
|
+
addDanglingComment,
|
|
9
|
+
addTrailingComment
|
|
10
|
+
} = require('../common/util.js');
|
|
11
|
+
|
|
12
|
+
const childNodesCache = new WeakMap();
|
|
13
|
+
|
|
14
|
+
function getSortedChildNodes(node, options, resultArray) {
|
|
15
|
+
if (!node) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const {
|
|
20
|
+
printer,
|
|
21
|
+
locStart,
|
|
22
|
+
locEnd
|
|
23
|
+
} = options;
|
|
24
|
+
|
|
25
|
+
if (resultArray) {
|
|
26
|
+
if (printer.canAttachComment && printer.canAttachComment(node)) {
|
|
27
|
+
let i;
|
|
28
|
+
|
|
29
|
+
for (i = resultArray.length - 1; i >= 0; --i) {
|
|
30
|
+
if (locStart(resultArray[i]) <= locStart(node) && locEnd(resultArray[i]) <= locEnd(node)) {
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
resultArray.splice(i + 1, 0, node);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
} else if (childNodesCache.has(node)) {
|
|
39
|
+
return childNodesCache.get(node);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const childNodes = printer.getCommentChildNodes && printer.getCommentChildNodes(node, options) || typeof node === 'object' && Object.entries(node).filter(([key]) => key !== 'enclosingNode' && key !== 'precedingNode' && key !== 'followingNode' && key !== 'tokens' && key !== 'comments' && key !== 'parent').map(([, value]) => value);
|
|
43
|
+
|
|
44
|
+
if (!childNodes) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (!resultArray) {
|
|
49
|
+
resultArray = [];
|
|
50
|
+
childNodesCache.set(node, resultArray);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
for (const childNode of childNodes) {
|
|
54
|
+
getSortedChildNodes(childNode, options, resultArray);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return resultArray;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function decorateComment(node, comment, options, enclosingNode) {
|
|
61
|
+
const {
|
|
62
|
+
locStart,
|
|
63
|
+
locEnd
|
|
64
|
+
} = options;
|
|
65
|
+
const commentStart = locStart(comment);
|
|
66
|
+
const commentEnd = locEnd(comment);
|
|
67
|
+
const childNodes = getSortedChildNodes(node, options);
|
|
68
|
+
let precedingNode;
|
|
69
|
+
let followingNode;
|
|
70
|
+
let left = 0;
|
|
71
|
+
let right = childNodes.length;
|
|
72
|
+
|
|
73
|
+
while (left < right) {
|
|
74
|
+
const middle = left + right >> 1;
|
|
75
|
+
const child = childNodes[middle];
|
|
76
|
+
const start = locStart(child);
|
|
77
|
+
const end = locEnd(child);
|
|
78
|
+
|
|
79
|
+
if (start <= commentStart && commentEnd <= end) {
|
|
80
|
+
return decorateComment(child, comment, options, child);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (end <= commentStart) {
|
|
84
|
+
precedingNode = child;
|
|
85
|
+
left = middle + 1;
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (commentEnd <= start) {
|
|
90
|
+
followingNode = child;
|
|
91
|
+
right = middle;
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
throw new Error('Comment location overlaps with node location');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (enclosingNode && enclosingNode.type === 'TemplateLiteral') {
|
|
99
|
+
const {
|
|
100
|
+
quasis
|
|
101
|
+
} = enclosingNode;
|
|
102
|
+
const commentIndex = findExpressionIndexForComment(quasis, comment, options);
|
|
103
|
+
|
|
104
|
+
if (precedingNode && findExpressionIndexForComment(quasis, precedingNode, options) !== commentIndex) {
|
|
105
|
+
precedingNode = null;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (followingNode && findExpressionIndexForComment(quasis, followingNode, options) !== commentIndex) {
|
|
109
|
+
followingNode = null;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return {
|
|
114
|
+
enclosingNode,
|
|
115
|
+
precedingNode,
|
|
116
|
+
followingNode
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const returnFalse = () => false;
|
|
121
|
+
|
|
122
|
+
function attach(comments, ast, text, options) {
|
|
123
|
+
if (!Array.isArray(comments)) {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const tiesToBreak = [];
|
|
128
|
+
const {
|
|
129
|
+
locStart,
|
|
130
|
+
locEnd,
|
|
131
|
+
printer: {
|
|
132
|
+
handleComments = {}
|
|
133
|
+
}
|
|
134
|
+
} = options;
|
|
135
|
+
const {
|
|
136
|
+
avoidAstMutation,
|
|
137
|
+
ownLine: handleOwnLineComment = returnFalse,
|
|
138
|
+
endOfLine: handleEndOfLineComment = returnFalse,
|
|
139
|
+
remaining: handleRemainingComment = returnFalse
|
|
140
|
+
} = handleComments;
|
|
141
|
+
const decoratedComments = comments.map((comment, index) => ({ ...decorateComment(ast, comment, options),
|
|
142
|
+
comment,
|
|
143
|
+
text,
|
|
144
|
+
options,
|
|
145
|
+
ast,
|
|
146
|
+
isLastComment: comments.length - 1 === index
|
|
147
|
+
}));
|
|
148
|
+
|
|
149
|
+
for (const [index, context] of decoratedComments.entries()) {
|
|
150
|
+
const {
|
|
151
|
+
comment,
|
|
152
|
+
precedingNode,
|
|
153
|
+
enclosingNode,
|
|
154
|
+
followingNode,
|
|
155
|
+
text,
|
|
156
|
+
options,
|
|
157
|
+
ast,
|
|
158
|
+
isLastComment
|
|
159
|
+
} = context;
|
|
160
|
+
|
|
161
|
+
if (options.parser === 'json' || options.parser === 'json5' || options.parser === '__js_expression' || options.parser === '__vue_expression') {
|
|
162
|
+
if (locStart(comment) - locStart(ast) <= 0) {
|
|
163
|
+
addLeadingComment(ast, comment);
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (locEnd(comment) - locEnd(ast) >= 0) {
|
|
168
|
+
addTrailingComment(ast, comment);
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
let args;
|
|
174
|
+
|
|
175
|
+
if (avoidAstMutation) {
|
|
176
|
+
args = [context];
|
|
177
|
+
} else {
|
|
178
|
+
comment.enclosingNode = enclosingNode;
|
|
179
|
+
comment.precedingNode = precedingNode;
|
|
180
|
+
comment.followingNode = followingNode;
|
|
181
|
+
args = [comment, text, options, ast, isLastComment];
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (isOwnLineComment(text, options, decoratedComments, index)) {
|
|
185
|
+
comment.placement = 'ownLine';
|
|
186
|
+
|
|
187
|
+
if (handleOwnLineComment(...args)) {} else if (followingNode) {
|
|
188
|
+
addLeadingComment(followingNode, comment);
|
|
189
|
+
} else if (precedingNode) {
|
|
190
|
+
addTrailingComment(precedingNode, comment);
|
|
191
|
+
} else if (enclosingNode) {
|
|
192
|
+
addDanglingComment(enclosingNode, comment);
|
|
193
|
+
} else {
|
|
194
|
+
addDanglingComment(ast, comment);
|
|
195
|
+
}
|
|
196
|
+
} else if (isEndOfLineComment(text, options, decoratedComments, index)) {
|
|
197
|
+
comment.placement = 'endOfLine';
|
|
198
|
+
|
|
199
|
+
if (handleEndOfLineComment(...args)) {} else if (precedingNode) {
|
|
200
|
+
addTrailingComment(precedingNode, comment);
|
|
201
|
+
} else if (followingNode) {
|
|
202
|
+
addLeadingComment(followingNode, comment);
|
|
203
|
+
} else if (enclosingNode) {
|
|
204
|
+
addDanglingComment(enclosingNode, comment);
|
|
205
|
+
} else {
|
|
206
|
+
addDanglingComment(ast, comment);
|
|
207
|
+
}
|
|
208
|
+
} else {
|
|
209
|
+
comment.placement = 'remaining';
|
|
210
|
+
|
|
211
|
+
if (handleRemainingComment(...args)) {} else if (precedingNode && followingNode) {
|
|
212
|
+
const tieCount = tiesToBreak.length;
|
|
213
|
+
|
|
214
|
+
if (tieCount > 0) {
|
|
215
|
+
const lastTie = tiesToBreak[tieCount - 1];
|
|
216
|
+
|
|
217
|
+
if (lastTie.followingNode !== followingNode) {
|
|
218
|
+
breakTies(tiesToBreak, text, options);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
tiesToBreak.push(context);
|
|
223
|
+
} else if (precedingNode) {
|
|
224
|
+
addTrailingComment(precedingNode, comment);
|
|
225
|
+
} else if (followingNode) {
|
|
226
|
+
addLeadingComment(followingNode, comment);
|
|
227
|
+
} else if (enclosingNode) {
|
|
228
|
+
addDanglingComment(enclosingNode, comment);
|
|
229
|
+
} else {
|
|
230
|
+
addDanglingComment(ast, comment);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
breakTies(tiesToBreak, text, options);
|
|
236
|
+
|
|
237
|
+
if (!avoidAstMutation) {
|
|
238
|
+
for (const comment of comments) {
|
|
239
|
+
delete comment.precedingNode;
|
|
240
|
+
delete comment.enclosingNode;
|
|
241
|
+
delete comment.followingNode;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const isAllEmptyAndNoLineBreak = text => !/[\S\n\u2028\u2029]/.test(text);
|
|
247
|
+
|
|
248
|
+
function isOwnLineComment(text, options, decoratedComments, commentIndex) {
|
|
249
|
+
const {
|
|
250
|
+
comment,
|
|
251
|
+
precedingNode
|
|
252
|
+
} = decoratedComments[commentIndex];
|
|
253
|
+
const {
|
|
254
|
+
locStart,
|
|
255
|
+
locEnd
|
|
256
|
+
} = options;
|
|
257
|
+
let start = locStart(comment);
|
|
258
|
+
|
|
259
|
+
if (precedingNode) {
|
|
260
|
+
for (let index = commentIndex - 1; index >= 0; index--) {
|
|
261
|
+
const {
|
|
262
|
+
comment,
|
|
263
|
+
precedingNode: currentCommentPrecedingNode
|
|
264
|
+
} = decoratedComments[index];
|
|
265
|
+
|
|
266
|
+
if (currentCommentPrecedingNode !== precedingNode || !isAllEmptyAndNoLineBreak(text.slice(locEnd(comment), start))) {
|
|
267
|
+
break;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
start = locStart(comment);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
return hasNewline(text, start, {
|
|
275
|
+
backwards: true
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function isEndOfLineComment(text, options, decoratedComments, commentIndex) {
|
|
280
|
+
const {
|
|
281
|
+
comment,
|
|
282
|
+
followingNode
|
|
283
|
+
} = decoratedComments[commentIndex];
|
|
284
|
+
const {
|
|
285
|
+
locStart,
|
|
286
|
+
locEnd
|
|
287
|
+
} = options;
|
|
288
|
+
let end = locEnd(comment);
|
|
289
|
+
|
|
290
|
+
if (followingNode) {
|
|
291
|
+
for (let index = commentIndex + 1; index < decoratedComments.length; index++) {
|
|
292
|
+
const {
|
|
293
|
+
comment,
|
|
294
|
+
followingNode: currentCommentFollowingNode
|
|
295
|
+
} = decoratedComments[index];
|
|
296
|
+
|
|
297
|
+
if (currentCommentFollowingNode !== followingNode || !isAllEmptyAndNoLineBreak(text.slice(end, locStart(comment)))) {
|
|
298
|
+
break;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
end = locEnd(comment);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
return hasNewline(text, end);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function breakTies(tiesToBreak, text, options) {
|
|
309
|
+
const tieCount = tiesToBreak.length;
|
|
310
|
+
|
|
311
|
+
if (tieCount === 0) {
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
const {
|
|
316
|
+
precedingNode,
|
|
317
|
+
followingNode,
|
|
318
|
+
enclosingNode
|
|
319
|
+
} = tiesToBreak[0];
|
|
320
|
+
const gapRegExp = options.printer.getGapRegex && options.printer.getGapRegex(enclosingNode) || /^[\s(]*$/;
|
|
321
|
+
let gapEndPos = options.locStart(followingNode);
|
|
322
|
+
let indexOfFirstLeadingComment;
|
|
323
|
+
|
|
324
|
+
for (indexOfFirstLeadingComment = tieCount; indexOfFirstLeadingComment > 0; --indexOfFirstLeadingComment) {
|
|
325
|
+
const {
|
|
326
|
+
comment,
|
|
327
|
+
precedingNode: currentCommentPrecedingNode,
|
|
328
|
+
followingNode: currentCommentFollowingNode
|
|
329
|
+
} = tiesToBreak[indexOfFirstLeadingComment - 1];
|
|
330
|
+
assert.strictEqual(currentCommentPrecedingNode, precedingNode);
|
|
331
|
+
assert.strictEqual(currentCommentFollowingNode, followingNode);
|
|
332
|
+
const gap = text.slice(options.locEnd(comment), gapEndPos);
|
|
333
|
+
|
|
334
|
+
if (gapRegExp.test(gap)) {
|
|
335
|
+
gapEndPos = options.locStart(comment);
|
|
336
|
+
} else {
|
|
337
|
+
break;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
for (const [i, {
|
|
342
|
+
comment
|
|
343
|
+
}] of tiesToBreak.entries()) {
|
|
344
|
+
if (i < indexOfFirstLeadingComment) {
|
|
345
|
+
addTrailingComment(precedingNode, comment);
|
|
346
|
+
} else {
|
|
347
|
+
addLeadingComment(followingNode, comment);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
for (const node of [precedingNode, followingNode]) {
|
|
352
|
+
if (node.comments && node.comments.length > 1) {
|
|
353
|
+
node.comments.sort((a, b) => options.locStart(a) - options.locStart(b));
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
tiesToBreak.length = 0;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
function findExpressionIndexForComment(quasis, comment, options) {
|
|
361
|
+
const startPos = options.locStart(comment) - 1;
|
|
362
|
+
|
|
363
|
+
for (let i = 1; i < quasis.length; ++i) {
|
|
364
|
+
if (startPos < options.locStart(quasis[i])) {
|
|
365
|
+
return i - 1;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
return 0;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
module.exports = {
|
|
373
|
+
attach
|
|
374
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
'use strict';
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
* @flow strict-local
|
|
8
|
+
* @format
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/*
|
|
12
|
+
* Minimal type-only stub for `DetachedNode<T>` and `MaybeDetachedNode<T>`.
|
|
13
|
+
* Upstream hermes-transform's `src/detachedNode.js` defines these as part of
|
|
14
|
+
* its broader detached-AST authoring surface used by node-builders. The
|
|
15
|
+
* vendored `print()` only needs the structural types for its public API
|
|
16
|
+
* signature; everything is type-erased at runtime by Babel's
|
|
17
|
+
* flow-strip-types pass. By keeping a local stub we avoid pulling in the
|
|
18
|
+
* full hermes-transform detached-node machinery.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
'use strict';
|
|
22
|
+
|
|
23
|
+
export type DetachedNode<+T> = T;
|
|
24
|
+
export type MaybeDetachedNode<+T> = T | DetachedNode<T>;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.print = print;
|
|
7
|
+
|
|
8
|
+
var _mutateESTreeASTForPrettier = _interopRequireDefault(require("../../utils/mutateESTreeASTForPrettier"));
|
|
9
|
+
|
|
10
|
+
var prettier = _interopRequireWildcard(require("prettier"));
|
|
11
|
+
|
|
12
|
+
var _comments = require("./comments/comments");
|
|
13
|
+
|
|
14
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
+
|
|
16
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
17
|
+
|
|
18
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
19
|
+
|
|
20
|
+
async function print(ast, originalCode, prettierOptions = {}, visitorKeys) {
|
|
21
|
+
const program = ast;
|
|
22
|
+
|
|
23
|
+
if (program.body.length === 0) {
|
|
24
|
+
var _program$docblock;
|
|
25
|
+
|
|
26
|
+
const docblockComment = (_program$docblock = program.docblock) == null ? void 0 : _program$docblock.comment;
|
|
27
|
+
|
|
28
|
+
if (docblockComment != null) {
|
|
29
|
+
return '/*' + docblockComment.value + '*/\n';
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return '';
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const codeForPrinting = (0, _comments.mutateESTreeASTCommentsForPrettier)(program, originalCode);
|
|
36
|
+
(0, _mutateESTreeASTForPrettier.default)(program, visitorKeys);
|
|
37
|
+
let pluginParserName = 'flow';
|
|
38
|
+
let pluginParser;
|
|
39
|
+
let pluginPrinter;
|
|
40
|
+
|
|
41
|
+
try {
|
|
42
|
+
const prettierHermesPlugin = await Promise.resolve().then(() => _interopRequireWildcard(require('prettier-plugin-hermes-parser')));
|
|
43
|
+
pluginParser = prettierHermesPlugin.parsers.hermes;
|
|
44
|
+
pluginPrinter = prettierHermesPlugin.printers;
|
|
45
|
+
pluginParserName = 'hermes';
|
|
46
|
+
} catch {
|
|
47
|
+
const prettierFlowPlugin = require('prettier/plugins/flow');
|
|
48
|
+
|
|
49
|
+
pluginParser = prettierFlowPlugin.parsers.flow;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return prettier.format(codeForPrinting, { ...prettierOptions,
|
|
53
|
+
parser: pluginParserName,
|
|
54
|
+
requirePragma: false,
|
|
55
|
+
plugins: [{
|
|
56
|
+
parsers: {
|
|
57
|
+
[pluginParserName]: { ...pluginParser,
|
|
58
|
+
|
|
59
|
+
parse() {
|
|
60
|
+
return program;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
printers: pluginPrinter
|
|
66
|
+
}]
|
|
67
|
+
});
|
|
68
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
* @flow strict-local
|
|
8
|
+
* @format
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
'use strict';
|
|
12
|
+
|
|
13
|
+
import type {MaybeDetachedNode} from './detachedNodeTypes';
|
|
14
|
+
import type {Program} from 'flow-estree';
|
|
15
|
+
|
|
16
|
+
import mutateESTreeASTForPrettier from '../../utils/mutateESTreeASTForPrettier';
|
|
17
|
+
import * as prettier from 'prettier';
|
|
18
|
+
import {mutateESTreeASTCommentsForPrettier} from './comments/comments';
|
|
19
|
+
import type {VisitorKeysType} from '../../traverse/getVisitorKeys';
|
|
20
|
+
|
|
21
|
+
export async function print(
|
|
22
|
+
ast: MaybeDetachedNode<Program>,
|
|
23
|
+
originalCode: string,
|
|
24
|
+
prettierOptions: {...} = {},
|
|
25
|
+
visitorKeys?: ?VisitorKeysType,
|
|
26
|
+
): Promise<string> {
|
|
27
|
+
// $FlowExpectedError[incompatible-type] This is now safe to access.
|
|
28
|
+
const program: Program = ast;
|
|
29
|
+
|
|
30
|
+
// If the AST body is empty, we can skip the cost of prettier by returning a static string of the contents.
|
|
31
|
+
if (program.body.length === 0) {
|
|
32
|
+
// If the program had a docblock comment, we need to create the string manually.
|
|
33
|
+
const docblockComment = program.docblock?.comment;
|
|
34
|
+
if (docblockComment != null) {
|
|
35
|
+
return '/*' + docblockComment.value + '*/\n';
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return '';
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Cleanup the comments from the AST and generate the "orginal" code needed for prettier.
|
|
42
|
+
const codeForPrinting = mutateESTreeASTCommentsForPrettier(
|
|
43
|
+
program,
|
|
44
|
+
originalCode,
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
// Fix up the AST to match what prettier expects.
|
|
48
|
+
mutateESTreeASTForPrettier(program, visitorKeys);
|
|
49
|
+
|
|
50
|
+
let pluginParserName = 'flow';
|
|
51
|
+
let pluginParser;
|
|
52
|
+
let pluginPrinter;
|
|
53
|
+
try {
|
|
54
|
+
// Use prettier-plugin-hermes-parser if we can. It has latest Flow syntax support.
|
|
55
|
+
// $FlowExpectedError[untyped-import]
|
|
56
|
+
const prettierHermesPlugin = await import('prettier-plugin-hermes-parser');
|
|
57
|
+
pluginParser = prettierHermesPlugin.parsers.hermes;
|
|
58
|
+
pluginPrinter = prettierHermesPlugin.printers;
|
|
59
|
+
pluginParserName = 'hermes';
|
|
60
|
+
} catch {
|
|
61
|
+
const prettierFlowPlugin = require('prettier/plugins/flow');
|
|
62
|
+
pluginParser = prettierFlowPlugin.parsers.flow;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return prettier.format(
|
|
66
|
+
codeForPrinting,
|
|
67
|
+
// $FlowExpectedError[incompatible-exact] - we don't want to create a dependency on the prettier types
|
|
68
|
+
{
|
|
69
|
+
...prettierOptions,
|
|
70
|
+
parser: pluginParserName,
|
|
71
|
+
requirePragma: false,
|
|
72
|
+
plugins: [
|
|
73
|
+
{
|
|
74
|
+
parsers: {
|
|
75
|
+
[pluginParserName]: {
|
|
76
|
+
...pluginParser,
|
|
77
|
+
parse() {
|
|
78
|
+
return program;
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
printers: pluginPrinter,
|
|
83
|
+
},
|
|
84
|
+
],
|
|
85
|
+
},
|
|
86
|
+
);
|
|
87
|
+
}
|