hermes-transform 0.36.1 → 0.37.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/dist/detachedNode.js +3 -57
- package/dist/detachedNode.js.flow +12 -12
- package/dist/generated/TransformCloneSignatures.js.flow +1 -1
- package/dist/generated/TransformModifySignatures.js.flow +1 -1
- package/dist/generated/TransformReplaceSignatures.js.flow +207 -207
- package/dist/generated/node-types.js +0 -21
- package/dist/generated/node-types.js.flow +1086 -978
- package/dist/generated/special-case-node-types/Comment.js +0 -13
- package/dist/generated/special-case-node-types/Comment.js.flow +2 -2
- package/dist/generated/special-case-node-types/DeclareExportDeclaration.js +0 -12
- package/dist/generated/special-case-node-types/DeclareExportDeclaration.js.flow +10 -10
- package/dist/generated/special-case-node-types/DeclareHook.js +0 -9
- package/dist/generated/special-case-node-types/DeclareHook.js.flow +4 -4
- package/dist/generated/special-case-node-types/ExportNamedDeclaration.js +0 -11
- package/dist/generated/special-case-node-types/ExportNamedDeclaration.js.flow +9 -9
- package/dist/generated/special-case-node-types/Literal.js +1 -12
- package/dist/generated/special-case-node-types/Literal.js.flow +20 -20
- package/dist/generated/special-case-node-types/ObjectTypeProperty.js +0 -10
- package/dist/generated/special-case-node-types/ObjectTypeProperty.js.flow +23 -21
- package/dist/generated/special-case-node-types/Property.js +0 -9
- package/dist/generated/special-case-node-types/Property.js.flow +50 -42
- package/dist/generated/special-case-node-types/misc.js +6 -35
- package/dist/generated/special-case-node-types/misc.js.flow +56 -48
- package/dist/index.js +0 -9
- package/dist/src/detachedNode.js +3 -57
- package/dist/src/generated/node-types.js +0 -21
- package/dist/src/generated/special-case-node-types/Comment.js +0 -13
- package/dist/src/generated/special-case-node-types/DeclareExportDeclaration.js +0 -12
- package/dist/src/generated/special-case-node-types/DeclareHook.js +0 -9
- package/dist/src/generated/special-case-node-types/ExportNamedDeclaration.js +0 -11
- package/dist/src/generated/special-case-node-types/Literal.js +1 -12
- package/dist/src/generated/special-case-node-types/ObjectTypeProperty.js +0 -10
- package/dist/src/generated/special-case-node-types/Property.js +0 -9
- package/dist/src/generated/special-case-node-types/misc.js +6 -35
- package/dist/src/index.js +0 -9
- package/dist/src/transform/Errors.js +0 -9
- package/dist/src/transform/MutationContext.js +1 -28
- package/dist/src/transform/TransformContext.js +0 -18
- package/dist/src/transform/comments/comments.js +12 -78
- package/dist/src/transform/comments/prettier/common/util.js +4 -133
- package/dist/src/transform/comments/prettier/language-js/comments.js +12 -137
- package/dist/src/transform/comments/prettier/language-js/loc.js +1 -13
- package/dist/src/transform/comments/prettier/language-js/printer-estree.js +1 -10
- package/dist/src/transform/comments/prettier/language-js/utils.js +3 -33
- package/dist/src/transform/comments/prettier/main/comments.js +11 -77
- package/dist/src/transform/comments/prettier/utils/get-last.js +0 -8
- package/dist/src/transform/mutations/AddComments.js +0 -9
- package/dist/src/transform/mutations/CloneCommentsTo.js +0 -9
- package/dist/src/transform/mutations/InsertStatement.js +4 -16
- package/dist/src/transform/mutations/ModifyNodeInPlace.js +4 -17
- package/dist/src/transform/mutations/RemoveComment.js +0 -10
- package/dist/src/transform/mutations/RemoveNode.js +1 -23
- package/dist/src/transform/mutations/RemoveStatement.js +0 -16
- package/dist/src/transform/mutations/ReplaceNode.js +2 -16
- package/dist/src/transform/mutations/ReplaceStatementWithMany.js +2 -14
- package/dist/src/transform/mutations/utils/getStatementParent.js +3 -19
- package/dist/src/transform/mutations/utils/isValidModuleDeclarationParent.js +5 -20
- package/dist/src/transform/parse.js +6 -25
- package/dist/src/transform/print.js +9 -35
- package/dist/src/transform/transform.js +0 -9
- package/dist/src/transform/transformAST.js +3 -31
- package/dist/src/traverse/NodeEventGenerator.js +2 -108
- package/dist/src/traverse/SafeEmitter.js +0 -19
- package/dist/src/traverse/esquery.js +0 -18
- package/dist/src/traverse/traverse.js +1 -22
- package/dist/transform/Errors.js +0 -9
- package/dist/transform/MutationContext.js +1 -28
- package/dist/transform/MutationContext.js.flow +3 -3
- package/dist/transform/TransformContext.js +0 -18
- package/dist/transform/TransformContext.js.flow +60 -54
- package/dist/transform/comments/comments.js +12 -78
- package/dist/transform/comments/comments.js.flow +8 -8
- package/dist/transform/comments/prettier/common/util.js +4 -133
- package/dist/transform/comments/prettier/language-js/comments.js +12 -137
- package/dist/transform/comments/prettier/language-js/loc.js +1 -13
- package/dist/transform/comments/prettier/language-js/printer-estree.js +1 -10
- package/dist/transform/comments/prettier/language-js/utils.js +3 -33
- package/dist/transform/comments/prettier/main/comments.js +11 -77
- package/dist/transform/comments/prettier/utils/get-last.js +0 -8
- package/dist/transform/mutations/AddComments.js +0 -9
- package/dist/transform/mutations/AddComments.js.flow +2 -2
- package/dist/transform/mutations/CloneCommentsTo.js +0 -9
- package/dist/transform/mutations/CloneCommentsTo.js.flow +1 -1
- package/dist/transform/mutations/InsertStatement.js +4 -16
- package/dist/transform/mutations/InsertStatement.js.flow +7 -6
- package/dist/transform/mutations/ModifyNodeInPlace.js +4 -17
- package/dist/transform/mutations/ModifyNodeInPlace.js.flow +2 -2
- package/dist/transform/mutations/RemoveComment.js +0 -10
- package/dist/transform/mutations/RemoveComment.js.flow +4 -4
- package/dist/transform/mutations/RemoveNode.js +1 -23
- package/dist/transform/mutations/RemoveNode.js.flow +9 -9
- package/dist/transform/mutations/RemoveStatement.js +0 -16
- package/dist/transform/mutations/RemoveStatement.js.flow +3 -3
- package/dist/transform/mutations/ReplaceNode.js +2 -16
- package/dist/transform/mutations/ReplaceNode.js.flow +6 -6
- package/dist/transform/mutations/ReplaceStatementWithMany.js +2 -14
- package/dist/transform/mutations/ReplaceStatementWithMany.js.flow +7 -8
- package/dist/transform/mutations/utils/getStatementParent.js +3 -19
- package/dist/transform/mutations/utils/getStatementParent.js.flow +5 -6
- package/dist/transform/mutations/utils/isValidModuleDeclarationParent.js +5 -20
- package/dist/transform/mutations/utils/isValidModuleDeclarationParent.js.flow +4 -3
- package/dist/transform/parse.js +6 -25
- package/dist/transform/print.js +9 -35
- package/dist/transform/print.js.flow +7 -15
- package/dist/transform/transform.js +0 -9
- package/dist/transform/transformAST.js +3 -31
- package/dist/traverse/NodeEventGenerator.js +2 -108
- package/dist/traverse/NodeEventGenerator.js.flow +13 -9
- package/dist/traverse/SafeEmitter.js +0 -19
- package/dist/traverse/SafeEmitter.js.flow +2 -2
- package/dist/traverse/esquery.js +0 -18
- package/dist/traverse/esquery.js.flow +10 -10
- package/dist/traverse/traverse.js +1 -22
- package/dist/traverse/traverse.js.flow +4 -4
- package/package.json +7 -12
|
@@ -1,11 +1,3 @@
|
|
|
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
|
-
* @format
|
|
8
|
-
*/
|
|
9
1
|
'use strict';
|
|
10
2
|
|
|
11
3
|
const stringWidth = require('string-width');
|
|
@@ -13,21 +5,10 @@ const stringWidth = require('string-width');
|
|
|
13
5
|
const getLast = require('../utils/get-last.js');
|
|
14
6
|
|
|
15
7
|
const notAsciiRegex = /[^\x20-\x7F]/;
|
|
16
|
-
/**
|
|
17
|
-
* @typedef {{backwards?: boolean}} SkipOptions
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* @param {string | RegExp} chars
|
|
22
|
-
* @returns {(text: string, index: number | false, opts?: SkipOptions) => number | false}
|
|
23
|
-
*/
|
|
24
8
|
|
|
25
9
|
function skip(chars) {
|
|
26
10
|
return (text, index, opts) => {
|
|
27
|
-
const backwards = opts && opts.backwards;
|
|
28
|
-
// to check for failures (did someone say monads?).
|
|
29
|
-
|
|
30
|
-
/* istanbul ignore next */
|
|
11
|
+
const backwards = opts && opts.backwards;
|
|
31
12
|
|
|
32
13
|
if (index === false) {
|
|
33
14
|
return false;
|
|
@@ -53,45 +34,19 @@ function skip(chars) {
|
|
|
53
34
|
}
|
|
54
35
|
|
|
55
36
|
if (cursor === -1 || cursor === length) {
|
|
56
|
-
// If we reached the beginning or end of the file, return the
|
|
57
|
-
// out-of-bounds cursor. It's up to the caller to handle this
|
|
58
|
-
// correctly. We don't want to indicate `false` though if it
|
|
59
|
-
// actually skipped valid characters.
|
|
60
37
|
return cursor;
|
|
61
38
|
}
|
|
62
39
|
|
|
63
40
|
return false;
|
|
64
41
|
};
|
|
65
42
|
}
|
|
66
|
-
/**
|
|
67
|
-
* @type {(text: string, index: number | false, opts?: SkipOptions) => number | false}
|
|
68
|
-
*/
|
|
69
|
-
|
|
70
43
|
|
|
71
44
|
const skipWhitespace = skip(/\s/);
|
|
72
|
-
/**
|
|
73
|
-
* @type {(text: string, index: number | false, opts?: SkipOptions) => number | false}
|
|
74
|
-
*/
|
|
75
|
-
|
|
76
45
|
const skipSpaces = skip(' \t');
|
|
77
|
-
/**
|
|
78
|
-
* @type {(text: string, index: number | false, opts?: SkipOptions) => number | false}
|
|
79
|
-
*/
|
|
80
|
-
|
|
81
46
|
const skipToLineEnd = skip(',; \t');
|
|
82
|
-
/**
|
|
83
|
-
* @type {(text: string, index: number | false, opts?: SkipOptions) => number | false}
|
|
84
|
-
*/
|
|
85
|
-
|
|
86
47
|
const skipEverythingButNewLine = skip(/[^\n\r]/);
|
|
87
|
-
/**
|
|
88
|
-
* @param {string} text
|
|
89
|
-
* @param {number | false} index
|
|
90
|
-
* @returns {number | false}
|
|
91
|
-
*/
|
|
92
48
|
|
|
93
49
|
function skipInlineComment(text, index) {
|
|
94
|
-
/* istanbul ignore next */
|
|
95
50
|
if (index === false) {
|
|
96
51
|
return false;
|
|
97
52
|
}
|
|
@@ -106,15 +61,8 @@ function skipInlineComment(text, index) {
|
|
|
106
61
|
|
|
107
62
|
return index;
|
|
108
63
|
}
|
|
109
|
-
/**
|
|
110
|
-
* @param {string} text
|
|
111
|
-
* @param {number | false} index
|
|
112
|
-
* @returns {number | false}
|
|
113
|
-
*/
|
|
114
|
-
|
|
115
64
|
|
|
116
65
|
function skipTrailingComment(text, index) {
|
|
117
|
-
/* istanbul ignore next */
|
|
118
66
|
if (index === false) {
|
|
119
67
|
return false;
|
|
120
68
|
}
|
|
@@ -124,17 +72,7 @@ function skipTrailingComment(text, index) {
|
|
|
124
72
|
}
|
|
125
73
|
|
|
126
74
|
return index;
|
|
127
|
-
}
|
|
128
|
-
// test \r\n in order and `skip` doesn't support ordering and we only
|
|
129
|
-
// want to skip one newline. It's simple to implement.
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* @param {string} text
|
|
133
|
-
* @param {number | false} index
|
|
134
|
-
* @param {SkipOptions=} opts
|
|
135
|
-
* @returns {number | false}
|
|
136
|
-
*/
|
|
137
|
-
|
|
75
|
+
}
|
|
138
76
|
|
|
139
77
|
function skipNewline(text, index, opts) {
|
|
140
78
|
const backwards = opts && opts.backwards;
|
|
@@ -146,9 +84,6 @@ function skipNewline(text, index, opts) {
|
|
|
146
84
|
const atIndex = text.charAt(index);
|
|
147
85
|
|
|
148
86
|
if (backwards) {
|
|
149
|
-
// We already replace `\r\n` with `\n` before parsing
|
|
150
|
-
|
|
151
|
-
/* istanbul ignore next */
|
|
152
87
|
if (text.charAt(index - 1) === '\r' && atIndex === '\n') {
|
|
153
88
|
return index - 2;
|
|
154
89
|
}
|
|
@@ -157,9 +92,6 @@ function skipNewline(text, index, opts) {
|
|
|
157
92
|
return index - 1;
|
|
158
93
|
}
|
|
159
94
|
} else {
|
|
160
|
-
// We already replace `\r\n` with `\n` before parsing
|
|
161
|
-
|
|
162
|
-
/* istanbul ignore next */
|
|
163
95
|
if (atIndex === '\r' && text.charAt(index + 1) === '\n') {
|
|
164
96
|
return index + 2;
|
|
165
97
|
}
|
|
@@ -171,26 +103,12 @@ function skipNewline(text, index, opts) {
|
|
|
171
103
|
|
|
172
104
|
return index;
|
|
173
105
|
}
|
|
174
|
-
/**
|
|
175
|
-
* @param {string} text
|
|
176
|
-
* @param {number} index
|
|
177
|
-
* @param {SkipOptions=} opts
|
|
178
|
-
* @returns {boolean}
|
|
179
|
-
*/
|
|
180
|
-
|
|
181
106
|
|
|
182
107
|
function hasNewline(text, index, opts = {}) {
|
|
183
108
|
const idx = skipSpaces(text, opts.backwards ? index - 1 : index, opts);
|
|
184
109
|
const idx2 = skipNewline(text, idx, opts);
|
|
185
110
|
return idx !== idx2;
|
|
186
111
|
}
|
|
187
|
-
/**
|
|
188
|
-
* @param {string} text
|
|
189
|
-
* @param {number} start
|
|
190
|
-
* @param {number} end
|
|
191
|
-
* @returns {boolean}
|
|
192
|
-
*/
|
|
193
|
-
|
|
194
112
|
|
|
195
113
|
function hasNewlineInRange(text, start, end) {
|
|
196
114
|
for (let i = start; i < end; ++i) {
|
|
@@ -201,22 +119,12 @@ function hasNewlineInRange(text, start, end) {
|
|
|
201
119
|
|
|
202
120
|
return false;
|
|
203
121
|
}
|
|
204
|
-
/**
|
|
205
|
-
* @param {string} text
|
|
206
|
-
* @param {number} index
|
|
207
|
-
* @returns {boolean}
|
|
208
|
-
*/
|
|
209
|
-
|
|
210
122
|
|
|
211
123
|
function isNextLineEmptyAfterIndex(text, index) {
|
|
212
|
-
/** @type {number | false} */
|
|
213
124
|
let oldIdx = null;
|
|
214
|
-
/** @type {number | false} */
|
|
215
|
-
|
|
216
125
|
let idx = index;
|
|
217
126
|
|
|
218
127
|
while (idx !== oldIdx) {
|
|
219
|
-
// We need to skip all the potential trailing inline comments
|
|
220
128
|
oldIdx = idx;
|
|
221
129
|
idx = skipToLineEnd(text, idx);
|
|
222
130
|
idx = skipInlineComment(text, idx);
|
|
@@ -227,18 +135,9 @@ function isNextLineEmptyAfterIndex(text, index) {
|
|
|
227
135
|
idx = skipNewline(text, idx);
|
|
228
136
|
return idx !== false && hasNewline(text, idx);
|
|
229
137
|
}
|
|
230
|
-
/**
|
|
231
|
-
* @param {string} text
|
|
232
|
-
* @param {number} idx
|
|
233
|
-
* @returns {number | false}
|
|
234
|
-
*/
|
|
235
|
-
|
|
236
138
|
|
|
237
139
|
function getNextNonSpaceNonCommentCharacterIndexWithStartIndex(text, idx) {
|
|
238
|
-
/** @type {number | false} */
|
|
239
140
|
let oldIdx = null;
|
|
240
|
-
/** @type {number | false} */
|
|
241
|
-
|
|
242
141
|
let nextIdx = idx;
|
|
243
142
|
|
|
244
143
|
while (nextIdx !== oldIdx) {
|
|
@@ -251,42 +150,19 @@ function getNextNonSpaceNonCommentCharacterIndexWithStartIndex(text, idx) {
|
|
|
251
150
|
|
|
252
151
|
return nextIdx;
|
|
253
152
|
}
|
|
254
|
-
/**
|
|
255
|
-
* @template N
|
|
256
|
-
* @param {string} text
|
|
257
|
-
* @param {N} node
|
|
258
|
-
* @param {(node: N) => number} locEnd
|
|
259
|
-
* @returns {number | false}
|
|
260
|
-
*/
|
|
261
|
-
|
|
262
153
|
|
|
263
154
|
function getNextNonSpaceNonCommentCharacterIndex(text, node, locEnd) {
|
|
264
155
|
return getNextNonSpaceNonCommentCharacterIndexWithStartIndex(text, locEnd(node));
|
|
265
156
|
}
|
|
266
|
-
/**
|
|
267
|
-
* @template N
|
|
268
|
-
* @param {string} text
|
|
269
|
-
* @param {N} node
|
|
270
|
-
* @param {(node: N) => number} locEnd
|
|
271
|
-
* @returns {string}
|
|
272
|
-
*/
|
|
273
|
-
|
|
274
157
|
|
|
275
158
|
function getNextNonSpaceNonCommentCharacter(text, node, locEnd) {
|
|
276
|
-
return text.charAt(
|
|
277
|
-
getNextNonSpaceNonCommentCharacterIndex(text, node, locEnd));
|
|
159
|
+
return text.charAt(getNextNonSpaceNonCommentCharacterIndex(text, node, locEnd));
|
|
278
160
|
}
|
|
279
|
-
/**
|
|
280
|
-
* @param {string} text
|
|
281
|
-
* @returns {number}
|
|
282
|
-
*/
|
|
283
|
-
|
|
284
161
|
|
|
285
162
|
function getStringWidth(text) {
|
|
286
163
|
if (!text) {
|
|
287
164
|
return 0;
|
|
288
|
-
}
|
|
289
|
-
|
|
165
|
+
}
|
|
290
166
|
|
|
291
167
|
if (!notAsciiRegex.test(text)) {
|
|
292
168
|
return text.length;
|
|
@@ -324,11 +200,6 @@ function addTrailingComment(node, comment) {
|
|
|
324
200
|
comment.trailing = true;
|
|
325
201
|
addCommentHelper(node, comment);
|
|
326
202
|
}
|
|
327
|
-
/**
|
|
328
|
-
* @param {any} object
|
|
329
|
-
* @returns {object is Array<any>}
|
|
330
|
-
*/
|
|
331
|
-
|
|
332
203
|
|
|
333
204
|
function isNonEmptyArray(object) {
|
|
334
205
|
return Array.isArray(object) && object.length > 0;
|
|
@@ -1,11 +1,3 @@
|
|
|
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
|
-
* @format
|
|
8
|
-
*/
|
|
9
1
|
'use strict';
|
|
10
2
|
|
|
11
3
|
const {
|
|
@@ -36,57 +28,20 @@ const {
|
|
|
36
28
|
locStart,
|
|
37
29
|
locEnd
|
|
38
30
|
} = require('./loc.js');
|
|
39
|
-
/**
|
|
40
|
-
* @typedef {import("./types/estree").Node} Node
|
|
41
|
-
* @typedef {import("./types/estree").Comment} Comment
|
|
42
|
-
* @typedef {import("../common/ast-path")} AstPath
|
|
43
|
-
*
|
|
44
|
-
* @typedef {Object} CommentContext
|
|
45
|
-
* @property {Comment} comment
|
|
46
|
-
* @property {Node} precedingNode
|
|
47
|
-
* @property {Node} enclosingNode
|
|
48
|
-
* @property {Node} followingNode
|
|
49
|
-
* @property {string} text
|
|
50
|
-
* @property {any} options
|
|
51
|
-
* @property {Node} ast
|
|
52
|
-
* @property {boolean} isLastComment
|
|
53
|
-
*/
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* @param {CommentContext} context
|
|
57
|
-
* @returns {boolean}
|
|
58
|
-
*/
|
|
59
|
-
|
|
60
31
|
|
|
61
32
|
function handleOwnLineComment(context) {
|
|
62
33
|
return [handleIgnoreComments, handleLastFunctionArgComments, handleMemberExpressionComments, handleIfStatementComments, handleWhileComments, handleTryStatementComments, handleClassComments, handleImportSpecifierComments, handleForComments, handleUnionTypeComments, handleMatchOrPatternComments, handleOnlyComments, handleImportDeclarationComments, handleAssignmentPatternComments, handleMethodNameComments, handleLabeledStatementComments].some(fn => fn(context));
|
|
63
34
|
}
|
|
64
|
-
/**
|
|
65
|
-
* @param {CommentContext} context
|
|
66
|
-
* @returns {boolean}
|
|
67
|
-
*/
|
|
68
|
-
|
|
69
35
|
|
|
70
36
|
function handleEndOfLineComment(context) {
|
|
71
37
|
return [handleClosureTypeCastComments, handleLastFunctionArgComments, handleConditionalExpressionComments, handleImportSpecifierComments, handleIfStatementComments, handleWhileComments, handleTryStatementComments, handleClassComments, handleLabeledStatementComments, handleCallExpressionComments, handlePropertyComments, handleOnlyComments, handleTypeAliasComments, handleVariableDeclaratorComments].some(fn => fn(context));
|
|
72
38
|
}
|
|
73
|
-
/**
|
|
74
|
-
* @param {CommentContext} context
|
|
75
|
-
* @returns {boolean}
|
|
76
|
-
*/
|
|
77
|
-
|
|
78
39
|
|
|
79
40
|
function handleRemainingComment(context) {
|
|
80
41
|
return [handleIgnoreComments, handleIfStatementComments, handleWhileComments, handleObjectPropertyAssignment, handleCommentInEmptyParens, handleMethodNameComments, handleOnlyComments, handleCommentAfterArrowParams, handleFunctionNameComments, handleTSMappedTypeComments, handleBreakAndContinueStatementComments, handleTSFunctionTrailingComments].some(fn => fn(context));
|
|
81
42
|
}
|
|
82
|
-
/**
|
|
83
|
-
* @param {Node} node
|
|
84
|
-
* @returns {void}
|
|
85
|
-
*/
|
|
86
|
-
|
|
87
43
|
|
|
88
44
|
function addBlockStatementFirstComment(node, comment) {
|
|
89
|
-
// @ts-expect-error
|
|
90
45
|
const firstNonEmptyNode = (node.body || node.properties).find(({
|
|
91
46
|
type
|
|
92
47
|
}) => type !== 'EmptyStatement');
|
|
@@ -97,11 +52,6 @@ function addBlockStatementFirstComment(node, comment) {
|
|
|
97
52
|
addDanglingComment(node, comment);
|
|
98
53
|
}
|
|
99
54
|
}
|
|
100
|
-
/**
|
|
101
|
-
* @param {Node} node
|
|
102
|
-
* @returns {void}
|
|
103
|
-
*/
|
|
104
|
-
|
|
105
55
|
|
|
106
56
|
function addBlockOrNotComment(node, comment) {
|
|
107
57
|
if (node.type === 'BlockStatement') {
|
|
@@ -121,23 +71,7 @@ function handleClosureTypeCastComments({
|
|
|
121
71
|
}
|
|
122
72
|
|
|
123
73
|
return false;
|
|
124
|
-
}
|
|
125
|
-
//
|
|
126
|
-
// if (1) { ... }
|
|
127
|
-
// // comment
|
|
128
|
-
// else { ... }
|
|
129
|
-
//
|
|
130
|
-
// They are being attached as leading comments of the BlockExpression which
|
|
131
|
-
// is not well printed. What we want is to instead move the comment inside
|
|
132
|
-
// of the block and make it leadingComment of the first element of the block
|
|
133
|
-
// or dangling comment of the block if there is nothing inside
|
|
134
|
-
//
|
|
135
|
-
// if (1) { ... }
|
|
136
|
-
// else {
|
|
137
|
-
// // comment
|
|
138
|
-
// ...
|
|
139
|
-
// }
|
|
140
|
-
|
|
74
|
+
}
|
|
141
75
|
|
|
142
76
|
function handleIfStatementComments({
|
|
143
77
|
comment,
|
|
@@ -148,22 +82,14 @@ function handleIfStatementComments({
|
|
|
148
82
|
}) {
|
|
149
83
|
if (!enclosingNode || enclosingNode.type !== 'IfStatement' || !followingNode) {
|
|
150
84
|
return false;
|
|
151
|
-
}
|
|
152
|
-
// if the comment is positioned before the condition parenthesis:
|
|
153
|
-
// if (a /* comment */) {}
|
|
154
|
-
// The only workaround I found is to look at the next character to see if
|
|
155
|
-
// it is a ).
|
|
156
|
-
|
|
85
|
+
}
|
|
157
86
|
|
|
158
87
|
const nextCharacter = getNextNonSpaceNonCommentCharacter(text, comment, locEnd);
|
|
159
88
|
|
|
160
89
|
if (nextCharacter === ')') {
|
|
161
90
|
addTrailingComment(precedingNode, comment);
|
|
162
91
|
return true;
|
|
163
|
-
}
|
|
164
|
-
// - treat as trailing comments of the consequent, if it's a BlockStatement
|
|
165
|
-
// - treat as a dangling comment otherwise
|
|
166
|
-
|
|
92
|
+
}
|
|
167
93
|
|
|
168
94
|
if (precedingNode === enclosingNode.consequent && followingNode === enclosingNode.alternate) {
|
|
169
95
|
if (precedingNode.type === 'BlockStatement') {
|
|
@@ -183,12 +109,7 @@ function handleIfStatementComments({
|
|
|
183
109
|
if (followingNode.type === 'IfStatement') {
|
|
184
110
|
addBlockOrNotComment(followingNode.consequent, comment);
|
|
185
111
|
return true;
|
|
186
|
-
}
|
|
187
|
-
// before the consequent without brackets on, such as
|
|
188
|
-
// if (a) /* comment */ true,
|
|
189
|
-
// we look at the next character to see if the following node
|
|
190
|
-
// is the consequent for the if statement
|
|
191
|
-
|
|
112
|
+
}
|
|
192
113
|
|
|
193
114
|
if (enclosingNode.consequent === followingNode) {
|
|
194
115
|
addLeadingComment(followingNode, comment);
|
|
@@ -207,12 +128,7 @@ function handleWhileComments({
|
|
|
207
128
|
}) {
|
|
208
129
|
if (!enclosingNode || enclosingNode.type !== 'WhileStatement' || !followingNode) {
|
|
209
130
|
return false;
|
|
210
|
-
}
|
|
211
|
-
// if the comment is positioned before the condition parenthesis:
|
|
212
|
-
// while (a /* comment */) {}
|
|
213
|
-
// The only workaround I found is to look at the next character to see if
|
|
214
|
-
// it is a ).
|
|
215
|
-
|
|
131
|
+
}
|
|
216
132
|
|
|
217
133
|
const nextCharacter = getNextNonSpaceNonCommentCharacter(text, comment, locEnd);
|
|
218
134
|
|
|
@@ -232,8 +148,7 @@ function handleWhileComments({
|
|
|
232
148
|
}
|
|
233
149
|
|
|
234
150
|
return false;
|
|
235
|
-
}
|
|
236
|
-
|
|
151
|
+
}
|
|
237
152
|
|
|
238
153
|
function handleTryStatementComments({
|
|
239
154
|
comment,
|
|
@@ -326,9 +241,7 @@ function handleClassComments({
|
|
|
326
241
|
if (enclosingNode.body && followingNode === enclosingNode.body) {
|
|
327
242
|
addBlockStatementFirstComment(enclosingNode.body, comment);
|
|
328
243
|
return true;
|
|
329
|
-
}
|
|
330
|
-
// avoid printing the comment after the keyword.
|
|
331
|
-
|
|
244
|
+
}
|
|
332
245
|
|
|
333
246
|
if (followingNode) {
|
|
334
247
|
for (const prop of ['implements', 'extends', 'mixins']) {
|
|
@@ -354,18 +267,10 @@ function handleMethodNameComments({
|
|
|
354
267
|
enclosingNode,
|
|
355
268
|
text
|
|
356
269
|
}) {
|
|
357
|
-
|
|
358
|
-
// after a method name:
|
|
359
|
-
// obj = { fn /*comment*/() {} };
|
|
360
|
-
if (enclosingNode && precedingNode && ( // "MethodDefinition" is handled in getCommentChildNodes
|
|
361
|
-
enclosingNode.type === 'Property' || enclosingNode.type === 'TSDeclareMethod' || enclosingNode.type === 'TSAbstractMethodDefinition') && precedingNode.type === 'Identifier' && enclosingNode.key === precedingNode && // special Property case: { key: /*comment*/(value) };
|
|
362
|
-
// comment should be attached to value instead of key
|
|
363
|
-
getNextNonSpaceNonCommentCharacter(text, precedingNode, locEnd) !== ':') {
|
|
270
|
+
if (enclosingNode && precedingNode && (enclosingNode.type === 'Property' || enclosingNode.type === 'TSDeclareMethod' || enclosingNode.type === 'TSAbstractMethodDefinition') && precedingNode.type === 'Identifier' && enclosingNode.key === precedingNode && getNextNonSpaceNonCommentCharacter(text, precedingNode, locEnd) !== ':') {
|
|
364
271
|
addTrailingComment(precedingNode, comment);
|
|
365
272
|
return true;
|
|
366
|
-
}
|
|
367
|
-
// on the decorator node instead of the method node
|
|
368
|
-
|
|
273
|
+
}
|
|
369
274
|
|
|
370
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')) {
|
|
371
276
|
addTrailingComment(precedingNode, comment);
|
|
@@ -419,9 +324,7 @@ function handleCommentInEmptyParens({
|
|
|
419
324
|
}) {
|
|
420
325
|
if (getNextNonSpaceNonCommentCharacter(text, comment, locEnd) !== ')') {
|
|
421
326
|
return false;
|
|
422
|
-
}
|
|
423
|
-
// i.e. a function without any argument.
|
|
424
|
-
|
|
327
|
+
}
|
|
425
328
|
|
|
426
329
|
if (enclosingNode && (isRealFunctionLikeNode(enclosingNode) && getFunctionParameters(enclosingNode).length === 0 || isCallLikeExpression(enclosingNode) && getCallArguments(enclosingNode).length === 0)) {
|
|
427
330
|
addDanglingComment(enclosingNode, comment);
|
|
@@ -443,12 +346,10 @@ function handleLastFunctionArgComments({
|
|
|
443
346
|
followingNode,
|
|
444
347
|
text
|
|
445
348
|
}) {
|
|
446
|
-
// Flow function type definitions
|
|
447
349
|
if (precedingNode && precedingNode.type === 'FunctionTypeParam' && enclosingNode && enclosingNode.type === 'FunctionTypeAnnotation' && followingNode && followingNode.type !== 'FunctionTypeParam') {
|
|
448
350
|
addTrailingComment(precedingNode, comment);
|
|
449
351
|
return true;
|
|
450
|
-
}
|
|
451
|
-
|
|
352
|
+
}
|
|
452
353
|
|
|
453
354
|
if (precedingNode && (precedingNode.type === 'Identifier' || precedingNode.type === 'AssignmentPattern') && enclosingNode && isRealFunctionLikeNode(enclosingNode) && getNextNonSpaceNonCommentCharacter(text, comment, locEnd) === ')') {
|
|
454
355
|
addTrailingComment(precedingNode, comment);
|
|
@@ -600,7 +501,6 @@ function handleOnlyComments({
|
|
|
600
501
|
ast,
|
|
601
502
|
isLastComment
|
|
602
503
|
}) {
|
|
603
|
-
// With Flow the enclosingNode is undefined so use the AST instead.
|
|
604
504
|
if (ast && ast.body && ast.body.length === 0) {
|
|
605
505
|
if (isLastComment) {
|
|
606
506
|
addDanglingComment(ast, comment);
|
|
@@ -740,44 +640,19 @@ function handleTSMappedTypeComments({
|
|
|
740
640
|
|
|
741
641
|
return false;
|
|
742
642
|
}
|
|
743
|
-
/**
|
|
744
|
-
* @param {Node} node
|
|
745
|
-
* @returns {boolean}
|
|
746
|
-
*/
|
|
747
|
-
|
|
748
643
|
|
|
749
644
|
function isRealFunctionLikeNode(node) {
|
|
750
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';
|
|
751
646
|
}
|
|
752
|
-
/**
|
|
753
|
-
* @param {any} node
|
|
754
|
-
* @returns {Node[] | void}
|
|
755
|
-
*/
|
|
756
|
-
|
|
757
647
|
|
|
758
648
|
function getCommentChildNodes(node, options) {
|
|
759
|
-
// Prevent attaching comments to FunctionExpression in this case:
|
|
760
|
-
// class Foo {
|
|
761
|
-
// bar() // comment
|
|
762
|
-
// {
|
|
763
|
-
// baz();
|
|
764
|
-
// }
|
|
765
|
-
// }
|
|
766
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) {
|
|
767
650
|
return [...(node.decorators || []), node.key, node.value.body];
|
|
768
651
|
}
|
|
769
652
|
}
|
|
770
|
-
/**
|
|
771
|
-
* @param {Comment} comment
|
|
772
|
-
* @returns {boolean}
|
|
773
|
-
*/
|
|
774
|
-
|
|
775
653
|
|
|
776
654
|
function isTypeCastComment(comment) {
|
|
777
|
-
return isBlockComment(comment) && comment.value[0] === '*' &&
|
|
778
|
-
// Closure Compiler accepts types in parens and even without any delimiters at all.
|
|
779
|
-
// That's why we just search for "@type".
|
|
780
|
-
/@type\b/.test(comment.value);
|
|
655
|
+
return isBlockComment(comment) && comment.value[0] === '*' && /@type\b/.test(comment.value);
|
|
781
656
|
}
|
|
782
657
|
|
|
783
658
|
module.exports = {
|
|
@@ -1,25 +1,13 @@
|
|
|
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
|
-
* @format
|
|
8
|
-
*/
|
|
9
1
|
'use strict';
|
|
10
2
|
|
|
11
3
|
const {
|
|
12
4
|
isNonEmptyArray
|
|
13
5
|
} = require('../common/util.js');
|
|
14
|
-
/**
|
|
15
|
-
* @typedef {import("./types/estree").Node} Node
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
6
|
|
|
19
7
|
function locStart(node, opts) {
|
|
20
8
|
const {
|
|
21
9
|
ignoreDecorators
|
|
22
|
-
} = opts || {};
|
|
10
|
+
} = opts || {};
|
|
23
11
|
|
|
24
12
|
if (!ignoreDecorators) {
|
|
25
13
|
const decorators = node.declaration && node.declaration.decorators || node.decorators;
|
|
@@ -1,11 +1,3 @@
|
|
|
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
|
-
* @format
|
|
8
|
-
*/
|
|
9
1
|
'use strict';
|
|
10
2
|
|
|
11
3
|
const handleComments = require('./comments.js');
|
|
@@ -16,8 +8,7 @@ const {
|
|
|
16
8
|
} = require('./utils.js');
|
|
17
9
|
|
|
18
10
|
function canAttachComment(node) {
|
|
19
|
-
return node.type && !isBlockComment(node) && !isLineComment(node) && node.type !== 'EmptyStatement' && node.type !== 'TemplateElement' && node.type !== 'Import' &&
|
|
20
|
-
node.type !== 'TSEmptyBodyFunctionExpression';
|
|
11
|
+
return node.type && !isBlockComment(node) && !isLineComment(node) && node.type !== 'EmptyStatement' && node.type !== 'TemplateElement' && node.type !== 'Import' && node.type !== 'TSEmptyBodyFunctionExpression';
|
|
21
12
|
}
|
|
22
13
|
|
|
23
14
|
module.exports = {
|
|
@@ -1,45 +1,16 @@
|
|
|
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
|
-
* @format
|
|
8
|
-
*/
|
|
9
1
|
'use strict';
|
|
10
|
-
/**
|
|
11
|
-
* @param {Comment} comment
|
|
12
|
-
* @returns {boolean}
|
|
13
|
-
*/
|
|
14
2
|
|
|
15
3
|
function isBlockComment(comment) {
|
|
16
|
-
return comment.type === 'Block' || comment.type === 'CommentBlock' ||
|
|
17
|
-
comment.type === 'MultiLine';
|
|
4
|
+
return comment.type === 'Block' || comment.type === 'CommentBlock' || comment.type === 'MultiLine';
|
|
18
5
|
}
|
|
19
|
-
/**
|
|
20
|
-
* @param {Comment} comment
|
|
21
|
-
* @returns {boolean}
|
|
22
|
-
*/
|
|
23
|
-
|
|
24
6
|
|
|
25
7
|
function isLineComment(comment) {
|
|
26
|
-
return comment.type === 'Line' || comment.type === 'CommentLine' ||
|
|
27
|
-
comment.type === 'SingleLine' || comment.type === 'HashbangComment' || comment.type === 'HTMLOpen' || comment.type === 'HTMLClose';
|
|
8
|
+
return comment.type === 'Line' || comment.type === 'CommentLine' || comment.type === 'SingleLine' || comment.type === 'HashbangComment' || comment.type === 'HTMLOpen' || comment.type === 'HTMLClose';
|
|
28
9
|
}
|
|
29
|
-
/**
|
|
30
|
-
* @param {Node} node
|
|
31
|
-
* @returns {boolean}
|
|
32
|
-
*/
|
|
33
|
-
|
|
34
10
|
|
|
35
11
|
function isCallExpression(node) {
|
|
36
12
|
return node && (node.type === 'CallExpression' || node.type === 'OptionalCallExpression');
|
|
37
13
|
}
|
|
38
|
-
/**
|
|
39
|
-
* @param {Node} node
|
|
40
|
-
* @returns {boolean}
|
|
41
|
-
*/
|
|
42
|
-
|
|
43
14
|
|
|
44
15
|
function isMemberExpression(node) {
|
|
45
16
|
return node && (node.type === 'MemberExpression' || node.type === 'OptionalMemberExpression');
|
|
@@ -56,8 +27,7 @@ function getFunctionParameters(node) {
|
|
|
56
27
|
|
|
57
28
|
if (node.this) {
|
|
58
29
|
parameters.push(node.this);
|
|
59
|
-
}
|
|
60
|
-
|
|
30
|
+
}
|
|
61
31
|
|
|
62
32
|
if (Array.isArray(node.parameters)) {
|
|
63
33
|
parameters.push(...node.parameters);
|