flow-parser 0.320.0 → 0.321.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 +14 -26
- package/package.json +6 -7
- package/flow_parser.js +0 -14585
- package/oxidized/oxidized-src/FlowParser.js +0 -249
- package/oxidized/oxidized-src/FlowParserDeserializer.js +0 -209
- package/oxidized/oxidized-src/FlowParserNodeDeserializers.js +0 -1781
- package/oxidized/oxidized-src/HermesParserDecodeUTF8String.js +0 -45
- package/oxidized/oxidized-src/ParserOptions.js +0 -8
- package/oxidized/oxidized-src/babel/TransformESTreeToBabel.js +0 -1063
- package/oxidized/oxidized-src/estree/StripFlowTypes.js +0 -158
- package/oxidized/oxidized-src/estree/StripFlowTypesForBabel.js +0 -191
- package/oxidized/oxidized-src/estree/TransformComponentSyntax.js +0 -764
- package/oxidized/oxidized-src/estree/TransformEnumSyntax.js +0 -92
- package/oxidized/oxidized-src/estree/TransformMatchSyntax.js +0 -990
- package/oxidized/oxidized-src/estree/TransformRecordSyntax.js +0 -283
- package/oxidized/oxidized-src/generated/ESTreeVisitorKeys.js +0 -234
- package/oxidized/oxidized-src/getModuleDocblock.js +0 -87
- package/oxidized/oxidized-src/index.js +0 -220
- package/oxidized/transform/SimpleTransform.js +0 -95
- package/oxidized/transform/astArrayMutationHelpers.js +0 -52
- package/oxidized/transform/astNodeMutationHelpers.js +0 -142
- package/oxidized/transform/print/comments/comments.js +0 -245
- package/oxidized/transform/print/comments/prettier/common/util.js +0 -235
- package/oxidized/transform/print/comments/prettier/language-js/comments.js +0 -663
- package/oxidized/transform/print/comments/prettier/language-js/loc.js +0 -30
- package/oxidized/transform/print/comments/prettier/language-js/printer-estree.js +0 -23
- package/oxidized/transform/print/comments/prettier/language-js/utils.js +0 -89
- package/oxidized/transform/print/comments/prettier/main/comments.js +0 -374
- package/oxidized/transform/print/comments/prettier/utils/get-last.js +0 -5
- package/oxidized/transform/print/detachedNodeTypes.js +0 -1
- package/oxidized/transform/print/print.js +0 -68
- package/oxidized/traverse/SimpleTraverser.js +0 -90
- package/oxidized/traverse/getVisitorKeys.js +0 -29
- package/oxidized/utils/Builders.js +0 -181
- package/oxidized/utils/GenID.js +0 -37
- package/oxidized/utils/createSyntaxError.js +0 -15
- package/oxidized/utils/isReservedWord.js +0 -53
- package/oxidized/utils/mutateESTreeASTForPrettier.js +0 -85
- /package/{oxidized → dist}/FlowParser.js +0 -0
- /package/{oxidized → dist}/FlowParser.js.flow +0 -0
- /package/{oxidized → dist}/FlowParserDeserializer.js +0 -0
- /package/{oxidized → dist}/FlowParserDeserializer.js.flow +0 -0
- /package/{oxidized → dist}/FlowParserNodeDeserializers.js +0 -0
- /package/{oxidized → dist}/FlowParserNodeDeserializers.js.flow +0 -0
- /package/{oxidized → dist}/FlowParserWASM.js +0 -0
- /package/{oxidized → dist}/FlowParserWASM.js.flow +0 -0
- /package/{oxidized → dist}/HermesAST.js.flow +0 -0
- /package/{oxidized → dist}/HermesParserDecodeUTF8String.js +0 -0
- /package/{oxidized → dist}/HermesParserDecodeUTF8String.js.flow +0 -0
- /package/{oxidized → dist}/ParserOptions.js +0 -0
- /package/{oxidized → dist}/ParserOptions.js.flow +0 -0
- /package/{oxidized → dist}/babel/TransformESTreeToBabel.js +0 -0
- /package/{oxidized → dist}/babel/TransformESTreeToBabel.js.flow +0 -0
- /package/{oxidized → dist}/estree/StripFlowTypes.js +0 -0
- /package/{oxidized → dist}/estree/StripFlowTypes.js.flow +0 -0
- /package/{oxidized → dist}/estree/StripFlowTypesForBabel.js +0 -0
- /package/{oxidized → dist}/estree/StripFlowTypesForBabel.js.flow +0 -0
- /package/{oxidized → dist}/estree/TransformComponentSyntax.js +0 -0
- /package/{oxidized → dist}/estree/TransformComponentSyntax.js.flow +0 -0
- /package/{oxidized → dist}/estree/TransformEnumSyntax.js +0 -0
- /package/{oxidized → dist}/estree/TransformEnumSyntax.js.flow +0 -0
- /package/{oxidized → dist}/estree/TransformMatchSyntax.js +0 -0
- /package/{oxidized → dist}/estree/TransformMatchSyntax.js.flow +0 -0
- /package/{oxidized → dist}/estree/TransformRecordSyntax.js +0 -0
- /package/{oxidized → dist}/estree/TransformRecordSyntax.js.flow +0 -0
- /package/{oxidized → dist}/generated/ESTreeVisitorKeys.js +0 -0
- /package/{oxidized → dist}/generated/ESTreeVisitorKeys.js.flow +0 -0
- /package/{oxidized → dist}/getModuleDocblock.js +0 -0
- /package/{oxidized → dist}/getModuleDocblock.js.flow +0 -0
- /package/{oxidized → dist}/index.js +0 -0
- /package/{oxidized → dist}/index.js.flow +0 -0
- /package/{oxidized/oxidized-src → dist}/transform/SimpleTransform.js +0 -0
- /package/{oxidized → dist}/transform/SimpleTransform.js.flow +0 -0
- /package/{oxidized/oxidized-src → dist}/transform/astArrayMutationHelpers.js +0 -0
- /package/{oxidized → dist}/transform/astArrayMutationHelpers.js.flow +0 -0
- /package/{oxidized/oxidized-src → dist}/transform/astNodeMutationHelpers.js +0 -0
- /package/{oxidized → dist}/transform/astNodeMutationHelpers.js.flow +0 -0
- /package/{oxidized/oxidized-src → dist}/transform/print/comments/comments.js +0 -0
- /package/{oxidized → dist}/transform/print/comments/comments.js.flow +0 -0
- /package/{oxidized/oxidized-src → dist}/transform/print/comments/prettier/common/util.js +0 -0
- /package/{oxidized/oxidized-src → dist}/transform/print/comments/prettier/language-js/comments.js +0 -0
- /package/{oxidized/oxidized-src → dist}/transform/print/comments/prettier/language-js/loc.js +0 -0
- /package/{oxidized/oxidized-src → dist}/transform/print/comments/prettier/language-js/printer-estree.js +0 -0
- /package/{oxidized/oxidized-src → dist}/transform/print/comments/prettier/language-js/utils.js +0 -0
- /package/{oxidized/oxidized-src → dist}/transform/print/comments/prettier/main/comments.js +0 -0
- /package/{oxidized/oxidized-src → dist}/transform/print/comments/prettier/utils/get-last.js +0 -0
- /package/{oxidized/oxidized-src → dist}/transform/print/detachedNodeTypes.js +0 -0
- /package/{oxidized → dist}/transform/print/detachedNodeTypes.js.flow +0 -0
- /package/{oxidized/oxidized-src → dist}/transform/print/print.js +0 -0
- /package/{oxidized → dist}/transform/print/print.js.flow +0 -0
- /package/{oxidized/oxidized-src → dist}/traverse/SimpleTraverser.js +0 -0
- /package/{oxidized → dist}/traverse/SimpleTraverser.js.flow +0 -0
- /package/{oxidized/oxidized-src → dist}/traverse/getVisitorKeys.js +0 -0
- /package/{oxidized → dist}/traverse/getVisitorKeys.js.flow +0 -0
- /package/{oxidized/oxidized-src → dist}/utils/Builders.js +0 -0
- /package/{oxidized → dist}/utils/Builders.js.flow +0 -0
- /package/{oxidized/oxidized-src → dist}/utils/GenID.js +0 -0
- /package/{oxidized → dist}/utils/GenID.js.flow +0 -0
- /package/{oxidized/oxidized-src → dist}/utils/createSyntaxError.js +0 -0
- /package/{oxidized → dist}/utils/createSyntaxError.js.flow +0 -0
- /package/{oxidized/oxidized-src → dist}/utils/isReservedWord.js +0 -0
- /package/{oxidized → dist}/utils/isReservedWord.js.flow +0 -0
- /package/{oxidized/oxidized-src → dist}/utils/mutateESTreeASTForPrettier.js +0 -0
- /package/{oxidized → dist}/utils/mutateESTreeASTForPrettier.js.flow +0 -0
|
@@ -1,245 +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
|
-
|
|
25
|
-
var _comments = require("./prettier/main/comments");
|
|
26
|
-
|
|
27
|
-
var _loc = require("./prettier/language-js/loc");
|
|
28
|
-
|
|
29
|
-
var _printerEstree = _interopRequireDefault(require("./prettier/language-js/printer-estree"));
|
|
30
|
-
|
|
31
|
-
var _util = require("./prettier/common/util");
|
|
32
|
-
|
|
33
|
-
var _flowEstree = require("flow-estree");
|
|
34
|
-
|
|
35
|
-
var _os = require("os");
|
|
36
|
-
|
|
37
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
38
|
-
|
|
39
|
-
const CommentPlacement = require("flow-enums-runtime").Mirrored(["LEADING_OWN_LINE", "LEADING_INLINE", "TRAILING_OWN_LINE", "TRAILING_INLINE"]);
|
|
40
|
-
|
|
41
|
-
exports.CommentPlacement = CommentPlacement;
|
|
42
|
-
|
|
43
|
-
function attachComments(comments, ast, text) {
|
|
44
|
-
(0, _comments.attach)(comments, ast, text, {
|
|
45
|
-
locStart: _loc.locStart,
|
|
46
|
-
locEnd: _loc.locEnd,
|
|
47
|
-
printer: _printerEstree.default
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function mutateESTreeASTCommentsForPrettier(program, text) {
|
|
52
|
-
let code = text;
|
|
53
|
-
delete program.comments;
|
|
54
|
-
|
|
55
|
-
if (program.docblock != null && program.docblock.comment != null) {
|
|
56
|
-
const docblockComment = program.docblock.comment;
|
|
57
|
-
const isDocblockCommentNew = !isAttachedComment(docblockComment);
|
|
58
|
-
|
|
59
|
-
if (isDocblockCommentNew) {
|
|
60
|
-
docblockComment.printed = false;
|
|
61
|
-
docblockComment.leading = true;
|
|
62
|
-
docblockComment.trailing = false;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
if (program.body.length > 0) {
|
|
66
|
-
const firstStatement = program.body[0];
|
|
67
|
-
const leadingComments = getLeadingCommentsForNode(firstStatement);
|
|
68
|
-
|
|
69
|
-
if (!leadingComments.includes(docblockComment)) {
|
|
70
|
-
setCommentsOnNode(firstStatement, [docblockComment, ...getCommentsForNode(firstStatement)]);
|
|
71
|
-
|
|
72
|
-
if (isDocblockCommentNew) {
|
|
73
|
-
code = makeCommentOwnLine(code, docblockComment);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
} else {
|
|
77
|
-
setCommentsOnNode(program, [docblockComment]);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
delete program.docblock;
|
|
82
|
-
return code;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function moveCommentsToNewNode(oldNode, newNode) {
|
|
86
|
-
setCommentsOnNode(newNode, getCommentsForNode(oldNode));
|
|
87
|
-
setCommentsOnNode(oldNode, []);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
function cloneCommentsToNewNode(oldNode, newNode) {
|
|
91
|
-
setCommentsOnNode(newNode, getCommentsForNode(oldNode).map(comment => cloneCommentWithMarkers(comment)));
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
function cloneJSDocCommentsToNewNode(oldNode, newNode) {
|
|
95
|
-
const comments = getCommentsForNode(oldNode).filter(comment => {
|
|
96
|
-
return (0, _flowEstree.isBlockComment)(comment) && comment.value.startsWith('*');
|
|
97
|
-
});
|
|
98
|
-
setCommentsOnNode(newNode, [...getCommentsForNode(newNode), ...comments.map(cloneCommentWithMarkers)]);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function setCommentsOnNode(node, comments) {
|
|
102
|
-
node.comments = comments;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
function getCommentsForNode(node) {
|
|
106
|
-
var _node$comments;
|
|
107
|
-
|
|
108
|
-
return (_node$comments = node.comments) != null ? _node$comments : [];
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
function isAttachedComment(comment) {
|
|
112
|
-
return comment.printed === false;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
function isLeadingComment(comment) {
|
|
116
|
-
return comment.leading === true;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
function isTrailingComment(comment) {
|
|
120
|
-
return comment.trailing === true;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
function getLeadingCommentsForNode(node) {
|
|
124
|
-
return getCommentsForNode(node).filter(isLeadingComment);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
function getTrailingCommentsForNode(node) {
|
|
128
|
-
return getCommentsForNode(node).filter(isTrailingComment);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
function addComment(node, comment, placement) {
|
|
132
|
-
switch (placement) {
|
|
133
|
-
case CommentPlacement.LEADING_OWN_LINE:
|
|
134
|
-
case CommentPlacement.LEADING_INLINE:
|
|
135
|
-
{
|
|
136
|
-
(0, _util.addLeadingComment)(node, comment);
|
|
137
|
-
break;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
case CommentPlacement.TRAILING_OWN_LINE:
|
|
141
|
-
case CommentPlacement.TRAILING_INLINE:
|
|
142
|
-
{
|
|
143
|
-
(0, _util.addTrailingComment)(node, comment);
|
|
144
|
-
break;
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
function cloneComment(comment) {
|
|
150
|
-
return {
|
|
151
|
-
type: comment.type,
|
|
152
|
-
value: comment.value,
|
|
153
|
-
loc: comment.loc,
|
|
154
|
-
range: comment.range
|
|
155
|
-
};
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
function cloneCommentWithMarkers(comment) {
|
|
159
|
-
return {
|
|
160
|
-
type: comment.type,
|
|
161
|
-
value: comment.value,
|
|
162
|
-
loc: comment.loc,
|
|
163
|
-
range: comment.range,
|
|
164
|
-
leading: isLeadingComment(comment),
|
|
165
|
-
trailing: isTrailingComment(comment)
|
|
166
|
-
};
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
function getFirstNewlineIndex(code) {
|
|
170
|
-
return code.search(/\r\n|\n|\r/);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
function getFirstNonWhitespaceIndex(code) {
|
|
174
|
-
return code.search(/\S/);
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
function makeCommentOwnLine(code, comment) {
|
|
178
|
-
let newCode = code;
|
|
179
|
-
let firstNewline = getFirstNewlineIndex(code);
|
|
180
|
-
|
|
181
|
-
if (firstNewline === -1) {
|
|
182
|
-
newCode += _os.EOL;
|
|
183
|
-
firstNewline = newCode.length;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
comment.range = [firstNewline + 1, firstNewline];
|
|
187
|
-
return newCode;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
function appendCommentToSource(code, comment, placement) {
|
|
191
|
-
let newCode = code;
|
|
192
|
-
|
|
193
|
-
switch (comment.type) {
|
|
194
|
-
case 'Block':
|
|
195
|
-
{
|
|
196
|
-
switch (placement) {
|
|
197
|
-
case CommentPlacement.LEADING_OWN_LINE:
|
|
198
|
-
case CommentPlacement.TRAILING_OWN_LINE:
|
|
199
|
-
{
|
|
200
|
-
newCode = makeCommentOwnLine(code, comment);
|
|
201
|
-
break;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
case CommentPlacement.LEADING_INLINE:
|
|
205
|
-
case CommentPlacement.TRAILING_INLINE:
|
|
206
|
-
{
|
|
207
|
-
let firstNonWhitespace = getFirstNonWhitespaceIndex(code);
|
|
208
|
-
|
|
209
|
-
if (firstNonWhitespace === -1) {
|
|
210
|
-
newCode += '$FORCE_INLINE_ON_EMPTY_FILE_TOKEN$;';
|
|
211
|
-
firstNonWhitespace = newCode.length;
|
|
212
|
-
break;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
comment.range = [firstNonWhitespace + 1, firstNonWhitespace];
|
|
216
|
-
break;
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
break;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
case 'Line':
|
|
224
|
-
{
|
|
225
|
-
const commentText = `//${comment.value}`;
|
|
226
|
-
const lastChar = newCode[newCode.length - 1];
|
|
227
|
-
|
|
228
|
-
if (lastChar !== '\n' && lastChar !== '\r') {
|
|
229
|
-
newCode += _os.EOL;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
if (placement === CommentPlacement.TRAILING_INLINE) {
|
|
233
|
-
newCode += '$FORCE_END_OF_LINE_COMMENT_TOKEN$;';
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
const start = newCode.length;
|
|
237
|
-
newCode += commentText;
|
|
238
|
-
const end = newCode.length;
|
|
239
|
-
comment.range = [start, end];
|
|
240
|
-
break;
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
return newCode;
|
|
245
|
-
}
|
|
@@ -1,235 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const stringWidth = require('string-width');
|
|
4
|
-
|
|
5
|
-
const getLast = require('../utils/get-last.js');
|
|
6
|
-
|
|
7
|
-
const notAsciiRegex = /[^\x20-\x7F]/;
|
|
8
|
-
|
|
9
|
-
function skip(chars) {
|
|
10
|
-
return (text, index, opts) => {
|
|
11
|
-
const backwards = opts && opts.backwards;
|
|
12
|
-
|
|
13
|
-
if (index === false) {
|
|
14
|
-
return false;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const {
|
|
18
|
-
length
|
|
19
|
-
} = text;
|
|
20
|
-
let cursor = index;
|
|
21
|
-
|
|
22
|
-
while (cursor >= 0 && cursor < length) {
|
|
23
|
-
const c = text.charAt(cursor);
|
|
24
|
-
|
|
25
|
-
if (chars instanceof RegExp) {
|
|
26
|
-
if (!chars.test(c)) {
|
|
27
|
-
return cursor;
|
|
28
|
-
}
|
|
29
|
-
} else if (!chars.includes(c)) {
|
|
30
|
-
return cursor;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
backwards ? cursor-- : cursor++;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
if (cursor === -1 || cursor === length) {
|
|
37
|
-
return cursor;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
return false;
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const skipWhitespace = skip(/\s/);
|
|
45
|
-
const skipSpaces = skip(' \t');
|
|
46
|
-
const skipToLineEnd = skip(',; \t');
|
|
47
|
-
const skipEverythingButNewLine = skip(/[^\n\r]/);
|
|
48
|
-
|
|
49
|
-
function skipInlineComment(text, index) {
|
|
50
|
-
if (index === false) {
|
|
51
|
-
return false;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
if (text.charAt(index) === '/' && text.charAt(index + 1) === '*') {
|
|
55
|
-
for (let i = index + 2; i < text.length; ++i) {
|
|
56
|
-
if (text.charAt(i) === '*' && text.charAt(i + 1) === '/') {
|
|
57
|
-
return i + 2;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
return index;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
function skipTrailingComment(text, index) {
|
|
66
|
-
if (index === false) {
|
|
67
|
-
return false;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
if (text.charAt(index) === '/' && text.charAt(index + 1) === '/') {
|
|
71
|
-
return skipEverythingButNewLine(text, index);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
return index;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function skipNewline(text, index, opts) {
|
|
78
|
-
const backwards = opts && opts.backwards;
|
|
79
|
-
|
|
80
|
-
if (index === false) {
|
|
81
|
-
return false;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const atIndex = text.charAt(index);
|
|
85
|
-
|
|
86
|
-
if (backwards) {
|
|
87
|
-
if (text.charAt(index - 1) === '\r' && atIndex === '\n') {
|
|
88
|
-
return index - 2;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
if (atIndex === '\n' || atIndex === '\r' || atIndex === '\u2028' || atIndex === '\u2029') {
|
|
92
|
-
return index - 1;
|
|
93
|
-
}
|
|
94
|
-
} else {
|
|
95
|
-
if (atIndex === '\r' && text.charAt(index + 1) === '\n') {
|
|
96
|
-
return index + 2;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
if (atIndex === '\n' || atIndex === '\r' || atIndex === '\u2028' || atIndex === '\u2029') {
|
|
100
|
-
return index + 1;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
return index;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
function hasNewline(text, index, opts = {}) {
|
|
108
|
-
const idx = skipSpaces(text, opts.backwards ? index - 1 : index, opts);
|
|
109
|
-
const idx2 = skipNewline(text, idx, opts);
|
|
110
|
-
return idx !== idx2;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function hasNewlineInRange(text, start, end) {
|
|
114
|
-
for (let i = start; i < end; ++i) {
|
|
115
|
-
if (text.charAt(i) === '\n') {
|
|
116
|
-
return true;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
return false;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
function isNextLineEmptyAfterIndex(text, index) {
|
|
124
|
-
let oldIdx = null;
|
|
125
|
-
let idx = index;
|
|
126
|
-
|
|
127
|
-
while (idx !== oldIdx) {
|
|
128
|
-
oldIdx = idx;
|
|
129
|
-
idx = skipToLineEnd(text, idx);
|
|
130
|
-
idx = skipInlineComment(text, idx);
|
|
131
|
-
idx = skipSpaces(text, idx);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
idx = skipTrailingComment(text, idx);
|
|
135
|
-
idx = skipNewline(text, idx);
|
|
136
|
-
return idx !== false && hasNewline(text, idx);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
function getNextNonSpaceNonCommentCharacterIndexWithStartIndex(text, idx) {
|
|
140
|
-
let oldIdx = null;
|
|
141
|
-
let nextIdx = idx;
|
|
142
|
-
|
|
143
|
-
while (nextIdx !== oldIdx) {
|
|
144
|
-
oldIdx = nextIdx;
|
|
145
|
-
nextIdx = skipSpaces(text, nextIdx);
|
|
146
|
-
nextIdx = skipInlineComment(text, nextIdx);
|
|
147
|
-
nextIdx = skipTrailingComment(text, nextIdx);
|
|
148
|
-
nextIdx = skipNewline(text, nextIdx);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
return nextIdx;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
function getNextNonSpaceNonCommentCharacterIndex(text, node, locEnd) {
|
|
155
|
-
return getNextNonSpaceNonCommentCharacterIndexWithStartIndex(text, locEnd(node));
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
function getNextNonSpaceNonCommentCharacter(text, node, locEnd) {
|
|
159
|
-
return text.charAt(getNextNonSpaceNonCommentCharacterIndex(text, node, locEnd));
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
function getStringWidth(text) {
|
|
163
|
-
if (!text) {
|
|
164
|
-
return 0;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
if (!notAsciiRegex.test(text)) {
|
|
168
|
-
return text.length;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
return stringWidth(text);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
function addCommentHelper(node, comment) {
|
|
175
|
-
const comments = node.comments || (node.comments = []);
|
|
176
|
-
comments.push(comment);
|
|
177
|
-
comment.printed = false;
|
|
178
|
-
comment.nodeDescription = describeNodeForDebugging(node);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
function addLeadingComment(node, comment) {
|
|
182
|
-
comment.leading = true;
|
|
183
|
-
comment.trailing = false;
|
|
184
|
-
addCommentHelper(node, comment);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
function addDanglingComment(node, comment, marker) {
|
|
188
|
-
comment.leading = false;
|
|
189
|
-
comment.trailing = false;
|
|
190
|
-
|
|
191
|
-
if (marker) {
|
|
192
|
-
comment.marker = marker;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
addCommentHelper(node, comment);
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
function addTrailingComment(node, comment) {
|
|
199
|
-
comment.leading = false;
|
|
200
|
-
comment.trailing = true;
|
|
201
|
-
addCommentHelper(node, comment);
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
function isNonEmptyArray(object) {
|
|
205
|
-
return Array.isArray(object) && object.length > 0;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
function describeNodeForDebugging(node) {
|
|
209
|
-
const nodeType = node.type || node.kind || '(unknown type)';
|
|
210
|
-
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 || '');
|
|
211
|
-
|
|
212
|
-
if (nodeName.length > 20) {
|
|
213
|
-
nodeName = nodeName.slice(0, 19) + '…';
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
return nodeType + (nodeName ? ' ' + nodeName : '');
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
module.exports = {
|
|
220
|
-
getStringWidth,
|
|
221
|
-
getLast,
|
|
222
|
-
getNextNonSpaceNonCommentCharacterIndexWithStartIndex,
|
|
223
|
-
getNextNonSpaceNonCommentCharacterIndex,
|
|
224
|
-
getNextNonSpaceNonCommentCharacter,
|
|
225
|
-
skipWhitespace,
|
|
226
|
-
skipSpaces,
|
|
227
|
-
skipNewline,
|
|
228
|
-
isNextLineEmptyAfterIndex,
|
|
229
|
-
hasNewline,
|
|
230
|
-
hasNewlineInRange,
|
|
231
|
-
addLeadingComment,
|
|
232
|
-
addDanglingComment,
|
|
233
|
-
addTrailingComment,
|
|
234
|
-
isNonEmptyArray
|
|
235
|
-
};
|