hermes-transform 0.36.1 → 0.37.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 (115) hide show
  1. package/dist/detachedNode.js +3 -57
  2. package/dist/detachedNode.js.flow +12 -12
  3. package/dist/generated/TransformCloneSignatures.js.flow +1 -1
  4. package/dist/generated/TransformModifySignatures.js.flow +1 -1
  5. package/dist/generated/TransformReplaceSignatures.js.flow +207 -207
  6. package/dist/generated/node-types.js +0 -21
  7. package/dist/generated/node-types.js.flow +1086 -978
  8. package/dist/generated/special-case-node-types/Comment.js +0 -13
  9. package/dist/generated/special-case-node-types/Comment.js.flow +2 -2
  10. package/dist/generated/special-case-node-types/DeclareExportDeclaration.js +0 -12
  11. package/dist/generated/special-case-node-types/DeclareExportDeclaration.js.flow +10 -10
  12. package/dist/generated/special-case-node-types/DeclareHook.js +0 -9
  13. package/dist/generated/special-case-node-types/DeclareHook.js.flow +4 -4
  14. package/dist/generated/special-case-node-types/ExportNamedDeclaration.js +0 -11
  15. package/dist/generated/special-case-node-types/ExportNamedDeclaration.js.flow +9 -9
  16. package/dist/generated/special-case-node-types/Literal.js +1 -12
  17. package/dist/generated/special-case-node-types/Literal.js.flow +20 -20
  18. package/dist/generated/special-case-node-types/ObjectTypeProperty.js +0 -10
  19. package/dist/generated/special-case-node-types/ObjectTypeProperty.js.flow +23 -21
  20. package/dist/generated/special-case-node-types/Property.js +0 -9
  21. package/dist/generated/special-case-node-types/Property.js.flow +50 -42
  22. package/dist/generated/special-case-node-types/misc.js +6 -35
  23. package/dist/generated/special-case-node-types/misc.js.flow +56 -48
  24. package/dist/index.js +0 -9
  25. package/dist/src/detachedNode.js +3 -57
  26. package/dist/src/generated/node-types.js +0 -21
  27. package/dist/src/generated/special-case-node-types/Comment.js +0 -13
  28. package/dist/src/generated/special-case-node-types/DeclareExportDeclaration.js +0 -12
  29. package/dist/src/generated/special-case-node-types/DeclareHook.js +0 -9
  30. package/dist/src/generated/special-case-node-types/ExportNamedDeclaration.js +0 -11
  31. package/dist/src/generated/special-case-node-types/Literal.js +1 -12
  32. package/dist/src/generated/special-case-node-types/ObjectTypeProperty.js +0 -10
  33. package/dist/src/generated/special-case-node-types/Property.js +0 -9
  34. package/dist/src/generated/special-case-node-types/misc.js +6 -35
  35. package/dist/src/index.js +0 -9
  36. package/dist/src/transform/Errors.js +0 -9
  37. package/dist/src/transform/MutationContext.js +1 -28
  38. package/dist/src/transform/TransformContext.js +0 -18
  39. package/dist/src/transform/comments/comments.js +12 -78
  40. package/dist/src/transform/comments/prettier/common/util.js +4 -133
  41. package/dist/src/transform/comments/prettier/language-js/comments.js +12 -137
  42. package/dist/src/transform/comments/prettier/language-js/loc.js +1 -13
  43. package/dist/src/transform/comments/prettier/language-js/printer-estree.js +1 -10
  44. package/dist/src/transform/comments/prettier/language-js/utils.js +3 -33
  45. package/dist/src/transform/comments/prettier/main/comments.js +11 -77
  46. package/dist/src/transform/comments/prettier/utils/get-last.js +0 -8
  47. package/dist/src/transform/mutations/AddComments.js +0 -9
  48. package/dist/src/transform/mutations/CloneCommentsTo.js +0 -9
  49. package/dist/src/transform/mutations/InsertStatement.js +4 -16
  50. package/dist/src/transform/mutations/ModifyNodeInPlace.js +4 -17
  51. package/dist/src/transform/mutations/RemoveComment.js +0 -10
  52. package/dist/src/transform/mutations/RemoveNode.js +1 -23
  53. package/dist/src/transform/mutations/RemoveStatement.js +0 -16
  54. package/dist/src/transform/mutations/ReplaceNode.js +2 -16
  55. package/dist/src/transform/mutations/ReplaceStatementWithMany.js +2 -14
  56. package/dist/src/transform/mutations/utils/getStatementParent.js +3 -19
  57. package/dist/src/transform/mutations/utils/isValidModuleDeclarationParent.js +5 -20
  58. package/dist/src/transform/parse.js +6 -25
  59. package/dist/src/transform/print.js +9 -35
  60. package/dist/src/transform/transform.js +0 -9
  61. package/dist/src/transform/transformAST.js +3 -31
  62. package/dist/src/traverse/NodeEventGenerator.js +2 -108
  63. package/dist/src/traverse/SafeEmitter.js +0 -19
  64. package/dist/src/traverse/esquery.js +0 -18
  65. package/dist/src/traverse/traverse.js +1 -22
  66. package/dist/transform/Errors.js +0 -9
  67. package/dist/transform/MutationContext.js +1 -28
  68. package/dist/transform/MutationContext.js.flow +3 -3
  69. package/dist/transform/TransformContext.js +0 -18
  70. package/dist/transform/TransformContext.js.flow +60 -54
  71. package/dist/transform/comments/comments.js +12 -78
  72. package/dist/transform/comments/comments.js.flow +8 -8
  73. package/dist/transform/comments/prettier/common/util.js +4 -133
  74. package/dist/transform/comments/prettier/language-js/comments.js +12 -137
  75. package/dist/transform/comments/prettier/language-js/loc.js +1 -13
  76. package/dist/transform/comments/prettier/language-js/printer-estree.js +1 -10
  77. package/dist/transform/comments/prettier/language-js/utils.js +3 -33
  78. package/dist/transform/comments/prettier/main/comments.js +11 -77
  79. package/dist/transform/comments/prettier/utils/get-last.js +0 -8
  80. package/dist/transform/mutations/AddComments.js +0 -9
  81. package/dist/transform/mutations/AddComments.js.flow +2 -2
  82. package/dist/transform/mutations/CloneCommentsTo.js +0 -9
  83. package/dist/transform/mutations/CloneCommentsTo.js.flow +1 -1
  84. package/dist/transform/mutations/InsertStatement.js +4 -16
  85. package/dist/transform/mutations/InsertStatement.js.flow +7 -6
  86. package/dist/transform/mutations/ModifyNodeInPlace.js +4 -17
  87. package/dist/transform/mutations/ModifyNodeInPlace.js.flow +2 -2
  88. package/dist/transform/mutations/RemoveComment.js +0 -10
  89. package/dist/transform/mutations/RemoveComment.js.flow +4 -4
  90. package/dist/transform/mutations/RemoveNode.js +1 -23
  91. package/dist/transform/mutations/RemoveNode.js.flow +9 -9
  92. package/dist/transform/mutations/RemoveStatement.js +0 -16
  93. package/dist/transform/mutations/RemoveStatement.js.flow +3 -3
  94. package/dist/transform/mutations/ReplaceNode.js +2 -16
  95. package/dist/transform/mutations/ReplaceNode.js.flow +6 -6
  96. package/dist/transform/mutations/ReplaceStatementWithMany.js +2 -14
  97. package/dist/transform/mutations/ReplaceStatementWithMany.js.flow +7 -8
  98. package/dist/transform/mutations/utils/getStatementParent.js +3 -19
  99. package/dist/transform/mutations/utils/getStatementParent.js.flow +5 -6
  100. package/dist/transform/mutations/utils/isValidModuleDeclarationParent.js +5 -20
  101. package/dist/transform/mutations/utils/isValidModuleDeclarationParent.js.flow +4 -3
  102. package/dist/transform/parse.js +6 -25
  103. package/dist/transform/print.js +9 -35
  104. package/dist/transform/print.js.flow +7 -15
  105. package/dist/transform/transform.js +0 -9
  106. package/dist/transform/transformAST.js +3 -31
  107. package/dist/traverse/NodeEventGenerator.js +2 -108
  108. package/dist/traverse/NodeEventGenerator.js.flow +13 -9
  109. package/dist/traverse/SafeEmitter.js +0 -19
  110. package/dist/traverse/SafeEmitter.js.flow +2 -2
  111. package/dist/traverse/esquery.js +0 -18
  112. package/dist/traverse/esquery.js.flow +10 -10
  113. package/dist/traverse/traverse.js +1 -22
  114. package/dist/traverse/traverse.js.flow +4 -4
  115. package/package.json +7 -12
