flow-api-translator 0.27.0 → 0.28.1

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.
@@ -32,11 +32,15 @@ const DUMMY_COMMON = {
32
32
  parent: DUMMY_PARENT,
33
33
  };
34
34
 
35
+ type LooseOmit<O: interface {}, K: $Keys<$FlowFixMe>> = Pick<
36
+ O,
37
+ Exclude<$Keys<O>, K>,
38
+ >;
35
39
  function constructFlowNode<T: FlowESTree.BaseNode>(
36
- node: $Diff<T, FlowESTree.BaseNode>,
40
+ node: LooseOmit<NoInfer<T>, 'parent'>,
37
41
  ): T {
38
42
  return {
39
- ...node,
43
+ ...(node: $FlowFixMe),
40
44
  ...DUMMY_COMMON,
41
45
  };
42
46
  }
@@ -42,7 +42,6 @@ const DUMMY_LOC = {
42
42
  };
43
43
 
44
44
  function constructFlowNode(node) {
45
- // $FlowFixMe[prop-missing]
46
45
  return node;
47
46
  }
48
47
 
@@ -1957,6 +1956,20 @@ const getTransforms = (originalCode, scopeManager, opts) => {
1957
1956
  return unsupportedAnnotation(node, fullTypeName);
1958
1957
  }
1959
1958
 
1959
+ case '$ArrayBufferView':
1960
+ {
1961
+ // `$ArrayBufferView` => `ArrayBufferView`
1962
+ return {
1963
+ type: 'TSTypeReference',
1964
+ loc: DUMMY_LOC,
1965
+ typeName: {
1966
+ type: 'Identifier',
1967
+ loc: DUMMY_LOC,
1968
+ name: 'ArrayBufferView'
1969
+ }
1970
+ };
1971
+ }
1972
+
1960
1973
  case '$ArrayLike':
1961
1974
  {
1962
1975
  // `$ArrayLike<T>` => `ArrayLike<T>`
@@ -2543,8 +2556,8 @@ const getTransforms = (originalCode, scopeManager, opts) => {
2543
2556
  }
2544
2557
  };
2545
2558
  }
2546
- // React.ElementRef<typeof Component> -> React.ElementRef<typeof Component>
2547
- // React$ElementRef<typeof Component> -> React.ElementRef<typeof Component>
2559
+ // React.ElementRef<typeof Component> -> React.ComponentRef<typeof Component>
2560
+ // React$ElementRef<typeof Component> -> React.ComponentRef<typeof Component>
2548
2561
 
2549
2562
  case 'React$ElementRef':
2550
2563
  case 'React.ElementRef':
@@ -2558,7 +2571,7 @@ const getTransforms = (originalCode, scopeManager, opts) => {
2558
2571
  right: {
2559
2572
  type: 'Identifier',
2560
2573
  loc: DUMMY_LOC,
2561
- name: `ElementRef`
2574
+ name: `ComponentRef`
2562
2575
  }
2563
2576
  },
