flow-upgrade 2.3.0 → 2.6.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.
package/README.md CHANGED
@@ -93,3 +93,23 @@ Run with `yarn run flow-codemod convertShapeToPartial`.
93
93
  Converts usages of the old casting syntax `(x: T)` to the new casting syntax `x as T`.
94
94
 
95
95
  Run with `yarn run flow-codemod typeCastToAsExpression`.
96
+
97
+ ### Migrate internal Flow types for React to public-facing ones
98
+ Converts usages of the internal React types like `React$Node` to public facing ones like `React.Node`.
99
+
100
+ Run with `yarn run flow-codemod replaceReactDollarUtilityTypes`.
101
+
102
+ ### Replace `$TEMPORARY$*` types
103
+ Converts:
104
+ - `$TEMPORARY$object<{props}>` to `$ReadOnly<{props}>`
105
+ - `$TEMPORARY$array<T>` to `$ReadOnlyArray<T>`
106
+ - `$TEMPORARY$number<42>` annotations to `number`
107
+ - `$TEMPORARY$string<"foo">` annotations to `string`
108
+
109
+ ### Replace `React.AbstractComponent<...>` types
110
+ Converts:
111
+
112
+ - `React.AbstractComponent<Props>` to `React.ComponentType<Props>`
113
+ - `React.ElementConfig<React.AbstractComponent<Props, Instance>>` to `Props`
114
+ - `React.ElementRef<React.AbstractComponent<Props, Instance>>` to `Instance`
115
+ - `React.ElementRef<React.AbstractComponent<Props>` to `mixed`
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _Types = require("../Types");
9
+
10
+ var _hermesTransform = require("hermes-transform");
11
+
12
+ function eliminateAbstractComponent(node) {
13
+ const {
14
+ id: maybeQualifiedIdentifier,
15
+ typeParameters
16
+ } = node;
17
+
18
+ if (maybeQualifiedIdentifier.type === 'QualifiedTypeIdentifier' && maybeQualifiedIdentifier.qualification.name === 'React' && maybeQualifiedIdentifier.id.name === 'AbstractComponent' && typeParameters != null) {
19
+ const [config, instance, renders] = typeParameters.params;
20
+
21
+ if (renders != null) {
22
+ return null;
23
+ }
24
+
25
+ if (instance == null || instance.type === 'MixedTypeAnnotation') {
26
+ return _hermesTransform.t.GenericTypeAnnotation({
27
+ id: _hermesTransform.t.QualifiedTypeIdentifier({
28
+ qualification: _hermesTransform.t.Identifier({
29
+ name: 'React'
30
+ }),
31
+ id: _hermesTransform.t.Identifier({
32
+ name: 'ComponentType'
33
+ })
34
+ }),
35
+ typeParameters: _hermesTransform.t.TypeParameterInstantiation({
36
+ params: [config]
37
+ })
38
+ });
39
+ }
40
+
41
+ return null;
42
+ }
43
+
44
+ if (maybeQualifiedIdentifier.type === 'QualifiedTypeIdentifier' && maybeQualifiedIdentifier.qualification.name === 'React' && maybeQualifiedIdentifier.id.name === 'ElementConfig' && typeParameters != null && typeParameters.params.length === 1 && typeParameters.params[0].type === 'GenericTypeAnnotation' && typeParameters.params[0].id.type === 'QualifiedTypeIdentifier' && typeParameters.params[0].id.qualification.name === 'React' && typeParameters.params[0].id.id.name === 'AbstractComponent' && typeParameters.params[0].typeParameters != null && typeParameters.params[0].typeParameters.params.length >= 1) {
45
+ return typeParameters.params[0].typeParameters.params[0];
46
+ }
47
+
48
+ if (maybeQualifiedIdentifier.type === 'QualifiedTypeIdentifier' && maybeQualifiedIdentifier.qualification.name === 'React' && maybeQualifiedIdentifier.id.name === 'ElementRef' && typeParameters != null && typeParameters.params.length === 1 && typeParameters.params[0].type === 'GenericTypeAnnotation' && typeParameters.params[0].id.type === 'QualifiedTypeIdentifier' && typeParameters.params[0].id.qualification.name === 'React' && typeParameters.params[0].id.id.name === 'AbstractComponent' && typeParameters.params[0].typeParameters != null && typeParameters.params[0].typeParameters.params.length >= 2) {
49
+ return typeParameters.params[0].typeParameters.params[1];
50
+ }
51
+
52
+ if (maybeQualifiedIdentifier.type === 'QualifiedTypeIdentifier' && maybeQualifiedIdentifier.qualification.name === 'React' && maybeQualifiedIdentifier.id.name === 'ElementRef' && typeParameters != null && typeParameters.params.length === 1 && typeParameters.params[0].type === 'GenericTypeAnnotation' && typeParameters.params[0].id.type === 'QualifiedTypeIdentifier' && typeParameters.params[0].id.qualification.name === 'React' && typeParameters.params[0].id.id.name === 'AbstractComponent' && typeParameters.params[0].typeParameters != null && typeParameters.params[0].typeParameters.params.length === 1) {
53
+ return _hermesTransform.t.MixedTypeAnnotation({});
54
+ }
55
+
56
+ return null;
57
+ }
58
+
59
+ var _default = (0, _Types.codemod)({
60
+ title: 'Eliminate `React.AbstractComponent<...>`',
61
+ describe: `
62
+ - \`React.AbstractComponent<Props>\` -> \`React.ComponentType<Props>\`
63
+ - \`React.ElementConfig<React.AbstractComponent<Props, Instance>>\` -> \`Props\`
64
+ - \`React.ElementRef<React.AbstractComponent<Props, Instance>>\` -> \`Instance\`
65
+ - \`React.ElementRef<React.AbstractComponent<Props>\` -> \`mixed\``,
66
+ transform: context => {
67
+ return {
68
+ GenericTypeAnnotation(node) {
69
+ const replacement = eliminateAbstractComponent(node);
70
+
71
+ if (replacement) {
72
+ context.replaceNode(node, replacement, {
73
+ keepComments: true
74
+ });
75
+ }
76
+ }
77
+
78
+ };
79
+ }
80
+ });
81
+
82
+ exports.default = _default;
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _hermesTransform = require("hermes-transform");
9
+
10
+ var _Types = require("../Types");
11
+
12
+ const UTILITY_TYPES = new Set(['React$ComponentType', 'React$AbstractComponent', 'React$Element', 'React$MixedElement', 'React$ElementType', 'React$Key', 'React$Ref', 'React$RefSetter', 'React$Node', 'React$Context', 'React$Portal', 'React$ElementProps', 'React$ElementConfig', 'React$ElementRef', 'React$Config', 'React$Config', 'React$RefObject']);
13
+
14
+ var _default = (0, _Types.codemod)({
15
+ title: 'Replace React$SomeUtilityType with React.SomeUtilityType.',
16
+ transform: context => {
17
+ return {
18
+ GenericTypeAnnotation(node) {
19
+ if (typeof node.id.name !== 'string') {
20
+ return;
21
+ }
22
+
23
+ const utilTypeName = node.id.name;
24
+
25
+ if (!UTILITY_TYPES.has(utilTypeName)) {
26
+ return;
27
+ }
28
+
29
+ const newNode = _hermesTransform.t.GenericTypeAnnotation({
30
+ id: _hermesTransform.t.QualifiedTypeIdentifier({
31
+ id: _hermesTransform.t.Identifier({
32
+ name: utilTypeName.substring('React$'.length)
33
+ }),
34
+ qualification: _hermesTransform.t.Identifier({
35
+ name: 'React'
36
+ })
37
+ }),
38
+ typeParameters: node.typeParameters
39
+ });
40
+
41
+ context.replaceNode(node, newNode);
42
+ }
43
+
44
+ };
45
+ }
46
+ });
47
+
48
+ exports.default = _default;
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _hermesTransform = require("hermes-transform");
9
+
10
+ var _Types = require("../Types");
11
+
12
+ var _default = (0, _Types.codemod)({
13
+ title: 'Replace $TEMPORARY$ types',
14
+ describe: ['Converts:', ' - `$TEMPORARY$object<{props}>` to `$ReadOnly<{props}>`', ' - `$TEMPORARY$array<T>` to `$ReadOnlyArray<T>`', ' - `$TEMPORARY$number<42>` annotations to `number`', ' - `$TEMPORARY$string<"foo">` annotations to `string`'].join('\n'),
15
+ transform: context => {
16
+ return {
17
+ GenericTypeAnnotation(node) {
18
+ const id = node.id;
19
+
20
+ if (id.type === 'Identifier' && id.name === '$TEMPORARY$object' && node.typeParameters != null) {
21
+ context.modifyNodeInPlace(node, {
22
+ id: _hermesTransform.t.Identifier({
23
+ name: '$ReadOnly'
24
+ })
25
+ });
26
+ }
27
+
28
+ if (id.type === 'Identifier' && id.name === '$TEMPORARY$array' && node.typeParameters != null) {
29
+ context.modifyNodeInPlace(node, {
30
+ id: _hermesTransform.t.Identifier({
31
+ name: '$ReadOnlyArray'
32
+ })
33
+ });
34
+ }
35
+
36
+ if (id.type === 'Identifier' && id.name === '$TEMPORARY$number') {
37
+ const new_node = _hermesTransform.t.NumberTypeAnnotation();
38
+
39
+ context.replaceNode(node, new_node, {
40
+ keepComments: true
41
+ });
42
+ }
43
+
44
+ if (id.type === 'Identifier' && id.name === '$TEMPORARY$string') {
45
+ const new_node = _hermesTransform.t.StringTypeAnnotation();
46
+
47
+ context.replaceNode(node, new_node, {
48
+ keepComments: true
49
+ });
50
+ }
51
+ }
52
+
53
+ };
54
+ }
55
+ });
56
+
57
+ exports.default = _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flow-upgrade",
3
- "version": "2.3.0",
3
+ "version": "2.6.0",
4
4
  "description": "A utility for upgrading your codebase to the latest version of Flow.",
5
5
  "engines": {
6
6
  "node": ">=14"
@@ -30,15 +30,15 @@
30
30
  "homepage": "https://github.com/facebook/flow/tree/master/packages/flow-upgrade#readme",
31
31
  "dependencies": {
32
32
  "@babel/highlight": "^7.18.6",
33
- "babel-plugin-syntax-hermes-parser": "0.19.0",
33
+ "babel-plugin-syntax-hermes-parser": "0.24.0",
34
34
  "chalk": "^2.0.1",
35
35
  "fs-extra": "10.1.0",
36
- "hermes-estree": "0.19.0",
37
- "hermes-parser": "0.19.0",
38
- "hermes-transform": "0.19.0",
36
+ "hermes-estree": "0.24.0",
37
+ "hermes-parser": "0.24.0",
38
+ "hermes-transform": "0.24.0",
39
39
  "klaw-sync": "^6.0.0",
40
40
  "ora": "^5.4.1",
41
- "prettier-plugin-hermes-parser": "0.19.0",
41
+ "prettier-plugin-hermes-parser": "0.24.0",
42
42
  "prompt-confirm": "^1.2.0",
43
43
  "semver": "^7.3.7",
44
44
  "yargs": "^17.0.1"