@ohos-ports/hermes-parser 0.37.0-beta.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/LICENSE +21 -0
- package/README.md +15 -0
- package/dist/HermesAST.js.flow +57 -0
- package/dist/HermesASTAdapter.js +192 -0
- package/dist/HermesASTAdapter.js.flow +189 -0
- package/dist/HermesParser.js +108 -0
- package/dist/HermesParser.js.flow +163 -0
- package/dist/HermesParserDecodeUTF8String.js +68 -0
- package/dist/HermesParserDecodeUTF8String.js.flow +65 -0
- package/dist/HermesParserDeserializer.js +242 -0
- package/dist/HermesParserDeserializer.js.flow +260 -0
- package/dist/HermesParserNodeDeserializers.js +2594 -0
- package/dist/HermesParserNodeDeserializers.js.flow +16 -0
- package/dist/HermesParserWASM.js +6 -0
- package/dist/HermesParserWASM.js.flow +97 -0
- package/dist/HermesToESTreeAdapter.js +441 -0
- package/dist/HermesToESTreeAdapter.js.flow +423 -0
- package/dist/ParserOptions.js +20 -0
- package/dist/ParserOptions.js.flow +44 -0
- package/dist/babel/TransformESTreeToBabel.js +1197 -0
- package/dist/babel/TransformESTreeToBabel.js.flow +1349 -0
- package/dist/estree/StripFlowTypes.js +175 -0
- package/dist/estree/StripFlowTypes.js.flow +158 -0
- package/dist/estree/StripFlowTypesForBabel.js +215 -0
- package/dist/estree/StripFlowTypesForBabel.js.flow +216 -0
- package/dist/estree/TransformComponentSyntax.js +792 -0
- package/dist/estree/TransformComponentSyntax.js.flow +864 -0
- package/dist/estree/TransformEnumSyntax.js +106 -0
- package/dist/estree/TransformEnumSyntax.js.flow +125 -0
- package/dist/estree/TransformMatchSyntax.js +1083 -0
- package/dist/estree/TransformMatchSyntax.js.flow +991 -0
- package/dist/estree/TransformRecordSyntax.js +296 -0
- package/dist/estree/TransformRecordSyntax.js.flow +310 -0
- package/dist/generated/ESTreeVisitorKeys.js +233 -0
- package/dist/generated/ESTreeVisitorKeys.js.flow +15 -0
- package/dist/generated/ParserVisitorKeys.js +842 -0
- package/dist/generated/ParserVisitorKeys.js.flow +17 -0
- package/dist/getModuleDocblock.js +112 -0
- package/dist/getModuleDocblock.js.flow +118 -0
- package/dist/index.js +170 -0
- package/dist/index.js.flow +131 -0
- package/dist/src/HermesASTAdapter.js +192 -0
- package/dist/src/HermesParser.js +108 -0
- package/dist/src/HermesParserDecodeUTF8String.js +68 -0
- package/dist/src/HermesParserDeserializer.js +242 -0
- package/dist/src/HermesParserNodeDeserializers.js +2594 -0
- package/dist/src/HermesToESTreeAdapter.js +441 -0
- package/dist/src/ParserOptions.js +20 -0
- package/dist/src/babel/TransformESTreeToBabel.js +1197 -0
- package/dist/src/estree/StripFlowTypes.js +175 -0
- package/dist/src/estree/StripFlowTypesForBabel.js +215 -0
- package/dist/src/estree/TransformComponentSyntax.js +792 -0
- package/dist/src/estree/TransformEnumSyntax.js +106 -0
- package/dist/src/estree/TransformMatchSyntax.js +1083 -0
- package/dist/src/estree/TransformRecordSyntax.js +296 -0
- package/dist/src/generated/ESTreeVisitorKeys.js +233 -0
- package/dist/src/generated/ParserVisitorKeys.js +842 -0
- package/dist/src/getModuleDocblock.js +112 -0
- package/dist/src/index.js +170 -0
- package/dist/src/transform/SimpleTransform.js +145 -0
- package/dist/src/transform/astArrayMutationHelpers.js +62 -0
- package/dist/src/transform/astNodeMutationHelpers.js +201 -0
- package/dist/src/traverse/SimpleTraverser.js +139 -0
- package/dist/src/traverse/getVisitorKeys.js +39 -0
- package/dist/src/utils/Builders.js +190 -0
- package/dist/src/utils/GenID.js +46 -0
- package/dist/src/utils/createSyntaxError.js +25 -0
- package/dist/src/utils/isReservedWord.js +62 -0
- package/dist/src/utils/mutateESTreeASTForPrettier.js +127 -0
- package/dist/transform/SimpleTransform.js +145 -0
- package/dist/transform/SimpleTransform.js.flow +168 -0
- package/dist/transform/astArrayMutationHelpers.js +62 -0
- package/dist/transform/astArrayMutationHelpers.js.flow +71 -0
- package/dist/transform/astNodeMutationHelpers.js +201 -0
- package/dist/transform/astNodeMutationHelpers.js.flow +252 -0
- package/dist/traverse/SimpleTraverser.js +139 -0
- package/dist/traverse/SimpleTraverser.js.flow +133 -0
- package/dist/traverse/getVisitorKeys.js +39 -0
- package/dist/traverse/getVisitorKeys.js.flow +38 -0
- package/dist/utils/Builders.js +190 -0
- package/dist/utils/Builders.js.flow +218 -0
- package/dist/utils/GenID.js +46 -0
- package/dist/utils/GenID.js.flow +39 -0
- package/dist/utils/createSyntaxError.js +25 -0
- package/dist/utils/createSyntaxError.js.flow +24 -0
- package/dist/utils/isReservedWord.js +62 -0
- package/dist/utils/isReservedWord.js.flow +57 -0
- package/dist/utils/mutateESTreeASTForPrettier.js +127 -0
- package/dist/utils/mutateESTreeASTForPrettier.js.flow +130 -0
- package/package.json +30 -0
|
@@ -0,0 +1,17 @@
|
|
|
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
|
+
declare export var NODE_CHILD: 'Node';
|
|
12
|
+
declare export var NODE_LIST_CHILD: 'NodeList';
|
|
13
|
+
declare export var HERMES_AST_VISITOR_KEYS: Readonly<{
|
|
14
|
+
[string]: Readonly<{
|
|
15
|
+
[string]: 'Node' | 'NodeList',
|
|
16
|
+
}>,
|
|
17
|
+
}>;
|
|
@@ -0,0 +1,112 @@
|
|
|
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
|
+
* strict
|
|
8
|
+
* @format
|
|
9
|
+
*/
|
|
10
|
+
'use strict';
|
|
11
|
+
|
|
12
|
+
Object.defineProperty(exports, "__esModule", {
|
|
13
|
+
value: true
|
|
14
|
+
});
|
|
15
|
+
exports.getModuleDocblock = getModuleDocblock;
|
|
16
|
+
exports.parseDocblockString = parseDocblockString;
|
|
17
|
+
const DIRECTIVE_REGEX = /^\s*@([a-zA-Z0-9_-]+)( +.+)?$/;
|
|
18
|
+
|
|
19
|
+
function parseDocblockString(docblock) {
|
|
20
|
+
const directiveLines = docblock.split('\n') // remove the leading " *" from each line
|
|
21
|
+
.map(line => line.trimStart().replace(/^\* ?/, '').trim()).filter(line => line.startsWith('@'));
|
|
22
|
+
const directives = // $FlowExpectedError[incompatible-type] - flow types this return as {...}
|
|
23
|
+
Object.create(null);
|
|
24
|
+
|
|
25
|
+
for (const line of directiveLines) {
|
|
26
|
+
var _match$;
|
|
27
|
+
|
|
28
|
+
const match = DIRECTIVE_REGEX.exec(line);
|
|
29
|
+
|
|
30
|
+
if (match == null) {
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const name = match[1]; // explicitly use an empty string if there's no value
|
|
35
|
+
// this way the array length tracks how many instances of the directive there was
|
|
36
|
+
|
|
37
|
+
const value = ((_match$ = match[2]) != null ? _match$ : '').trim();
|
|
38
|
+
|
|
39
|
+
if (directives[name]) {
|
|
40
|
+
directives[name].push(value);
|
|
41
|
+
} else {
|
|
42
|
+
directives[name] = [value];
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return directives;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function getModuleDocblock(hermesProgram) {
|
|
50
|
+
const docblockNode = (() => {
|
|
51
|
+
if (hermesProgram.type !== 'Program') {
|
|
52
|
+
return null;
|
|
53
|
+
} // $FlowExpectedError[incompatible-type] - escape out of the unsafe hermes types
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
const program = hermesProgram;
|
|
57
|
+
|
|
58
|
+
if (program.comments.length === 0) {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const firstComment = (() => {
|
|
63
|
+
const first = program.comments[0];
|
|
64
|
+
|
|
65
|
+
if (first.type === 'Block') {
|
|
66
|
+
return first;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (program.comments.length === 1) {
|
|
70
|
+
return null;
|
|
71
|
+
} // ESLint will always strip out the shebang comment from the code before passing it to the parser
|
|
72
|
+
// https://github.com/eslint/eslint/blob/21d647904dc30f9484b22acdd9243a6d0ecfba38/lib/linter/linter.js#L779
|
|
73
|
+
// this means that we're forced to parse it as a line comment :(
|
|
74
|
+
// this hacks around it by selecting the second comment in this case
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
const second = program.comments[1];
|
|
78
|
+
|
|
79
|
+
if (first.type === 'Line' && first.range[0] === 0 && second.type === 'Block') {
|
|
80
|
+
return second;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return null;
|
|
84
|
+
})();
|
|
85
|
+
|
|
86
|
+
if (firstComment == null) {
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
/*
|
|
90
|
+
Handle cases like this where the comment isn't actually the first thing in the code:
|
|
91
|
+
```
|
|
92
|
+
const x = 1; /* docblock *./
|
|
93
|
+
```
|
|
94
|
+
*/
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
if (program.body.length > 0 && program.body[0].range[0] < firstComment.range[0]) {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return firstComment;
|
|
102
|
+
})();
|
|
103
|
+
|
|
104
|
+
if (docblockNode == null) {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return {
|
|
109
|
+
directives: parseDocblockString(docblockNode.value),
|
|
110
|
+
comment: docblockNode
|
|
111
|
+
};
|
|
112
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
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
|
+
import type {HermesNode} from './HermesAST';
|
|
14
|
+
import type {DocblockDirectives, Program} from 'hermes-estree';
|
|
15
|
+
|
|
16
|
+
const DIRECTIVE_REGEX = /^\s*@([a-zA-Z0-9_-]+)( +.+)?$/;
|
|
17
|
+
|
|
18
|
+
export function parseDocblockString(docblock: string): DocblockDirectives {
|
|
19
|
+
const directiveLines = docblock
|
|
20
|
+
.split('\n')
|
|
21
|
+
// remove the leading " *" from each line
|
|
22
|
+
.map(line => line.trimStart().replace(/^\* ?/, '').trim())
|
|
23
|
+
.filter(line => line.startsWith('@'));
|
|
24
|
+
|
|
25
|
+
const directives: {
|
|
26
|
+
[string]: Array<string>,
|
|
27
|
+
} =
|
|
28
|
+
// $FlowExpectedError[incompatible-type] - flow types this return as {...}
|
|
29
|
+
Object.create(null);
|
|
30
|
+
|
|
31
|
+
for (const line of directiveLines) {
|
|
32
|
+
const match = DIRECTIVE_REGEX.exec(line);
|
|
33
|
+
if (match == null) {
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
const name = match[1];
|
|
37
|
+
// explicitly use an empty string if there's no value
|
|
38
|
+
// this way the array length tracks how many instances of the directive there was
|
|
39
|
+
const value = (match[2] ?? '').trim();
|
|
40
|
+
if (directives[name]) {
|
|
41
|
+
directives[name].push(value);
|
|
42
|
+
} else {
|
|
43
|
+
directives[name] = [value];
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return directives;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function getModuleDocblock(
|
|
51
|
+
hermesProgram: HermesNode,
|
|
52
|
+
): Program['docblock'] {
|
|
53
|
+
const docblockNode = (() => {
|
|
54
|
+
if (hermesProgram.type !== 'Program') {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// $FlowExpectedError[incompatible-type] - escape out of the unsafe hermes types
|
|
59
|
+
const program: Program = hermesProgram;
|
|
60
|
+
|
|
61
|
+
if (program.comments.length === 0) {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const firstComment = (() => {
|
|
66
|
+
const first = program.comments[0];
|
|
67
|
+
if (first.type === 'Block') {
|
|
68
|
+
return first;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (program.comments.length === 1) {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// ESLint will always strip out the shebang comment from the code before passing it to the parser
|
|
76
|
+
// https://github.com/eslint/eslint/blob/21d647904dc30f9484b22acdd9243a6d0ecfba38/lib/linter/linter.js#L779
|
|
77
|
+
// this means that we're forced to parse it as a line comment :(
|
|
78
|
+
// this hacks around it by selecting the second comment in this case
|
|
79
|
+
const second = program.comments[1];
|
|
80
|
+
if (
|
|
81
|
+
first.type === 'Line' &&
|
|
82
|
+
first.range[0] === 0 &&
|
|
83
|
+
second.type === 'Block'
|
|
84
|
+
) {
|
|
85
|
+
return second;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return null;
|
|
89
|
+
})();
|
|
90
|
+
if (firstComment == null) {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/*
|
|
95
|
+
Handle cases like this where the comment isn't actually the first thing in the code:
|
|
96
|
+
```
|
|
97
|
+
const x = 1; /* docblock *./
|
|
98
|
+
```
|
|
99
|
+
*/
|
|
100
|
+
if (
|
|
101
|
+
program.body.length > 0 &&
|
|
102
|
+
program.body[0].range[0] < firstComment.range[0]
|
|
103
|
+
) {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return firstComment;
|
|
108
|
+
})();
|
|
109
|
+
|
|
110
|
+
if (docblockNode == null) {
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return {
|
|
115
|
+
directives: parseDocblockString(docblockNode.value),
|
|
116
|
+
comment: docblockNode,
|
|
117
|
+
};
|
|
118
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
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
|
+
* strict
|
|
8
|
+
* @format
|
|
9
|
+
*/
|
|
10
|
+
'use strict';
|
|
11
|
+
|
|
12
|
+
Object.defineProperty(exports, "__esModule", {
|
|
13
|
+
value: true
|
|
14
|
+
});
|
|
15
|
+
var _exportNames = {
|
|
16
|
+
parse: true,
|
|
17
|
+
mutateESTreeASTForPrettier: true,
|
|
18
|
+
Transforms: true,
|
|
19
|
+
FlowVisitorKeys: true,
|
|
20
|
+
astArrayMutationHelpers: true,
|
|
21
|
+
astNodeMutationHelpers: true
|
|
22
|
+
};
|
|
23
|
+
exports.mutateESTreeASTForPrettier = exports.astNodeMutationHelpers = exports.astArrayMutationHelpers = exports.Transforms = void 0;
|
|
24
|
+
exports.parse = parse;
|
|
25
|
+
|
|
26
|
+
var HermesParser = _interopRequireWildcard(require("./HermesParser"));
|
|
27
|
+
|
|
28
|
+
var _HermesToESTreeAdapter = _interopRequireDefault(require("./HermesToESTreeAdapter"));
|
|
29
|
+
|
|
30
|
+
var _ESTreeVisitorKeys = _interopRequireDefault(require("./generated/ESTreeVisitorKeys"));
|
|
31
|
+
|
|
32
|
+
exports.FlowVisitorKeys = _ESTreeVisitorKeys.default;
|
|
33
|
+
|
|
34
|
+
var TransformComponentSyntax = _interopRequireWildcard(require("./estree/TransformComponentSyntax"));
|
|
35
|
+
|
|
36
|
+
var TransformEnumSyntax = _interopRequireWildcard(require("./estree/TransformEnumSyntax"));
|
|
37
|
+
|
|
38
|
+
var TransformMatchSyntax = _interopRequireWildcard(require("./estree/TransformMatchSyntax"));
|
|
39
|
+
|
|
40
|
+
var TransformRecordSyntax = _interopRequireWildcard(require("./estree/TransformRecordSyntax"));
|
|
41
|
+
|
|
42
|
+
var StripFlowTypesForBabel = _interopRequireWildcard(require("./estree/StripFlowTypesForBabel"));
|
|
43
|
+
|
|
44
|
+
var TransformESTreeToBabel = _interopRequireWildcard(require("./babel/TransformESTreeToBabel"));
|
|
45
|
+
|
|
46
|
+
var StripFlowTypes = _interopRequireWildcard(require("./estree/StripFlowTypes"));
|
|
47
|
+
|
|
48
|
+
var _ParserOptions = require("./ParserOptions");
|
|
49
|
+
|
|
50
|
+
Object.keys(_ParserOptions).forEach(function (key) {
|
|
51
|
+
if (key === "default" || key === "__esModule") return;
|
|
52
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
53
|
+
if (key in exports && exports[key] === _ParserOptions[key]) return;
|
|
54
|
+
exports[key] = _ParserOptions[key];
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
var _SimpleTraverser = require("./traverse/SimpleTraverser");
|
|
58
|
+
|
|
59
|
+
Object.keys(_SimpleTraverser).forEach(function (key) {
|
|
60
|
+
if (key === "default" || key === "__esModule") return;
|
|
61
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
62
|
+
if (key in exports && exports[key] === _SimpleTraverser[key]) return;
|
|
63
|
+
exports[key] = _SimpleTraverser[key];
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
var _SimpleTransform = require("./transform/SimpleTransform");
|
|
67
|
+
|
|
68
|
+
Object.keys(_SimpleTransform).forEach(function (key) {
|
|
69
|
+
if (key === "default" || key === "__esModule") return;
|
|
70
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
71
|
+
if (key in exports && exports[key] === _SimpleTransform[key]) return;
|
|
72
|
+
exports[key] = _SimpleTransform[key];
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
var _getVisitorKeys = require("./traverse/getVisitorKeys");
|
|
76
|
+
|
|
77
|
+
Object.keys(_getVisitorKeys).forEach(function (key) {
|
|
78
|
+
if (key === "default" || key === "__esModule") return;
|
|
79
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
80
|
+
if (key in exports && exports[key] === _getVisitorKeys[key]) return;
|
|
81
|
+
exports[key] = _getVisitorKeys[key];
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
var _astArrayMutationHelpers = _interopRequireWildcard(require("./transform/astArrayMutationHelpers"));
|
|
85
|
+
|
|
86
|
+
exports.astArrayMutationHelpers = _astArrayMutationHelpers;
|
|
87
|
+
|
|
88
|
+
var _astNodeMutationHelpers = _interopRequireWildcard(require("./transform/astNodeMutationHelpers"));
|
|
89
|
+
|
|
90
|
+
exports.astNodeMutationHelpers = _astNodeMutationHelpers;
|
|
91
|
+
|
|
92
|
+
var _mutateESTreeASTForPrettier = _interopRequireDefault(require("./utils/mutateESTreeASTForPrettier"));
|
|
93
|
+
|
|
94
|
+
exports.mutateESTreeASTForPrettier = _mutateESTreeASTForPrettier.default;
|
|
95
|
+
|
|
96
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
97
|
+
|
|
98
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
99
|
+
|
|
100
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
101
|
+
|
|
102
|
+
const DEFAULTS = {
|
|
103
|
+
flow: 'detect'
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
function getOptions(options = { ...DEFAULTS
|
|
107
|
+
}) {
|
|
108
|
+
// Default to detecting whether to parse Flow syntax by the presence
|
|
109
|
+
// of an pragma.
|
|
110
|
+
if (options.flow == null) {
|
|
111
|
+
options.flow = DEFAULTS.flow;
|
|
112
|
+
} else if (options.flow !== 'all' && options.flow !== 'detect') {
|
|
113
|
+
throw new Error('flow option must be "all" or "detect"');
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (options.sourceType === 'unambiguous') {
|
|
117
|
+
// Clear source type so that it will be detected from the contents of the file
|
|
118
|
+
delete options.sourceType;
|
|
119
|
+
} else if (options.sourceType != null && options.sourceType !== 'script' && options.sourceType !== 'module') {
|
|
120
|
+
throw new Error('sourceType option must be "script", "module", or "unambiguous" if set');
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (options.enableExperimentalComponentSyntax == null) {
|
|
124
|
+
options.enableExperimentalComponentSyntax = true; // Enable by default
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (options.enableExperimentalFlowMatchSyntax == null) {
|
|
128
|
+
options.enableExperimentalFlowMatchSyntax = true; // Enable by default
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (options.enableExperimentalFlowRecordSyntax == null) {
|
|
132
|
+
options.enableExperimentalFlowRecordSyntax = true; // Enable by default
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
options.tokens = options.tokens === true;
|
|
136
|
+
options.allowReturnOutsideFunction = options.allowReturnOutsideFunction === true;
|
|
137
|
+
return options;
|
|
138
|
+
} // eslint-disable-next-line no-redeclare
|
|
139
|
+
// eslint-disable-next-line no-redeclare
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
function parse(code, opts) {
|
|
143
|
+
var _options$transformOpt, _options$transformOpt2;
|
|
144
|
+
|
|
145
|
+
const options = getOptions(opts);
|
|
146
|
+
const ast = HermesParser.parse(code, options);
|
|
147
|
+
const estreeAdapter = new _HermesToESTreeAdapter.default(options, code);
|
|
148
|
+
const estreeAST = estreeAdapter.transform(ast);
|
|
149
|
+
|
|
150
|
+
if (options.babel !== true) {
|
|
151
|
+
return estreeAST;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const loweredESTreeAST = [(_options$transformOpt = options.transformOptions) != null && (_options$transformOpt2 = _options$transformOpt.TransformEnumSyntax) != null && _options$transformOpt2.enable ? TransformEnumSyntax.transformProgram : null, TransformMatchSyntax.transformProgram, TransformComponentSyntax.transformProgram, TransformRecordSyntax.transformProgram, StripFlowTypesForBabel.transformProgram].reduce((ast, transform) => {
|
|
155
|
+
var _transform;
|
|
156
|
+
|
|
157
|
+
return (_transform = transform == null ? void 0 : transform(ast, options)) != null ? _transform : ast;
|
|
158
|
+
}, estreeAST);
|
|
159
|
+
return TransformESTreeToBabel.transformProgram(loweredESTreeAST, options);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const Transforms = {
|
|
163
|
+
transformEnumSyntax: TransformEnumSyntax.transformProgram,
|
|
164
|
+
transformMatchSyntax: TransformMatchSyntax.transformProgram,
|
|
165
|
+
transformComponentSyntax: TransformComponentSyntax.transformProgram,
|
|
166
|
+
transformRecordSyntax: TransformRecordSyntax.transformProgram,
|
|
167
|
+
stripFlowTypesForBabel: StripFlowTypesForBabel.transformProgram,
|
|
168
|
+
stripFlowTypes: StripFlowTypes.transformProgram
|
|
169
|
+
};
|
|
170
|
+
exports.Transforms = Transforms;
|
|
@@ -0,0 +1,131 @@
|
|
|
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
|
+
import type {Program as ESTreeProgram} from 'hermes-estree';
|
|
14
|
+
import type {ParserOptions} from './ParserOptions';
|
|
15
|
+
import type {BabelFile} from './babel/TransformESTreeToBabel';
|
|
16
|
+
|
|
17
|
+
import * as HermesParser from './HermesParser';
|
|
18
|
+
import HermesToESTreeAdapter from './HermesToESTreeAdapter';
|
|
19
|
+
import FlowVisitorKeys from './generated/ESTreeVisitorKeys';
|
|
20
|
+
import * as TransformComponentSyntax from './estree/TransformComponentSyntax';
|
|
21
|
+
import * as TransformEnumSyntax from './estree/TransformEnumSyntax';
|
|
22
|
+
import * as TransformMatchSyntax from './estree/TransformMatchSyntax';
|
|
23
|
+
import * as TransformRecordSyntax from './estree/TransformRecordSyntax';
|
|
24
|
+
import * as StripFlowTypesForBabel from './estree/StripFlowTypesForBabel';
|
|
25
|
+
import * as TransformESTreeToBabel from './babel/TransformESTreeToBabel';
|
|
26
|
+
import * as StripFlowTypes from './estree/StripFlowTypes';
|
|
27
|
+
|
|
28
|
+
const DEFAULTS: {flow: 'detect'} = {
|
|
29
|
+
flow: 'detect',
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
function getOptions(options?: ParserOptions = {...DEFAULTS}) {
|
|
33
|
+
// Default to detecting whether to parse Flow syntax by the presence
|
|
34
|
+
// of an @flow pragma.
|
|
35
|
+
if (options.flow == null) {
|
|
36
|
+
options.flow = DEFAULTS.flow;
|
|
37
|
+
} else if (options.flow !== 'all' && options.flow !== 'detect') {
|
|
38
|
+
throw new Error('flow option must be "all" or "detect"');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (options.sourceType === 'unambiguous') {
|
|
42
|
+
// Clear source type so that it will be detected from the contents of the file
|
|
43
|
+
delete options.sourceType;
|
|
44
|
+
} else if (
|
|
45
|
+
options.sourceType != null &&
|
|
46
|
+
options.sourceType !== 'script' &&
|
|
47
|
+
options.sourceType !== 'module'
|
|
48
|
+
) {
|
|
49
|
+
throw new Error(
|
|
50
|
+
'sourceType option must be "script", "module", or "unambiguous" if set',
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (options.enableExperimentalComponentSyntax == null) {
|
|
55
|
+
options.enableExperimentalComponentSyntax = true; // Enable by default
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (options.enableExperimentalFlowMatchSyntax == null) {
|
|
59
|
+
options.enableExperimentalFlowMatchSyntax = true; // Enable by default
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (options.enableExperimentalFlowRecordSyntax == null) {
|
|
63
|
+
options.enableExperimentalFlowRecordSyntax = true; // Enable by default
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
options.tokens = options.tokens === true;
|
|
67
|
+
options.allowReturnOutsideFunction =
|
|
68
|
+
options.allowReturnOutsideFunction === true;
|
|
69
|
+
|
|
70
|
+
return options;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
declare function parse(
|
|
74
|
+
code: string,
|
|
75
|
+
opts: Readonly<{...ParserOptions, babel: true}>,
|
|
76
|
+
): BabelFile;
|
|
77
|
+
// eslint-disable-next-line no-redeclare
|
|
78
|
+
declare function parse(
|
|
79
|
+
code: string,
|
|
80
|
+
opts?:
|
|
81
|
+
| Readonly<{...ParserOptions, babel?: false | void}>
|
|
82
|
+
| Readonly<{...ParserOptions, babel: false}>,
|
|
83
|
+
): ESTreeProgram;
|
|
84
|
+
|
|
85
|
+
// eslint-disable-next-line no-redeclare
|
|
86
|
+
export function parse(
|
|
87
|
+
code: string,
|
|
88
|
+
opts?: ParserOptions,
|
|
89
|
+
): BabelFile | ESTreeProgram {
|
|
90
|
+
const options = getOptions(opts);
|
|
91
|
+
const ast = HermesParser.parse(code, options);
|
|
92
|
+
|
|
93
|
+
const estreeAdapter = new HermesToESTreeAdapter(options, code);
|
|
94
|
+
const estreeAST = estreeAdapter.transform(ast);
|
|
95
|
+
|
|
96
|
+
if (options.babel !== true) {
|
|
97
|
+
return estreeAST;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const loweredESTreeAST = [
|
|
101
|
+
options.transformOptions?.TransformEnumSyntax?.enable
|
|
102
|
+
? TransformEnumSyntax.transformProgram
|
|
103
|
+
: null,
|
|
104
|
+
TransformMatchSyntax.transformProgram,
|
|
105
|
+
TransformComponentSyntax.transformProgram,
|
|
106
|
+
TransformRecordSyntax.transformProgram,
|
|
107
|
+
StripFlowTypesForBabel.transformProgram,
|
|
108
|
+
].reduce((ast, transform) => transform?.(ast, options) ?? ast, estreeAST);
|
|
109
|
+
|
|
110
|
+
return TransformESTreeToBabel.transformProgram(loweredESTreeAST, options);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export type {ParserOptions} from './ParserOptions';
|
|
114
|
+
export * from './ParserOptions';
|
|
115
|
+
export * from './traverse/SimpleTraverser';
|
|
116
|
+
export * from './transform/SimpleTransform';
|
|
117
|
+
export * from './traverse/getVisitorKeys';
|
|
118
|
+
export {FlowVisitorKeys};
|
|
119
|
+
export * as astArrayMutationHelpers from './transform/astArrayMutationHelpers';
|
|
120
|
+
export * as astNodeMutationHelpers from './transform/astNodeMutationHelpers';
|
|
121
|
+
export {default as mutateESTreeASTForPrettier} from './utils/mutateESTreeASTForPrettier';
|
|
122
|
+
|
|
123
|
+
const Transforms = {
|
|
124
|
+
transformEnumSyntax: TransformEnumSyntax.transformProgram,
|
|
125
|
+
transformMatchSyntax: TransformMatchSyntax.transformProgram,
|
|
126
|
+
transformComponentSyntax: TransformComponentSyntax.transformProgram,
|
|
127
|
+
transformRecordSyntax: TransformRecordSyntax.transformProgram,
|
|
128
|
+
stripFlowTypesForBabel: StripFlowTypesForBabel.transformProgram,
|
|
129
|
+
stripFlowTypes: StripFlowTypes.transformProgram,
|
|
130
|
+
};
|
|
131
|
+
export {Transforms};
|