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