hermes-parser 0.17.0 → 0.18.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.
@@ -129,7 +129,7 @@ function mapComponentParameters(params) {
129
129
  } // Optimize `component Foo(...props: Props) {}` to `function Foo(props: Props) {}
130
130
 
131
131
 
132
- if (params.length === 1 && params[0].type === 'RestElement' && (params[0].argument.type === 'Identifier' || params[0].argument.type === 'ObjectPattern')) {
132
+ if (params.length === 1 && params[0].type === 'RestElement' && params[0].argument.type === 'Identifier') {
133
133
  var _restElementArgument$, _restElementArgument$2;
134
134
 
135
135
  const restElementArgument = params[0].argument;
@@ -151,7 +151,7 @@ function mapComponentParameters(params) {
151
151
 
152
152
  return true;
153
153
  });
154
- const propsProperties = paramsWithoutRef.map(mapComponentParameter);
154
+ const propsProperties = paramsWithoutRef.flatMap(mapComponentParameter);
155
155
  let props = null;
156
156
 
157
157
  if (propsProperties.length === 0) {
@@ -195,16 +195,7 @@ function mapComponentParameters(params) {
195
195
  let ref = null;
196
196
 
197
197
  if (refParam != null) {
198
- const refType = refParam.local;
199
- ref = {
200
- type: 'Identifier',
201
- name: 'ref',
202
- optional: false,
203
- typeAnnotation: refType.type === 'AssignmentPattern' ? refType.left.typeAnnotation : refType.typeAnnotation,
204
- loc: refParam.loc,
205
- range: refParam.range,
206
- parent: EMPTY_PARENT
207
- };
198
+ ref = refParam.local;
208
199
  }
209
200
 
210
201
  return {
@@ -217,21 +208,36 @@ function mapComponentParameter(param) {
217
208
  switch (param.type) {
218
209
  case 'RestElement':
219
210
  {
220
- const a = nodeWith(param, {
221
- typeAnnotation: null,
222
- argument: nodeWith(param.argument, {
223
- typeAnnotation: null
224
- })
225
- });
226
- return a;
211
+ switch (param.argument.type) {
212
+ case 'Identifier':
213
+ {
214
+ const a = nodeWith(param, {
215
+ typeAnnotation: null,
216
+ argument: nodeWith(param.argument, {
217
+ typeAnnotation: null
218
+ })
219
+ });
220
+ return [a];
221
+ }
222
+
223
+ case 'ObjectPattern':
224
+ {
225
+ return param.argument.properties.map(property => {
226
+ return nodeWith(property, {
227
+ typeAnnotation: null
228
+ });
229
+ });
230
+ }
231
+
232
+ default:
233
+ {
234
+ throw (0, _createSyntaxError.createSyntaxError)(param, `Unhandled ${param.argument.type} encountered in restParameter`);
235
+ }
236
+ }
227
237
  }
228
238
 
229
239
  case 'ComponentParameter':
230
240
  {
231
- if (getComponentParameterName(param.name) === 'ref') {
232
- throw (0, _createSyntaxError.createSyntaxError)(param, 'Component parameters named "ref" are currently not supported');
233
- }
234
-
235
241
  let value;
236
242
 
237
243
  if (param.local.type === 'AssignmentPattern') {
@@ -250,7 +256,7 @@ function mapComponentParameter(param) {
250
256
 
251
257
 
252
258
  if (param.name.type === 'Identifier' && param.shorthand && (value.type === 'Identifier' || value.type === 'AssignmentPattern')) {
253
- return {
259
+ return [{
254
260
  type: 'Property',
255
261
  key: param.name,
256
262
  kind: 'init',
@@ -261,11 +267,11 @@ function mapComponentParameter(param) {
261
267
  loc: param.loc,
262
268
  range: param.range,
263
269
  parent: EMPTY_PARENT
264
- };
270
+ }];
265
271
  } // Complex params
266
272
 
267
273
 
268
- return {
274
+ return [{
269
275
  type: 'Property',
270
276
  key: param.name,
271
277
  kind: 'init',
@@ -276,7 +282,7 @@ function mapComponentParameter(param) {
276
282
  loc: param.loc,
277
283
  range: param.range,
278
284
  parent: EMPTY_PARENT
279
- };
285
+ }];
280
286
  }
281
287
 
282
288
  default:
@@ -38,6 +38,8 @@ import type {
38
38
  VariableDeclaration,
39
39
  ModuleDeclaration,
40
40
  Statement,
41
+ AssignmentPattern,
42
+ BindingName,
41
43
  } from 'hermes-estree';
42
44
 
43
45
  import {SimpleTransform} from '../transform/SimpleTransform';
@@ -148,7 +150,7 @@ function mapComponentParameters(
148
150
  params: $ReadOnlyArray<ComponentParameter | RestElement>,
149
151
  ): $ReadOnly<{
150
152
  props: ?(ObjectPattern | Identifier),
151
- ref: ?Identifier,
153
+ ref: ?(BindingName | AssignmentPattern),
152
154
  }> {
153
155
  if (params.length === 0) {
154
156
  return {props: null, ref: null};
@@ -158,8 +160,7 @@ function mapComponentParameters(
158
160
  if (
159
161
  params.length === 1 &&
160
162
  params[0].type === 'RestElement' &&
161
- (params[0].argument.type === 'Identifier' ||
162
- params[0].argument.type === 'ObjectPattern')
163
+ params[0].argument.type === 'Identifier'
163
164
  ) {
164
165
  const restElementArgument = params[0].argument;
165
166
  return {
@@ -187,7 +188,7 @@ function mapComponentParameters(
187
188
  return true;
188
189
  });
189
190
 
190
- const propsProperties = paramsWithoutRef.map(mapComponentParameter);
191
+ const propsProperties = paramsWithoutRef.flatMap(mapComponentParameter);
191
192
 
192
193
  let props = null;
193
194
  if (propsProperties.length === 0) {
@@ -237,19 +238,7 @@ function mapComponentParameters(
237
238
 
238
239
  let ref = null;
239
240
  if (refParam != null) {
240
- const refType = refParam.local;
241
- ref = {
242
- type: 'Identifier',
243
- name: 'ref',
244
- optional: false,
245
- typeAnnotation:
246
- refType.type === 'AssignmentPattern'
247
- ? refType.left.typeAnnotation
248
- : refType.typeAnnotation,
249
- loc: refParam.loc,
250
- range: refParam.range,
251
- parent: EMPTY_PARENT,
252
- };
241
+ ref = refParam.local;
253
242
  }
254
243
 
255
244
  return {
@@ -260,23 +249,33 @@ function mapComponentParameters(
260
249
 
261
250
  function mapComponentParameter(
262
251
  param: ComponentParameter | RestElement,
263
- ): DestructuringObjectProperty | RestElement {
252
+ ): Array<DestructuringObjectProperty | RestElement> {
264
253
  switch (param.type) {
265
254
  case 'RestElement': {
266
- const a = nodeWith(param, {
267
- typeAnnotation: null,
268
- argument: nodeWith(param.argument, {typeAnnotation: null}),
269
- });
270
- return a;
255
+ switch (param.argument.type) {
256
+ case 'Identifier': {
257
+ const a = nodeWith(param, {
258
+ typeAnnotation: null,
259
+ argument: nodeWith(param.argument, {typeAnnotation: null}),
260
+ });
261
+ return [a];
262
+ }
263
+ case 'ObjectPattern': {
264
+ return param.argument.properties.map(property => {
265
+ return nodeWith(property, {
266
+ typeAnnotation: null,
267
+ });
268
+ });
269
+ }
270
+ default: {
271
+ throw createSyntaxError(
272
+ param,
273
+ `Unhandled ${param.argument.type} encountered in restParameter`,
274
+ );
275
+ }
276
+ }
271
277
  }
272
278
  case 'ComponentParameter': {
273
- if (getComponentParameterName(param.name) === 'ref') {
274
- throw createSyntaxError(
275
- param,
276
- 'Component parameters named "ref" are currently not supported',
277
- );
278
- }
279
-
280
279
  let value;
281
280
  if (param.local.type === 'AssignmentPattern') {
282
281
  value = nodeWith(param.local, {
@@ -298,33 +297,37 @@ function mapComponentParameter(
298
297
  param.shorthand &&
299
298
  (value.type === 'Identifier' || value.type === 'AssignmentPattern')
300
299
  ) {
301
- return {
300
+ return [
301
+ {
302
+ type: 'Property',
303
+ key: param.name,
304
+ kind: 'init',
305
+ value,
306
+ method: false,
307
+ shorthand: true,
308
+ computed: false,
309
+ loc: param.loc,
310
+ range: param.range,
311
+ parent: EMPTY_PARENT,
312
+ },
313
+ ];
314
+ }
315
+
316
+ // Complex params
317
+ return [
318
+ {
302
319
  type: 'Property',
303
320
  key: param.name,
304
321
  kind: 'init',
305
322
  value,
306
323
  method: false,
307
- shorthand: true,
324
+ shorthand: false,
308
325
  computed: false,
309
326
  loc: param.loc,
310
327
  range: param.range,
311
328
  parent: EMPTY_PARENT,
312
- };
313
- }
314
-
315
- // Complex params
316
- return {
317
- type: 'Property',
318
- key: param.name,
319
- kind: 'init',
320
- value,
321
- method: false,
322
- shorthand: false,
323
- computed: false,
324
- loc: param.loc,
325
- range: param.range,
326
- parent: EMPTY_PARENT,
327
- };
329
+ },
330
+ ];
328
331
  }
329
332
  default: {
330
333
  throw createSyntaxError(
@@ -17,7 +17,17 @@ exports.default = mutate;
17
17
  var _SimpleTransform = require("../transform/SimpleTransform");
18
18
 
19
19
  // https://github.com/prettier/prettier/blob/d962466a828f8ef51435e3e8840178d90b7ec6cd/src/language-js/parse/postprocess/index.js#L161-L182
20
- function transformChainExpression(node) {
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
+
21
31
  switch (node.type) {
22
32
  case 'CallExpression':
23
33
  // $FlowExpectedError[cannot-spread-interface]
@@ -56,7 +66,8 @@ function mutate(rootNode, visitorKeys) {
56
66
 
57
67
 
58
68
  if (node.type === 'ChainExpression') {
59
- return transformChainExpression(node.expression);
69
+ // $FlowFixMe[prop-missing]
70
+ return transformChainExpression(node.expression, node == null ? void 0 : node.comments);
60
71
  } // Prettier currently relies on comparing the `node` vs `node.value` start positions to know if an
61
72
  // `ObjectTypeProperty` is a method or not (instead of using the `node.method` boolean). To correctly print
62
73
  // the node when its not a method we need the start position to be different from the `node.value`s start
@@ -10,12 +10,22 @@
10
10
 
11
11
  'use strict';
12
12
 
13
- import type {ESNode, Program} from 'hermes-estree';
13
+ import type {ESNode, Program, Comment} from 'hermes-estree';
14
14
  import type {VisitorKeysType} from '../traverse/getVisitorKeys';
15
15
  import {SimpleTransform} from '../transform/SimpleTransform';
16
16
 
17
17
  // https://github.com/prettier/prettier/blob/d962466a828f8ef51435e3e8840178d90b7ec6cd/src/language-js/parse/postprocess/index.js#L161-L182
18
- function transformChainExpression(node: ESNode): ESNode {
18
+ function transformChainExpression(
19
+ node: ESNode,
20
+ comments: ?$ReadOnlyArray<Comment>,
21
+ ): ESNode {
22
+ if (comments != null) {
23
+ // $FlowExpectedError[prop-missing]
24
+ const joinedComments = comments.concat(node.comments ?? []);
25
+ // $FlowExpectedError[prop-missing]
26
+ // $FlowFixMe[cannot-write]
27
+ node.comments = joinedComments;
28
+ }
19
29
  switch (node.type) {
20
30
  case 'CallExpression':
21
31
  // $FlowExpectedError[cannot-spread-interface]
@@ -59,7 +69,8 @@ export default function mutate(
59
69
  // so we have to apply their transform to our AST so it can actually format it.
60
70
  // Note: Only needed for prettier V2, this is supported in V3
61
71
  if (node.type === 'ChainExpression') {
62
- return transformChainExpression(node.expression);
72
+ // $FlowFixMe[prop-missing]
73
+ return transformChainExpression(node.expression, node?.comments);
63
74
  }
64
75
 
65
76
  // Prettier currently relies on comparing the `node` vs `node.value` start positions to know if an
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hermes-parser",
3
- "version": "0.17.0",
3
+ "version": "0.18.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.17.0"
12
+ "hermes-estree": "0.18.0"
13
13
  },
14
14
  "devDependencies": {
15
15
  "@babel/parser": "7.7.4",