2564
2577
  typeParameters: {
@@ -3211,8 +3224,8 @@ const getTransforms = (originalCode, scopeManager, opts) => {
3211
3224
  type T = { ...T1, ...T2, ...T3, b: string };
3212
3225
  // becomes
3213
3226
  type T =
3214
- & Omit<T1, keyof (T2 | T3 | { b: string })>
3215
- & Omit<T2, keyof (T3 | { b: string })>
3227
+ & Omit<T1, keyof T2 | keyof T3 | keyof { b: string }>
3228
+ & Omit<T2, keyof T3 | keyof { b: string }>
3216
3229
  & Omit<T3, keyof { b: string }>
3217
3230
  & { b: string };
3218
3231
  ```
@@ -3276,14 +3289,19 @@ const getTransforms = (originalCode, scopeManager, opts) => {
3276
3289
  type: 'TSTypeParameterInstantiation',
3277
3290
  loc: DUMMY_LOC,
3278
3291
  params: [currentType, {
3279
- type: 'TSTypeOperator',
3292
+ type: 'TSUnionType',
3280
3293
  loc: DUMMY_LOC,
3281
- operator: 'keyof',
3282
- typeAnnotation: {
3283
- type: 'TSUnionType',
3294
+ types: [...remainingTypes.map(t => ({
3295
+ type: 'TSTypeOperator',
3284
3296
  loc: DUMMY_LOC,
3285
- types: [...remainingTypes, objectType]
3286
- }
3297
+ operator: 'keyof',
3298
+ typeAnnotation: t
3299
+ })), {
3300
+ type: 'TSTypeOperator',
3301
+ loc: DUMMY_LOC,
3302
+ operator: 'keyof',
3303
+ typeAnnotation: objectType
3304
+ }]
3287
3305
  }]
3288
3306
  }
3289
3307
  });
@@ -35,11 +35,14 @@ const DUMMY_LOC: FlowESTree.SourceLocation = {
35
35
  end: {line: 1, column: 0},
36
36
  };
37
37
 
38
+ type LooseOmit<O: interface {}, K: $Keys<$FlowFixMe>> = Pick<
39
+ O,
40
+ Exclude<$Keys<O>, K>,
41
+ >;
38
42
  function constructFlowNode<T: FlowESTree.BaseNode>(
39
- node: $Diff<T, FlowESTree.BaseNode>,
43
+ node: LooseOmit<NoInfer<T>, 'parent'>,
40
44
  ): T {
41
- // $FlowFixMe[prop-missing]
42
- return node;
45
+ return (node: $FlowFixMe);
43
46
  }
44
47
 
45
48
  const cloneJSDocCommentsToNewNode =
@@ -2173,6 +2176,19 @@ const getTransforms = (
2173
2176
  return unsupportedAnnotation(node, fullTypeName);
2174
2177
  }
2175
2178
 
2179
+ case '$ArrayBufferView': {
2180
+ // `$ArrayBufferView` => `ArrayBufferView`
2181
+ return {
2182
+ type: 'TSTypeReference',
2183
+ loc: DUMMY_LOC,
2184
+ typeName: {
2185
+ type: 'Identifier',
2186
+ loc: DUMMY_LOC,
2187
+ name: 'ArrayBufferView',
2188
+ },
2189
+ };
2190
+ }
2191
+
2176
2192
  case '$ArrayLike': {
2177
2193
  // `$ArrayLike<T>` => `ArrayLike<T>`
2178
2194
  return {
@@ -2765,8 +2781,8 @@ const getTransforms = (
2765
2781
  },
2766
2782
  };
2767
2783
  }
2768
- // React.ElementRef<typeof Component> -> React.ElementRef<typeof Component>
2769
- // React$ElementRef<typeof Component> -> React.ElementRef<typeof Component>
2784
+ // React.ElementRef<typeof Component> -> React.ComponentRef<typeof Component>
2785
+ // React$ElementRef<typeof Component> -> React.ComponentRef<typeof Component>
2770
2786
  case 'React$ElementRef':
2771
2787
  case 'React.ElementRef':
2772
2788
  return {
@@ -2779,7 +2795,7 @@ const getTransforms = (
2779
2795
  right: {
2780
2796
  type: 'Identifier',
2781
2797
  loc: DUMMY_LOC,
2782
- name: `ElementRef`,
2798
+ name: `ComponentRef`,
2783
2799
  },
2784
2800
  },
2785
2801
  typeParameters: {
@@ -3495,8 +3511,8 @@ const getTransforms = (
3495
3511
  type T = { ...T1, ...T2, ...T3, b: string };
3496
3512
  // becomes
3497
3513
  type T =
3498
- & Omit<T1, keyof (T2 | T3 | { b: string })>
3499
- & Omit<T2, keyof (T3 | { b: string })>
3514
+ & Omit<T1, keyof T2 | keyof T3 | keyof { b: string }>
3515
+ & Omit<T2, keyof T3 | keyof { b: string }>
3500
3516
  & Omit<T3, keyof { b: string }>
3501
3517
  & { b: string };
3502
3518
  ```
@@ -3577,14 +3593,22 @@ const getTransforms = (
3577
3593
  params: [
3578
3594
  currentType,
3579
3595
  {
3580
- type: 'TSTypeOperator',
3596
+ type: 'TSUnionType',
3581
3597
  loc: DUMMY_LOC,
3582
- operator: 'keyof',
3583
- typeAnnotation: {
3584
- type: 'TSUnionType',
3585
- loc: DUMMY_LOC,
3586
- types: [...remainingTypes, objectType],
3587
- },
3598
+ types: [
3599
+ ...remainingTypes.map(t => ({
3600
+ type: 'TSTypeOperator',
3601
+ loc: DUMMY_LOC,
3602
+ operator: 'keyof',
3603
+ typeAnnotation: t,
3604
+ })),
3605
+ {
3606
+ type: 'TSTypeOperator',
3607
+ loc: DUMMY_LOC,
3608
+ operator: 'keyof',
3609
+ typeAnnotation: objectType,
3610
+ },
3611
+ ],
3588
3612
  },
3589
3613
  ],
3590
3614
  },
@@ -1113,19 +1113,19 @@ function convertAFunction(func, context) {
1113
1113
  }
1114
1114
 
1115
1115
  function convertFunctionParameters(params, context) {
1116
- return params.reduce(([resultParams, restParam, paramsDeps], param) => {
1116
+ return params.reduce(([resultParams, restParam, paramsDeps], param, index) => {
1117
1117
  switch (param.type) {
1118
1118
  case 'Identifier':
1119
1119
  case 'ArrayPattern':
1120
1120
  case 'ObjectPattern':
1121
1121
  {
1122
- const [resultParam, deps] = convertBindingNameToFunctionTypeParam(param, context);
1122
+ const [resultParam, deps] = convertBindingNameToFunctionTypeParam(param, context, index, false);
1123
1123
  return [[...resultParams, resultParam], restParam, [...paramsDeps, ...deps]];
1124
1124
  }
1125
1125
 
1126
1126
  case 'AssignmentPattern':
1127
1127
  {
1128
- const [resultParam, deps] = convertBindingNameToFunctionTypeParam(param.left, context);
1128
+ const [resultParam, deps] = convertBindingNameToFunctionTypeParam(param.left, context, index, true);
1129
1129
  return [[...resultParams, resultParam], restParam, [...paramsDeps, ...deps]];
1130
1130
  }
1131
1131
 
@@ -1136,22 +1136,22 @@ function convertFunctionParameters(params, context) {
1136
1136
  }
1137
1137
 
1138
1138
  const [resultParam, deps] = convertBindingNameToFunctionTypeParam( // $FlowFixMe[incompatible-call] I dont think these other cases are possible
1139
- param.argument, context);
1139
+ param.argument, context, index, false);
1140
1140
  return [resultParams, resultParam, [...paramsDeps, ...deps]];
1141
1141
  }
1142
1142
  }
1143
1143
  }, [[], null, []]);
1144
1144
  }
1145
1145
 
1146
- function convertBindingNameToFunctionTypeParam(pat, context) {
1147
- const name = pat.type === 'Identifier' ? pat.name : null;
1146
+ function convertBindingNameToFunctionTypeParam(pat, context, index, isAssignment) {
1147
+ const name = pat.type === 'Identifier' ? pat.name : `$$PARAM_${index}$$`;
1148
1148
  const [resultParamTypeAnnotation, paramDeps] = convertTypeAnnotation(pat.typeAnnotation, pat, context);
1149
1149
  return [_hermesTransform.t.FunctionTypeParam({
1150
1150
  name: name != null ? _hermesTransform.t.Identifier({
1151
1151
  name
1152
1152
  }) : null,
1153
1153
  typeAnnotation: resultParamTypeAnnotation,
1154
- optional: pat.type === 'Identifier' ? pat.optional : false
1154
+ optional: isAssignment || (pat.type === 'Identifier' ? pat.optional : false)
1155
1155
  }), paramDeps];
1156
1156
  }
1157
1157
 
@@ -1549,7 +1549,7 @@ function convertFunctionParameters(
1549
1549
  context: TranslationContext,
1550
1550
  ): TranslatedFunctionParametersResults {
1551
1551
  return params.reduce<TranslatedFunctionParametersResults>(
1552
- ([resultParams, restParam, paramsDeps], param) => {
1552
+ ([resultParams, restParam, paramsDeps], param, index) => {
1553
1553
  switch (param.type) {
1554
1554
  case 'Identifier':
1555
1555
  case 'ArrayPattern':
@@ -1557,6 +1557,8 @@ function convertFunctionParameters(
1557
1557
  const [resultParam, deps] = convertBindingNameToFunctionTypeParam(
1558
1558
  param,
1559
1559
  context,
1560
+ index,
1561
+ false,
1560
1562
  );
1561
1563
  return [
1562
1564
  [...resultParams, resultParam],
@@ -1568,6 +1570,8 @@ function convertFunctionParameters(
1568
1570
  const [resultParam, deps] = convertBindingNameToFunctionTypeParam(
1569
1571
  param.left,
1570
1572
  context,
1573
+ index,
1574
+ true,
1571
1575
  );
1572
1576
  return [
1573
1577
  [...resultParams, resultParam],
@@ -1587,6 +1591,8 @@ function convertFunctionParameters(
1587
1591
  // $FlowFixMe[incompatible-call] I dont think these other cases are possible
1588
1592
  param.argument,
1589
1593
  context,
1594
+ index,
1595
+ false,
1590
1596
  );
1591
1597
  return [resultParams, resultParam, [...paramsDeps, ...deps]];
1592
1598
  }
@@ -1599,8 +1605,10 @@ function convertFunctionParameters(
1599
1605
  function convertBindingNameToFunctionTypeParam(
1600
1606
  pat: BindingName,
1601
1607
  context: TranslationContext,
1608
+ index: number,
1609
+ isAssignment: boolean,
1602
1610
  ): TranslatedResult<FunctionTypeParam> {
1603
- const name = pat.type === 'Identifier' ? pat.name : null;
1611
+ const name = pat.type === 'Identifier' ? pat.name : `$$PARAM_${index}$$`;
1604
1612
  const [resultParamTypeAnnotation, paramDeps] = convertTypeAnnotation(
1605
1613
  pat.typeAnnotation,
1606
1614
  pat,
@@ -1610,7 +1618,8 @@ function convertBindingNameToFunctionTypeParam(
1610
1618
  t.FunctionTypeParam({
1611
1619
  name: name != null ? t.Identifier({name}) : null,
1612
1620
  typeAnnotation: resultParamTypeAnnotation,
1613
- optional: pat.type === 'Identifier' ? pat.optional : false,
1621
+ optional:
1622
+ isAssignment || (pat.type === 'Identifier' ? pat.optional : false),
1614
1623
  }),
1615
1624
  paramDeps,
1616
1625
  ];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flow-api-translator",
3
- "version": "0.27.0",
3
+ "version": "0.28.1",
4
4
  "description": "Toolkit for creating Flow and TypeScript compatible libraries from Flow source code.",
5
5
  "main": "dist/index.js",
6
6
  "license": "MIT",
@@ -13,10 +13,10 @@
13
13
  "@typescript-eslint/parser": "7.2.0",
14
14
  "@typescript-eslint/visitor-keys": "7.2.0",
15
15
  "flow-enums-runtime": "^0.0.6",
16
- "hermes-eslint": "0.27.0",
17
- "hermes-estree": "0.27.0",
18
- "hermes-parser": "0.27.0",
19
- "hermes-transform": "0.27.0",
16
+ "hermes-eslint": "0.28.1",
17
+ "hermes-estree": "0.28.1",
18
+ "hermes-parser": "0.28.1",
19
+ "hermes-transform": "0.28.1",
20
20
  "typescript": "5.3.2"
21
21
  },
22
22
  "peerDependencies": {