hermes-parser 0.28.1 → 0.29.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 (41) hide show
  1. package/dist/HermesASTAdapter.js +1 -2
  2. package/dist/HermesASTAdapter.js.flow +2 -5
  3. package/dist/HermesParserNodeDeserializers.js +3 -2
  4. package/dist/HermesParserWASM.js +1 -1
  5. package/dist/HermesToESTreeAdapter.js +12 -0
  6. package/dist/HermesToESTreeAdapter.js.flow +10 -0
  7. package/dist/babel/TransformESTreeToBabel.js.flow +21 -19
  8. package/dist/estree/StripComponentSyntax.js.flow +6 -5
  9. package/dist/estree/TransformMatchSyntax.js +4 -15
  10. package/dist/estree/TransformMatchSyntax.js.flow +18 -19
  11. package/dist/generated/ESTreeVisitorKeys.js +1 -1
  12. package/dist/generated/ParserVisitorKeys.js +1 -1
  13. package/dist/index.js.flow +1 -1
  14. package/dist/src/HermesASTAdapter.js +192 -0
  15. package/dist/src/HermesParser.js +108 -0
  16. package/dist/src/HermesParserDecodeUTF8String.js +68 -0
  17. package/dist/src/HermesParserDeserializer.js +243 -0
  18. package/dist/src/HermesParserNodeDeserializers.js +2473 -0
  19. package/dist/src/HermesToESTreeAdapter.js +453 -0
  20. package/dist/src/ParserOptions.js +18 -0
  21. package/dist/src/babel/TransformESTreeToBabel.js +1104 -0
  22. package/dist/src/estree/StripComponentSyntax.js +788 -0
  23. package/dist/src/estree/StripFlowTypes.js +175 -0
  24. package/dist/src/estree/StripFlowTypesForBabel.js +215 -0
  25. package/dist/src/estree/TransformMatchSyntax.js +1005 -0
  26. package/dist/src/generated/ESTreeVisitorKeys.js +220 -0
  27. package/dist/src/generated/ParserVisitorKeys.js +790 -0
  28. package/dist/src/getModuleDocblock.js +112 -0
  29. package/dist/src/index.js +153 -0
  30. package/dist/src/transform/SimpleTransform.js +120 -0
  31. package/dist/src/transform/astArrayMutationHelpers.js +62 -0
  32. package/dist/src/transform/astNodeMutationHelpers.js +195 -0
  33. package/dist/src/traverse/SimpleTraverser.js +137 -0
  34. package/dist/src/traverse/getVisitorKeys.js +37 -0
  35. package/dist/src/utils/Builders.js +191 -0
  36. package/dist/src/utils/GenID.js +41 -0
  37. package/dist/src/utils/createSyntaxError.js +25 -0
  38. package/dist/src/utils/mutateESTreeASTForPrettier.js +127 -0
  39. package/dist/utils/Builders.js +17 -0
  40. package/dist/utils/Builders.js.flow +20 -0
  41. package/package.json +2 -2
