hermes-transform 0.5.0 → 0.8.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 +1728 -9
- package/dist/generated/TransformReplaceSignatures.js.flow +921 -780
- package/dist/generated/node-types.js +1099 -1156
- package/dist/generated/node-types.js.flow +77 -125
- 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 +8 -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 +4 -3
- package/dist/transform/TransformContext.js +100 -58
- package/dist/transform/TransformContext.js.flow +277 -141
- package/dist/transform/comments/comments.js +127 -40
- package/dist/transform/comments/comments.js.flow +106 -29
- 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 +81 -104
- package/dist/transform/getTransformedAST.js.flow +18 -13
- package/dist/transform/mutations/AddComments.js +43 -0
- package/dist/transform/mutations/AddComments.js.flow +50 -0
- package/dist/transform/mutations/CloneCommentsTo.js +16 -24
- package/dist/transform/mutations/CloneCommentsTo.js.flow +2 -3
- package/dist/transform/mutations/InsertStatement.js +28 -30
- package/dist/transform/mutations/InsertStatement.js.flow +7 -2
- package/dist/transform/mutations/RemoveComment.js +25 -43
- package/dist/transform/mutations/RemoveComment.js.flow +1 -1
- package/dist/transform/mutations/RemoveNode.js +192 -0
- package/dist/transform/mutations/RemoveNode.js.flow +290 -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 +16 -14
- package/dist/transform/mutations/utils/arrayUtils.js.flow +16 -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 +58 -30
- package/dist/transform/transform.js.flow +51 -13
- 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 +64 -52
- package/dist/traverse/traverse.js.flow +63 -10
- package/package.json +5 -3
- package/dist/transform/mutations/AddLeadingComments.js +0 -47
- package/dist/transform/mutations/AddLeadingComments.js.flow +0 -49
- package/dist/transform/mutations/AddTrailingComments.js +0 -47
- package/dist/transform/mutations/AddTrailingComments.js.flow +0 -49
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
+
exports.CommentPlacement = void 0;
|
|
7
|
+
exports.addComment = addComment;
|
|
6
8
|
exports.addCommentsToNode = addCommentsToNode;
|
|
7
|
-
exports.addLeadingComment = addLeadingComment;
|
|
8
|
-
exports.addTrailingComment = addTrailingComment;
|
|
9
9
|
exports.appendCommentToSource = appendCommentToSource;
|
|
10
10
|
exports.attachComments = attachComments;
|
|
11
11
|
exports.cloneComment = cloneComment;
|
|
@@ -24,25 +24,31 @@ var _printerEstree = _interopRequireDefault(require("./prettier/language-js/prin
|
|
|
24
24
|
|
|
25
25
|
var _util = require("./prettier/common/util");
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
var _os = require("os");
|
|
28
28
|
|
|
29
|
-
function
|
|
29
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
/**
|
|
32
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
33
|
+
*
|
|
34
|
+
* This source code is licensed under the MIT license found in the
|
|
35
|
+
* LICENSE file in the root directory of this source tree.
|
|
36
|
+
*
|
|
37
|
+
*
|
|
38
|
+
* @format
|
|
39
|
+
*/
|
|
40
|
+
// $FlowExpectedError[untyped-import]
|
|
41
|
+
// $FlowExpectedError[untyped-import]
|
|
42
|
+
// $FlowExpectedError[untyped-import]
|
|
43
|
+
const CommentPlacement = require("flow-enums-runtime").Mirrored(["LEADING_OWN_LINE", "LEADING_INLINE", "TRAILING_OWN_LINE", "TRAILING_INLINE"]);
|
|
32
44
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
36
|
-
|
|
37
|
-
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
38
|
-
|
|
39
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
45
|
+
exports.CommentPlacement = CommentPlacement;
|
|
40
46
|
|
|
41
47
|
function attachComments(comments, ast, text) {
|
|
42
48
|
(0, _comments.attach)(comments, ast, text, {
|
|
43
49
|
locStart: _loc.locStart,
|
|
44
50
|
locEnd: _loc.locEnd,
|
|
45
|
-
printer: _printerEstree
|
|
51
|
+
printer: _printerEstree.default
|
|
46
52
|
});
|
|
47
53
|
}
|
|
48
54
|
|
|
@@ -57,19 +63,19 @@ function setCommentsOnNode(node, comments) {
|
|
|
57
63
|
}
|
|
58
64
|
|
|
59
65
|
function addCommentsToNode(node, comments) {
|
|
60
|
-
var _node$comments
|
|
66
|
+
var _node$comments;
|
|
61
67
|
|
|
62
68
|
// $FlowExpectedError - this property is secretly added by prettier.
|
|
63
|
-
node.comments = (_node$comments = node.comments)
|
|
69
|
+
node.comments = (_node$comments = node.comments) != null ? _node$comments : []; // $FlowExpectedError
|
|
64
70
|
|
|
65
|
-
|
|
71
|
+
node.comments.push(...comments);
|
|
66
72
|
}
|
|
67
73
|
|
|
68
74
|
function getCommentsForNode(node) {
|
|
69
75
|
var _node$comments2;
|
|
70
76
|
|
|
71
77
|
// $FlowExpectedError - this property is secretly added by prettier.
|
|
72
|
-
return (_node$comments2 = node.comments)
|
|
78
|
+
return (_node$comments2 = node.comments) != null ? _node$comments2 : [];
|
|
73
79
|
}
|
|
74
80
|
|
|
75
81
|
function isLeadingComment(comment) {
|
|
@@ -82,12 +88,22 @@ function isTrailingComment(comment) {
|
|
|
82
88
|
return comment.trailing === true;
|
|
83
89
|
}
|
|
84
90
|
|
|
85
|
-
function
|
|
86
|
-
(
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
+
function addComment(node, comment, placement) {
|
|
92
|
+
switch (placement) {
|
|
93
|
+
case CommentPlacement.LEADING_OWN_LINE:
|
|
94
|
+
case CommentPlacement.LEADING_INLINE:
|
|
95
|
+
{
|
|
96
|
+
(0, _util.addLeadingComment)(node, comment);
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
case CommentPlacement.TRAILING_OWN_LINE:
|
|
101
|
+
case CommentPlacement.TRAILING_INLINE:
|
|
102
|
+
{
|
|
103
|
+
(0, _util.addTrailingComment)(node, comment);
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
91
107
|
}
|
|
92
108
|
|
|
93
109
|
function cloneComment(comment) {
|
|
@@ -112,29 +128,100 @@ function cloneCommentWithMarkers(comment) {
|
|
|
112
128
|
};
|
|
113
129
|
}
|
|
114
130
|
|
|
115
|
-
function
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
131
|
+
function getFirstNewlineIndex(code) {
|
|
132
|
+
return code.search(/\r\n|\n|\r/);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function getFirstNonWhitespaceIndex(code) {
|
|
136
|
+
return code.search(/\S/);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function appendCommentToSource(code, comment, placement) {
|
|
140
|
+
let newCode = code;
|
|
121
141
|
|
|
122
142
|
switch (comment.type) {
|
|
123
143
|
case 'Block':
|
|
124
|
-
|
|
125
|
-
|
|
144
|
+
{
|
|
145
|
+
// Prettier decides if a newline is necessary between the comment and its node by looking
|
|
146
|
+
// to see if a newline seperates them in the source text. We can trick prettier into
|
|
147
|
+
// formatting how we want for new comments by placing the range such that a newline
|
|
148
|
+
// will (OWN_LINE) or will not (INLINE) be found when searching from the specified range
|
|
149
|
+
// position.
|
|
150
|
+
switch (placement) {
|
|
151
|
+
case CommentPlacement.LEADING_OWN_LINE:
|
|
152
|
+
case CommentPlacement.TRAILING_OWN_LINE:
|
|
153
|
+
{
|
|
154
|
+
// Since we always want a line break we need to ensure a newline is found when
|
|
155
|
+
// searching out from either side of the comment range.
|
|
156
|
+
let firstNewline = getFirstNewlineIndex(code);
|
|
157
|
+
|
|
158
|
+
if (firstNewline === -1) {
|
|
159
|
+
// No newline in file, lets add one.
|
|
160
|
+
newCode += _os.EOL;
|
|
161
|
+
firstNewline = newCode.length;
|
|
162
|
+
} // Prettier only uses these ranges for detecting whitespace, so this nonsensical
|
|
163
|
+
// range is safe.
|
|
164
|
+
// $FlowExpectedError[cannot-write]
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
comment.range = [firstNewline + 1, firstNewline];
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
case CommentPlacement.LEADING_INLINE:
|
|
172
|
+
case CommentPlacement.TRAILING_INLINE:
|
|
173
|
+
{
|
|
174
|
+
// Since we don't want a line break we need to ensure a non whitespace char is
|
|
175
|
+
// always found before a newline when searching out from either side of the
|
|
176
|
+
// comment range.
|
|
177
|
+
let firstNonWhitespace = getFirstNonWhitespaceIndex(code);
|
|
178
|
+
|
|
179
|
+
if (firstNonWhitespace === -1) {
|
|
180
|
+
// No non whitespace chars in file, lets add an identifiable statement for prettier to find.
|
|
181
|
+
newCode += '$FORCE_INLINE_ON_EMPTY_FILE_TOKEN$;';
|
|
182
|
+
firstNonWhitespace = newCode.length;
|
|
183
|
+
break;
|
|
184
|
+
} // $FlowExpectedError[cannot-write]
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
comment.range = [firstNonWhitespace + 1, firstNonWhitespace];
|
|
188
|
+
break;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
break;
|
|
193
|
+
}
|
|
126
194
|
|
|
127
195
|
case 'Line':
|
|
128
|
-
|
|
129
|
-
|
|
196
|
+
{
|
|
197
|
+
// For `Line` comments prettier slices comments directly from the source code when printing
|
|
198
|
+
// https://github.com/prettier/prettier/blob/5f0ee39fa03532c85bd1c35291450fe7ac3667b3/src/language-js/print/comment.js#L15-L20
|
|
199
|
+
// this means that we need to have any appended comments directly in the
|
|
200
|
+
// source code or else prettier will slice nothing and bork up the transform
|
|
201
|
+
const commentText = `//${comment.value}`;
|
|
202
|
+
const lastChar = newCode[newCode.length - 1];
|
|
203
|
+
|
|
204
|
+
if (lastChar !== '\n' && lastChar !== '\r') {
|
|
205
|
+
newCode += _os.EOL;
|
|
206
|
+
} // Line comments cannot be inline before a node so we only place trailing Line comments inline.
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
if (placement === CommentPlacement.TRAILING_INLINE) {
|
|
210
|
+
// Prettier determines an "end of line" comment by walking backwards from
|
|
211
|
+
// the comment start range through the source code to see if it finds a non
|
|
212
|
+
// newline token. In order to trick prettier for new comments we need to
|
|
213
|
+
// insert fake source code for it to find.
|
|
214
|
+
newCode += '$FORCE_END_OF_LINE_COMMENT_TOKEN$;';
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const start = newCode.length;
|
|
218
|
+
newCode += commentText;
|
|
219
|
+
const end = newCode.length; // $FlowExpectedError[cannot-write]
|
|
220
|
+
|
|
221
|
+
comment.range = [start, end];
|
|
222
|
+
break;
|
|
223
|
+
}
|
|
130
224
|
}
|
|
131
225
|
|
|
132
|
-
var newCode = code;
|
|
133
|
-
newCode += '\n';
|
|
134
|
-
var start = newCode.length;
|
|
135
|
-
newCode += commentText;
|
|
136
|
-
var end = newCode.length; // $FlowExpectedError[cannot-write]
|
|
137
|
-
|
|
138
|
-
comment.range = [start, end];
|
|
139
226
|
return newCode;
|
|
140
227
|
}
|
|
@@ -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.
|
|
@@ -23,9 +23,17 @@ import {
|
|
|
23
23
|
// $FlowExpectedError[untyped-import]
|
|
24
24
|
addTrailingComment as untypedAddTrailingComment,
|
|
25
25
|
} from './prettier/common/util';
|
|
26
|
+
import {EOL} from 'os';
|
|
26
27
|
|
|
27
28
|
export type Options = $ReadOnly<{}>;
|
|
28
29
|
|
|
30
|
+
export enum CommentPlacement {
|
|
31
|
+
LEADING_OWN_LINE,
|
|
32
|
+
LEADING_INLINE,
|
|
33
|
+
TRAILING_OWN_LINE,
|
|
34
|
+
TRAILING_INLINE,
|
|
35
|
+
}
|
|
36
|
+
|
|
29
37
|
export function attachComments(
|
|
30
38
|
comments: $ReadOnlyArray<Comment>,
|
|
31
39
|
ast: Program,
|
|
@@ -80,18 +88,23 @@ export function isTrailingComment(comment: Comment): boolean {
|
|
|
80
88
|
return comment.trailing === true;
|
|
81
89
|
}
|
|
82
90
|
|
|
83
|
-
export function
|
|
84
|
-
node: ESNode | DetachedNode<ESNode>,
|
|
85
|
-
comment: Comment,
|
|
86
|
-
): void {
|
|
87
|
-
untypedAddLeadingComment(node, comment);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
export function addTrailingComment(
|
|
91
|
+
export function addComment(
|
|
91
92
|
node: ESNode | DetachedNode<ESNode>,
|
|
92
93
|
comment: Comment,
|
|
94
|
+
placement: CommentPlacement,
|
|
93
95
|
): void {
|
|
94
|
-
|
|
96
|
+
switch (placement) {
|
|
97
|
+
case CommentPlacement.LEADING_OWN_LINE:
|
|
98
|
+
case CommentPlacement.LEADING_INLINE: {
|
|
99
|
+
untypedAddLeadingComment(node, comment);
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
case CommentPlacement.TRAILING_OWN_LINE:
|
|
103
|
+
case CommentPlacement.TRAILING_INLINE: {
|
|
104
|
+
untypedAddTrailingComment(node, comment);
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
95
108
|
}
|
|
96
109
|
|
|
97
110
|
export function cloneComment<T: Comment>(comment: T): T {
|
|
@@ -116,30 +129,94 @@ export function cloneCommentWithMarkers<T: Comment>(comment: T): T {
|
|
|
116
129
|
};
|
|
117
130
|
}
|
|
118
131
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
132
|
+
function getFirstNewlineIndex(code: string): number {
|
|
133
|
+
return code.search(/\r\n|\n|\r/);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function getFirstNonWhitespaceIndex(code: string): number {
|
|
137
|
+
return code.search(/\S/);
|
|
138
|
+
}
|
|
124
139
|
|
|
125
|
-
|
|
140
|
+
export function appendCommentToSource(
|
|
141
|
+
code: string,
|
|
142
|
+
comment: Comment,
|
|
143
|
+
placement: CommentPlacement,
|
|
144
|
+
): string {
|
|
145
|
+
let newCode = code;
|
|
126
146
|
switch (comment.type) {
|
|
127
|
-
case 'Block':
|
|
128
|
-
|
|
147
|
+
case 'Block': {
|
|
148
|
+
// Prettier decides if a newline is necessary between the comment and its node by looking
|
|
149
|
+
// to see if a newline seperates them in the source text. We can trick prettier into
|
|
150
|
+
// formatting how we want for new comments by placing the range such that a newline
|
|
151
|
+
// will (OWN_LINE) or will not (INLINE) be found when searching from the specified range
|
|
152
|
+
// position.
|
|
153
|
+
switch (placement) {
|
|
154
|
+
case CommentPlacement.LEADING_OWN_LINE:
|
|
155
|
+
case CommentPlacement.TRAILING_OWN_LINE: {
|
|
156
|
+
// Since we always want a line break we need to ensure a newline is found when
|
|
157
|
+
// searching out from either side of the comment range.
|
|
158
|
+
let firstNewline = getFirstNewlineIndex(code);
|
|
159
|
+
if (firstNewline === -1) {
|
|
160
|
+
// No newline in file, lets add one.
|
|
161
|
+
newCode += EOL;
|
|
162
|
+
firstNewline = newCode.length;
|
|
163
|
+
}
|
|
164
|
+
// Prettier only uses these ranges for detecting whitespace, so this nonsensical
|
|
165
|
+
// range is safe.
|
|
166
|
+
// $FlowExpectedError[cannot-write]
|
|
167
|
+
comment.range = [firstNewline + 1, firstNewline];
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
case CommentPlacement.LEADING_INLINE:
|
|
171
|
+
case CommentPlacement.TRAILING_INLINE: {
|
|
172
|
+
// Since we don't want a line break we need to ensure a non whitespace char is
|
|
173
|
+
// always found before a newline when searching out from either side of the
|
|
174
|
+
// comment range.
|
|
175
|
+
let firstNonWhitespace = getFirstNonWhitespaceIndex(code);
|
|
176
|
+
if (firstNonWhitespace === -1) {
|
|
177
|
+
// No non whitespace chars in file, lets add an identifiable statement for prettier to find.
|
|
178
|
+
newCode += '$FORCE_INLINE_ON_EMPTY_FILE_TOKEN$;';
|
|
179
|
+
firstNonWhitespace = newCode.length;
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// $FlowExpectedError[cannot-write]
|
|
184
|
+
comment.range = [firstNonWhitespace + 1, firstNonWhitespace];
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
129
188
|
break;
|
|
130
|
-
|
|
131
|
-
|
|
189
|
+
}
|
|
190
|
+
case 'Line': {
|
|
191
|
+
// For `Line` comments prettier slices comments directly from the source code when printing
|
|
192
|
+
// https://github.com/prettier/prettier/blob/5f0ee39fa03532c85bd1c35291450fe7ac3667b3/src/language-js/print/comment.js#L15-L20
|
|
193
|
+
// this means that we need to have any appended comments directly in the
|
|
194
|
+
// source code or else prettier will slice nothing and bork up the transform
|
|
195
|
+
const commentText = `//${comment.value}`;
|
|
196
|
+
|
|
197
|
+
const lastChar = newCode[newCode.length - 1];
|
|
198
|
+
if (lastChar !== '\n' && lastChar !== '\r') {
|
|
199
|
+
newCode += EOL;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// Line comments cannot be inline before a node so we only place trailing Line comments inline.
|
|
203
|
+
if (placement === CommentPlacement.TRAILING_INLINE) {
|
|
204
|
+
// Prettier determines an "end of line" comment by walking backwards from
|
|
205
|
+
// the comment start range through the source code to see if it finds a non
|
|
206
|
+
// newline token. In order to trick prettier for new comments we need to
|
|
207
|
+
// insert fake source code for it to find.
|
|
208
|
+
newCode += '$FORCE_END_OF_LINE_COMMENT_TOKEN$;';
|
|
209
|
+
}
|
|
210
|
+
const start = newCode.length;
|
|
211
|
+
newCode += commentText;
|
|
212
|
+
const end = newCode.length;
|
|
213
|
+
|
|
214
|
+
// $FlowExpectedError[cannot-write]
|
|
215
|
+
comment.range = [start, end];
|
|
216
|
+
|
|
132
217
|
break;
|
|
218
|
+
}
|
|
133
219
|
}
|
|
134
220
|
|
|
135
|
-
let newCode = code;
|
|
136
|
-
newCode += '\n';
|
|
137
|
-
const start = newCode.length;
|
|
138
|
-
newCode += commentText;
|
|
139
|
-
const end = newCode.length;
|
|
140
|
-
|
|
141
|
-
// $FlowExpectedError[cannot-write]
|
|
142
|
-
comment.range = [start, end];
|
|
143
|
-
|
|
144
221
|
return newCode;
|
|
145
222
|
}
|
|
@@ -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
|
};
|