@@ -5,34 +5,19 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.isValidModuleDeclarationParent = isValidModuleDeclarationParent;
7
7
 
8
- /**
9
- * Copyright (c) Meta Platforms, Inc. and affiliates.
10
- *
11
- * This source code is licensed under the MIT license found in the
12
- * LICENSE file in the root directory of this source tree.
13
- *
14
- *
15
- * @noformat
16
- */
17
- function isModuleDeclaration(node)
18
- /*: node is (
19
- | ImportDeclaration
20
- | ExportNamedDeclaration
21
- | ExportDefaultDeclaration
22
- | ExportAllDeclaration
23
- ) */
24
- {
8
+ function isModuleDeclaration(node) {
25
9
  return node.type === 'ImportDeclaration' || node.type === 'ExportNamedDeclaration' || node.type === 'ExportDefaultDeclaration' || node.type === 'ExportAllDeclaration';
26
10
  }
27
11
 
28
12
  function isValidModuleDeclarationParent(target, nodesToInsertOrReplace) {
29
- if (target.type === 'Program' || target.type === 'BlockStatement' && target.parent.type === 'DeclareModule') {
13
+ var _target$parent;
14
+
15
+ if (target.type === 'Program' || target.type === 'BlockStatement' && ((_target$parent = target.parent) == null ? void 0 : _target$parent.type) === 'DeclareModule') {
30
16
  return true;
31
17
  }
32
18
 
33
19
  for (const node of nodesToInsertOrReplace) {
34
- if (!isModuleDeclaration( // $FlowExpectedError[incompatible-type]
35
- node)) {
20
+ if (!isModuleDeclaration(node)) {
36
21
  continue;
37
22
  }
38
23
 
@@ -1,12 +1,3 @@
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
- *
8
- * @format
9
- */
10
1
  'use strict';
11
2
 
12
3
  Object.defineProperty(exports, "__esModule", {
@@ -25,27 +16,17 @@ function parse(code) {
25
16
  } = (0, _hermesEslint.parseForESLint)(code, {
26
17
  sourceType: 'module',
27
18
  enableExperimentalComponentSyntax: true
28
- }); // Don't include the docblock comment in the comment list as we don't want to attach it
29
- // as it should be maintained at the top of the file as nodes are moved around.
30
-
19
+ });
31
20
  let comments = ast.comments;
32
21
 
33
22
  if (ast.docblock != null && ast.docblock.comment === comments[0]) {
34
- const [first, ...nonDocblockComments] = comments; // Since we will not be attaching this comment automatically we need to add the
35
- // properties prettier expects for printing.
36
- // $FlowExpectedError[prop-missing]
37
-
38
- first.placement = 'endOfLine'; // $FlowExpectedError[prop-missing]
39
-
40
- first.leading = true; // $FlowExpectedError[prop-missing]
41
-
42
- first.trailing = false; // $FlowExpectedError[prop-missing]
43
-
23
+ const [first, ...nonDocblockComments] = comments;
24
+ first.placement = 'endOfLine';
25
+ first.leading = true;
26
+ first.trailing = false;
44
27
  first.printed = false;
45
28
  comments = nonDocblockComments;
46
- } // attach comments before mutation. this will ensure that as nodes are
47
- // cloned / moved around - comments remain in the correct place with respect to the node
48
-
29
+ }
49
30
 
50
31
  (0, _comments.attachComments)(comments, ast, code);
51
32
  return {
@@ -1,12 +1,3 @@
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
- *
8
- * @format
9
- */
10
1
  'use strict';
11
2
 
12
3
  Object.defineProperty(exports, "__esModule", {
@@ -25,13 +16,11 @@ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "functio
25
16
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
26
17
 
27
18
  async function print(ast, originalCode, prettierOptions = {}, visitorKeys) {
28
- // $FlowExpectedError[incompatible-type] This is now safe to access.
29
- const program = ast; // If the AST body is empty, we can skip the cost of prettier by returning a static string of the contents.
19
+ const program = ast;
30
20
 
31
21
  if (program.body.length === 0) {
32
22
  var _program$docblock;
33
23
 
34
- // If the program had a docblock comment, we need to create the string manually.
35
24
  const docblockComment = (_program$docblock = program.docblock) == null ? void 0 : _program$docblock.comment;
36
25
 
37
26
  if (docblockComment != null) {
@@ -39,31 +28,16 @@ async function print(ast, originalCode, prettierOptions = {}, visitorKeys) {
39
28
  }
40
29
 
41
30
  return '';
42
- } // Cleanup the comments from the AST and generate the "orginal" code needed for prettier.
43
-
44
-
45
- const codeForPrinting = (0, _comments.mutateESTreeASTCommentsForPrettier)(program, originalCode); // Fix up the AST to match what prettier expects.
31
+ }
46
32
 
33
+ const codeForPrinting = (0, _comments.mutateESTreeASTCommentsForPrettier)(program, originalCode);
47
34
  (0, _hermesParser.mutateESTreeASTForPrettier)(program, visitorKeys);
48
- let pluginParserName = 'flow';
49
- let pluginParser;
50
- let pluginPrinter;
51
-
52
- try {
53
- // Use prettier-plugin-hermes-parser if we can. It has latest Flow syntax support.
54
- // $FlowExpectedError[untyped-import]
55
- const prettierHermesPlugin = await Promise.resolve().then(() => _interopRequireWildcard(require('prettier-plugin-hermes-parser')));
56
- pluginParser = prettierHermesPlugin.parsers.hermes;
57
- pluginPrinter = prettierHermesPlugin.printers;
58
- pluginParserName = 'hermes';
59
- } catch {
60
- const prettierFlowPlugin = require('prettier/plugins/flow');
61
-
62
- pluginParser = prettierFlowPlugin.parsers.flow;
63
- }
64
35
 
65
- return prettier.format(codeForPrinting, // $FlowExpectedError[incompatible-exact] - we don't want to create a dependency on the prettier types
66
- { ...prettierOptions,
36
+ const prettierEstreePlugin = require('prettier/plugins/estree');
37
+
38
+ const pluginParserName = visitorKeys != null ? 'typescript' : 'flow';
39
+ const pluginParser = visitorKeys != null ? require('prettier/plugins/typescript').parsers.typescript : require('prettier/plugins/flow').parsers.flow;
40
+ return prettier.format(codeForPrinting, { ...prettierOptions,
67
41
  parser: pluginParserName,
68
42
  requirePragma: false,
69
43
  plugins: [{
@@ -76,7 +50,7 @@ async function print(ast, originalCode, prettierOptions = {}, visitorKeys) {
76
50
 
77
51
  }
78
52
  },
79
- printers: pluginPrinter
53
+ printers: prettierEstreePlugin.printers
80
54
  }]
81
55
  });
82
56
  }
@@ -1,12 +1,3 @@
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
- *
8
- * @format
9
- */
10
1
  'use strict';
11
2
 
12
3
  Object.defineProperty(exports, "__esModule", {
@@ -1,12 +1,3 @@
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
- *
8
- * @format
9
- */
10
1
  'use strict';
11
2
 
12
3
  Object.defineProperty(exports, "__esModule", {
@@ -45,10 +36,8 @@ function transformAST({
45
36
  scopeManager,
46
37
  code
47
38
  }, visitors) {
48
- // traverse the AST and collect the mutations
49
39
  const transformContext = (0, _TransformContext.getTransformContext)();
50
- (0, _traverse.traverseWithContext)(code, ast, scopeManager, () => transformContext, visitors); // apply the mutations to the AST
51
-
40
+ (0, _traverse.traverseWithContext)(code, ast, scopeManager, () => transformContext, visitors);
52
41
  const mutationContext = new _MutationContext.MutationContext(code);
53
42
  const removeCommentMutations = [];
54
43
 
@@ -87,7 +76,6 @@ function transformAST({
87
76
 
88
77
  case 'removeComment':
89
78
  {
90
- // these are handled later
91
79
  removeCommentMutations.push(mutation);
92
80
  return null;
93
81
  }
@@ -102,28 +90,12 @@ function transformAST({
102
90
  return (0, _CloneCommentsTo.performCloneCommentsToMutation)(mutationContext, mutation);
103
91
  }
104
92
  }
105
- })(); // ensure the subtree's parent pointers are correct
106
- // this is required for two reasons:
107
- // 1) The userland transform is just JS - so there's nothing stopping them
108
- // from doing anything dodgy. The flow types have some enforcement, but
109
- // ofc that can just be ignored with a suppression.
110
- // 2) Shallow clones are a necessary evil in the transform because they
111
- // allow codemods to do simple changes to just one node without the
112
- // weight that comes with deeply cloning the entire AST.
113
- // However we can't update the parent pointers of the cloned node's
114
- // children until the mutation step or else we would be mutating
115
- // real AST nodes and potentially break the traverse step.
116
- //
117
- // Being strict here just helps us ensure we keep everything in sync
118
-
93
+ })();
119
94
 
120
95
  if (mutationRoot) {
121
96
  (0, _detachedNode.updateAllParentPointers)(mutationRoot);
122
97
  }
123
- } // remove the comments
124
- // this is done at the end because it requires a complete traversal of the AST
125
- // so that we can find relevant node's attachment array
126
-
98
+ }
127
99
 
128
100
  (0, _RemoveComment.performRemoveCommentMutations)(ast, removeCommentMutations);
129
101
  return {
@@ -1,12 +1,3 @@
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
- *
8
- * @format
9
- */
10
1
  'use strict';
11
2
 
12
3
  Object.defineProperty(exports, "__esModule", {
@@ -28,21 +19,10 @@ const ESQUERY_OPTIONS = Object.freeze({
28
19
  throw new Error(`No visitor keys found for node type "${node.type}".`);
29
20
  }
30
21
  });
31
- /**
32
- * Computes the union of one or more arrays
33
- * @param arrays One or more arrays to union
34
- * @returns The union of the input arrays
35
- */
36
22
 
37
23
  function union(...arrays) {
38
24
  return [...new Set(arrays.flat())];
39
25
  }
40
- /**
41
- * Computes the intersection of one or more arrays
42
- * @param arrays One or more arrays to intersect
43
- * @returns The intersection of the input arrays
44
- */
45
-
46
26
 
47
27
  function intersection(...arrays) {
48
28
  if (arrays.length === 0) {
@@ -57,20 +37,13 @@ function intersection(...arrays) {
57
37
 
58
38
  return result;
59
39
  }
60
- /**
61
- * Gets the possible types of a selector
62
- * @param parsedSelector An object (from esquery) describing the matching behavior of the selector
63
- * @returns The node types that could possibly trigger this selector, or `null` if all node types could trigger it
64
- */
65
-
66
40
 
67
41
  function getPossibleTypes(parsedSelector) {
68
42
  switch (parsedSelector.type) {
69
43
  case 'identifier':
70
44
  if (!(parsedSelector.value in _hermesParser.FlowVisitorKeys)) {
71
45
  throw new Error(`Unexpected selector ${parsedSelector.value}`);
72
- } // $FlowExpectedError[incompatible-type]
73
-
46
+ }
74
47
 
75
48
  return [parsedSelector.value];
76
49
 
@@ -88,16 +61,11 @@ function getPossibleTypes(parsedSelector) {
88
61
 
89
62
  case 'compound':
90
63
  {
91
- const typesForComponents = parsedSelector.selectors.map(getPossibleTypes).filter(Boolean); // If all of the components could match any type, then the compound could also match any type.
64
+ const typesForComponents = parsedSelector.selectors.map(getPossibleTypes).filter(Boolean);
92
65
 
93
66
  if (!typesForComponents.length) {
94
67
  return null;
95
68
  }
96
- /*
97
- * If at least one of the components could only match a particular type, the compound could only match
98
- * the intersection of those types.
99
- */
100
-
101
69
 
102
70
  return intersection(...typesForComponents);
103
71
  }
@@ -119,12 +87,6 @@ function getPossibleTypes(parsedSelector) {
119
87
  return null;
120
88
  }
121
89
  }
122
- /**
123
- * Counts the number of class, pseudo-class, and attribute queries in this selector
124
- * @param parsedSelector An object (from esquery) describing the selector's matching behavior
125
- * @returns The number of class, pseudo-class, and attribute queries in this selector
126
- */
127
-
128
90
 
129
91
  function countClassAttributes(parsedSelector) {
130
92
  switch (parsedSelector.type) {
@@ -149,12 +111,6 @@ function countClassAttributes(parsedSelector) {
149
111
  return 0;
150
112
  }
151
113
  }
152
- /**
153
- * Counts the number of identifier queries in this selector
154
- * @param parsedSelector An object (from esquery) describing the selector's matching behavior
155
- * @returns The number of identifier queries
156
- */
157
-
158
114
 
159
115
  function countIdentifiers(parsedSelector) {
160
116
  switch (parsedSelector.type) {
@@ -176,28 +132,10 @@ function countIdentifiers(parsedSelector) {
176
132
  return 0;
177
133
  }
178
134
  }
179
- /**
180
- * Compares the specificity of two selector objects, with CSS-like rules.
181
- * @param selectorA An AST selector descriptor
182
- * @param selectorB Another AST selector descriptor
183
- * @returns
184
- * a value less than 0 if selectorA is less specific than selectorB
185
- * a value greater than 0 if selectorA is more specific than selectorB
186
- * a value less than 0 if selectorA and selectorB have the same specificity, and selectorA <= selectorB alphabetically
187
- * a value greater than 0 if selectorA and selectorB have the same specificity, and selectorA > selectorB alphabetically
188
- */
189
-
190
135
 
191
136
  function compareSpecificity(selectorA, selectorB) {
192
137
  return selectorA.attributeCount - selectorB.attributeCount || selectorA.identifierCount - selectorB.identifierCount || (selectorA.rawSelector <= selectorB.rawSelector ? -1 : 1);
193
138
  }
194
- /**
195
- * Parses a raw selector string, and throws a useful error if parsing fails.
196
- * @param rawSelector A raw AST selector
197
- * @returns An object (from esquery) describing the matching behavior of this selector
198
- * @throws An error if the selector is invalid
199
- */
200
-
201
139
 
202
140
  function tryParseSelector(rawSelector) {
203
141
  try {
@@ -212,11 +150,6 @@ function tryParseSelector(rawSelector) {
212
150
  }
213
151
 
214
152
  const selectorCache = new Map();
215
- /**
216
- * Parses a raw selector string, and returns the parsed selector along with specificity and type information.
217
- * @param rawSelector A raw AST selector
218
- * @returns A selector descriptor
219
- */
220
153
 
221
154
  function parseSelector(rawSelector) {
222
155
  const cachedSelector = selectorCache.get(rawSelector);
@@ -237,18 +170,8 @@ function parseSelector(rawSelector) {
237
170
  selectorCache.set(rawSelector, result);
238
171
  return result;
239
172
  }
240
- /**
241
- * The event generator for AST nodes.
242
- */
243
-
244
173
 
245
174
  class NodeEventGenerator {
246
- /**
247
- * @param emitter
248
- * An SafeEmitter which is the destination of events. This emitter must already
249
- * have registered listeners for all of the events that it needs to listen for.
250
- * (See lib/linter/safe-emitter.js for more details on `SafeEmitter`.)
251
- */
252
175
  constructor(emitter) {
253
176
  this.emitter = void 0;
254
177
  this._currentAncestry = [];
@@ -285,35 +208,16 @@ class NodeEventGenerator {
285
208
 
286
209
  this._exitSelectorsByNodeType.forEach(selectorList => selectorList.sort(compareSpecificity));
287
210
  }
288
- /**
289
- * Checks a selector against a node, and emits it if it matches
290
- * @param node The node to check
291
- * @param selector An AST selector descriptor
292
- * @private
293
- */
294
-
295
211
 
296
212
  _applySelector(node, selector) {
297
213
  if (esquery.matches(node, selector.parsedSelector, this._currentAncestry, ESQUERY_OPTIONS)) {
298
214
  this.emitter.emit(selector.rawSelector, node);
299
215
  }
300
216
  }
301
- /**
302
- * Applies all appropriate selectors to a node, in specificity order
303
- * @param node The node to check
304
- * @param isExit `false` if the node is currently being entered, `true` if it's currently being exited
305
- * @private
306
- */
307
-
308
217
 
309
218
  _applySelectors(node, isExit) {
310
219
  const selectorsByNodeType = (isExit ? this._exitSelectorsByNodeType : this._enterSelectorsByNodeType).get(node.type) || [];
311
220
  const anyTypeSelectors = isExit ? this._anyTypeExitSelectors : this._anyTypeEnterSelectors;
312
- /*
313
- * selectorsByNodeType and anyTypeSelectors were already sorted by specificity in the constructor.
314
- * Iterate through each of them, applying selectors in the right order.
315
- */
316
-
317
221
  let selectorsByTypeIndex = 0;
318
222
  let anyTypeSelectorsIndex = 0;
319
223
 
@@ -325,22 +229,12 @@ class NodeEventGenerator {
325
229
  }
326
230
  }
327
231
  }
328
- /**
329
- * Emits an event of entering AST node.
330
- * @param node A node which was entered.
331
- */
332
-
333
232
 
334
233
  enterNode(node) {
335
234
  this._applySelectors(node, false);
336
235
 
337
236
  this._currentAncestry.unshift(node);
338
237
  }
339
- /**
340
- * Emits an event of leaving AST node.
341
- * @param node A node which was left.
342
- */
343
-
344
238
 
345
239
  leaveNode(node) {
346
240
  this._currentAncestry.shift();
@@ -1,12 +1,3 @@
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
- *
8
- * @format
9
- */
10
1
  'use strict';
11
2
 
12
3
  Object.defineProperty(exports, "__esModule", {
@@ -14,16 +5,6 @@ Object.defineProperty(exports, "__esModule", {
14
5
  });
15
6
  exports.SafeEmitter = void 0;
16
7
 
17
- /**
18
- * Creates an object which can listen for and emit events.
19
- * This is similar to the EventEmitter API in Node's standard library, but it has a few differences.
20
- * The goal is to allow multiple modules to attach arbitrary listeners to the same emitter, without
21
- * letting the modules know about each other at all.
22
- * 1. It has no special keys like `error` and `newListener`, which would allow modules to detect when
23
- * another module throws an error or registers a listener.
24
- * 2. It calls listener functions without any `this` value. (`EventEmitter` calls listeners with a
25
- * `this` value of the emitter instance, which would give listeners access to other listeners.)
26
- */
27
8
  class SafeEmitter {
28
9
  constructor() {
29
10
  this.listeners = Object.create(null);
@@ -1,12 +1,3 @@
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
- *
8
- * @format
9
- */
10
1
  'use strict';
11
2
 
12
3
  Object.defineProperty(exports, "__esModule", {
@@ -18,20 +9,11 @@ var _esquery = _interopRequireDefault(require("esquery"));
18
9
 
19
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
20
11
 
21
- // $FlowExpectedError[cannot-resolve-module]
22
-
23
- /** Parse a selector and return its AST. */
24
12
  const parse = _esquery.default.parse;
25
- /** From a JS AST and a selector AST, collect all JS AST nodes that match the selector. */
26
-
27
13
  exports.parse = parse;
28
14
  const match = _esquery.default.match;
29
- /** Given a `node` and its ancestors, determine if `node` is matched by `selector`. */
30
-
31
15
  exports.match = match;
32
16
  const matches = _esquery.default.matches;
33
- /** Query the code AST using the selector string. */
34
-
35
17
  exports.matches = matches;
36
18
  const query = _esquery.default.query;
37
19
  exports.query = query;
@@ -14,22 +14,6 @@ var _SafeEmitter = require("./SafeEmitter");
14
14
 
15
15
  var _hermesParser = require("hermes-parser");
16
16
 
17
- /**
18
- * Copyright (c) Meta Platforms, Inc. and affiliates.
19
- *
20
- * This source code is licensed under the MIT license found in the
21
- * LICENSE file in the root directory of this source tree.
22
- *
23
- *
24
- * @format
25
- */
26
-
27
- /**
28
- * Traverse the AST with additional context members provided by `additionalContext`.
29
- * @param ast the ESTree AST to traverse
30
- * @param scopeManager the eslint-scope compatible scope manager instance calculated using the ast
31
- * @param additionalContext a callback function which returns additional context members to add to the context provided to the visitor
32
- */
33
17
  function traverseWithContext(code, ast, scopeManager, additionalContext, visitor) {
34
18
  const emitter = new _SafeEmitter.SafeEmitter();
35
19
  let currentNode = ast;
@@ -37,7 +21,6 @@ function traverseWithContext(code, ast, scopeManager, additionalContext, visitor
37
21
  let shouldStopTraversal = false;
38
22
 
39
23
  const getScope = (givenNode = currentNode) => {
40
- // On Program node, get the outermost scope to avoid return Node.js special function scope or ES modules scope.
41
24
  const inner = givenNode.type !== 'Program';
42
25
 
43
26
  for (let node = givenNode; node; node = node.parent) {
@@ -57,7 +40,6 @@ function traverseWithContext(code, ast, scopeManager, additionalContext, visitor
57
40
 
58
41
  const traversalContextBase = Object.freeze({
59
42
  buildCodeFrame: (node, message) => {
60
- // babel uses 1-indexed columns
61
43
  const locForBabel = {
62
44
  start: {
63
45
  line: node.loc.start.line,
@@ -105,10 +87,8 @@ function traverseWithContext(code, ast, scopeManager, additionalContext, visitor
105
87
  const traversalContext = Object.freeze({ ...traversalContextBase,
106
88
  ...additionalContext(traversalContextBase)
107
89
  });
108
- const selectors = visitor(traversalContext); // add all the selectors from the visitor as listeners
109
-
90
+ const selectors = visitor(traversalContext);
110
91
  Object.keys(selectors).forEach(selector => {
111
- // flow doesn't want us to be general here - but it's safe
112
92
  const listener = selectors[selector];
113
93
 
114
94
  if (listener) {
@@ -119,7 +99,6 @@ function traverseWithContext(code, ast, scopeManager, additionalContext, visitor
119
99
 
120
100
  function checkTraversalFlags() {
121
101
  if (shouldStopTraversal) {
122
- // No need to reset the flag since we won't enter any more nodes.
123
102
  throw _hermesParser.SimpleTraverser.Break;
124
103
  }
125
104
 
@@ -5,15 +5,6 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.UnexpectedTransformationState = exports.NodeIsMutatedError = exports.NodeIsDeletedError = exports.InvalidStatementError = exports.InvalidReplacementError = exports.InvalidRemovalError = exports.InvalidInsertionError = void 0;
7
7
 
8
- /**
9
- * Copyright (c) Meta Platforms, Inc. and affiliates.
10
- *
11
- * This source code is licensed under the MIT license found in the
12
- * LICENSE file in the root directory of this source tree.
13
- *
14
- *
15
- * @format
16
- */
17
8
  class NodeIsDeletedError extends Error {}
18
9
 
19
10
  exports.NodeIsDeletedError = NodeIsDeletedError;
@@ -9,17 +9,7 @@ var _Errors = require("./Errors");
9
9
 
10
10
  var _comments = require("./comments/comments");
11
11
 
12
- /**
13
- * Copyright (c) Meta Platforms, Inc. and affiliates.
14
- *
15
- * This source code is licensed under the MIT license found in the
16
- * LICENSE file in the root directory of this source tree.
17
- *
18
- *
19
- * @format
20
- */
21
12
  class MutationContext {
22
- // TODO - do we care about this? Arrays are pretty safe to concurrently mutate
23
13
  constructor(code) {
24
14
  this._deletedNodes = new Set();
25
15
  this._mutatedKeys = new Map();
@@ -27,42 +17,25 @@ class MutationContext {
27
17
  this.code = void 0;
28
18
  this.code = code;
29
19
  }
30
- /**
31
- * Marks a node and its entire subtree as deleted.
32
- */
33
-
34
20
 
35
21
  markDeletion(node) {
36
22
  this._deletedNodes.add(node);
37
23
  }
38
- /**
39
- * Marks the key of the node as having been mutated.
40
- */
41
-
42
24
 
43
25
  markMutation(node, key) {
44
26
  var _map$get$add, _map$get;
45
27
 
46
28
  this.assertNotDeleted(node, `Attempted to mutate a \`${node.type}.${key}\` on a deleted node.`);
47
29
  this.assertNotMutated(node, key, `Attempted to mutate a \`${node.type}.${key}\` when it has already been mutated.`);
48
- const map = Array.isArray( // $FlowExpectedError[prop-missing]
49
- node[key]) ? this._mutatedArrays : this._mutatedKeys;
30
+ const map = Array.isArray(node[key]) ? this._mutatedArrays : this._mutatedKeys;
50
31
  map.set(node, (_map$get$add = (_map$get = map.get(node)) == null ? void 0 : _map$get.add(key)) != null ? _map$get$add : new Set([key]));
51
32
  }
52
- /**
53
- * Throws if the node has been deleted
54
- */
55
-
56
33
 
57
34
  assertNotDeleted(node, message) {
58
35
  if (this._deletedNodes.has(node)) {
59
36
  throw new _Errors.NodeIsDeletedError(message);
60
37
  }
61
38
  }
62
- /**
63
- * Throws if the key of the node has been mutated
64
- */
65
-
66
39
 
67
40
  assertNotMutated(node, key, message) {
68
41
  var _this$_mutatedKeys$ge;
@@ -15,10 +15,10 @@ import {NodeIsDeletedError, NodeIsMutatedError} from './Errors';
15
15
  import {appendCommentToSource} from './comments/comments';
16
16
 
17
17
  export class MutationContext {
18
- +_deletedNodes: Set<ESNode> = new Set();
19
- +_mutatedKeys: Map<ESNode, Set<string>> = new Map();
18
+ readonly _deletedNodes: Set<ESNode> = new Set();
19
+ readonly _mutatedKeys: Map<ESNode, Set<string>> = new Map();
20
20
  // TODO - do we care about this? Arrays are pretty safe to concurrently mutate
21
- +_mutatedArrays: Map<ESNode, Set<string>> = new Map();
21
+ readonly _mutatedArrays: Map<ESNode, Set<string>> = new Map();
22
22
 
23
23
  code: string;
24
24