@@ -0,0 +1,137 @@
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
+ 'use strict';
11
+
12
+ Object.defineProperty(exports, "__esModule", {
13
+ value: true
14
+ });
15
+ exports.SimpleTraverserSkip = exports.SimpleTraverserBreak = exports.SimpleTraverser = void 0;
16
+
17
+ var _getVisitorKeys = require("./getVisitorKeys");
18
+
19
+ /**
20
+ * Can be thrown within the traversal "enter" function to prevent the traverser
21
+ * from traversing the node any further, essentially culling the remainder of the
22
+ * AST branch
23
+ */
24
+ const SimpleTraverserSkip = new Error();
25
+ /**
26
+ * Can be thrown at any point during the traversal to immediately stop traversal
27
+ * entirely.
28
+ */
29
+
30
+ exports.SimpleTraverserSkip = SimpleTraverserSkip;
31
+ const SimpleTraverserBreak = new Error();
32
+ /**
33
+ * A very simple traverser class to traverse AST trees.
34
+ */
35
+
36
+ exports.SimpleTraverserBreak = SimpleTraverserBreak;
37
+
38
+ class SimpleTraverser {
39
+ /**
40
+ * Traverse the given AST tree.
41
+ * @param node The root node to traverse.
42
+ * @param options The option object.
43
+ */
44
+ traverse(node, options) {
45
+ try {
46
+ this._traverse(node, null, options);
47
+ } catch (ex) {
48
+ if (ex === SimpleTraverserBreak) {
49
+ return;
50
+ }
51
+
52
+ throw ex;
53
+ }
54
+ }
55
+ /**
56
+ * Traverse the given AST tree recursively.
57
+ * @param node The current node.
58
+ * @param parent The parent node.
59
+ * @private
60
+ */
61
+
62
+
63
+ _traverse(node, parent, options) {
64
+ if (!(0, _getVisitorKeys.isNode)(node)) {
65
+ return;
66
+ }
67
+
68
+ try {
69
+ options.enter(node, parent);
70
+ } catch (ex) {
71
+ if (ex === SimpleTraverserSkip) {
72
+ return;
73
+ }
74
+
75
+ this._setErrorContext(ex, node);
76
+
77
+ throw ex;
78
+ }
79
+
80
+ const keys = (0, _getVisitorKeys.getVisitorKeys)(node, options.visitorKeys);
81
+
82
+ for (const key of keys) {
83
+ const child = node[key];
84
+
85
+ if (Array.isArray(child)) {
86
+ for (let j = 0; j < child.length; ++j) {
87
+ this._traverse(child[j], node, options);
88
+ }
89
+ } else {
90
+ this._traverse(child, node, options);
91
+ }
92
+ }
93
+
94
+ try {
95
+ options.leave(node, parent);
96
+ } catch (ex) {
97
+ if (ex === SimpleTraverserSkip) {
98
+ return;
99
+ }
100
+
101
+ this._setErrorContext(ex, node);
102
+
103
+ throw ex;
104
+ }
105
+ }
106
+ /**
107
+ * Set useful contextual information onto the error object.
108
+ * @param ex The error object.
109
+ * @param node The current node.
110
+ * @private
111
+ */
112
+
113
+
114
+ _setErrorContext(ex, node) {
115
+ // $FlowFixMe[prop-missing]
116
+ ex.currentNode = {
117
+ type: node.type,
118
+ range: node.range,
119
+ loc: node.loc
120
+ };
121
+ }
122
+ /**
123
+ * Traverse the given AST tree.
124
+ * @param node The root node to traverse.
125
+ * @param options The option object.
126
+ */
127
+
128
+
129
+ static traverse(node, options) {
130
+ new SimpleTraverser().traverse(node, options);
131
+ }
132
+
133
+ }
134
+
135
+ exports.SimpleTraverser = SimpleTraverser;
136
+ SimpleTraverser.Break = SimpleTraverserBreak;
137
+ SimpleTraverser.Skip = SimpleTraverserSkip;
@@ -0,0 +1,37 @@
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
+ * @noformat
9
+ */
10
+ 'use strict';
11
+
12
+ Object.defineProperty(exports, "__esModule", {
13
+ value: true
14
+ });
15
+ exports.getVisitorKeys = getVisitorKeys;
16
+ exports.isNode = isNode;
17
+
18
+ var _ESTreeVisitorKeys = _interopRequireDefault(require("../generated/ESTreeVisitorKeys"));
19
+
20
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
+
22
+ function isNode(thing)
23
+ /*: implies thing is {+[string]: mixed} */
24
+ {
25
+ return typeof thing === 'object' && thing != null && typeof thing.type === 'string';
26
+ }
27
+
28
+ function getVisitorKeys(node, visitorKeys) {
29
+ const keys = (visitorKeys != null ? visitorKeys : _ESTreeVisitorKeys.default)[node.type];
30
+
31
+ if (keys == null) {
32
+ throw new Error(`No visitor keys found for node type "${node.type}".`);
33
+ } // $FlowExpectedError[prop-missing]
34
+
35
+
36
+ return keys;
37
+ }
@@ -0,0 +1,191 @@
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
+ 'use strict';
11
+
12
+ Object.defineProperty(exports, "__esModule", {
13
+ value: true
14
+ });
15
+ exports.EMPTY_PARENT = void 0;
16
+ exports.callExpression = callExpression;
17
+ exports.conjunction = conjunction;
18
+ exports.createDefaultPosition = createDefaultPosition;
19
+ exports.disjunction = disjunction;
20
+ exports.etc = etc;
21
+ exports.ident = ident;
22
+ exports.iife = iife;
23
+ exports.nullLiteral = nullLiteral;
24
+ exports.numberLiteral = numberLiteral;
25
+ exports.stringLiteral = stringLiteral;
26
+ exports.throwStatement = throwStatement;
27
+ exports.typeofExpression = typeofExpression;
28
+ exports.variableDeclaration = variableDeclaration;
29
+ // Rely on the mapper to fix up parent relationships.
30
+ const EMPTY_PARENT = null;
31
+ exports.EMPTY_PARENT = EMPTY_PARENT;
32
+
33
+ function createDefaultPosition() {
34
+ return {
35
+ line: 1,
36
+ column: 0
37
+ };
38
+ }
39
+
40
+ function etc({
41
+ loc,
42
+ range,
43
+ parent
44
+ } = {}) {
45
+ return {
46
+ loc: {
47
+ start: (loc == null ? void 0 : loc.start) != null ? loc.start : createDefaultPosition(),
48
+ end: (loc == null ? void 0 : loc.end) != null ? loc.end : createDefaultPosition()
49
+ },
50
+ range: range != null ? range : [0, 0],
51
+ parent: parent != null ? parent : EMPTY_PARENT
52
+ };
53
+ }
54
+
55
+ function ident(name, info) {
56
+ return {
57
+ type: 'Identifier',
58
+ name,
59
+ optional: false,
60
+ typeAnnotation: null,
61
+ ...etc(info)
62
+ };
63
+ }
64
+
65
+ function stringLiteral(value, info) {
66
+ return {
67
+ type: 'Literal',
68
+ value,
69
+ raw: `"${value}"`,
70
+ literalType: 'string',
71
+ ...etc(info)
72
+ };
73
+ }
74
+
75
+ function numberLiteral(value, info) {
76
+ return {
77
+ type: 'Literal',
78
+ value,
79
+ raw: String(value),
80
+ literalType: 'numeric',
81
+ ...etc(info)
82
+ };
83
+ }
84
+
85
+ function nullLiteral(info) {
86
+ return {
87
+ type: 'Literal',
88
+ value: null,
89
+ raw: 'null',
90
+ literalType: 'null',
91
+ ...etc(info)
92
+ };
93
+ }
94
+
95
+ function conjunction(tests) {
96
+ if (tests.length === 0) {
97
+ throw new Error('Must have at least one test.');
98
+ }
99
+
100
+ return tests.reduce((acc, test) => ({
101
+ type: 'LogicalExpression',
102
+ left: acc,
103
+ right: test,
104
+ operator: '&&',
105
+ ...etc()
106
+ }));
107
+ }
108
+
109
+ function disjunction(tests) {
110
+ if (tests.length === 0) {
111
+ throw new Error('Must have at least one test.');
112
+ }
113
+
114
+ return tests.reduce((acc, test) => ({
115
+ type: 'LogicalExpression',
116
+ left: acc,
117
+ right: test,
118
+ operator: '||',
119
+ ...etc()
120
+ }));
121
+ }
122
+
123
+ function variableDeclaration(kind, id, init, info) {
124
+ return {
125
+ type: 'VariableDeclaration',
126
+ kind,
127
+ declarations: [{
128
+ type: 'VariableDeclarator',
129
+ init,
130
+ id,
131
+ ...etc(),
132
+ parent: EMPTY_PARENT
133
+ }],
134
+ ...etc(info)
135
+ };
136
+ }
137
+
138
+ function callExpression(callee, args, info) {
139
+ return {
140
+ type: 'CallExpression',
141
+ callee,
142
+ arguments: args,
143
+ typeArguments: null,
144
+ optional: false,
145
+ ...etc(info)
146
+ };
147
+ }
148
+
149
+ function throwStatement(arg, info) {
150
+ return {
151
+ type: 'ThrowStatement',
152
+ argument: callExpression(ident('Error'), [arg]),
153
+ ...etc(info)
154
+ };
155
+ }
156
+
157
+ function iife(statements, params = [], args = []) {
158
+ const callee = {
159
+ type: 'ArrowFunctionExpression',
160
+ params,
161
+ expression: false,
162
+ async: false,
163
+ predicate: null,
164
+ returnType: null,
165
+ typeParameters: null,
166
+ id: null,
167
+ body: {
168
+ type: 'BlockStatement',
169
+ body: statements,
170
+ ...etc()
171
+ },
172
+ ...etc()
173
+ };
174
+ return callExpression(callee, args);
175
+ }
176
+
177
+ function typeofExpression(arg, kind) {
178
+ return {
179
+ type: 'BinaryExpression',
180
+ left: {
181
+ type: 'UnaryExpression',
182
+ operator: 'typeof',
183
+ argument: arg,
184
+ prefix: true,
185
+ ...etc()
186
+ },
187
+ right: stringLiteral(kind),
188
+ operator: '===',
189
+ ...etc()
190
+ };
191
+ }
@@ -0,0 +1,41 @@
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
+ 'use strict';
11
+
12
+ Object.defineProperty(exports, "__esModule", {
13
+ value: true
14
+ });
15
+ exports.createGenID = createGenID;
16
+ const genPrefix = '$$gen$';
17
+
18
+ function createGenID(uniqueTransformPrefix) {
19
+ let genN = 0;
20
+ const used = new Set();
21
+ return {
22
+ genID() {
23
+ let name;
24
+
25
+ do {
26
+ name = `${genPrefix}${uniqueTransformPrefix}${genN}`;
27
+ genN++;
28
+ } while (used.has(name));
29
+
30
+ used.add(name);
31
+ return name;
32
+ },
33
+
34
+ addUsage(name) {
35
+ if (name.startsWith(genPrefix)) {
36
+ used.add(name);
37
+ }
38
+ }
39
+
40
+ };
41
+ }
@@ -0,0 +1,25 @@
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
+ 'use strict';
11
+
12
+ Object.defineProperty(exports, "__esModule", {
13
+ value: true
14
+ });
15
+ exports.createSyntaxError = createSyntaxError;
16
+
17
+ function createSyntaxError(node, err) {
18
+ const syntaxError = new SyntaxError(err); // $FlowExpectedError[prop-missing]
19
+
20
+ syntaxError.loc = {
21
+ line: node.loc.start.line,
22
+ column: node.loc.start.column
23
+ };
24
+ return syntaxError;
25
+ }
@@ -0,0 +1,127 @@
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
+ 'use strict';
11
+
12
+ Object.defineProperty(exports, "__esModule", {
13
+ value: true
14
+ });
15
+ exports.default = mutate;
16
+
17
+ var _SimpleTransform = require("../transform/SimpleTransform");
18
+
19
+ // https://github.com/prettier/prettier/blob/d962466a828f8ef51435e3e8840178d90b7ec6cd/src/language-js/parse/postprocess/index.js#L161-L182
20
+ function transformChainExpression(node, comments) {
21
+ if (comments != null) {
22
+ var _node$comments;
23
+
24
+ // $FlowExpectedError[prop-missing]
25
+ const joinedComments = comments.concat((_node$comments = node.comments) != null ? _node$comments : []); // $FlowExpectedError[prop-missing]
26
+ // $FlowFixMe[cannot-write]
27
+
28
+ node.comments = joinedComments;
29
+ }
30
+
31
+ switch (node.type) {
32
+ case 'CallExpression':
33
+ // $FlowExpectedError[cannot-spread-interface]
34
+ return { ...node,
35
+ type: 'OptionalCallExpression',
36
+ callee: transformChainExpression(node.callee)
37
+ };
38
+
39
+ case 'MemberExpression':
40
+ // $FlowExpectedError[cannot-spread-interface]
41
+ return { ...node,
42
+ type: 'OptionalMemberExpression',
43
+ object: transformChainExpression(node.object)
44
+ };
45
+ // No default
46
+ }
47
+
48
+ return node;
49
+ }
50
+
51
+ function mutate(rootNode, visitorKeys) {
52
+ // Since we don't return the result of `transform` we need to be careful not to replace the Program root node.
53
+ _SimpleTransform.SimpleTransform.transform(rootNode, {
54
+ transform(node) {
55
+ // prettier fully expects the parent pointers are NOT set and
56
+ // certain cases can crash due to prettier infinite-looping
57
+ // whilst naively traversing the parent property
58
+ // https://github.com/prettier/prettier/issues/11793
59
+ // Note: Only needed for prettier V2, this is supported in V3
60
+ if (node.parent) {
61
+ // $FlowExpectedError[cannot-write]
62
+ delete node.parent;
63
+ } // prettier currently relies on the AST being in the old-school, deprecated AST format for optional chaining
64
+ // so we have to apply their transform to our AST so it can actually format it.
65
+ // Note: Only needed for prettier V2, this is supported in V3
66
+
67
+
68
+ if (node.type === 'ChainExpression') {
69
+ // $FlowFixMe[prop-missing]
70
+ return transformChainExpression(node.expression, node == null ? void 0 : node.comments);
71
+ } // Prettier currently relies on comparing the `node` vs `node.value` start positions to know if an
72
+ // `ObjectTypeProperty` is a method or not (instead of using the `node.method` boolean). To correctly print
73
+ // the node when its not a method we need the start position to be different from the `node.value`s start
74
+ // position.
75
+
76
+
77
+ if (node.type === 'ObjectTypeProperty') {
78
+ if (node.method === false && node.kind === 'init' && node.range[0] === 1 && node.value.range[0] === 1) {
79
+ // $FlowExpectedError[cannot-write]
80
+ // $FlowExpectedError[cannot-spread-interface]
81
+ node.value = { ...node.value,
82
+ range: [2, node.value.range[1]]
83
+ };
84
+ }
85
+
86
+ return node;
87
+ } // Prettier currently relies on comparing the the start positions to know if the import/export specifier should have a
88
+ // rename (eg `Name` vs `Name as Name`) when the name is exactly the same
89
+ // So we need to ensure that the range is always the same to avoid the useless code printing
90
+
91
+
92
+ if (node.type === 'ImportSpecifier') {
93
+ if (node.local.name === node.imported.name) {
94
+ if (node.local.range == null) {
95
+ // for our TS-ast printing which has no locs
96
+ // $FlowExpectedError[cannot-write]
97
+ node.local.range = [0, 0];
98
+ } // $FlowExpectedError[cannot-write]
99
+
100
+
101
+ node.imported.range = [...node.local.range];
102
+ }
103
+
104
+ return node;
105
+ }
106
+
107
+ if (node.type === 'ExportSpecifier') {
108
+ if (node.local.name === node.exported.name) {
109
+ if (node.local.range == null) {
110
+ // for our TS-ast printing which has no locs
111
+ // $FlowExpectedError[cannot-write]
112
+ node.local.range = [0, 0];
113
+ } // $FlowExpectedError[cannot-write]
114
+
115
+
116
+ node.exported.range = [...node.local.range];
117
+ }
118
+
119
+ return node;
120
+ }
121
+
122
+ return node;
123
+ },
124
+
125
+ visitorKeys
126
+ });
127
+ }
@@ -24,6 +24,7 @@ exports.nullLiteral = nullLiteral;
24
24
  exports.numberLiteral = numberLiteral;
