hermes-parser 0.15.1 → 0.17.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.
@@ -0,0 +1,173 @@
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
+
11
+ /**
12
+ * This transform strips all Flow types.
13
+ *
14
+ * It is expected that all transforms create valid ESTree AST output. If
15
+ * the transform requires outputting Babel specific AST nodes then it
16
+ * should live in `ConvertESTreeToBabel.js`
17
+ */
18
+ 'use strict';
19
+
20
+ Object.defineProperty(exports, "__esModule", {
21
+ value: true
22
+ });
23
+ exports.transformProgram = transformProgram;
24
+
25
+ var _SimpleTransform = require("../transform/SimpleTransform");
26
+
27
+ const nodeWith = _SimpleTransform.SimpleTransform.nodeWith;
28
+
29
+ function transformProgram(program, _options) {
30
+ return _SimpleTransform.SimpleTransform.transformProgram(program, {
31
+ transform(node) {
32
+ switch (node.type) {
33
+ case 'AsExpression':
34
+ case 'TypeCastExpression':
35
+ {
36
+ return node.expression;
37
+ }
38
+
39
+ case 'CallExpression':
40
+ case 'NewExpression':
41
+ {
42
+ if (node.typeArguments != null) {
43
+ return nodeWith(node, {
44
+ typeArguments: null
45
+ });
46
+ }
47
+
48
+ return node;
49
+ }
50
+
51
+ case 'ObjectPattern':
52
+ case 'ArrayPattern':
53
+ case 'Identifier':
54
+ {
55
+ if (node.typeAnnotation != null) {
56
+ return nodeWith(node, {
57
+ typeAnnotation: null
58
+ });
59
+ }
60
+
61
+ return node;
62
+ }
63
+
64
+ case 'DeclareClass':
65
+ case 'DeclareFunction':
66
+ case 'DeclareInterface':
67
+ case 'DeclareModule':
68
+ case 'DeclareModuleExports':
69
+ case 'DeclareOpaqueType':
70
+ case 'DeclareTypeAlias':
71
+ case 'DeclareVariable':
72
+ case 'InterfaceDeclaration':
73
+ case 'OpaqueType':
74
+ case 'TypeAlias':
75
+ {
76
+ return null;
77
+ }
78
+
79
+ case 'FunctionDeclaration':
80
+ case 'ArrowFunctionExpression':
81
+ case 'FunctionExpression':
82
+ {
83
+ const newParams = [];
84
+
85
+ for (let i = 0; i < node.params.length; i++) {
86
+ if (i === 0 && node.params[0].type === 'Identifier' && node.params[0].name === 'this') {
87
+ continue;
88
+ }
89
+
90
+ let param = node.params[i];
91
+
92
+ if (param.type === 'AssignmentPattern') {
93
+ param = param.left;
94
+ }
95
+
96
+ if (param.optional === true) {
97
+ param = nodeWith(param, {
98
+ optional: false
99
+ });
100
+ }
101
+
102
+ newParams.push(param);
103
+ }
104
+
105
+ return nodeWith(node, {
106
+ params: newParams,
107
+ returnType: null,
108
+ typeParameters: null,
109
+ predicate: null
110
+ });
111
+ }
112
+
113
+ case 'ClassDeclaration':
114
+ case 'ClassExpression':
115
+ {
116
+ return nodeWith(node, {
117
+ typeParameters: null,
118
+ superTypeParameters: null,
119
+ implements: [],
120
+ decorators: []
121
+ });
122
+ }
123
+
124
+ case 'PropertyDefinition':
125
+ {
126
+ return nodeWith(node, {
127
+ typeAnnotation: null,
128
+ variance: null,
129
+ declare: false,
130
+ optional: false
131
+ });
132
+ }
133
+
134
+ case 'ImportDeclaration':
135
+ {
136
+ if (node.importKind === 'type' || node.importKind === 'typeof') {
137
+ return null;
138
+ }
139
+
140
+ const nonTypeSpecifiers = node.specifiers.filter(s => s.type !== 'ImportSpecifier' || s.importKind !== 'type' && s.importKind !== 'typeof');
141
+
142
+ if (nonTypeSpecifiers.length === 0) {
143
+ return null;
144
+ }
145
+
146
+ if (nonTypeSpecifiers.length === node.specifiers.length) {
147
+ return node;
148
+ }
149
+
150
+ return nodeWith(node, {
151
+ specifiers: nonTypeSpecifiers
152
+ });
153
+ }
154
+
155
+ case 'ExportAllDeclaration':
156
+ case 'ExportNamedDeclaration':
157
+ {
158
+ if (node.exportKind === 'type') {
159
+ return null;
160
+ }
161
+
162
+ return node;
163
+ }
164
+
165
+ default:
166
+ {
167
+ return node;
168
+ }
169
+ }
170
+ }
171
+
172
+ });
173
+ }
@@ -0,0 +1,156 @@
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
+ /**
12
+ * This transform strips all Flow types.
13
+ *
14
+ * It is expected that all transforms create valid ESTree AST output. If
15
+ * the transform requires outputting Babel specific AST nodes then it
16
+ * should live in `ConvertESTreeToBabel.js`
17
+ */
18
+
19
+ 'use strict';
20
+
21
+ import type {ParserOptions} from '../ParserOptions';
22
+ import type {Program, ESNode} from 'hermes-estree';
23
+
24
+ import {SimpleTransform} from '../transform/SimpleTransform';
25
+
26
+ const nodeWith = SimpleTransform.nodeWith;
27
+
28
+ export function transformProgram(
29
+ program: Program,
30
+ _options: ParserOptions,
31
+ ): Program {
32
+ return SimpleTransform.transformProgram(program, {
33
+ transform(node: ESNode) {
34
+ switch (node.type) {
35
+ case 'AsExpression':
36
+ case 'TypeCastExpression': {
37
+ return node.expression;
38
+ }
39
+
40
+ case 'CallExpression':
41
+ case 'NewExpression': {
42
+ if (node.typeArguments != null) {
43
+ return nodeWith(node, {typeArguments: null});
44
+ }
45
+ return node;
46
+ }
47
+
48
+ case 'ObjectPattern':
49
+ case 'ArrayPattern':
50
+ case 'Identifier': {
51
+ if (node.typeAnnotation != null) {
52
+ return nodeWith(node, {typeAnnotation: null});
53
+ }
54
+ return node;
55
+ }
56
+
57
+ case 'DeclareClass':
58
+ case 'DeclareFunction':
59
+ case 'DeclareInterface':
60
+ case 'DeclareModule':
61
+ case 'DeclareModuleExports':
62
+ case 'DeclareOpaqueType':
63
+ case 'DeclareTypeAlias':
64
+ case 'DeclareVariable':
65
+ case 'InterfaceDeclaration':
66
+ case 'OpaqueType':
67
+ case 'TypeAlias': {
68
+ return null;
69
+ }
70
+
71
+ case 'FunctionDeclaration':
72
+ case 'ArrowFunctionExpression':
73
+ case 'FunctionExpression': {
74
+ const newParams = [];
75
+ for (let i = 0; i < node.params.length; i++) {
76
+ if (
77
+ i === 0 &&
78
+ node.params[0].type === 'Identifier' &&
79
+ node.params[0].name === 'this'
80
+ ) {
81
+ continue;
82
+ }
83
+
84
+ let param = node.params[i];
85
+ if (param.type === 'AssignmentPattern') {
86
+ param = param.left;
87
+ }
88
+ if (param.optional === true) {
89
+ param = nodeWith(param, {optional: false});
90
+ }
91
+ newParams.push(param);
92
+ }
93
+
94
+ return nodeWith(node, {
95
+ params: newParams,
96
+ returnType: null,
97
+ typeParameters: null,
98
+ predicate: null,
99
+ });
100
+ }
101
+
102
+ case 'ClassDeclaration':
103
+ case 'ClassExpression': {
104
+ return nodeWith(node, {
105
+ typeParameters: null,
106
+ superTypeParameters: null,
107
+ implements: [],
108
+ decorators: [],
109
+ });
110
+ }
111
+
112
+ case 'PropertyDefinition': {
113
+ return nodeWith(node, {
114
+ typeAnnotation: null,
115
+ variance: null,
116
+ declare: false,
117
+ optional: false,
118
+ });
119
+ }
120
+
121
+ case 'ImportDeclaration': {
122
+ if (node.importKind === 'type' || node.importKind === 'typeof') {
123
+ return null;
124
+ }
125
+ const nonTypeSpecifiers = node.specifiers.filter(
126
+ s =>
127
+ s.type !== 'ImportSpecifier' ||
128
+ (s.importKind !== 'type' && s.importKind !== 'typeof'),
129
+ );
130
+ if (nonTypeSpecifiers.length === 0) {
131
+ return null;
132
+ }
133
+ if (nonTypeSpecifiers.length === node.specifiers.length) {
134
+ return node;
135
+ }
136
+
137
+ return nodeWith(node, {
138
+ specifiers: nonTypeSpecifiers,
139
+ });
140
+ }
141
+
142
+ case 'ExportAllDeclaration':
143
+ case 'ExportNamedDeclaration': {
144
+ if (node.exportKind === 'type') {
145
+ return null;
146
+ }
147
+ return node;
148
+ }
149
+
150
+ default: {
151
+ return node;
152
+ }
153
+ }
154
+ },
155
+ });
156
+ }
@@ -0,0 +1,186 @@
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
+
11
+ /**
12
+ * This transform strips Flow types that are not supported past Babel 7.
13
+ *
14
+ * It is expected that all transforms create valid ESTree AST output. If
15
+ * the transform requires outputting Babel specific AST nodes then it
16
+ * should live in `ConvertESTreeToBabel.js`
17
+ */
18
+ 'use strict';
19
+
20
+ Object.defineProperty(exports, "__esModule", {
21
+ value: true
22
+ });
23
+ exports.transformProgram = transformProgram;
24
+
25
+ var _SimpleTransform = require("../transform/SimpleTransform");
26
+
27
+ var _createSyntaxError = require("../utils/createSyntaxError");
28
+
29
+ const nodeWith = _SimpleTransform.SimpleTransform.nodeWith; // Rely on the mapper to fix up parent relationships.
30
+
31
+ const EMPTY_PARENT = null;
32
+
33
+ function createSimpleGenericTypeAnnotation(name, nodeForLoc) {
34
+ return {
35
+ type: 'GenericTypeAnnotation',
36
+ id: {
37
+ type: 'Identifier',
38
+ name,
39
+ optional: false,
40
+ typeAnnotation: null,
41
+ loc: nodeForLoc.loc,
42
+ range: nodeForLoc.range,
43
+ parent: EMPTY_PARENT
44
+ },
45
+ typeParameters: null,
46
+ loc: nodeForLoc.loc,
47
+ range: nodeForLoc.range,
48
+ parent: nodeForLoc.parent
49
+ };
50
+ }
51
+
52
+ function createAnyTypeAnnotation(node) {
53
+ return {
54
+ type: 'AnyTypeAnnotation',
55
+ loc: node.loc,
56
+ range: node.range,
57
+ parent: node.parent
58
+ };
59
+ }
60
+ /**
61
+ * Convert DeclareEnum nodes to DeclareVariable
62
+ */
63
+
64
+
65
+ function mapDeclareEnum(node) {
66
+ return {
67
+ type: 'DeclareVariable',
68
+ kind: 'const',
69
+ id: nodeWith(node.id, {
70
+ typeAnnotation: {
71
+ type: 'TypeAnnotation',
72
+ typeAnnotation: createAnyTypeAnnotation(node.body),
73
+ loc: node.body.loc,
74
+ range: node.body.range,
75
+ parent: EMPTY_PARENT
76
+ }
77
+ }),
78
+ loc: node.loc,
79
+ range: node.range,
80
+ parent: node.parent
81
+ };
82
+ }
83
+ /**
84
+ * Remove `this` param from functions.
85
+ */
86
+
87
+
88
+ function mapFunction(node) {
89
+ // Remove the first parameter if it is a this-type annotation,
90
+ // which is not recognized by Babel.
91
+ if (node.params.length !== 0 && node.params[0].name === 'this') {
92
+ return nodeWith(node, {
93
+ params: node.params.slice(1)
94
+ });
95
+ }
96
+
97
+ return node;
98
+ }
99
+ /**
100
+ * Convert to QualifiedTypeIdentifier
101
+ */
102
+
103
+
104
+ function mapQualifiedTypeofIdentifier(node) {
105
+ return {
106
+ type: 'QualifiedTypeIdentifier',
107
+ qualification: node.qualification.type === 'QualifiedTypeofIdentifier' ? mapQualifiedTypeofIdentifier(node.qualification) : node.qualification,
108
+ id: node.id,
109
+ loc: node.loc,
110
+ range: node.range,
111
+ parent: node.parent
112
+ };
113
+ }
114
+
115
+ function transformProgram(program, _options) {
116
+ return _SimpleTransform.SimpleTransform.transformProgram(program, {
117
+ transform(node) {
118
+ switch (node.type) {
119
+ case 'SymbolTypeAnnotation':
120
+ {
121
+ // Convert to simple generic type annotation
122
+ return createSimpleGenericTypeAnnotation('symbol', node);
123
+ }
124
+
125
+ case 'BigIntTypeAnnotation':
126
+ {
127
+ // Convert to simple generic type annotation
128
+ return createSimpleGenericTypeAnnotation('bigint', node);
129
+ }
130
+
131
+ case 'ObjectTypeAnnotation':
132
+ {
133
+ const shouldStrip = node.properties.some(prop => prop.type === 'ObjectTypeMappedTypeProperty');
134
+
135
+ if (shouldStrip) {
136
+ return createAnyTypeAnnotation(node);
137
+ }
138
+
139
+ return node;
140
+ }
141
+
142
+ case 'ObjectTypeMappedTypeProperty':
143
+ {
144
+ throw (0, _createSyntaxError.createSyntaxError)(node, `Invalid AST structure, ObjectTypeMappedTypeProperty found outside of an ObjectTypeAnnotation`);
145
+ }
146
+
147
+ case 'IndexedAccessType':
148
+ case 'OptionalIndexedAccessType':
149
+ case 'KeyofTypeAnnotation':
150
+ case 'ConditionalTypeAnnotation':
151
+ case 'InferTypeAnnotation':
152
+ case 'TupleTypeLabeledElement':
153
+ case 'TupleTypeSpreadElement':
154
+ case 'ComponentTypeAnnotation':
155
+ case 'TypeOperator':
156
+ case 'TypePredicate':
157
+ {
158
+ // Babel does not support these generic types, so convert to any
159
+ return createAnyTypeAnnotation(node);
160
+ }
161
+
162
+ case 'QualifiedTypeofIdentifier':
163
+ {
164
+ return mapQualifiedTypeofIdentifier(node);
165
+ }
166
+
167
+ case 'DeclareEnum':
168
+ {
169
+ return mapDeclareEnum(node);
170
+ }
171
+
172
+ case 'FunctionDeclaration':
173
+ case 'FunctionExpression':
174
+ {
175
+ return mapFunction(node);
176
+ }
177
+
178
+ default:
179
+ {
180
+ return node;
181
+ }
182
+ }
183
+ }
184
+
185
+ });
186
+ }
@@ -0,0 +1,189 @@
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
+ /**
12
+ * This transform strips Flow types that are not supported past Babel 7.
13
+ *
14
+ * It is expected that all transforms create valid ESTree AST output. If
15
+ * the transform requires outputting Babel specific AST nodes then it
16
+ * should live in `ConvertESTreeToBabel.js`
17
+ */
18
+
19
+ 'use strict';
20
+
21
+ import type {ParserOptions} from '../ParserOptions';
22
+ import type {
23
+ Program,
24
+ ESNode,
25
+ DeclareEnum,
26
+ DeclareVariable,
27
+ AnyTypeAnnotation,
28
+ GenericTypeAnnotation,
29
+ QualifiedTypeIdentifier,
30
+ QualifiedTypeofIdentifier,
31
+ AFunction,
32
+ } from 'hermes-estree';
33
+
34
+ import {SimpleTransform} from '../transform/SimpleTransform';
35
+ import {createSyntaxError} from '../utils/createSyntaxError';
36
+
37
+ const nodeWith = SimpleTransform.nodeWith;
38
+
39
+ // Rely on the mapper to fix up parent relationships.
40
+ const EMPTY_PARENT: $FlowFixMe = null;
41
+
42
+ function createSimpleGenericTypeAnnotation(
43
+ name: string,
44
+ nodeForLoc: ESNode,
45
+ ): GenericTypeAnnotation {
46
+ return {
47
+ type: 'GenericTypeAnnotation',
48
+ id: {
49
+ type: 'Identifier',
50
+ name,
51
+ optional: false,
52
+ typeAnnotation: null,
53
+ loc: nodeForLoc.loc,
54
+ range: nodeForLoc.range,
55
+ parent: EMPTY_PARENT,
56
+ },
57
+ typeParameters: null,
58
+ loc: nodeForLoc.loc,
59
+ range: nodeForLoc.range,
60
+ parent: nodeForLoc.parent,
61
+ };
62
+ }
63
+
64
+ function createAnyTypeAnnotation(node: ESNode): AnyTypeAnnotation {
65
+ return {
66
+ type: 'AnyTypeAnnotation',
67
+ loc: node.loc,
68
+ range: node.range,
69
+ parent: node.parent,
70
+ };
71
+ }
72
+
73
+ /**
74
+ * Convert DeclareEnum nodes to DeclareVariable
75
+ */
76
+ function mapDeclareEnum(node: DeclareEnum): DeclareVariable {
77
+ return {
78
+ type: 'DeclareVariable',
79
+ kind: 'const',
80
+ id: nodeWith(node.id, {
81
+ typeAnnotation: {
82
+ type: 'TypeAnnotation',
83
+ typeAnnotation: createAnyTypeAnnotation(node.body),
84
+ loc: node.body.loc,
85
+ range: node.body.range,
86
+ parent: EMPTY_PARENT,
87
+ },
88
+ }),
89
+ loc: node.loc,
90
+ range: node.range,
91
+ parent: node.parent,
92
+ };
93
+ }
94
+
95
+ /**
96
+ * Remove `this` param from functions.
97
+ */
98
+ function mapFunction(node: AFunction): AFunction {
99
+ // Remove the first parameter if it is a this-type annotation,
100
+ // which is not recognized by Babel.
101
+ if (node.params.length !== 0 && node.params[0].name === 'this') {
102
+ return nodeWith(node, {
103
+ params: node.params.slice(1),
104
+ });
105
+ }
106
+
107
+ return node;
108
+ }
109
+
110
+ /**
111
+ * Convert to QualifiedTypeIdentifier
112
+ */
113
+ function mapQualifiedTypeofIdentifier(
114
+ node: QualifiedTypeofIdentifier,
115
+ ): QualifiedTypeIdentifier {
116
+ return {
117
+ type: 'QualifiedTypeIdentifier',
118
+ qualification:
119
+ node.qualification.type === 'QualifiedTypeofIdentifier'
120
+ ? mapQualifiedTypeofIdentifier(node.qualification)
121
+ : node.qualification,
122
+ id: node.id,
123
+ loc: node.loc,
124
+ range: node.range,
125
+ parent: node.parent,
126
+ };
127
+ }
128
+
129
+ export function transformProgram(
130
+ program: Program,
131
+ _options: ParserOptions,
132
+ ): Program {
133
+ return SimpleTransform.transformProgram(program, {
134
+ transform(node: ESNode) {
135
+ switch (node.type) {
136
+ case 'SymbolTypeAnnotation': {
137
+ // Convert to simple generic type annotation
138
+ return createSimpleGenericTypeAnnotation('symbol', node);
139
+ }
140
+ case 'BigIntTypeAnnotation': {
141
+ // Convert to simple generic type annotation
142
+ return createSimpleGenericTypeAnnotation('bigint', node);
143
+ }
144
+ case 'ObjectTypeAnnotation': {
145
+ const shouldStrip = node.properties.some(
146
+ prop => prop.type === 'ObjectTypeMappedTypeProperty',
147
+ );
148
+ if (shouldStrip) {
149
+ return createAnyTypeAnnotation(node);
150
+ }
151
+
152
+ return node;
153
+ }
154
+ case 'ObjectTypeMappedTypeProperty': {
155
+ throw createSyntaxError(
156
+ node,
157
+ `Invalid AST structure, ObjectTypeMappedTypeProperty found outside of an ObjectTypeAnnotation`,
158
+ );
159
+ }
160
+ case 'IndexedAccessType':
161
+ case 'OptionalIndexedAccessType':
162
+ case 'KeyofTypeAnnotation':
163
+ case 'ConditionalTypeAnnotation':
164
+ case 'InferTypeAnnotation':
165
+ case 'TupleTypeLabeledElement':
166
+ case 'TupleTypeSpreadElement':
167
+ case 'ComponentTypeAnnotation':
168
+ case 'TypeOperator':
169
+ case 'TypePredicate': {
170
+ // Babel does not support these generic types, so convert to any
171
+ return createAnyTypeAnnotation(node);
172
+ }
173
+ case 'QualifiedTypeofIdentifier': {
174
+ return mapQualifiedTypeofIdentifier(node);
175
+ }
176
+ case 'DeclareEnum': {
177
+ return mapDeclareEnum(node);
178
+ }
179
+ case 'FunctionDeclaration':
180
+ case 'FunctionExpression': {
181
+ return mapFunction(node);
182
+ }
183
+ default: {
184
+ return node;
185
+ }
186
+ }
187
+ },
188
+ });
189
+ }
@@ -27,6 +27,7 @@ module.exports = {
27
27
  ArrayPattern: ['elements', 'typeAnnotation'],
28
28
  ArrayTypeAnnotation: ['elementType'],
29
29
  ArrowFunctionExpression: ['id', 'params', 'body', 'typeParameters', 'returnType', 'predicate'],
30
+ AsExpression: ['expression', 'typeAnnotation'],
30
31
  AssignmentExpression: ['left', 'right'],
31
32
  AssignmentPattern: ['left', 'right'],
32
33
  AwaitExpression: ['argument'],
@@ -174,6 +175,7 @@ module.exports = {
174
175
  TypeAnnotation: ['typeAnnotation'],
175
176
  TypeCastExpression: ['expression', 'typeAnnotation'],
176
177
  TypeofTypeAnnotation: ['argument'],
178
+ TypeOperator: ['typeAnnotation'],
177
179
  TypeParameter: ['bound', 'variance', 'default'],
178
180
  TypeParameterDeclaration: ['params'],
179
181
  TypeParameterInstantiation: ['params'],