hermes-transform 0.5.0 → 0.6.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/LICENSE +1 -1
- package/dist/detachedNode.js +25 -49
- package/dist/detachedNode.js.flow +3 -3
- package/dist/generated/TransformCloneSignatures.js.flow +1761 -9
- package/dist/generated/TransformReplaceSignatures.js.flow +936 -774
- package/dist/generated/node-types.js +1117 -1132
- package/dist/generated/node-types.js.flow +26 -1
- package/dist/generated/special-case-node-types.js +106 -85
- package/dist/generated/special-case-node-types.js.flow +28 -2
- package/dist/getVisitorKeys.js +4 -6
- package/dist/getVisitorKeys.js.flow +1 -1
- package/dist/index.js +13 -18
- package/dist/index.js.flow +7 -2
- package/dist/transform/Errors.js +8 -116
- package/dist/transform/Errors.js.flow +1 -1
- package/dist/transform/MutationContext.js +54 -67
- package/dist/transform/MutationContext.js.flow +1 -1
- package/dist/transform/TransformContext.js +106 -54
- package/dist/transform/TransformContext.js.flow +255 -121
- package/dist/transform/comments/comments.js +25 -25
- package/dist/transform/comments/comments.js.flow +1 -1
- package/dist/transform/comments/prettier/common/util.js +46 -47
- package/dist/transform/comments/prettier/common/util.js.flow +1 -1
- package/dist/transform/comments/prettier/language-js/comments.js +199 -216
- package/dist/transform/comments/prettier/language-js/comments.js.flow +1 -1
- package/dist/transform/comments/prettier/language-js/loc.js +10 -9
- package/dist/transform/comments/prettier/language-js/loc.js.flow +1 -1
- package/dist/transform/comments/prettier/language-js/printer-estree.js +7 -6
- package/dist/transform/comments/prettier/language-js/printer-estree.js.flow +1 -1
- package/dist/transform/comments/prettier/language-js/utils.js +18 -30
- package/dist/transform/comments/prettier/language-js/utils.js.flow +1 -1
- package/dist/transform/comments/prettier/main/comments.js +225 -298
- package/dist/transform/comments/prettier/main/comments.js.flow +1 -1
- package/dist/transform/comments/prettier/utils/get-last.js +2 -4
- package/dist/transform/comments/prettier/utils/get-last.js.flow +1 -1
- package/dist/transform/getTransformedAST.js +85 -101
- package/dist/transform/getTransformedAST.js.flow +7 -2
- package/dist/transform/mutations/AddLeadingComments.js +15 -22
- package/dist/transform/mutations/AddLeadingComments.js.flow +1 -1
- package/dist/transform/mutations/AddTrailingComments.js +15 -22
- package/dist/transform/mutations/AddTrailingComments.js.flow +1 -1
- package/dist/transform/mutations/CloneCommentsTo.js +16 -23
- package/dist/transform/mutations/CloneCommentsTo.js.flow +1 -1
- package/dist/transform/mutations/InsertStatement.js +27 -30
- package/dist/transform/mutations/InsertStatement.js.flow +1 -1
- package/dist/transform/mutations/RemoveComment.js +25 -43
- package/dist/transform/mutations/RemoveComment.js.flow +1 -1
- package/dist/transform/mutations/RemoveNode.js +185 -0
- package/dist/transform/mutations/RemoveNode.js.flow +279 -0
- package/dist/transform/mutations/RemoveStatement.js +7 -9
- package/dist/transform/mutations/RemoveStatement.js.flow +1 -1
- package/dist/transform/mutations/ReplaceNode.js +36 -44
- package/dist/transform/mutations/ReplaceNode.js.flow +1 -1
- package/dist/transform/mutations/ReplaceStatementWithMany.js +11 -13
- package/dist/transform/mutations/ReplaceStatementWithMany.js.flow +1 -1
- package/dist/transform/mutations/utils/arrayUtils.js +2 -14
- package/dist/transform/mutations/utils/arrayUtils.js.flow +1 -1
- package/dist/transform/mutations/utils/getStatementParent.js +23 -28
- package/dist/transform/mutations/utils/getStatementParent.js.flow +2 -2
- package/dist/transform/mutations/utils/isValidModuleDeclarationParent.js +7 -24
- package/dist/transform/mutations/utils/isValidModuleDeclarationParent.js.flow +1 -1
- package/dist/transform/transform.js +20 -24
- package/dist/transform/transform.js.flow +12 -8
- package/dist/traverse/NodeEventGenerator.js +91 -165
- package/dist/traverse/NodeEventGenerator.js.flow +1 -1
- package/dist/traverse/SafeEmitter.js +20 -38
- package/dist/traverse/SafeEmitter.js.flow +1 -1
- package/dist/traverse/SimpleTraverser.js +67 -98
- package/dist/traverse/SimpleTraverser.js.flow +4 -1
- package/dist/traverse/esquery.js +6 -6
- package/dist/traverse/esquery.js.flow +1 -1
- package/dist/traverse/traverse.js +39 -51
- package/dist/traverse/traverse.js.flow +1 -1
- package/package.json +4 -3
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright (c)
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
3
|
*
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
@@ -8,13 +8,11 @@
|
|
|
8
8
|
*/
|
|
9
9
|
'use strict';
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
const stringWidth = require('string-width');
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
const getLast = require('../utils/get-last.js');
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
var notAsciiRegex = /[^\x20-\x7F]/;
|
|
15
|
+
const notAsciiRegex = /[^\x20-\x7F]/;
|
|
18
16
|
/**
|
|
19
17
|
* @typedef {{backwards?: boolean}} SkipOptions
|
|
20
18
|
*/
|
|
@@ -25,8 +23,8 @@ var notAsciiRegex = /[^\x20-\x7F]/;
|
|
|
25
23
|
*/
|
|
26
24
|
|
|
27
25
|
function skip(chars) {
|
|
28
|
-
return
|
|
29
|
-
|
|
26
|
+
return (text, index, opts) => {
|
|
27
|
+
const backwards = opts && opts.backwards; // Allow `skip` functions to be threaded together without having
|
|
30
28
|
// to check for failures (did someone say monads?).
|
|
31
29
|
|
|
32
30
|
/* istanbul ignore next */
|
|
@@ -35,11 +33,13 @@ function skip(chars) {
|
|
|
35
33
|
return false;
|
|
36
34
|
}
|
|
37
35
|
|
|
38
|
-
|
|
39
|
-
|
|
36
|
+
const {
|
|
37
|
+
length
|
|
38
|
+
} = text;
|
|
39
|
+
let cursor = index;
|
|
40
40
|
|
|
41
41
|
while (cursor >= 0 && cursor < length) {
|
|
42
|
-
|
|
42
|
+
const c = text.charAt(cursor);
|
|
43
43
|
|
|
44
44
|
if (chars instanceof RegExp) {
|
|
45
45
|
if (!chars.test(c)) {
|
|
@@ -68,22 +68,22 @@ function skip(chars) {
|
|
|
68
68
|
*/
|
|
69
69
|
|
|
70
70
|
|
|
71
|
-
|
|
71
|
+
const skipWhitespace = skip(/\s/);
|
|
72
72
|
/**
|
|
73
73
|
* @type {(text: string, index: number | false, opts?: SkipOptions) => number | false}
|
|
74
74
|
*/
|
|
75
75
|
|
|
76
|
-
|
|
76
|
+
const skipSpaces = skip(' \t');
|
|
77
77
|
/**
|
|
78
78
|
* @type {(text: string, index: number | false, opts?: SkipOptions) => number | false}
|
|
79
79
|
*/
|
|
80
80
|
|
|
81
|
-
|
|
81
|
+
const skipToLineEnd = skip(',; \t');
|
|
82
82
|
/**
|
|
83
83
|
* @type {(text: string, index: number | false, opts?: SkipOptions) => number | false}
|
|
84
84
|
*/
|
|
85
85
|
|
|
86
|
-
|
|
86
|
+
const skipEverythingButNewLine = skip(/[^\n\r]/);
|
|
87
87
|
/**
|
|
88
88
|
* @param {string} text
|
|
89
89
|
* @param {number | false} index
|
|
@@ -97,7 +97,7 @@ function skipInlineComment(text, index) {
|
|
|
97
97
|
}
|
|
98
98
|
|
|
99
99
|
if (text.charAt(index) === '/' && text.charAt(index + 1) === '*') {
|
|
100
|
-
for (
|
|
100
|
+
for (let i = index + 2; i < text.length; ++i) {
|
|
101
101
|
if (text.charAt(i) === '*' && text.charAt(i + 1) === '/') {
|
|
102
102
|
return i + 2;
|
|
103
103
|
}
|
|
@@ -137,13 +137,13 @@ function skipTrailingComment(text, index) {
|
|
|
137
137
|
|
|
138
138
|
|
|
139
139
|
function skipNewline(text, index, opts) {
|
|
140
|
-
|
|
140
|
+
const backwards = opts && opts.backwards;
|
|
141
141
|
|
|
142
142
|
if (index === false) {
|
|
143
143
|
return false;
|
|
144
144
|
}
|
|
145
145
|
|
|
146
|
-
|
|
146
|
+
const atIndex = text.charAt(index);
|
|
147
147
|
|
|
148
148
|
if (backwards) {
|
|
149
149
|
// We already replace `\r\n` with `\n` before parsing
|
|
@@ -153,7 +153,7 @@ function skipNewline(text, index, opts) {
|
|
|
153
153
|
return index - 2;
|
|
154
154
|
}
|
|
155
155
|
|
|
156
|
-
if (atIndex === '\n' || atIndex === '\r' || atIndex ===
|
|
156
|
+
if (atIndex === '\n' || atIndex === '\r' || atIndex === '\u2028' || atIndex === '\u2029') {
|
|
157
157
|
return index - 1;
|
|
158
158
|
}
|
|
159
159
|
} else {
|
|
@@ -164,7 +164,7 @@ function skipNewline(text, index, opts) {
|
|
|
164
164
|
return index + 2;
|
|
165
165
|
}
|
|
166
166
|
|
|
167
|
-
if (atIndex === '\n' || atIndex === '\r' || atIndex ===
|
|
167
|
+
if (atIndex === '\n' || atIndex === '\r' || atIndex === '\u2028' || atIndex === '\u2029') {
|
|
168
168
|
return index + 1;
|
|
169
169
|
}
|
|
170
170
|
}
|
|
@@ -179,10 +179,9 @@ function skipNewline(text, index, opts) {
|
|
|
179
179
|
*/
|
|
180
180
|
|
|
181
181
|
|
|
182
|
-
function hasNewline(text, index) {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
var idx2 = skipNewline(text, idx, opts);
|
|
182
|
+
function hasNewline(text, index, opts = {}) {
|
|
183
|
+
const idx = skipSpaces(text, opts.backwards ? index - 1 : index, opts);
|
|
184
|
+
const idx2 = skipNewline(text, idx, opts);
|
|
186
185
|
return idx !== idx2;
|
|
187
186
|
}
|
|
188
187
|
/**
|
|
@@ -194,7 +193,7 @@ function hasNewline(text, index) {
|
|
|
194
193
|
|
|
195
194
|
|
|
196
195
|
function hasNewlineInRange(text, start, end) {
|
|
197
|
-
for (
|
|
196
|
+
for (let i = start; i < end; ++i) {
|
|
198
197
|
if (text.charAt(i) === '\n') {
|
|
199
198
|
return true;
|
|
200
199
|
}
|
|
@@ -211,10 +210,10 @@ function hasNewlineInRange(text, start, end) {
|
|
|
211
210
|
|
|
212
211
|
function isNextLineEmptyAfterIndex(text, index) {
|
|
213
212
|
/** @type {number | false} */
|
|
214
|
-
|
|
213
|
+
let oldIdx = null;
|
|
215
214
|
/** @type {number | false} */
|
|
216
215
|
|
|
217
|
-
|
|
216
|
+
let idx = index;
|
|
218
217
|
|
|
219
218
|
while (idx !== oldIdx) {
|
|
220
219
|
// We need to skip all the potential trailing inline comments
|
|
@@ -237,10 +236,10 @@ function isNextLineEmptyAfterIndex(text, index) {
|
|
|
237
236
|
|
|
238
237
|
function getNextNonSpaceNonCommentCharacterIndexWithStartIndex(text, idx) {
|
|
239
238
|
/** @type {number | false} */
|
|
240
|
-
|
|
239
|
+
let oldIdx = null;
|
|
241
240
|
/** @type {number | false} */
|
|
242
241
|
|
|
243
|
-
|
|
242
|
+
let nextIdx = idx;
|
|
244
243
|
|
|
245
244
|
while (nextIdx !== oldIdx) {
|
|
246
245
|
oldIdx = nextIdx;
|
|
@@ -297,7 +296,7 @@ function getStringWidth(text) {
|
|
|
297
296
|
}
|
|
298
297
|
|
|
299
298
|
function addCommentHelper(node, comment) {
|
|
300
|
-
|
|
299
|
+
const comments = node.comments || (node.comments = []);
|
|
301
300
|
comments.push(comment);
|
|
302
301
|
comment.printed = false;
|
|
303
302
|
comment.nodeDescription = describeNodeForDebugging(node);
|
|
@@ -336,8 +335,8 @@ function isNonEmptyArray(object) {
|
|
|
336
335
|
}
|
|
337
336
|
|
|
338
337
|
function describeNodeForDebugging(node) {
|
|
339
|
-
|
|
340
|
-
|
|
338
|
+
const nodeType = node.type || node.kind || '(unknown type)';
|
|
339
|
+
let nodeName = String(node.name || node.id && (typeof node.id === 'object' ? node.id.name : node.id) || node.key && (typeof node.key === 'object' ? node.key.name : node.key) || node.value && (typeof node.value === 'object' ? '' : String(node.value)) || node.operator || '');
|
|
341
340
|
|
|
342
341
|
if (nodeName.length > 20) {
|
|
343
342
|
nodeName = nodeName.slice(0, 19) + '…';
|
|
@@ -347,19 +346,19 @@ function describeNodeForDebugging(node) {
|
|
|
347
346
|
}
|
|
348
347
|
|
|
349
348
|
module.exports = {
|
|
350
|
-
getStringWidth
|
|
351
|
-
getLast
|
|
352
|
-
getNextNonSpaceNonCommentCharacterIndexWithStartIndex
|
|
353
|
-
getNextNonSpaceNonCommentCharacterIndex
|
|
354
|
-
getNextNonSpaceNonCommentCharacter
|
|
355
|
-
skipWhitespace
|
|
356
|
-
skipSpaces
|
|
357
|
-
skipNewline
|
|
358
|
-
isNextLineEmptyAfterIndex
|
|
359
|
-
hasNewline
|
|
360
|
-
hasNewlineInRange
|
|
361
|
-
addLeadingComment
|
|
362
|
-
addDanglingComment
|
|
363
|
-
addTrailingComment
|
|
364
|
-
isNonEmptyArray
|
|
349
|
+
getStringWidth,
|
|
350
|
+
getLast,
|
|
351
|
+
getNextNonSpaceNonCommentCharacterIndexWithStartIndex,
|
|
352
|
+
getNextNonSpaceNonCommentCharacterIndex,
|
|
353
|
+
getNextNonSpaceNonCommentCharacter,
|
|
354
|
+
skipWhitespace,
|
|
355
|
+
skipSpaces,
|
|
356
|
+
skipNewline,
|
|
357
|
+
isNextLineEmptyAfterIndex,
|
|
358
|
+
hasNewline,
|
|
359
|
+
hasNewlineInRange,
|
|
360
|
+
addLeadingComment,
|
|
361
|
+
addDanglingComment,
|
|
362
|
+
addTrailingComment,
|
|
363
|
+
isNonEmptyArray
|
|
365
364
|
};
|