25
25
  exports.stringLiteral = stringLiteral;
26
26
  exports.throwStatement = throwStatement;
27
+ exports.typeofExpression = typeofExpression;
27
28
  exports.variableDeclaration = variableDeclaration;
28
29
  // Rely on the mapper to fix up parent relationships.
29
30
  const EMPTY_PARENT = null;
@@ -171,4 +172,20 @@ function iife(statements, params = [], args = []) {
171
172
  ...etc()
172
173
  };
173
174
  return callExpression(callee, args);
175
+ }
176
+
177
+ function typeofExpression(arg, kind) {
178
+ return {
179
+ type: 'BinaryExpression',
180
+ left: {
181
+ type: 'UnaryExpression',
182
+ operator: 'typeof',
183
+ argument: arg,
184
+ prefix: true,
185
+ ...etc()
186
+ },
187
+ right: stringLiteral(kind),
188
+ operator: '===',
189
+ ...etc()
190
+ };
174
191
  }
@@ -12,6 +12,7 @@
12
12
 
13
13
  import type {
14
14
  ArrowFunctionExpression,
15
+ BinaryExpression,
15
16
  BindingName,
16
17
  CallExpression,
17
18
  ESNode,
@@ -196,3 +197,22 @@ export function iife(
196
197
  };
197
198
  return callExpression(callee, args);
198
199
  }
200
+
201
+ export function typeofExpression(
202
+ arg: Expression,
203
+ kind: string,
204
+ ): BinaryExpression {
205
+ return {
206
+ type: 'BinaryExpression',
207
+ left: {
208
+ type: 'UnaryExpression',
209
+ operator: 'typeof',
210
+ argument: arg,
211
+ prefix: true,
212
+ ...etc(),
213
+ },
214
+ right: stringLiteral(kind),
215
+ operator: '===',
216
+ ...etc(),
217
+ };
218
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hermes-parser",
3
- "version": "0.28.1",
3
+ "version": "0.29.0",
4
4
  "description": "A JavaScript parser built from the Hermes engine",
5
5
  "main": "dist/index.js",
6
6
  "license": "MIT",
@@ -9,7 +9,7 @@
9
9
  "url": "git@github.com:facebook/hermes.git"
10
10
  },
11
11
  "dependencies": {
12
- "hermes-estree": "0.28.1"
12
+ "hermes-estree": "0.29.0"
13
13
  },
14
14
  "devDependencies": {
15
15
  "@babel/parser": "7.7.4",