hermes-parser 0.32.0 → 0.32.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.
- package/README.md +3 -0
- package/dist/HermesASTAdapter.js +1 -1
- package/dist/HermesASTAdapter.js.flow +1 -1
- package/dist/HermesParserDeserializer.js +0 -1
- package/dist/HermesParserDeserializer.js.flow +0 -1
- package/dist/HermesParserWASM.js +1 -1
- package/dist/ParserOptions.js +1 -1
- package/dist/ParserOptions.js.flow +8 -0
- package/dist/babel/TransformESTreeToBabel.js +3 -0
- package/dist/babel/TransformESTreeToBabel.js.flow +3 -0
- package/dist/estree/{StripComponentSyntax.js → TransformComponentSyntax.js} +1 -1
- package/dist/estree/{StripComponentSyntax.js.flow → TransformComponentSyntax.js.flow} +1 -1
- package/dist/estree/TransformEnumSyntax.js +106 -0
- package/dist/estree/TransformEnumSyntax.js.flow +125 -0
- package/dist/index.js +6 -3
- package/dist/index.js.flow +6 -3
- package/dist/src/HermesASTAdapter.js +1 -1
- package/dist/src/HermesParserDeserializer.js +0 -1
- package/dist/src/ParserOptions.js +1 -1
- package/dist/src/babel/TransformESTreeToBabel.js +3 -0
- package/dist/src/estree/{StripComponentSyntax.js → TransformComponentSyntax.js} +1 -1
- package/dist/src/estree/TransformEnumSyntax.js +106 -0
- package/dist/src/index.js +6 -3
- package/dist/src/transform/SimpleTransform.js +20 -4
- package/dist/src/transform/astNodeMutationHelpers.js +7 -2
- package/dist/transform/SimpleTransform.js +20 -4
- package/dist/transform/SimpleTransform.js.flow +34 -8
- package/dist/transform/astNodeMutationHelpers.js +7 -2
- package/dist/transform/astNodeMutationHelpers.js.flow +10 -2
- package/package.json +2 -2
package/dist/ParserOptions.js
CHANGED
|
@@ -14,5 +14,5 @@ exports.ParserOptionsKeys = void 0;
|
|
|
14
14
|
*
|
|
15
15
|
* @format
|
|
16
16
|
*/
|
|
17
|
-
const ParserOptionsKeys = new Set(['allowReturnOutsideFunction', 'babel', 'flow', 'enableExperimentalComponentSyntax', 'enableExperimentalFlowMatchSyntax', 'reactRuntimeTarget', 'sourceFilename', 'sourceType', 'tokens']);
|
|
17
|
+
const ParserOptionsKeys = new Set(['allowReturnOutsideFunction', 'babel', 'flow', 'enableExperimentalComponentSyntax', 'enableExperimentalFlowMatchSyntax', 'reactRuntimeTarget', 'sourceFilename', 'sourceType', 'tokens', 'transformOptions']);
|
|
18
18
|
exports.ParserOptionsKeys = ParserOptionsKeys;
|
|
@@ -8,6 +8,8 @@
|
|
|
8
8
|
* @format
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
+
import type {Expression} from 'hermes-estree';
|
|
12
|
+
|
|
11
13
|
export type ParserOptions = {
|
|
12
14
|
allowReturnOutsideFunction?: boolean,
|
|
13
15
|
babel?: boolean,
|
|
@@ -18,6 +20,11 @@ export type ParserOptions = {
|
|
|
18
20
|
sourceFilename?: string,
|
|
19
21
|
sourceType?: 'module' | 'script' | 'unambiguous',
|
|
20
22
|
tokens?: boolean,
|
|
23
|
+
transformOptions?: {
|
|
24
|
+
+TransformEnumSyntax?: {
|
|
25
|
+
+getRuntime: () => Expression,
|
|
26
|
+
},
|
|
27
|
+
},
|
|
21
28
|
};
|
|
22
29
|
|
|
23
30
|
export const ParserOptionsKeys: $ReadOnlySet<$Keys<ParserOptions>> = new Set([
|
|
@@ -30,4 +37,5 @@ export const ParserOptionsKeys: $ReadOnlySet<$Keys<ParserOptions>> = new Set([
|
|
|
30
37
|
'sourceFilename',
|
|
31
38
|
'sourceType',
|
|
32
39
|
'tokens',
|
|
40
|
+
'transformOptions',
|
|
33
41
|
]);
|
|
@@ -480,6 +480,7 @@ function mapPropertyDefinition(node) {
|
|
|
480
480
|
function mapTypeofTypeAnnotation(node) {
|
|
481
481
|
// $FlowExpectedError[cannot-write]
|
|
482
482
|
delete node.typeArguments; // $FlowFixMe[incompatible-type]
|
|
483
|
+
// $FlowFixMe[invalid-compare]
|
|
483
484
|
|
|
484
485
|
if (node.argument.type !== 'GenericTypeAnnotation') {
|
|
485
486
|
return nodeWith(node, {
|
|
@@ -656,6 +657,7 @@ function transformNode(node) {
|
|
|
656
657
|
|
|
657
658
|
// Check if we have already processed this node.
|
|
658
659
|
// $FlowFixMe[incompatible-type]
|
|
660
|
+
// $FlowFixMe[invalid-compare]
|
|
659
661
|
if (((_node$parent = node.parent) == null ? void 0 : _node$parent.type) === 'File') {
|
|
660
662
|
return node;
|
|
661
663
|
}
|
|
@@ -1110,6 +1112,7 @@ function transformProgram(program, options) {
|
|
|
1110
1112
|
|
|
1111
1113
|
visitorKeys: FlowESTreeAndBabelVisitorKeys
|
|
1112
1114
|
}); // $FlowFixMe[incompatible-type]
|
|
1115
|
+
// $FlowFixMe[invalid-compare]
|
|
1113
1116
|
|
|
1114
1117
|
|
|
1115
1118
|
if ((resultNode == null ? void 0 : resultNode.type) === 'File') {
|
|
@@ -753,6 +753,7 @@ function mapTypeofTypeAnnotation(
|
|
|
753
753
|
// $FlowExpectedError[cannot-write]
|
|
754
754
|
delete node.typeArguments;
|
|
755
755
|
// $FlowFixMe[incompatible-type]
|
|
756
|
+
// $FlowFixMe[invalid-compare]
|
|
756
757
|
if (node.argument.type !== 'GenericTypeAnnotation') {
|
|
757
758
|
return nodeWith(node, {
|
|
758
759
|
// $FlowExpectedError[incompatible-type] Special override for Babel
|
|
@@ -920,6 +921,7 @@ function transformNode(node: ESNodeOrBabelNode): ESNodeOrBabelNode | null {
|
|
|
920
921
|
case 'Program': {
|
|
921
922
|
// Check if we have already processed this node.
|
|
922
923
|
// $FlowFixMe[incompatible-type]
|
|
924
|
+
// $FlowFixMe[invalid-compare]
|
|
923
925
|
if (node.parent?.type === 'File') {
|
|
924
926
|
return node;
|
|
925
927
|
}
|
|
@@ -1263,6 +1265,7 @@ export function transformProgram(
|
|
|
1263
1265
|
});
|
|
1264
1266
|
|
|
1265
1267
|
// $FlowFixMe[incompatible-type]
|
|
1268
|
+
// $FlowFixMe[invalid-compare]
|
|
1266
1269
|
if (resultNode?.type === 'File') {
|
|
1267
1270
|
return resultNode;
|
|
1268
1271
|
}
|
|
@@ -191,7 +191,7 @@ function mapComponentParameters(params, options) {
|
|
|
191
191
|
|
|
192
192
|
if (propsProperties.length === 0) {
|
|
193
193
|
if (refParam == null) {
|
|
194
|
-
throw new Error('
|
|
194
|
+
throw new Error('TransformComponentSyntax: Invalid state, ref should always be set at this point if props are empty');
|
|
195
195
|
}
|
|
196
196
|
|
|
197
197
|
const emptyParamsLoc = {
|
|
@@ -243,7 +243,7 @@ function mapComponentParameters(
|
|
|
243
243
|
if (propsProperties.length === 0) {
|
|
244
244
|
if (refParam == null) {
|
|
245
245
|
throw new Error(
|
|
246
|
-
'
|
|
246
|
+
'TransformComponentSyntax: Invalid state, ref should always be set at this point if props are empty',
|
|
247
247
|
);
|
|
248
248
|
}
|
|
249
249
|
const emptyParamsLoc = {
|
|
@@ -0,0 +1,106 @@
|
|
|
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
|
+
* Transform Flow Enum declarations (https://flow.org/en/docs/enums/).
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
Object.defineProperty(exports, "__esModule", {
|
|
16
|
+
value: true
|
|
17
|
+
});
|
|
18
|
+
exports.transformProgram = transformProgram;
|
|
19
|
+
|
|
20
|
+
var _hermesEstree = require("hermes-estree");
|
|
21
|
+
|
|
22
|
+
var _SimpleTransform = require("../transform/SimpleTransform");
|
|
23
|
+
|
|
24
|
+
var _Builders = require("../utils/Builders");
|
|
25
|
+
|
|
26
|
+
function mapEnumDeclaration(node, options) {
|
|
27
|
+
var _options$transformOpt, _options$transformOpt2;
|
|
28
|
+
|
|
29
|
+
const {
|
|
30
|
+
body
|
|
31
|
+
} = node;
|
|
32
|
+
const {
|
|
33
|
+
members
|
|
34
|
+
} = body;
|
|
35
|
+
const getRuntime = (_options$transformOpt = options.transformOptions) == null ? void 0 : (_options$transformOpt2 = _options$transformOpt.TransformEnumSyntax) == null ? void 0 : _options$transformOpt2.getRuntime;
|
|
36
|
+
const enumModule = typeof getRuntime === 'function' ? getRuntime() : (0, _Builders.callExpression)((0, _Builders.ident)('require'), [(0, _Builders.stringLiteral)('flow-enums-runtime')]);
|
|
37
|
+
const mirrored = body.type === 'EnumStringBody' && (!members.length || members[0].type === 'EnumDefaultedMember');
|
|
38
|
+
const enumExpression = mirrored ? (0, _Builders.callExpression)({
|
|
39
|
+
type: 'MemberExpression',
|
|
40
|
+
object: enumModule,
|
|
41
|
+
property: (0, _Builders.ident)('Mirrored'),
|
|
42
|
+
computed: false,
|
|
43
|
+
optional: false,
|
|
44
|
+
...(0, _Builders.etc)()
|
|
45
|
+
}, [{
|
|
46
|
+
type: 'ArrayExpression',
|
|
47
|
+
elements: members.map(member => (0, _Builders.stringLiteral)(member.id.name)),
|
|
48
|
+
trailingComma: false,
|
|
49
|
+
...(0, _Builders.etc)()
|
|
50
|
+
}]) : (0, _Builders.callExpression)(enumModule, [{
|
|
51
|
+
type: 'ObjectExpression',
|
|
52
|
+
properties: members.map(member => ({
|
|
53
|
+
type: 'Property',
|
|
54
|
+
key: member.id,
|
|
55
|
+
value: // String enums with `EnumDefaultedMember` are handled above by
|
|
56
|
+
// calculation of `mirrored`.
|
|
57
|
+
member.type === 'EnumDefaultedMember' ? (0, _Builders.callExpression)((0, _Builders.ident)('Symbol'), [(0, _Builders.stringLiteral)(member.id.name)]) : member.init,
|
|
58
|
+
kind: 'init',
|
|
59
|
+
method: false,
|
|
60
|
+
shorthand: false,
|
|
61
|
+
computed: false,
|
|
62
|
+
...(0, _Builders.etc)(),
|
|
63
|
+
parent: _Builders.EMPTY_PARENT
|
|
64
|
+
})),
|
|
65
|
+
...(0, _Builders.etc)()
|
|
66
|
+
}]);
|
|
67
|
+
return (0, _Builders.variableDeclaration)('const', node.id, enumExpression);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function transformProgram(program, options) {
|
|
71
|
+
return _SimpleTransform.SimpleTransform.transformProgram(program, {
|
|
72
|
+
transform(node) {
|
|
73
|
+
switch (node.type) {
|
|
74
|
+
case 'EnumDeclaration':
|
|
75
|
+
{
|
|
76
|
+
return mapEnumDeclaration(node, options);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
case 'ExportDefaultDeclaration':
|
|
80
|
+
{
|
|
81
|
+
const {
|
|
82
|
+
declaration
|
|
83
|
+
} = node;
|
|
84
|
+
|
|
85
|
+
if ((0, _hermesEstree.isEnumDeclaration)(declaration)) {
|
|
86
|
+
const enumDeclaration = mapEnumDeclaration(declaration, options);
|
|
87
|
+
|
|
88
|
+
const exportDefault = _SimpleTransform.SimpleTransform.nodeWith(node, {
|
|
89
|
+
declaration: (0, _Builders.ident)(declaration.id.name)
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
return [enumDeclaration, exportDefault];
|
|
93
|
+
} else {
|
|
94
|
+
return node;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
default:
|
|
99
|
+
{
|
|
100
|
+
return node;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
});
|
|
106
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
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
|
+
* @flow strict
|
|
8
|
+
* @format
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
'use strict';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Transform Flow Enum declarations (https://flow.org/en/docs/enums/).
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import type {ParserOptions} from '../ParserOptions';
|
|
18
|
+
import type {
|
|
19
|
+
ESNode,
|
|
20
|
+
EnumDeclaration,
|
|
21
|
+
ExportDefaultDeclaration,
|
|
22
|
+
ObjectPropertyWithNonShorthandStaticName,
|
|
23
|
+
Program,
|
|
24
|
+
} from 'hermes-estree';
|
|
25
|
+
|
|
26
|
+
import {isEnumDeclaration} from 'hermes-estree';
|
|
27
|
+
import {SimpleTransform} from '../transform/SimpleTransform';
|
|
28
|
+
import {
|
|
29
|
+
EMPTY_PARENT,
|
|
30
|
+
callExpression,
|
|
31
|
+
etc,
|
|
32
|
+
ident,
|
|
33
|
+
stringLiteral,
|
|
34
|
+
variableDeclaration,
|
|
35
|
+
} from '../utils/Builders';
|
|
36
|
+
|
|
37
|
+
function mapEnumDeclaration(node: EnumDeclaration, options: ParserOptions) {
|
|
38
|
+
const {body} = node;
|
|
39
|
+
const {members} = body;
|
|
40
|
+
const getRuntime = options.transformOptions?.TransformEnumSyntax?.getRuntime;
|
|
41
|
+
const enumModule =
|
|
42
|
+
typeof getRuntime === 'function'
|
|
43
|
+
? getRuntime()
|
|
44
|
+
: callExpression(ident('require'), [stringLiteral('flow-enums-runtime')]);
|
|
45
|
+
const mirrored =
|
|
46
|
+
body.type === 'EnumStringBody' &&
|
|
47
|
+
(!members.length || members[0].type === 'EnumDefaultedMember');
|
|
48
|
+
const enumExpression = mirrored
|
|
49
|
+
? callExpression(
|
|
50
|
+
{
|
|
51
|
+
type: 'MemberExpression',
|
|
52
|
+
object: enumModule,
|
|
53
|
+
property: ident('Mirrored'),
|
|
54
|
+
computed: false,
|
|
55
|
+
optional: false,
|
|
56
|
+
...etc(),
|
|
57
|
+
},
|
|
58
|
+
[
|
|
59
|
+
{
|
|
60
|
+
type: 'ArrayExpression',
|
|
61
|
+
elements: members.map(member => stringLiteral(member.id.name)),
|
|
62
|
+
trailingComma: false,
|
|
63
|
+
...etc(),
|
|
64
|
+
},
|
|
65
|
+
],
|
|
66
|
+
)
|
|
67
|
+
: callExpression(enumModule, [
|
|
68
|
+
{
|
|
69
|
+
type: 'ObjectExpression',
|
|
70
|
+
properties: members.map(
|
|
71
|
+
(member): ObjectPropertyWithNonShorthandStaticName => ({
|
|
72
|
+
type: 'Property',
|
|
73
|
+
key: member.id,
|
|
74
|
+
value:
|
|
75
|
+
// String enums with `EnumDefaultedMember` are handled above by
|
|
76
|
+
// calculation of `mirrored`.
|
|
77
|
+
member.type === 'EnumDefaultedMember'
|
|
78
|
+
? callExpression(ident('Symbol'), [
|
|
79
|
+
stringLiteral(member.id.name),
|
|
80
|
+
])
|
|
81
|
+
: member.init,
|
|
82
|
+
kind: 'init',
|
|
83
|
+
method: false,
|
|
84
|
+
shorthand: false,
|
|
85
|
+
computed: false,
|
|
86
|
+
...etc(),
|
|
87
|
+
parent: EMPTY_PARENT,
|
|
88
|
+
}),
|
|
89
|
+
),
|
|
90
|
+
...etc(),
|
|
91
|
+
},
|
|
92
|
+
]);
|
|
93
|
+
return variableDeclaration('const', node.id, enumExpression);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function transformProgram(
|
|
97
|
+
program: Program,
|
|
98
|
+
options: ParserOptions,
|
|
99
|
+
): Program {
|
|
100
|
+
return SimpleTransform.transformProgram(program, {
|
|
101
|
+
transform(node: ESNode): ESNode | $ReadOnlyArray<ESNode> {
|
|
102
|
+
switch (node.type) {
|
|
103
|
+
case 'EnumDeclaration': {
|
|
104
|
+
return mapEnumDeclaration(node, options);
|
|
105
|
+
}
|
|
106
|
+
case 'ExportDefaultDeclaration': {
|
|
107
|
+
const {declaration} = node;
|
|
108
|
+
if (isEnumDeclaration(declaration)) {
|
|
109
|
+
const enumDeclaration = mapEnumDeclaration(declaration, options);
|
|
110
|
+
const exportDefault: ExportDefaultDeclaration =
|
|
111
|
+
SimpleTransform.nodeWith(node, {
|
|
112
|
+
declaration: ident(declaration.id.name),
|
|
113
|
+
});
|
|
114
|
+
return [enumDeclaration, exportDefault];
|
|
115
|
+
} else {
|
|
116
|
+
return node;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
default: {
|
|
120
|
+
return node;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -31,7 +31,9 @@ var _ESTreeVisitorKeys = _interopRequireDefault(require("./generated/ESTreeVisit
|
|
|
31
31
|
|
|
32
32
|
exports.FlowVisitorKeys = _ESTreeVisitorKeys.default;
|
|
33
33
|
|
|
34
|
-
var
|
|
34
|
+
var TransformComponentSyntax = _interopRequireWildcard(require("./estree/TransformComponentSyntax"));
|
|
35
|
+
|
|
36
|
+
var TransformEnumSyntax = _interopRequireWildcard(require("./estree/TransformEnumSyntax"));
|
|
35
37
|
|
|
36
38
|
var TransformMatchSyntax = _interopRequireWildcard(require("./estree/TransformMatchSyntax"));
|
|
37
39
|
|
|
@@ -140,13 +142,14 @@ function parse(code, opts) {
|
|
|
140
142
|
return estreeAST;
|
|
141
143
|
}
|
|
142
144
|
|
|
143
|
-
const loweredESTreeAST = [TransformMatchSyntax.transformProgram,
|
|
145
|
+
const loweredESTreeAST = [TransformEnumSyntax.transformProgram, TransformMatchSyntax.transformProgram, TransformComponentSyntax.transformProgram, StripFlowTypesForBabel.transformProgram].reduce((ast, transform) => transform(ast, options), estreeAST);
|
|
144
146
|
return TransformESTreeToBabel.transformProgram(loweredESTreeAST, options);
|
|
145
147
|
}
|
|
146
148
|
|
|
147
149
|
const Transforms = {
|
|
150
|
+
transformEnumSyntax: TransformEnumSyntax.transformProgram,
|
|
148
151
|
transformMatchSyntax: TransformMatchSyntax.transformProgram,
|
|
149
|
-
|
|
152
|
+
transformComponentSyntax: TransformComponentSyntax.transformProgram,
|
|
150
153
|
stripFlowTypesForBabel: StripFlowTypesForBabel.transformProgram,
|
|
151
154
|
stripFlowTypes: StripFlowTypes.transformProgram
|
|
152
155
|
};
|
package/dist/index.js.flow
CHANGED
|
@@ -17,7 +17,8 @@ import type {BabelFile} from './babel/TransformESTreeToBabel';
|
|
|
17
17
|
import * as HermesParser from './HermesParser';
|
|
18
18
|
import HermesToESTreeAdapter from './HermesToESTreeAdapter';
|
|
19
19
|
import FlowVisitorKeys from './generated/ESTreeVisitorKeys';
|
|
20
|
-
import * as
|
|
20
|
+
import * as TransformComponentSyntax from './estree/TransformComponentSyntax';
|
|
21
|
+
import * as TransformEnumSyntax from './estree/TransformEnumSyntax';
|
|
21
22
|
import * as TransformMatchSyntax from './estree/TransformMatchSyntax';
|
|
22
23
|
import * as StripFlowTypesForBabel from './estree/StripFlowTypesForBabel';
|
|
23
24
|
import * as TransformESTreeToBabel from './babel/TransformESTreeToBabel';
|
|
@@ -92,8 +93,9 @@ export function parse(
|
|
|
92
93
|
}
|
|
93
94
|
|
|
94
95
|
const loweredESTreeAST = [
|
|
96
|
+
TransformEnumSyntax.transformProgram,
|
|
95
97
|
TransformMatchSyntax.transformProgram,
|
|
96
|
-
|
|
98
|
+
TransformComponentSyntax.transformProgram,
|
|
97
99
|
StripFlowTypesForBabel.transformProgram,
|
|
98
100
|
].reduce((ast, transform) => transform(ast, options), estreeAST);
|
|
99
101
|
|
|
@@ -111,8 +113,9 @@ export * as astNodeMutationHelpers from './transform/astNodeMutationHelpers';
|
|
|
111
113
|
export {default as mutateESTreeASTForPrettier} from './utils/mutateESTreeASTForPrettier';
|
|
112
114
|
|
|
113
115
|
const Transforms = {
|
|
116
|
+
transformEnumSyntax: TransformEnumSyntax.transformProgram,
|
|
114
117
|
transformMatchSyntax: TransformMatchSyntax.transformProgram,
|
|
115
|
-
|
|
118
|
+
transformComponentSyntax: TransformComponentSyntax.transformProgram,
|
|
116
119
|
stripFlowTypesForBabel: StripFlowTypesForBabel.transformProgram,
|
|
117
120
|
stripFlowTypes: StripFlowTypes.transformProgram,
|
|
118
121
|
};
|
|
@@ -14,5 +14,5 @@ exports.ParserOptionsKeys = void 0;
|
|
|
14
14
|
*
|
|
15
15
|
* @format
|
|
16
16
|
*/
|
|
17
|
-
const ParserOptionsKeys = new Set(['allowReturnOutsideFunction', 'babel', 'flow', 'enableExperimentalComponentSyntax', 'enableExperimentalFlowMatchSyntax', 'reactRuntimeTarget', 'sourceFilename', 'sourceType', 'tokens']);
|
|
17
|
+
const ParserOptionsKeys = new Set(['allowReturnOutsideFunction', 'babel', 'flow', 'enableExperimentalComponentSyntax', 'enableExperimentalFlowMatchSyntax', 'reactRuntimeTarget', 'sourceFilename', 'sourceType', 'tokens', 'transformOptions']);
|
|
18
18
|
exports.ParserOptionsKeys = ParserOptionsKeys;
|
|
@@ -480,6 +480,7 @@ function mapPropertyDefinition(node) {
|
|
|
480
480
|
function mapTypeofTypeAnnotation(node) {
|
|
481
481
|
// $FlowExpectedError[cannot-write]
|
|
482
482
|
delete node.typeArguments; // $FlowFixMe[incompatible-type]
|
|
483
|
+
// $FlowFixMe[invalid-compare]
|
|
483
484
|
|
|
484
485
|
if (node.argument.type !== 'GenericTypeAnnotation') {
|
|
485
486
|
return nodeWith(node, {
|
|
@@ -656,6 +657,7 @@ function transformNode(node) {
|
|
|
656
657
|
|
|
657
658
|
// Check if we have already processed this node.
|
|
658
659
|
// $FlowFixMe[incompatible-type]
|
|
660
|
+
// $FlowFixMe[invalid-compare]
|
|
659
661
|
if (((_node$parent = node.parent) == null ? void 0 : _node$parent.type) === 'File') {
|
|
660
662
|
return node;
|
|
661
663
|
}
|
|
@@ -1110,6 +1112,7 @@ function transformProgram(program, options) {
|
|
|
1110
1112
|
|
|
1111
1113
|
visitorKeys: FlowESTreeAndBabelVisitorKeys
|
|
1112
1114
|
}); // $FlowFixMe[incompatible-type]
|
|
1115
|
+
// $FlowFixMe[invalid-compare]
|
|
1113
1116
|
|
|
1114
1117
|
|
|
1115
1118
|
if ((resultNode == null ? void 0 : resultNode.type) === 'File') {
|
|
@@ -191,7 +191,7 @@ function mapComponentParameters(params, options) {
|
|
|
191
191
|
|
|
192
192
|
if (propsProperties.length === 0) {
|
|
193
193
|
if (refParam == null) {
|
|
194
|
-
throw new Error('
|
|
194
|
+
throw new Error('TransformComponentSyntax: Invalid state, ref should always be set at this point if props are empty');
|
|
195
195
|
}
|
|
196
196
|
|
|
197
197
|
const emptyParamsLoc = {
|
|
@@ -0,0 +1,106 @@
|
|
|
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
|
+
* Transform Flow Enum declarations (https://flow.org/en/docs/enums/).
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
Object.defineProperty(exports, "__esModule", {
|
|
16
|
+
value: true
|
|
17
|
+
});
|
|
18
|
+
exports.transformProgram = transformProgram;
|
|
19
|
+
|
|
20
|
+
var _hermesEstree = require("hermes-estree");
|
|
21
|
+
|
|
22
|
+
var _SimpleTransform = require("../transform/SimpleTransform");
|
|
23
|
+
|
|
24
|
+
var _Builders = require("../utils/Builders");
|
|
25
|
+
|
|
26
|
+
function mapEnumDeclaration(node, options) {
|
|
27
|
+
var _options$transformOpt, _options$transformOpt2;
|
|
28
|
+
|
|
29
|
+
const {
|
|
30
|
+
body
|
|
31
|
+
} = node;
|
|
32
|
+
const {
|
|
33
|
+
members
|
|
34
|
+
} = body;
|
|
35
|
+
const getRuntime = (_options$transformOpt = options.transformOptions) == null ? void 0 : (_options$transformOpt2 = _options$transformOpt.TransformEnumSyntax) == null ? void 0 : _options$transformOpt2.getRuntime;
|
|
36
|
+
const enumModule = typeof getRuntime === 'function' ? getRuntime() : (0, _Builders.callExpression)((0, _Builders.ident)('require'), [(0, _Builders.stringLiteral)('flow-enums-runtime')]);
|
|
37
|
+
const mirrored = body.type === 'EnumStringBody' && (!members.length || members[0].type === 'EnumDefaultedMember');
|
|
38
|
+
const enumExpression = mirrored ? (0, _Builders.callExpression)({
|
|
39
|
+
type: 'MemberExpression',
|
|
40
|
+
object: enumModule,
|
|
41
|
+
property: (0, _Builders.ident)('Mirrored'),
|
|
42
|
+
computed: false,
|
|
43
|
+
optional: false,
|
|
44
|
+
...(0, _Builders.etc)()
|
|
45
|
+
}, [{
|
|
46
|
+
type: 'ArrayExpression',
|
|
47
|
+
elements: members.map(member => (0, _Builders.stringLiteral)(member.id.name)),
|
|
48
|
+
trailingComma: false,
|
|
49
|
+
...(0, _Builders.etc)()
|
|
50
|
+
}]) : (0, _Builders.callExpression)(enumModule, [{
|
|
51
|
+
type: 'ObjectExpression',
|
|
52
|
+
properties: members.map(member => ({
|
|
53
|
+
type: 'Property',
|
|
54
|
+
key: member.id,
|
|
55
|
+
value: // String enums with `EnumDefaultedMember` are handled above by
|
|
56
|
+
// calculation of `mirrored`.
|
|
57
|
+
member.type === 'EnumDefaultedMember' ? (0, _Builders.callExpression)((0, _Builders.ident)('Symbol'), [(0, _Builders.stringLiteral)(member.id.name)]) : member.init,
|
|
58
|
+
kind: 'init',
|
|
59
|
+
method: false,
|
|
60
|
+
shorthand: false,
|
|
61
|
+
computed: false,
|
|
62
|
+
...(0, _Builders.etc)(),
|
|
63
|
+
parent: _Builders.EMPTY_PARENT
|
|
64
|
+
})),
|
|
65
|
+
...(0, _Builders.etc)()
|
|
66
|
+
}]);
|
|
67
|
+
return (0, _Builders.variableDeclaration)('const', node.id, enumExpression);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function transformProgram(program, options) {
|
|
71
|
+
return _SimpleTransform.SimpleTransform.transformProgram(program, {
|
|
72
|
+
transform(node) {
|
|
73
|
+
switch (node.type) {
|
|
74
|
+
case 'EnumDeclaration':
|
|
75
|
+
{
|
|
76
|
+
return mapEnumDeclaration(node, options);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
case 'ExportDefaultDeclaration':
|
|
80
|
+
{
|
|
81
|
+
const {
|
|
82
|
+
declaration
|
|
83
|
+
} = node;
|
|
84
|
+
|
|
85
|
+
if ((0, _hermesEstree.isEnumDeclaration)(declaration)) {
|
|
86
|
+
const enumDeclaration = mapEnumDeclaration(declaration, options);
|
|
87
|
+
|
|
88
|
+
const exportDefault = _SimpleTransform.SimpleTransform.nodeWith(node, {
|
|
89
|
+
declaration: (0, _Builders.ident)(declaration.id.name)
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
return [enumDeclaration, exportDefault];
|
|
93
|
+
} else {
|
|
94
|
+
return node;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
default:
|
|
99
|
+
{
|
|
100
|
+
return node;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
});
|
|
106
|
+
}
|
package/dist/src/index.js
CHANGED
|
@@ -31,7 +31,9 @@ var _ESTreeVisitorKeys = _interopRequireDefault(require("./generated/ESTreeVisit
|
|
|
31
31
|
|
|
32
32
|
exports.FlowVisitorKeys = _ESTreeVisitorKeys.default;
|
|
33
33
|
|
|
34
|
-
var
|
|
34
|
+
var TransformComponentSyntax = _interopRequireWildcard(require("./estree/TransformComponentSyntax"));
|
|
35
|
+
|
|
36
|
+
var TransformEnumSyntax = _interopRequireWildcard(require("./estree/TransformEnumSyntax"));
|
|
35
37
|
|
|
36
38
|
var TransformMatchSyntax = _interopRequireWildcard(require("./estree/TransformMatchSyntax"));
|
|
37
39
|
|
|
@@ -140,13 +142,14 @@ function parse(code, opts) {
|
|
|
140
142
|
return estreeAST;
|
|
141
143
|
}
|
|
142
144
|
|
|
143
|
-
const loweredESTreeAST = [TransformMatchSyntax.transformProgram,
|
|
145
|
+
const loweredESTreeAST = [TransformEnumSyntax.transformProgram, TransformMatchSyntax.transformProgram, TransformComponentSyntax.transformProgram, StripFlowTypesForBabel.transformProgram].reduce((ast, transform) => transform(ast, options), estreeAST);
|
|
144
146
|
return TransformESTreeToBabel.transformProgram(loweredESTreeAST, options);
|
|
145
147
|
}
|
|
146
148
|
|
|
147
149
|
const Transforms = {
|
|
150
|
+
transformEnumSyntax: TransformEnumSyntax.transformProgram,
|
|
148
151
|
transformMatchSyntax: TransformMatchSyntax.transformProgram,
|
|
149
|
-
|
|
152
|
+
transformComponentSyntax: TransformComponentSyntax.transformProgram,
|
|
150
153
|
stripFlowTypesForBabel: StripFlowTypesForBabel.transformProgram,
|
|
151
154
|
stripFlowTypes: StripFlowTypes.transformProgram
|
|
152
155
|
};
|
|
@@ -20,8 +20,15 @@ var _astNodeMutationHelpers = require("./astNodeMutationHelpers");
|
|
|
20
20
|
|
|
21
21
|
function setParentPointer(node, parent) {
|
|
22
22
|
if (parent != null) {
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
if (Array.isArray(node)) {
|
|
24
|
+
for (const item of node) {
|
|
25
|
+
// $FlowExpectedError[cannot-write]
|
|
26
|
+
item.parent = parent;
|
|
27
|
+
}
|
|
28
|
+
} else {
|
|
29
|
+
// $FlowExpectedError[cannot-write]
|
|
30
|
+
node.parent = parent;
|
|
31
|
+
}
|
|
25
32
|
}
|
|
26
33
|
}
|
|
27
34
|
/**
|
|
@@ -51,7 +58,12 @@ class SimpleTransform {
|
|
|
51
58
|
if (resultNode != null) {
|
|
52
59
|
// Ensure the new node has the correct parent pointers before recursing again.
|
|
53
60
|
setParentPointer(resultNode, parent);
|
|
54
|
-
|
|
61
|
+
|
|
62
|
+
if (Array.isArray(resultNode)) {
|
|
63
|
+
traversedResultNode = resultNode.map(item => this.transform(item, options)).filter(item => item != null);
|
|
64
|
+
} else {
|
|
65
|
+
traversedResultNode = this.transform(resultNode, options);
|
|
66
|
+
}
|
|
55
67
|
}
|
|
56
68
|
|
|
57
69
|
if (parent == null) {
|
|
@@ -59,7 +71,11 @@ class SimpleTransform {
|
|
|
59
71
|
throw new Error('SimpleTransform infra error: Parent not set on non root node, this should not be possible');
|
|
60
72
|
}
|
|
61
73
|
|
|
62
|
-
|
|
74
|
+
if (Array.isArray(traversedResultNode)) {
|
|
75
|
+
throw new Error('SimpleTransform: invalid array result for root node');
|
|
76
|
+
} else {
|
|
77
|
+
resultRootNode = traversedResultNode;
|
|
78
|
+
}
|
|
63
79
|
} else if (traversedResultNode == null) {
|
|
64
80
|
(0, _astNodeMutationHelpers.removeNodeOnParent)(node, parent, options.visitorKeys);
|
|
65
81
|
} else {
|
|
@@ -34,6 +34,7 @@ function getParentKey(target, parent, visitorKeys) {
|
|
|
34
34
|
for (const key of (0, _getVisitorKeys.getVisitorKeys)(parent, visitorKeys)) {
|
|
35
35
|
if ((0, _getVisitorKeys.isNode)( // $FlowExpectedError[prop-missing]
|
|
36
36
|
parent[key])) {
|
|
37
|
+
// $FlowFixMe[invalid-compare]
|
|
37
38
|
if (parent[key] === target) {
|
|
38
39
|
return {
|
|
39
40
|
type: 'single',
|
|
@@ -73,9 +74,13 @@ function replaceNodeOnParent(originalNode, originalNodeParent, nodeToReplaceWith
|
|
|
73
74
|
if (replacementParent.type === 'array') {
|
|
74
75
|
// $FlowExpectedError[prop-missing]
|
|
75
76
|
parent[replacementParent.key] = (0, _astArrayMutationHelpers.replaceInArray)( // $FlowExpectedError[prop-missing]
|
|
76
|
-
parent[replacementParent.key], replacementParent.targetIndex, [nodeToReplaceWith]);
|
|
77
|
+
parent[replacementParent.key], replacementParent.targetIndex, Array.isArray(nodeToReplaceWith) ? nodeToReplaceWith : [nodeToReplaceWith]);
|
|
77
78
|
} else {
|
|
78
|
-
|
|
79
|
+
if (Array.isArray(nodeToReplaceWith)) {
|
|
80
|
+
throw new Error(`Cannot insert array into non-array parent type: ${parent.type}`);
|
|
81
|
+
} // $FlowExpectedError[prop-missing]
|
|
82
|
+
|
|
83
|
+
|
|
79
84
|
parent[replacementParent.key] = nodeToReplaceWith;
|
|
80
85
|
}
|
|
81
86
|
}
|