hermes-parser 0.4.8 → 0.7.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 +1 -1
- package/dist/HermesAST.js.flow +57 -0
- package/dist/HermesASTAdapter.js +134 -146
- package/dist/HermesASTAdapter.js.flow +178 -0
- package/dist/HermesParser.js +39 -25
- package/dist/HermesParser.js.flow +123 -0
- package/dist/HermesParserDecodeUTF8String.js +17 -12
- package/dist/HermesParserDecodeUTF8String.js.flow +65 -0
- package/dist/HermesParserDeserializer.js +200 -207
- package/dist/HermesParserDeserializer.js.flow +261 -0
- package/dist/HermesParserNodeDeserializers.js +60 -28
- package/dist/HermesParserNodeDeserializers.js.flow +16 -0
- package/dist/HermesParserWASM.js +2 -2
- package/dist/HermesParserWASM.js.flow +75 -0
- package/dist/HermesToBabelAdapter.js +331 -364
- package/dist/HermesToBabelAdapter.js.flow +383 -0
- package/dist/HermesToESTreeAdapter.js +198 -159
- package/dist/HermesToESTreeAdapter.js.flow +233 -0
- package/dist/ParserOptions.js.flow +18 -0
- package/dist/generated/visitor-keys.js +605 -0
- package/dist/generated/visitor-keys.js.flow +17 -0
- package/dist/index.js +32 -19
- package/dist/index.js.flow +72 -13
- package/package.json +7 -1
- package/dist/HermesParserVisitorKeys.js +0 -729
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c)
|
|
3
|
+
Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
|
@@ -0,0 +1,57 @@
|
|
|
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
|
+
// flowlint unclear-type:off
|
|
12
|
+
|
|
13
|
+
export interface HermesPosition {
|
|
14
|
+
/** >= 1 */
|
|
15
|
+
+line: number;
|
|
16
|
+
/** >= 0 */
|
|
17
|
+
+column: number;
|
|
18
|
+
}
|
|
19
|
+
export type HermesSourceLocation = {
|
|
20
|
+
start?: HermesPosition,
|
|
21
|
+
end?: HermesPosition,
|
|
22
|
+
rangeStart?: number,
|
|
23
|
+
rangeEnd?: number,
|
|
24
|
+
};
|
|
25
|
+
export type HermesNode = {
|
|
26
|
+
type: string,
|
|
27
|
+
[string]: any,
|
|
28
|
+
};
|
|
29
|
+
export type HermesToken = {
|
|
30
|
+
type:
|
|
31
|
+
| 'Boolean'
|
|
32
|
+
| 'Identifier'
|
|
33
|
+
| 'Keyword'
|
|
34
|
+
| 'Null'
|
|
35
|
+
| 'Numeric'
|
|
36
|
+
| 'BigInt'
|
|
37
|
+
| 'Punctuator'
|
|
38
|
+
| 'String'
|
|
39
|
+
| 'RegularExpression'
|
|
40
|
+
| 'Template'
|
|
41
|
+
| 'JSXText',
|
|
42
|
+
loc: HermesSourceLocation,
|
|
43
|
+
value: ?string,
|
|
44
|
+
};
|
|
45
|
+
export type HermesComment = {
|
|
46
|
+
type: 'CommentLine' | 'CommentBlock' | 'InterpreterDirective',
|
|
47
|
+
loc: HermesSourceLocation,
|
|
48
|
+
value: ?string,
|
|
49
|
+
};
|
|
50
|
+
export type HermesProgram = {
|
|
51
|
+
type: 'Program',
|
|
52
|
+
loc: HermesSourceLocation,
|
|
53
|
+
body: Array<?HermesNode>,
|
|
54
|
+
comments: Array<HermesComment>,
|
|
55
|
+
tokens?: Array<HermesToken>,
|
|
56
|
+
interpreter?: ?HermesComment,
|
|
57
|
+
};
|
package/dist/HermesASTAdapter.js
CHANGED
|
@@ -1,33 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _visitorKeys = require("./generated/visitor-keys");
|
|
9
|
+
|
|
1
10
|
/**
|
|
2
|
-
* Copyright (c)
|
|
11
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
12
|
*
|
|
4
13
|
* This source code is licensed under the MIT license found in the
|
|
5
14
|
* LICENSE file in the root directory of this source tree.
|
|
6
15
|
*
|
|
16
|
+
*
|
|
7
17
|
* @format
|
|
8
18
|
*/
|
|
9
|
-
'use strict';
|
|
10
19
|
|
|
11
|
-
|
|
20
|
+
/*
|
|
21
|
+
This class does some very "javascripty" things in the name of
|
|
22
|
+
performance which are ultimately impossible to soundly type.
|
|
12
23
|
|
|
13
|
-
|
|
24
|
+
So instead of adding strict types and a large number of suppression
|
|
25
|
+
comments, instead it is left untyped and subclasses are strictly
|
|
26
|
+
typed via a separate flow declaration file.
|
|
27
|
+
*/
|
|
14
28
|
|
|
15
|
-
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
|
16
|
-
|
|
17
|
-
var _require = require('./HermesParserVisitorKeys'),
|
|
18
|
-
HERMES_AST_VISITOR_KEYS = _require.HERMES_AST_VISITOR_KEYS,
|
|
19
|
-
NODE_CHILD = _require.NODE_CHILD,
|
|
20
|
-
NODE_LIST_CHILD = _require.NODE_LIST_CHILD;
|
|
21
29
|
/**
|
|
22
30
|
* The base class for transforming the Hermes AST to the desired output format.
|
|
23
31
|
* Extended by concrete adapters which output an ESTree or Babel AST.
|
|
24
32
|
*/
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
_classCallCheck(this, HermesASTAdapter);
|
|
30
|
-
|
|
33
|
+
class HermesASTAdapter {
|
|
34
|
+
constructor(options) {
|
|
35
|
+
this.sourceFilename = void 0;
|
|
36
|
+
this.sourceType = void 0;
|
|
31
37
|
this.sourceFilename = options.sourceFilename;
|
|
32
38
|
this.sourceType = options.sourceType;
|
|
33
39
|
}
|
|
@@ -37,162 +43,144 @@ var HermesASTAdapter = /*#__PURE__*/function () {
|
|
|
37
43
|
*/
|
|
38
44
|
|
|
39
45
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
// Comments are not traversed via visitor keys
|
|
44
|
-
var comments = program.comments;
|
|
46
|
+
transform(program) {
|
|
47
|
+
// Comments are not traversed via visitor keys
|
|
48
|
+
const comments = program.comments;
|
|
45
49
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
50
|
+
for (let i = 0; i < comments.length; i++) {
|
|
51
|
+
const comment = comments[i];
|
|
52
|
+
this.fixSourceLocation(comment);
|
|
53
|
+
comments[i] = this.mapComment(comment);
|
|
54
|
+
} // The first comment may be an interpreter directive and is stored directly on the program node
|
|
51
55
|
|
|
52
56
|
|
|
53
|
-
|
|
57
|
+
program.interpreter = comments.length > 0 && comments[0].type === 'InterpreterDirective' ? comments.shift() : null; // Tokens are not traversed via visitor keys
|
|
54
58
|
|
|
55
|
-
|
|
59
|
+
const tokens = program.tokens;
|
|
56
60
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
61
|
+
if (tokens) {
|
|
62
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
63
|
+
this.fixSourceLocation(tokens[i]);
|
|
61
64
|
}
|
|
62
|
-
|
|
63
|
-
return this.mapNode(program);
|
|
64
65
|
}
|
|
65
|
-
/**
|
|
66
|
-
* Transform a Hermes AST node to the output AST format.
|
|
67
|
-
*
|
|
68
|
-
* This may modify the input node in-place and return that same node, or a completely
|
|
69
|
-
* new node may be constructed and returned. Overriden in child classes.
|
|
70
|
-
*/
|
|
71
|
-
|
|
72
|
-
}, {
|
|
73
|
-
key: "mapNode",
|
|
74
|
-
value: function mapNode(node) {
|
|
75
|
-
this.fixSourceLocation(node);
|
|
76
|
-
return this.mapNodeDefault(node);
|
|
77
|
-
}
|
|
78
|
-
}, {
|
|
79
|
-
key: "mapNodeDefault",
|
|
80
|
-
value: function mapNodeDefault(node) {
|
|
81
|
-
var visitorKeys = HERMES_AST_VISITOR_KEYS[node.type];
|
|
82
66
|
|
|
83
|
-
|
|
84
|
-
|
|
67
|
+
return this.mapNode(program);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Transform a Hermes AST node to the output AST format.
|
|
71
|
+
*
|
|
72
|
+
* This may modify the input node in-place and return that same node, or a completely
|
|
73
|
+
* new node may be constructed and returned. Overriden in child classes.
|
|
74
|
+
*/
|
|
85
75
|
|
|
86
|
-
if (childType === NODE_CHILD) {
|
|
87
|
-
var child = node[key];
|
|
88
76
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
} else if (childType === NODE_LIST_CHILD) {
|
|
93
|
-
var children = node[key];
|
|
77
|
+
mapNode(_node) {
|
|
78
|
+
throw new Error('Implemented in subclasses');
|
|
79
|
+
}
|
|
94
80
|
|
|
95
|
-
|
|
96
|
-
|
|
81
|
+
mapNodeDefault(node) {
|
|
82
|
+
const visitorKeys = _visitorKeys.HERMES_AST_VISITOR_KEYS[node.type];
|
|
97
83
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
84
|
+
for (const key in visitorKeys) {
|
|
85
|
+
const childType = visitorKeys[key];
|
|
86
|
+
|
|
87
|
+
if (childType === _visitorKeys.NODE_CHILD) {
|
|
88
|
+
const child = node[key];
|
|
89
|
+
|
|
90
|
+
if (child != null) {
|
|
91
|
+
node[key] = this.mapNode(child);
|
|
102
92
|
}
|
|
103
|
-
}
|
|
93
|
+
} else if (childType === _visitorKeys.NODE_LIST_CHILD) {
|
|
94
|
+
const children = node[key];
|
|
104
95
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Update the source location for this node depending on the output AST format.
|
|
109
|
-
* This can modify the input node in-place. Overriden in child classes.
|
|
110
|
-
*/
|
|
111
|
-
|
|
112
|
-
}, {
|
|
113
|
-
key: "fixSourceLocation",
|
|
114
|
-
value: function fixSourceLocation(node) {
|
|
115
|
-
throw new Error('Implemented in subclasses');
|
|
116
|
-
}
|
|
117
|
-
}, {
|
|
118
|
-
key: "getSourceType",
|
|
119
|
-
value: function getSourceType() {
|
|
120
|
-
var _this$sourceType;
|
|
96
|
+
for (let i = 0; i < children.length; i++) {
|
|
97
|
+
const child = children[i];
|
|
121
98
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
value: function setModuleSourceType() {
|
|
127
|
-
if (this.sourceType == null) {
|
|
128
|
-
this.sourceType = 'module';
|
|
99
|
+
if (child != null) {
|
|
100
|
+
children[i] = this.mapNode(child);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
129
103
|
}
|
|
130
104
|
}
|
|
131
|
-
}, {
|
|
132
|
-
key: "mapComment",
|
|
133
|
-
value: function mapComment(node) {
|
|
134
|
-
return node;
|
|
135
|
-
}
|
|
136
|
-
}, {
|
|
137
|
-
key: "mapEmpty",
|
|
138
|
-
value: function mapEmpty(node) {
|
|
139
|
-
return null;
|
|
140
|
-
}
|
|
141
|
-
}, {
|
|
142
|
-
key: "mapImportDeclaration",
|
|
143
|
-
value: function mapImportDeclaration(node) {
|
|
144
|
-
if (node.importKind === 'value') {
|
|
145
|
-
this.setModuleSourceType();
|
|
146
|
-
}
|
|
147
105
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
106
|
+
return node;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Update the source location for this node depending on the output AST format.
|
|
110
|
+
* This can modify the input node in-place. Overriden in child classes.
|
|
111
|
+
*/
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
fixSourceLocation(_node) {
|
|
115
|
+
throw new Error('Implemented in subclasses');
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
getSourceType() {
|
|
119
|
+
var _this$sourceType;
|
|
156
120
|
|
|
157
|
-
|
|
121
|
+
return (_this$sourceType = this.sourceType) != null ? _this$sourceType : 'script';
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
setModuleSourceType() {
|
|
125
|
+
if (this.sourceType == null) {
|
|
126
|
+
this.sourceType = 'module';
|
|
158
127
|
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
mapComment(node) {
|
|
131
|
+
return node;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
mapEmpty(_node) {
|
|
135
|
+
// $FlowExpectedError
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
mapImportDeclaration(node) {
|
|
140
|
+
if (node.importKind === 'value') {
|
|
162
141
|
this.setModuleSourceType();
|
|
163
|
-
return this.mapNodeDefault(node);
|
|
164
142
|
}
|
|
165
|
-
}, {
|
|
166
|
-
key: "mapExportNamedDeclaration",
|
|
167
|
-
value: function mapExportNamedDeclaration(node) {
|
|
168
|
-
if (node.exportKind === 'value') {
|
|
169
|
-
this.setModuleSourceType();
|
|
170
|
-
}
|
|
171
143
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
}, {
|
|
175
|
-
key: "mapExportAllDeclaration",
|
|
176
|
-
value: function mapExportAllDeclaration(node) {
|
|
177
|
-
if (node.exportKind === 'value') {
|
|
178
|
-
this.setModuleSourceType();
|
|
179
|
-
}
|
|
144
|
+
return this.mapNodeDefault(node);
|
|
145
|
+
}
|
|
180
146
|
|
|
181
|
-
|
|
147
|
+
mapImportSpecifier(node) {
|
|
148
|
+
if (node.importKind === 'value') {
|
|
149
|
+
node.importKind = null;
|
|
182
150
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
151
|
+
|
|
152
|
+
return this.mapNodeDefault(node);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
mapExportDefaultDeclaration(node) {
|
|
156
|
+
this.setModuleSourceType();
|
|
157
|
+
return this.mapNodeDefault(node);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
mapExportNamedDeclaration(node) {
|
|
161
|
+
if (node.exportKind === 'value') {
|
|
162
|
+
this.setModuleSourceType();
|
|
187
163
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
164
|
+
|
|
165
|
+
return this.mapNodeDefault(node);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
mapExportAllDeclaration(node) {
|
|
169
|
+
if (node.exportKind === 'value') {
|
|
170
|
+
this.setModuleSourceType();
|
|
192
171
|
}
|
|
193
|
-
}]);
|
|
194
172
|
|
|
195
|
-
|
|
196
|
-
}
|
|
173
|
+
return this.mapNodeDefault(node);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
mapPrivateProperty(node) {
|
|
177
|
+
throw new SyntaxError(this.formatError(node, 'Private properties are not supported'));
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
formatError(node, message) {
|
|
181
|
+
return `${message} (${node.loc.start.line}:${node.loc.start.column})`;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
}
|
|
197
185
|
|
|
198
|
-
|
|
186
|
+
exports.default = HermesASTAdapter;
|
|
@@ -0,0 +1,178 @@
|
|
|
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
|
|
8
|
+
* @format
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/*
|
|
12
|
+
This class does some very "javascripty" things in the name of
|
|
13
|
+
performance which are ultimately impossible to soundly type.
|
|
14
|
+
|
|
15
|
+
So instead of adding strict types and a large number of suppression
|
|
16
|
+
comments, instead it is left untyped and subclasses are strictly
|
|
17
|
+
typed via a separate flow declaration file.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import type {HermesNode} from './HermesAST';
|
|
21
|
+
import type {ParserOptions} from './ParserOptions';
|
|
22
|
+
|
|
23
|
+
import {
|
|
24
|
+
HERMES_AST_VISITOR_KEYS,
|
|
25
|
+
NODE_CHILD,
|
|
26
|
+
NODE_LIST_CHILD,
|
|
27
|
+
} from './generated/visitor-keys';
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The base class for transforming the Hermes AST to the desired output format.
|
|
31
|
+
* Extended by concrete adapters which output an ESTree or Babel AST.
|
|
32
|
+
*/
|
|
33
|
+
export default class HermesASTAdapter {
|
|
34
|
+
sourceFilename: ParserOptions['sourceFilename'];
|
|
35
|
+
sourceType: ParserOptions['sourceType'];
|
|
36
|
+
|
|
37
|
+
constructor(options: ParserOptions) {
|
|
38
|
+
this.sourceFilename = options.sourceFilename;
|
|
39
|
+
this.sourceType = options.sourceType;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Transform the input Hermes AST to the desired output format.
|
|
44
|
+
* This modifies the input AST in place instead of constructing a new AST.
|
|
45
|
+
*/
|
|
46
|
+
transform(program: HermesNode): ?HermesNode {
|
|
47
|
+
// Comments are not traversed via visitor keys
|
|
48
|
+
const comments = program.comments;
|
|
49
|
+
for (let i = 0; i < comments.length; i++) {
|
|
50
|
+
const comment = comments[i];
|
|
51
|
+
this.fixSourceLocation(comment);
|
|
52
|
+
comments[i] = this.mapComment(comment);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// The first comment may be an interpreter directive and is stored directly on the program node
|
|
56
|
+
program.interpreter =
|
|
57
|
+
comments.length > 0 && comments[0].type === 'InterpreterDirective'
|
|
58
|
+
? comments.shift()
|
|
59
|
+
: null;
|
|
60
|
+
|
|
61
|
+
// Tokens are not traversed via visitor keys
|
|
62
|
+
const tokens = program.tokens;
|
|
63
|
+
if (tokens) {
|
|
64
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
65
|
+
this.fixSourceLocation(tokens[i]);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return this.mapNode(program);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Transform a Hermes AST node to the output AST format.
|
|
74
|
+
*
|
|
75
|
+
* This may modify the input node in-place and return that same node, or a completely
|
|
76
|
+
* new node may be constructed and returned. Overriden in child classes.
|
|
77
|
+
*/
|
|
78
|
+
mapNode(_node: HermesNode): HermesNode {
|
|
79
|
+
throw new Error('Implemented in subclasses');
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
mapNodeDefault(node: HermesNode): HermesNode {
|
|
83
|
+
const visitorKeys = HERMES_AST_VISITOR_KEYS[node.type];
|
|
84
|
+
for (const key in visitorKeys) {
|
|
85
|
+
const childType = visitorKeys[key];
|
|
86
|
+
if (childType === NODE_CHILD) {
|
|
87
|
+
const child = node[key];
|
|
88
|
+
if (child != null) {
|
|
89
|
+
node[key] = this.mapNode(child);
|
|
90
|
+
}
|
|
91
|
+
} else if (childType === NODE_LIST_CHILD) {
|
|
92
|
+
const children = node[key];
|
|
93
|
+
for (let i = 0; i < children.length; i++) {
|
|
94
|
+
const child = children[i];
|
|
95
|
+
if (child != null) {
|
|
96
|
+
children[i] = this.mapNode(child);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return node;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Update the source location for this node depending on the output AST format.
|
|
107
|
+
* This can modify the input node in-place. Overriden in child classes.
|
|
108
|
+
*/
|
|
109
|
+
fixSourceLocation(_node: HermesNode): void {
|
|
110
|
+
throw new Error('Implemented in subclasses');
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
getSourceType(): ParserOptions['sourceType'] {
|
|
114
|
+
return this.sourceType ?? 'script';
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
setModuleSourceType(): void {
|
|
118
|
+
if (this.sourceType == null) {
|
|
119
|
+
this.sourceType = 'module';
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
mapComment(node: HermesNode): HermesNode {
|
|
124
|
+
return node;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
mapEmpty(_node: HermesNode): HermesNode {
|
|
128
|
+
// $FlowExpectedError
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
mapImportDeclaration(node: HermesNode): HermesNode {
|
|
133
|
+
if (node.importKind === 'value') {
|
|
134
|
+
this.setModuleSourceType();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return this.mapNodeDefault(node);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
mapImportSpecifier(node: HermesNode): HermesNode {
|
|
141
|
+
if (node.importKind === 'value') {
|
|
142
|
+
node.importKind = null;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return this.mapNodeDefault(node);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
mapExportDefaultDeclaration(node: HermesNode): HermesNode {
|
|
149
|
+
this.setModuleSourceType();
|
|
150
|
+
return this.mapNodeDefault(node);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
mapExportNamedDeclaration(node: HermesNode): HermesNode {
|
|
154
|
+
if (node.exportKind === 'value') {
|
|
155
|
+
this.setModuleSourceType();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return this.mapNodeDefault(node);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
mapExportAllDeclaration(node: HermesNode): HermesNode {
|
|
162
|
+
if (node.exportKind === 'value') {
|
|
163
|
+
this.setModuleSourceType();
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return this.mapNodeDefault(node);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
mapPrivateProperty(node: HermesNode): HermesNode {
|
|
170
|
+
throw new SyntaxError(
|
|
171
|
+
this.formatError(node, 'Private properties are not supported'),
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
formatError(node: HermesNode, message: string): string {
|
|
176
|
+
return `${message} (${node.loc.start.line}:${node.loc.start.column})`;
|
|
177
|
+
}
|
|
178
|
+
}
|
package/dist/HermesParser.js
CHANGED
|
@@ -1,36 +1,53 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright (c)
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
3
|
*
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*
|
|
7
|
+
*
|
|
7
8
|
* @format
|
|
8
9
|
*/
|
|
9
10
|
'use strict';
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
Object.defineProperty(exports, "__esModule", {
|
|
13
|
+
value: true
|
|
14
|
+
});
|
|
15
|
+
exports.parse = parse;
|
|
12
16
|
|
|
13
|
-
var
|
|
17
|
+
var _HermesParserDeserializer = _interopRequireDefault(require("./HermesParserDeserializer"));
|
|
18
|
+
|
|
19
|
+
var _HermesParserWASM = _interopRequireDefault(require("./HermesParserWASM"));
|
|
20
|
+
|
|
21
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
22
|
+
|
|
23
|
+
const hermesParse = _HermesParserWASM.default.cwrap('hermesParse', 'number', ['number', 'number', 'number', 'number', 'number']);
|
|
24
|
+
|
|
25
|
+
const hermesParseResult_free = _HermesParserWASM.default.cwrap('hermesParseResult_free', 'void', ['number']);
|
|
26
|
+
|
|
27
|
+
const hermesParseResult_getError = _HermesParserWASM.default.cwrap('hermesParseResult_getError', 'string', ['number']);
|
|
28
|
+
|
|
29
|
+
const hermesParseResult_getErrorLine = _HermesParserWASM.default.cwrap('hermesParseResult_getErrorLine', 'number', ['number']);
|
|
30
|
+
|
|
31
|
+
const hermesParseResult_getErrorColumn = _HermesParserWASM.default.cwrap('hermesParseResult_getErrorColumn', 'number', ['number']);
|
|
32
|
+
|
|
33
|
+
const hermesParseResult_getProgramBuffer = _HermesParserWASM.default.cwrap('hermesParseResult_getProgramBuffer', 'number', ['number']);
|
|
34
|
+
|
|
35
|
+
const hermesParseResult_getPositionBuffer = _HermesParserWASM.default.cwrap('hermesParseResult_getPositionBuffer', 'number', ['number']);
|
|
36
|
+
|
|
37
|
+
const hermesParseResult_getPositionBufferSize = _HermesParserWASM.default.cwrap('hermesParseResult_getPositionBufferSize', 'number', ['number']); // Copy a string into the WASM heap and null-terminate
|
|
14
38
|
|
|
15
|
-
var hermesParse = HermesParserWASM.cwrap('hermesParse', 'number', ['number', 'number', 'number', 'number', 'number']);
|
|
16
|
-
var hermesParseResult_free = HermesParserWASM.cwrap('hermesParseResult_free', 'void', ['number']);
|
|
17
|
-
var hermesParseResult_getError = HermesParserWASM.cwrap('hermesParseResult_getError', 'string', ['number']);
|
|
18
|
-
var hermesParseResult_getErrorLine = HermesParserWASM.cwrap('hermesParseResult_getErrorLine', 'number', ['number']);
|
|
19
|
-
var hermesParseResult_getErrorColumn = HermesParserWASM.cwrap('hermesParseResult_getErrorColumn', 'number', ['number']);
|
|
20
|
-
var hermesParseResult_getProgramBuffer = HermesParserWASM.cwrap('hermesParseResult_getProgramBuffer', 'number', ['number']);
|
|
21
|
-
var hermesParseResult_getPositionBuffer = HermesParserWASM.cwrap('hermesParseResult_getPositionBuffer', 'number', ['number']);
|
|
22
|
-
var hermesParseResult_getPositionBufferSize = HermesParserWASM.cwrap('hermesParseResult_getPositionBufferSize', 'number', ['number']); // Copy a string into the WASM heap and null-terminate
|
|
23
39
|
|
|
24
40
|
function copyToHeap(buffer, addr) {
|
|
25
|
-
|
|
26
|
-
|
|
41
|
+
_HermesParserWASM.default.HEAP8.set(buffer, addr);
|
|
42
|
+
|
|
43
|
+
_HermesParserWASM.default.HEAP8[addr + buffer.length] = 0;
|
|
27
44
|
}
|
|
28
45
|
|
|
29
46
|
function parse(source, options) {
|
|
30
47
|
// Allocate space on heap for source text
|
|
31
|
-
|
|
48
|
+
const sourceBuffer = Buffer.from(source, 'utf8');
|
|
32
49
|
|
|
33
|
-
|
|
50
|
+
const sourceAddr = _HermesParserWASM.default._malloc(sourceBuffer.length + 1);
|
|
34
51
|
|
|
35
52
|
if (!sourceAddr) {
|
|
36
53
|
throw new Error('Parser out of memory');
|
|
@@ -39,14 +56,15 @@ function parse(source, options) {
|
|
|
39
56
|
try {
|
|
40
57
|
// Copy source text onto WASM heap
|
|
41
58
|
copyToHeap(sourceBuffer, sourceAddr);
|
|
42
|
-
|
|
59
|
+
const parseResult = hermesParse(sourceAddr, sourceBuffer.length + 1, options.flow === 'detect', options.tokens, options.allowReturnOutsideFunction);
|
|
43
60
|
|
|
44
61
|
try {
|
|
45
62
|
// Extract and throw error from parse result if parsing failed
|
|
46
|
-
|
|
63
|
+
const err = hermesParseResult_getError(parseResult);
|
|
47
64
|
|
|
48
65
|
if (err) {
|
|
49
|
-
|
|
66
|
+
const syntaxError = new SyntaxError(err); // $FlowExpectedError[prop-missing]
|
|
67
|
+
|
|
50
68
|
syntaxError.loc = {
|
|
51
69
|
line: hermesParseResult_getErrorLine(parseResult),
|
|
52
70
|
column: hermesParseResult_getErrorColumn(parseResult)
|
|
@@ -54,16 +72,12 @@ function parse(source, options) {
|
|
|
54
72
|
throw syntaxError;
|
|
55
73
|
}
|
|
56
74
|
|
|
57
|
-
|
|
75
|
+
const deserializer = new _HermesParserDeserializer.default(hermesParseResult_getProgramBuffer(parseResult), hermesParseResult_getPositionBuffer(parseResult), hermesParseResult_getPositionBufferSize(parseResult), _HermesParserWASM.default, options);
|
|
58
76
|
return deserializer.deserialize();
|
|
59
77
|
} finally {
|
|
60
78
|
hermesParseResult_free(parseResult);
|
|
61
79
|
}
|
|
62
80
|
} finally {
|
|
63
|
-
|
|
81
|
+
_HermesParserWASM.default._free(sourceAddr);
|
|
64
82
|
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
module.exports = {
|
|
68
|
-
parse: parse
|
|
69
|
-
};
|
|
83
|
+
}
|