flow-parser 0.322.0 → 0.324.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/README.md +25 -32
- package/dist/FlowParserWASM.js +1 -1
- package/dist/estree/TransformComponentSyntax.js +1 -0
- package/dist/estree/TransformComponentSyntax.js.flow +1 -0
- package/package.json +2 -2
- package/dist/transform/print/comments/comments.js +0 -196
- package/dist/transform/print/comments/comments.js.flow +0 -330
- package/dist/transform/print/comments/prettier/common/util.js +0 -186
- package/dist/transform/print/comments/prettier/language-js/comments.js +0 -552
- package/dist/transform/print/comments/prettier/language-js/loc.js +0 -24
- package/dist/transform/print/comments/prettier/language-js/printer-estree.js +0 -20
- package/dist/transform/print/comments/prettier/language-js/utils.js +0 -69
- package/dist/transform/print/comments/prettier/main/comments.js +0 -313
- package/dist/transform/print/comments/prettier/utils/get-last.js +0 -4
- package/dist/transform/print/detachedNodeTypes.js +0 -1
- package/dist/transform/print/detachedNodeTypes.js.flow +0 -24
- package/dist/transform/print/print.js +0 -52
- package/dist/transform/print/print.js.flow +0 -87
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "flow-parser",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.324.0",
|
|
4
4
|
"description": "JavaScript parser written in OCaml. Produces ESTree AST",
|
|
5
5
|
"homepage": "https://flow.org",
|
|
6
6
|
"license": "MIT",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"test": "node test/run_tests.js"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"flow-estree": "0.
|
|
25
|
+
"flow-estree": "0.324.0"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
28
|
"@babel/generator": "7.7.4",
|
|
@@ -1,196 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.CommentPlacement = void 0;
|
|
7
|
-
exports.addComment = addComment;
|
|
8
|
-
exports.appendCommentToSource = appendCommentToSource;
|
|
9
|
-
exports.attachComments = attachComments;
|
|
10
|
-
exports.cloneComment = cloneComment;
|
|
11
|
-
exports.cloneCommentWithMarkers = cloneCommentWithMarkers;
|
|
12
|
-
exports.cloneCommentsToNewNode = cloneCommentsToNewNode;
|
|
13
|
-
exports.cloneJSDocCommentsToNewNode = cloneJSDocCommentsToNewNode;
|
|
14
|
-
exports.getCommentsForNode = getCommentsForNode;
|
|
15
|
-
exports.getLeadingCommentsForNode = getLeadingCommentsForNode;
|
|
16
|
-
exports.getTrailingCommentsForNode = getTrailingCommentsForNode;
|
|
17
|
-
exports.isAttachedComment = isAttachedComment;
|
|
18
|
-
exports.isLeadingComment = isLeadingComment;
|
|
19
|
-
exports.isTrailingComment = isTrailingComment;
|
|
20
|
-
exports.makeCommentOwnLine = makeCommentOwnLine;
|
|
21
|
-
exports.moveCommentsToNewNode = moveCommentsToNewNode;
|
|
22
|
-
exports.mutateESTreeASTCommentsForPrettier = mutateESTreeASTCommentsForPrettier;
|
|
23
|
-
exports.setCommentsOnNode = setCommentsOnNode;
|
|
24
|
-
var _comments = require("./prettier/main/comments");
|
|
25
|
-
var _loc = require("./prettier/language-js/loc");
|
|
26
|
-
var _printerEstree = _interopRequireDefault(require("./prettier/language-js/printer-estree"));
|
|
27
|
-
var _util = require("./prettier/common/util");
|
|
28
|
-
var _flowEstree = require("flow-estree");
|
|
29
|
-
var _os = require("os");
|
|
30
|
-
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
31
|
-
const CommentPlacement = exports.CommentPlacement = require("flow-enums-runtime").Mirrored(["LEADING_OWN_LINE", "LEADING_INLINE", "TRAILING_OWN_LINE", "TRAILING_INLINE"]);
|
|
32
|
-
function attachComments(comments, ast, text) {
|
|
33
|
-
(0, _comments.attach)(comments, ast, text, {
|
|
34
|
-
locStart: _loc.locStart,
|
|
35
|
-
locEnd: _loc.locEnd,
|
|
36
|
-
printer: _printerEstree.default
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
function mutateESTreeASTCommentsForPrettier(program, text) {
|
|
40
|
-
let code = text;
|
|
41
|
-
delete program.comments;
|
|
42
|
-
if (program.docblock != null && program.docblock.comment != null) {
|
|
43
|
-
const docblockComment = program.docblock.comment;
|
|
44
|
-
const isDocblockCommentNew = !isAttachedComment(docblockComment);
|
|
45
|
-
if (isDocblockCommentNew) {
|
|
46
|
-
docblockComment.printed = false;
|
|
47
|
-
docblockComment.leading = true;
|
|
48
|
-
docblockComment.trailing = false;
|
|
49
|
-
}
|
|
50
|
-
if (program.body.length > 0) {
|
|
51
|
-
const firstStatement = program.body[0];
|
|
52
|
-
const leadingComments = getLeadingCommentsForNode(firstStatement);
|
|
53
|
-
if (!leadingComments.includes(docblockComment)) {
|
|
54
|
-
setCommentsOnNode(firstStatement, [docblockComment, ...getCommentsForNode(firstStatement)]);
|
|
55
|
-
if (isDocblockCommentNew) {
|
|
56
|
-
code = makeCommentOwnLine(code, docblockComment);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
} else {
|
|
60
|
-
setCommentsOnNode(program, [docblockComment]);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
delete program.docblock;
|
|
64
|
-
return code;
|
|
65
|
-
}
|
|
66
|
-
function moveCommentsToNewNode(oldNode, newNode) {
|
|
67
|
-
setCommentsOnNode(newNode, getCommentsForNode(oldNode));
|
|
68
|
-
setCommentsOnNode(oldNode, []);
|
|
69
|
-
}
|
|
70
|
-
function cloneCommentsToNewNode(oldNode, newNode) {
|
|
71
|
-
setCommentsOnNode(newNode, getCommentsForNode(oldNode).map(comment => cloneCommentWithMarkers(comment)));
|
|
72
|
-
}
|
|
73
|
-
function cloneJSDocCommentsToNewNode(oldNode, newNode) {
|
|
74
|
-
const comments = getCommentsForNode(oldNode).filter(comment => {
|
|
75
|
-
return (0, _flowEstree.isBlockComment)(comment) && comment.value.startsWith('*');
|
|
76
|
-
});
|
|
77
|
-
setCommentsOnNode(newNode, [...getCommentsForNode(newNode), ...comments.map(cloneCommentWithMarkers)]);
|
|
78
|
-
}
|
|
79
|
-
function setCommentsOnNode(node, comments) {
|
|
80
|
-
node.comments = comments;
|
|
81
|
-
}
|
|
82
|
-
function getCommentsForNode(node) {
|
|
83
|
-
var _node$comments;
|
|
84
|
-
return (_node$comments = node.comments) != null ? _node$comments : [];
|
|
85
|
-
}
|
|
86
|
-
function isAttachedComment(comment) {
|
|
87
|
-
return comment.printed === false;
|
|
88
|
-
}
|
|
89
|
-
function isLeadingComment(comment) {
|
|
90
|
-
return comment.leading === true;
|
|
91
|
-
}
|
|
92
|
-
function isTrailingComment(comment) {
|
|
93
|
-
return comment.trailing === true;
|
|
94
|
-
}
|
|
95
|
-
function getLeadingCommentsForNode(node) {
|
|
96
|
-
return getCommentsForNode(node).filter(isLeadingComment);
|
|
97
|
-
}
|
|
98
|
-
function getTrailingCommentsForNode(node) {
|
|
99
|
-
return getCommentsForNode(node).filter(isTrailingComment);
|
|
100
|
-
}
|
|
101
|
-
function addComment(node, comment, placement) {
|
|
102
|
-
switch (placement) {
|
|
103
|
-
case CommentPlacement.LEADING_OWN_LINE:
|
|
104
|
-
case CommentPlacement.LEADING_INLINE:
|
|
105
|
-
{
|
|
106
|
-
(0, _util.addLeadingComment)(node, comment);
|
|
107
|
-
break;
|
|
108
|
-
}
|
|
109
|
-
case CommentPlacement.TRAILING_OWN_LINE:
|
|
110
|
-
case CommentPlacement.TRAILING_INLINE:
|
|
111
|
-
{
|
|
112
|
-
(0, _util.addTrailingComment)(node, comment);
|
|
113
|
-
break;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
function cloneComment(comment) {
|
|
118
|
-
return {
|
|
119
|
-
type: comment.type,
|
|
120
|
-
value: comment.value,
|
|
121
|
-
loc: comment.loc,
|
|
122
|
-
range: comment.range
|
|
123
|
-
};
|
|
124
|
-
}
|
|
125
|
-
function cloneCommentWithMarkers(comment) {
|
|
126
|
-
return {
|
|
127
|
-
type: comment.type,
|
|
128
|
-
value: comment.value,
|
|
129
|
-
loc: comment.loc,
|
|
130
|
-
range: comment.range,
|
|
131
|
-
leading: isLeadingComment(comment),
|
|
132
|
-
trailing: isTrailingComment(comment)
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
function getFirstNewlineIndex(code) {
|
|
136
|
-
return code.search(/\r\n|\n|\r/);
|
|
137
|
-
}
|
|
138
|
-
function getFirstNonWhitespaceIndex(code) {
|
|
139
|
-
return code.search(/\S/);
|
|
140
|
-
}
|
|
141
|
-
function makeCommentOwnLine(code, comment) {
|
|
142
|
-
let newCode = code;
|
|
143
|
-
let firstNewline = getFirstNewlineIndex(code);
|
|
144
|
-
if (firstNewline === -1) {
|
|
145
|
-
newCode += _os.EOL;
|
|
146
|
-
firstNewline = newCode.length;
|
|
147
|
-
}
|
|
148
|
-
comment.range = [firstNewline + 1, firstNewline];
|
|
149
|
-
return newCode;
|
|
150
|
-
}
|
|
151
|
-
function appendCommentToSource(code, comment, placement) {
|
|
152
|
-
let newCode = code;
|
|
153
|
-
switch (comment.type) {
|
|
154
|
-
case 'Block':
|
|
155
|
-
{
|
|
156
|
-
switch (placement) {
|
|
157
|
-
case CommentPlacement.LEADING_OWN_LINE:
|
|
158
|
-
case CommentPlacement.TRAILING_OWN_LINE:
|
|
159
|
-
{
|
|
160
|
-
newCode = makeCommentOwnLine(code, comment);
|
|
161
|
-
break;
|
|
162
|
-
}
|
|
163
|
-
case CommentPlacement.LEADING_INLINE:
|
|
164
|
-
case CommentPlacement.TRAILING_INLINE:
|
|
165
|
-
{
|
|
166
|
-
let firstNonWhitespace = getFirstNonWhitespaceIndex(code);
|
|
167
|
-
if (firstNonWhitespace === -1) {
|
|
168
|
-
newCode += '$FORCE_INLINE_ON_EMPTY_FILE_TOKEN$;';
|
|
169
|
-
firstNonWhitespace = newCode.length;
|
|
170
|
-
break;
|
|
171
|
-
}
|
|
172
|
-
comment.range = [firstNonWhitespace + 1, firstNonWhitespace];
|
|
173
|
-
break;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
break;
|
|
177
|
-
}
|
|
178
|
-
case 'Line':
|
|
179
|
-
{
|
|
180
|
-
const commentText = `//${comment.value}`;
|
|
181
|
-
const lastChar = newCode[newCode.length - 1];
|
|
182
|
-
if (lastChar !== '\n' && lastChar !== '\r') {
|
|
183
|
-
newCode += _os.EOL;
|
|
184
|
-
}
|
|
185
|
-
if (placement === CommentPlacement.TRAILING_INLINE) {
|
|
186
|
-
newCode += '$FORCE_END_OF_LINE_COMMENT_TOKEN$;';
|
|
187
|
-
}
|
|
188
|
-
const start = newCode.length;
|
|
189
|
-
newCode += commentText;
|
|
190
|
-
const end = newCode.length;
|
|
191
|
-
comment.range = [start, end];
|
|
192
|
-
break;
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
return newCode;
|
|
196
|
-
}
|
|
@@ -1,330 +0,0 @@
|
|
|
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
|
-
import type {Comment, ESNode, Program} from 'flow-estree';
|
|
12
|
-
import type {DetachedNode, MaybeDetachedNode} from '../detachedNodeTypes';
|
|
13
|
-
|
|
14
|
-
// $FlowExpectedError[untyped-import]
|
|
15
|
-
import {attach as untypedAttach} from './prettier/main/comments';
|
|
16
|
-
// $FlowExpectedError[untyped-import]
|
|
17
|
-
import {locEnd, locStart} from './prettier/language-js/loc';
|
|
18
|
-
// $FlowExpectedError[untyped-import]
|
|
19
|
-
import printer from './prettier/language-js/printer-estree';
|
|
20
|
-
import {
|
|
21
|
-
// $FlowExpectedError[untyped-import]
|
|
22
|
-
addLeadingComment as untypedAddLeadingComment,
|
|
23
|
-
// $FlowExpectedError[untyped-import]
|
|
24
|
-
addTrailingComment as untypedAddTrailingComment,
|
|
25
|
-
// $FlowFixMe[untyped-import]
|
|
26
|
-
} from './prettier/common/util';
|
|
27
|
-
import {isBlockComment} from 'flow-estree';
|
|
28
|
-
import {EOL} from 'os';
|
|
29
|
-
|
|
30
|
-
export type Options = Readonly<{}>;
|
|
31
|
-
|
|
32
|
-
export enum CommentPlacement {
|
|
33
|
-
LEADING_OWN_LINE,
|
|
34
|
-
LEADING_INLINE,
|
|
35
|
-
TRAILING_OWN_LINE,
|
|
36
|
-
TRAILING_INLINE,
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export function attachComments(
|
|
40
|
-
comments: ReadonlyArray<Comment>,
|
|
41
|
-
ast: Program,
|
|
42
|
-
text: string,
|
|
43
|
-
): void {
|
|
44
|
-
untypedAttach(comments, ast, text, {
|
|
45
|
-
locStart,
|
|
46
|
-
locEnd,
|
|
47
|
-
printer,
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export function mutateESTreeASTCommentsForPrettier(
|
|
52
|
-
program: Program,
|
|
53
|
-
text: string,
|
|
54
|
-
): string {
|
|
55
|
-
let code = text;
|
|
56
|
-
|
|
57
|
-
// we need to delete the comments prop or else prettier will do
|
|
58
|
-
// its own attachment pass after the mutation and duplicate the
|
|
59
|
-
// comments on each node, borking the output
|
|
60
|
-
// $FlowExpectedError[cannot-write]
|
|
61
|
-
delete program.comments;
|
|
62
|
-
|
|
63
|
-
// The docblock comment is never attached to any AST nodes, since its technically
|
|
64
|
-
// attached to the program. However this is specific to our AST and in order for
|
|
65
|
-
// prettier to correctly print it we need to attach it to the first node in the
|
|
66
|
-
// program body.
|
|
67
|
-
if (program.docblock != null && program.docblock.comment != null) {
|
|
68
|
-
const docblockComment = program.docblock.comment;
|
|
69
|
-
|
|
70
|
-
const isDocblockCommentNew = !isAttachedComment(docblockComment);
|
|
71
|
-
if (isDocblockCommentNew) {
|
|
72
|
-
// $FlowExpectedError[prop-missing]
|
|
73
|
-
docblockComment.printed = false;
|
|
74
|
-
// $FlowExpectedError[prop-missing]
|
|
75
|
-
docblockComment.leading = true;
|
|
76
|
-
// $FlowExpectedError[prop-missing]
|
|
77
|
-
docblockComment.trailing = false;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// If we have a first node in the program body, attach the comment to that
|
|
81
|
-
// otherwise set it on the program.
|
|
82
|
-
if (program.body.length > 0) {
|
|
83
|
-
const firstStatement = program.body[0];
|
|
84
|
-
const leadingComments = getLeadingCommentsForNode(firstStatement);
|
|
85
|
-
if (!leadingComments.includes(docblockComment)) {
|
|
86
|
-
setCommentsOnNode(firstStatement, [
|
|
87
|
-
docblockComment,
|
|
88
|
-
...getCommentsForNode(firstStatement),
|
|
89
|
-
]);
|
|
90
|
-
|
|
91
|
-
if (isDocblockCommentNew) {
|
|
92
|
-
// Add markers to the code block to ensure docblock comment is printed on its
|
|
93
|
-
// own line.
|
|
94
|
-
code = makeCommentOwnLine(code, docblockComment);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
} else {
|
|
98
|
-
setCommentsOnNode(program, [docblockComment]);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// Should not be needed anymore
|
|
103
|
-
// $FlowExpectedError[cannot-write]
|
|
104
|
-
delete program.docblock;
|
|
105
|
-
|
|
106
|
-
return code;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
export function moveCommentsToNewNode(
|
|
110
|
-
oldNode: ESNode,
|
|
111
|
-
newNode: DetachedNode<ESNode>,
|
|
112
|
-
): void {
|
|
113
|
-
setCommentsOnNode(newNode, getCommentsForNode(oldNode));
|
|
114
|
-
setCommentsOnNode(oldNode, []);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
export function cloneCommentsToNewNode(
|
|
118
|
-
oldNode: MaybeDetachedNode<ESNode>,
|
|
119
|
-
newNode: MaybeDetachedNode<ESNode>,
|
|
120
|
-
): void {
|
|
121
|
-
setCommentsOnNode(
|
|
122
|
-
newNode,
|
|
123
|
-
getCommentsForNode(oldNode).map(comment =>
|
|
124
|
-
cloneCommentWithMarkers(comment),
|
|
125
|
-
),
|
|
126
|
-
);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
export function cloneJSDocCommentsToNewNode(
|
|
130
|
-
oldNode: ESNode,
|
|
131
|
-
newNode: MaybeDetachedNode<ESNode>,
|
|
132
|
-
): void {
|
|
133
|
-
const comments = getCommentsForNode(oldNode).filter(comment => {
|
|
134
|
-
return (
|
|
135
|
-
isBlockComment(comment) &&
|
|
136
|
-
// JSDoc comments always start with an extra asterisk
|
|
137
|
-
comment.value.startsWith('*')
|
|
138
|
-
);
|
|
139
|
-
});
|
|
140
|
-
setCommentsOnNode(newNode, [
|
|
141
|
-
...getCommentsForNode(newNode),
|
|
142
|
-
...comments.map(cloneCommentWithMarkers),
|
|
143
|
-
]);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
export function setCommentsOnNode(
|
|
147
|
-
node: ESNode | DetachedNode<ESNode>,
|
|
148
|
-
comments: ReadonlyArray<Comment>,
|
|
149
|
-
): void {
|
|
150
|
-
// $FlowExpectedError[prop-missing] - this property is secretly added by prettier.
|
|
151
|
-
// $FlowExpectedError[incompatible-use] - this property is secretly added by prettier.
|
|
152
|
-
// $FlowExpectedError[cannot-write] - this property is secretly added by prettier.
|
|
153
|
-
node.comments = comments;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
export function getCommentsForNode(
|
|
157
|
-
node: ESNode | DetachedNode<ESNode>,
|
|
158
|
-
): ReadonlyArray<Comment> {
|
|
159
|
-
// $FlowExpectedError[prop-missing] - this property is secretly added by prettier.
|
|
160
|
-
// $FlowExpectedError[incompatible-use] - this property is secretly added by prettier.
|
|
161
|
-
return node.comments ?? [];
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
export function isAttachedComment(comment: Comment): boolean {
|
|
165
|
-
// Prettier adds this property to comments that have been attached.
|
|
166
|
-
// $FlowExpectedError[prop-missing] - this property is secretly added by prettier.
|
|
167
|
-
return comment.printed === false;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
export function isLeadingComment(comment: Comment): boolean {
|
|
171
|
-
// $FlowExpectedError[prop-missing] - this property is secretly added by prettier.
|
|
172
|
-
return comment.leading === true;
|
|
173
|
-
}
|
|
174
|
-
export function isTrailingComment(comment: Comment): boolean {
|
|
175
|
-
// $FlowExpectedError[prop-missing] - this property is secretly added by prettier.
|
|
176
|
-
return comment.trailing === true;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
export function getLeadingCommentsForNode(
|
|
180
|
-
node: ESNode | DetachedNode<ESNode>,
|
|
181
|
-
): ReadonlyArray<Comment> {
|
|
182
|
-
return getCommentsForNode(node).filter(isLeadingComment);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
export function getTrailingCommentsForNode(
|
|
186
|
-
node: ESNode | DetachedNode<ESNode>,
|
|
187
|
-
): ReadonlyArray<Comment> {
|
|
188
|
-
return getCommentsForNode(node).filter(isTrailingComment);
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
export function addComment(
|
|
192
|
-
node: ESNode | DetachedNode<ESNode>,
|
|
193
|
-
comment: Comment,
|
|
194
|
-
placement: CommentPlacement,
|
|
195
|
-
): void {
|
|
196
|
-
switch (placement) {
|
|
197
|
-
case CommentPlacement.LEADING_OWN_LINE:
|
|
198
|
-
case CommentPlacement.LEADING_INLINE: {
|
|
199
|
-
untypedAddLeadingComment(node, comment);
|
|
200
|
-
break;
|
|
201
|
-
}
|
|
202
|
-
case CommentPlacement.TRAILING_OWN_LINE:
|
|
203
|
-
case CommentPlacement.TRAILING_INLINE: {
|
|
204
|
-
untypedAddTrailingComment(node, comment);
|
|
205
|
-
break;
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
export function cloneComment<T extends Comment>(comment: T): T {
|
|
211
|
-
// $FlowExpectedError[incompatible-type]
|
|
212
|
-
return {
|
|
213
|
-
type: comment.type,
|
|
214
|
-
value: comment.value,
|
|
215
|
-
loc: comment.loc,
|
|
216
|
-
range: comment.range,
|
|
217
|
-
};
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
export function cloneCommentWithMarkers<T extends Comment>(comment: T): T {
|
|
221
|
-
// $FlowExpectedError[incompatible-type]
|
|
222
|
-
return {
|
|
223
|
-
type: comment.type,
|
|
224
|
-
value: comment.value,
|
|
225
|
-
loc: comment.loc,
|
|
226
|
-
range: comment.range,
|
|
227
|
-
leading: isLeadingComment(comment),
|
|
228
|
-
trailing: isTrailingComment(comment),
|
|
229
|
-
};
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
function getFirstNewlineIndex(code: string): number {
|
|
233
|
-
return code.search(/\r\n|\n|\r/);
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
function getFirstNonWhitespaceIndex(code: string): number {
|
|
237
|
-
return code.search(/\S/);
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
export function makeCommentOwnLine(code: string, comment: Comment): string {
|
|
241
|
-
let newCode = code;
|
|
242
|
-
// Since we always want a line break we need to ensure a newline is found when
|
|
243
|
-
// searching out from either side of the comment range.
|
|
244
|
-
let firstNewline = getFirstNewlineIndex(code);
|
|
245
|
-
if (firstNewline === -1) {
|
|
246
|
-
// No newline in file, lets add one.
|
|
247
|
-
newCode += EOL;
|
|
248
|
-
firstNewline = newCode.length;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
// Prettier only uses these ranges for detecting whitespace, so this nonsensical
|
|
252
|
-
// range is safe.
|
|
253
|
-
// $FlowExpectedError[cannot-write]
|
|
254
|
-
comment.range = [firstNewline + 1, firstNewline];
|
|
255
|
-
|
|
256
|
-
return newCode;
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
export function appendCommentToSource(
|
|
260
|
-
code: string,
|
|
261
|
-
comment: Comment,
|
|
262
|
-
placement: CommentPlacement,
|
|
263
|
-
): string {
|
|
264
|
-
let newCode = code;
|
|
265
|
-
switch (comment.type) {
|
|
266
|
-
case 'Block': {
|
|
267
|
-
// Prettier decides if a newline is necessary between the comment and its node by looking
|
|
268
|
-
// to see if a newline seperates them in the source text. We can trick prettier into
|
|
269
|
-
// formatting how we want for new comments by placing the range such that a newline
|
|
270
|
-
// will (OWN_LINE) or will not (INLINE) be found when searching from the specified range
|
|
271
|
-
// position.
|
|
272
|
-
switch (placement) {
|
|
273
|
-
case CommentPlacement.LEADING_OWN_LINE:
|
|
274
|
-
case CommentPlacement.TRAILING_OWN_LINE: {
|
|
275
|
-
newCode = makeCommentOwnLine(code, comment);
|
|
276
|
-
break;
|
|
277
|
-
}
|
|
278
|
-
case CommentPlacement.LEADING_INLINE:
|
|
279
|
-
case CommentPlacement.TRAILING_INLINE: {
|
|
280
|
-
// Since we don't want a line break we need to ensure a non whitespace char is
|
|
281
|
-
// always found before a newline when searching out from either side of the
|
|
282
|
-
// comment range.
|
|
283
|
-
let firstNonWhitespace = getFirstNonWhitespaceIndex(code);
|
|
284
|
-
if (firstNonWhitespace === -1) {
|
|
285
|
-
// No non whitespace chars in file, lets add an identifiable statement for prettier to find.
|
|
286
|
-
newCode += '$FORCE_INLINE_ON_EMPTY_FILE_TOKEN$;';
|
|
287
|
-
firstNonWhitespace = newCode.length;
|
|
288
|
-
break;
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
// $FlowExpectedError[cannot-write]
|
|
292
|
-
comment.range = [firstNonWhitespace + 1, firstNonWhitespace];
|
|
293
|
-
break;
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
break;
|
|
297
|
-
}
|
|
298
|
-
case 'Line': {
|
|
299
|
-
// For `Line` comments prettier slices comments directly from the source code when printing
|
|
300
|
-
// https://github.com/prettier/prettier/blob/5f0ee39fa03532c85bd1c35291450fe7ac3667b3/src/language-js/print/comment.js#L15-L20
|
|
301
|
-
// this means that we need to have any appended comments directly in the
|
|
302
|
-
// source code or else prettier will slice nothing and bork up the transform
|
|
303
|
-
const commentText = `//${comment.value}`;
|
|
304
|
-
|
|
305
|
-
const lastChar = newCode[newCode.length - 1];
|
|
306
|
-
if (lastChar !== '\n' && lastChar !== '\r') {
|
|
307
|
-
newCode += EOL;
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
// Line comments cannot be inline before a node so we only place trailing Line comments inline.
|
|
311
|
-
if (placement === CommentPlacement.TRAILING_INLINE) {
|
|
312
|
-
// Prettier determines an "end of line" comment by walking backwards from
|
|
313
|
-
// the comment start range through the source code to see if it finds a non
|
|
314
|
-
// newline token. In order to trick prettier for new comments we need to
|
|
315
|
-
// insert fake source code for it to find.
|
|
316
|
-
newCode += '$FORCE_END_OF_LINE_COMMENT_TOKEN$;';
|
|
317
|
-
}
|
|
318
|
-
const start = newCode.length;
|
|
319
|
-
newCode += commentText;
|
|
320
|
-
const end = newCode.length;
|
|
321
|
-
|
|
322
|
-
// $FlowExpectedError[cannot-write]
|
|
323
|
-
comment.range = [start, end];
|
|
324
|
-
|
|
325
|
-
break;
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
return newCode;
|
|
330
|
-
}
|