hermes-parser 0.15.0 → 0.16.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,172 @@
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 'TypeCastExpression':
34
+ {
35
+ return node.expression;
36
+ }
37
+
38
+ case 'CallExpression':
39
+ case 'NewExpression':
40
+ {
41
+ if (node.typeArguments != null) {
42
+ return nodeWith(node, {
43
+ typeArguments: null
44
+ });
45
+ }
46
+
47
+ return node;
48
+ }
49
+
50
+ case 'ObjectPattern':
51
+ case 'ArrayPattern':
52
+ case 'Identifier':
53
+ {
54
+ if (node.typeAnnotation != null) {
55
+ return nodeWith(node, {
56
+ typeAnnotation: null
57
+ });
58
+ }
59
+
60
+ return node;
61
+ }
62
+
63
+ case 'DeclareClass':
64
+ case 'DeclareFunction':
65
+ case 'DeclareInterface':
66
+ case 'DeclareModule':
67
+ case 'DeclareModuleExports':
68
+ case 'DeclareOpaqueType':
69
+ case 'DeclareTypeAlias':
70
+ case 'DeclareVariable':
71
+ case 'InterfaceDeclaration':
72
+ case 'OpaqueType':
73
+ case 'TypeAlias':
74
+ {
75
+ return null;
76
+ }
77
+
78
+ case 'FunctionDeclaration':
79
+ case 'ArrowFunctionExpression':
80
+ case 'FunctionExpression':
81
+ {
82
+ const newParams = [];
83
+
84
+ for (let i = 0; i < node.params.length; i++) {
85
+ if (i === 0 && node.params[0].type === 'Identifier' && node.params[0].name === 'this') {
86
+ continue;
87
+ }
88
+
89
+ let param = node.params[i];
90
+
91
+ if (param.type === 'AssignmentPattern') {
92
+ param = param.left;
93
+ }
94
+
95
+ if (param.optional === true) {
96
+ param = nodeWith(param, {
97
+ optional: false
98
+ });
99
+ }
100
+
101
+ newParams.push(param);
102
+ }
103
+
104
+ return nodeWith(node, {
105
+ params: newParams,
106
+ returnType: null,
107
+ typeParameters: null,
108
+ predicate: null
109
+ });
110
+ }
111
+
112
+ case 'ClassDeclaration':
113
+ case 'ClassExpression':
114
+ {
115
+ return nodeWith(node, {
116
+ typeParameters: null,
117
+ superTypeParameters: null,
118
+ implements: [],
119
+ decorators: []
120
+ });
121
+ }
122
+
123
+ case 'PropertyDefinition':
124
+ {
125
+ return nodeWith(node, {
126
+ typeAnnotation: null,
127
+ variance: null,
128
+ declare: false,
129
+ optional: false
130
+ });
131
+ }
132
+
133
+ case 'ImportDeclaration':
134
+ {
135
+ if (node.importKind === 'type' || node.importKind === 'typeof') {
136
+ return null;
137
+ }
138
+
139
+ const nonTypeSpecifiers = node.specifiers.filter(s => s.type !== 'ImportSpecifier' || s.importKind !== 'type' && s.importKind !== 'typeof');
140
+
141
+ if (nonTypeSpecifiers.length === 0) {
142
+ return null;
143
+ }
144
+
145
+ if (nonTypeSpecifiers.length === node.specifiers.length) {
146
+ return node;
147
+ }
148
+
149
+ return nodeWith(node, {
150
+ specifiers: nonTypeSpecifiers
151
+ });
152
+ }
153
+
154
+ case 'ExportAllDeclaration':
155
+ case 'ExportNamedDeclaration':
156
+ {
157
+ if (node.exportKind === 'type') {
158
+ return null;
159
+ }
160
+
161
+ return node;
162
+ }
163
+
164
+ default:
165
+ {
166
+ return node;
167
+ }
168
+ }
169
+ }
170
+
171
+ });
172
+ }
@@ -0,0 +1,155 @@
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 'TypeCastExpression': {
36
+ return node.expression;
37
+ }
38
+
39
+ case 'CallExpression':
40
+ case 'NewExpression': {
41
+ if (node.typeArguments != null) {
42
+ return nodeWith(node, {typeArguments: null});
43
+ }
44
+ return node;
45
+ }
46
+
47
+ case 'ObjectPattern':
48
+ case 'ArrayPattern':
49
+ case 'Identifier': {
50
+ if (node.typeAnnotation != null) {
51
+ return nodeWith(node, {typeAnnotation: null});
52
+ }
53
+ return node;
54
+ }
55
+
56
+ case 'DeclareClass':
57
+ case 'DeclareFunction':
58
+ case 'DeclareInterface':
59
+ case 'DeclareModule':
60
+ case 'DeclareModuleExports':
61
+ case 'DeclareOpaqueType':
62
+ case 'DeclareTypeAlias':
63
+ case 'DeclareVariable':
64
+ case 'InterfaceDeclaration':
65
+ case 'OpaqueType':
66
+ case 'TypeAlias': {
67
+ return null;
68
+ }
69
+
70
+ case 'FunctionDeclaration':
71
+ case 'ArrowFunctionExpression':
72
+ case 'FunctionExpression': {
73
+ const newParams = [];
74
+ for (let i = 0; i < node.params.length; i++) {
75
+ if (
76
+ i === 0 &&
77
+ node.params[0].type === 'Identifier' &&
78
+ node.params[0].name === 'this'
79
+ ) {
80
+ continue;
81
+ }
82
+
83
+ let param = node.params[i];
84
+ if (param.type === 'AssignmentPattern') {
85
+ param = param.left;
86
+ }
87
+ if (param.optional === true) {
88
+ param = nodeWith(param, {optional: false});
89
+ }
90
+ newParams.push(param);
91
+ }
92
+
93
+ return nodeWith(node, {
94
+ params: newParams,
95
+ returnType: null,
96
+ typeParameters: null,
97
+ predicate: null,
98
+ });
99
+ }
100
+
101
+ case 'ClassDeclaration':
102
+ case 'ClassExpression': {
103
+ return nodeWith(node, {
104
+ typeParameters: null,
105
+ superTypeParameters: null,
106
+ implements: [],
107
+ decorators: [],
108
+ });
109
+ }
110
+
111
+ case 'PropertyDefinition': {
112
+ return nodeWith(node, {
113
+ typeAnnotation: null,
114
+ variance: null,
115
+ declare: false,
116
+ optional: false,
117
+ });
118
+ }
119
+
120
+ case 'ImportDeclaration': {
121
+ if (node.importKind === 'type' || node.importKind === 'typeof') {
122
+ return null;
123
+ }
124
+ const nonTypeSpecifiers = node.specifiers.filter(
125
+ s =>
126
+ s.type !== 'ImportSpecifier' ||
127
+ (s.importKind !== 'type' && s.importKind !== 'typeof'),
128
+ );
129
+ if (nonTypeSpecifiers.length === 0) {
130
+ return null;
131
+ }
132
+ if (nonTypeSpecifiers.length === node.specifiers.length) {
133
+ return node;
134
+ }
135
+
136
+ return nodeWith(node, {
137
+ specifiers: nonTypeSpecifiers,
138
+ });
139
+ }
140
+
141
+ case 'ExportAllDeclaration':
142
+ case 'ExportNamedDeclaration': {
143
+ if (node.exportKind === 'type') {
144
+ return null;
145
+ }
146
+ return node;
147
+ }
148
+
149
+ default: {
150
+ return node;
151
+ }
152
+ }
153
+ },
154
+ });
155
+ }
@@ -0,0 +1,169 @@
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
+ const nodeWith = _SimpleTransform.SimpleTransform.nodeWith; // Rely on the mapper to fix up parent relationships.
28
+
29
+ const EMPTY_PARENT = null;
30
+
31
+ function createSimpleGenericTypeAnnotation(name, nodeForLoc) {
32
+ return {
33
+ type: 'GenericTypeAnnotation',
34
+ id: {
35
+ type: 'Identifier',
36
+ name,
37
+ optional: false,
38
+ typeAnnotation: null,
39
+ loc: nodeForLoc.loc,
40
+ range: nodeForLoc.range,
41
+ parent: EMPTY_PARENT
42
+ },
43
+ typeParameters: null,
44
+ loc: nodeForLoc.loc,
45
+ range: nodeForLoc.range,
46
+ parent: nodeForLoc.parent
47
+ };
48
+ }
49
+
50
+ function createAnyTypeAnnotation(node) {
51
+ return {
52
+ type: 'AnyTypeAnnotation',
53
+ loc: node.loc,
54
+ range: node.range,
55
+ parent: node.parent
56
+ };
57
+ }
58
+ /**
59
+ * Convert DeclareEnum nodes to DeclareVariable
60
+ */
61
+
62
+
63
+ function mapDeclareEnum(node) {
64
+ return {
65
+ type: 'DeclareVariable',
66
+ kind: 'const',
67
+ id: nodeWith(node.id, {
68
+ typeAnnotation: {
69
+ type: 'TypeAnnotation',
70
+ typeAnnotation: createAnyTypeAnnotation(node.body),
71
+ loc: node.body.loc,
72
+ range: node.body.range,
73
+ parent: EMPTY_PARENT
74
+ }
75
+ }),
76
+ loc: node.loc,
77
+ range: node.range,
78
+ parent: node.parent
79
+ };
80
+ }
81
+ /**
82
+ * Remove `this` param from functions.
83
+ */
84
+
85
+
86
+ function mapFunction(node) {
87
+ // Remove the first parameter if it is a this-type annotation,
88
+ // which is not recognized by Babel.
89
+ if (node.params.length !== 0 && node.params[0].name === 'this') {
90
+ return nodeWith(node, {
91
+ params: node.params.slice(1)
92
+ });
93
+ }
94
+
95
+ return node;
96
+ }
97
+ /**
98
+ * Convert to QualifiedTypeIdentifier
99
+ */
100
+
101
+
102
+ function mapQualifiedTypeofIdentifier(node) {
103
+ return {
104
+ type: 'QualifiedTypeIdentifier',
105
+ qualification: node.qualification.type === 'QualifiedTypeofIdentifier' ? mapQualifiedTypeofIdentifier(node.qualification) : node.qualification,
106
+ id: node.id,
107
+ loc: node.loc,
108
+ range: node.range,
109
+ parent: node.parent
110
+ };
111
+ }
112
+
113
+ function transformProgram(program, _options) {
114
+ return _SimpleTransform.SimpleTransform.transformProgram(program, {
115
+ transform(node) {
116
+ switch (node.type) {
117
+ case 'SymbolTypeAnnotation':
118
+ {
119
+ // Convert to simple generic type annotation
120
+ return createSimpleGenericTypeAnnotation('symbol', node);
121
+ }
122
+
123
+ case 'BigIntTypeAnnotation':
124
+ {
125
+ // Convert to simple generic type annotation
126
+ return createSimpleGenericTypeAnnotation('bigint', node);
127
+ }
128
+
129
+ case 'IndexedAccessType':
130
+ case 'OptionalIndexedAccessType':
131
+ case 'KeyofTypeAnnotation':
132
+ case 'ConditionalTypeAnnotation':
133
+ case 'InferTypeAnnotation':
134
+ case 'TupleTypeLabeledElement':
135
+ case 'TupleTypeSpreadElement':
136
+ case 'ObjectTypeMappedTypeProperty':
137
+ case 'ComponentTypeAnnotation':
138
+ case 'TypeOperator':
139
+ case 'TypePredicate':
140
+ {
141
+ // Babel does not support these generic types, so convert to any
142
+ return createAnyTypeAnnotation(node);
143
+ }
144
+
145
+ case 'QualifiedTypeofIdentifier':
146
+ {
147
+ return mapQualifiedTypeofIdentifier(node);
148
+ }
149
+
150
+ case 'DeclareEnum':
151
+ {
152
+ return mapDeclareEnum(node);
153
+ }
154
+
155
+ case 'FunctionDeclaration':
156
+ case 'FunctionExpression':
157
+ {
158
+ return mapFunction(node);
159
+ }
160
+
161
+ default:
162
+ {
163
+ return node;
164
+ }
165
+ }
166
+ }
167
+
168
+ });
169
+ }
@@ -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
+ * @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
+
36
+ const nodeWith = SimpleTransform.nodeWith;
37
+
38
+ // Rely on the mapper to fix up parent relationships.
39
+ const EMPTY_PARENT: $FlowFixMe = null;
40
+
41
+ function createSimpleGenericTypeAnnotation(
42
+ name: string,
43
+ nodeForLoc: ESNode,
44
+ ): GenericTypeAnnotation {
45
+ return {
46
+ type: 'GenericTypeAnnotation',
47
+ id: {
48
+ type: 'Identifier',
49
+ name,
50
+ optional: false,
51
+ typeAnnotation: null,
52
+ loc: nodeForLoc.loc,
53
+ range: nodeForLoc.range,
54
+ parent: EMPTY_PARENT,
55
+ },
56
+ typeParameters: null,
57
+ loc: nodeForLoc.loc,
58
+ range: nodeForLoc.range,
59
+ parent: nodeForLoc.parent,
60
+ };
61
+ }
62
+
63
+ function createAnyTypeAnnotation(node: ESNode): AnyTypeAnnotation {
64
+ return {
65
+ type: 'AnyTypeAnnotation',
66
+ loc: node.loc,
67
+ range: node.range,
68
+ parent: node.parent,
69
+ };
70
+ }
71
+
72
+ /**
73
+ * Convert DeclareEnum nodes to DeclareVariable
74
+ */
75
+ function mapDeclareEnum(node: DeclareEnum): DeclareVariable {
76
+ return {
77
+ type: 'DeclareVariable',
78
+ kind: 'const',
79
+ id: nodeWith(node.id, {
80
+ typeAnnotation: {
81
+ type: 'TypeAnnotation',
82
+ typeAnnotation: createAnyTypeAnnotation(node.body),
83
+ loc: node.body.loc,
84
+ range: node.body.range,
85
+ parent: EMPTY_PARENT,
86
+ },
87
+ }),
88
+ loc: node.loc,
89
+ range: node.range,
90
+ parent: node.parent,
91
+ };
92
+ }
93
+
94
+ /**
95
+ * Remove `this` param from functions.
96
+ */
97
+ function mapFunction(node: AFunction): AFunction {
98
+ // Remove the first parameter if it is a this-type annotation,
99
+ // which is not recognized by Babel.
100
+ if (node.params.length !== 0 && node.params[0].name === 'this') {
101
+ return nodeWith(node, {
102
+ params: node.params.slice(1),
103
+ });
104
+ }
105
+
106
+ return node;
107
+ }
108
+
109
+ /**
110
+ * Convert to QualifiedTypeIdentifier
111
+ */
112
+ function mapQualifiedTypeofIdentifier(
113
+ node: QualifiedTypeofIdentifier,
114
+ ): QualifiedTypeIdentifier {
115
+ return {
116
+ type: 'QualifiedTypeIdentifier',
117
+ qualification:
118
+ node.qualification.type === 'QualifiedTypeofIdentifier'
119
+ ? mapQualifiedTypeofIdentifier(node.qualification)
120
+ : node.qualification,
121
+ id: node.id,
122
+ loc: node.loc,
123
+ range: node.range,
124
+ parent: node.parent,
125
+ };
126
+ }
127
+
128
+ export function transformProgram(
129
+ program: Program,
130
+ _options: ParserOptions,
131
+ ): Program {
132
+ return SimpleTransform.transformProgram(program, {
133
+ transform(node: ESNode) {
134
+ switch (node.type) {
135
+ case 'SymbolTypeAnnotation': {
136
+ // Convert to simple generic type annotation
137
+ return createSimpleGenericTypeAnnotation('symbol', node);
138
+ }
139
+ case 'BigIntTypeAnnotation': {
140
+ // Convert to simple generic type annotation
141
+ return createSimpleGenericTypeAnnotation('bigint', node);
142
+ }
143
+ case 'IndexedAccessType':
144
+ case 'OptionalIndexedAccessType':
145
+ case 'KeyofTypeAnnotation':
146
+ case 'ConditionalTypeAnnotation':
147
+ case 'InferTypeAnnotation':
148
+ case 'TupleTypeLabeledElement':
149
+ case 'TupleTypeSpreadElement':
150
+ case 'ObjectTypeMappedTypeProperty':
151
+ case 'ComponentTypeAnnotation':
152
+ case 'TypeOperator':
153
+ case 'TypePredicate': {
154
+ // Babel does not support these generic types, so convert to any
155
+ return createAnyTypeAnnotation(node);
156
+ }
157
+ case 'QualifiedTypeofIdentifier': {
158
+ return mapQualifiedTypeofIdentifier(node);
159
+ }
160
+ case 'DeclareEnum': {
161
+ return mapDeclareEnum(node);
162
+ }
163
+ case 'FunctionDeclaration':
164
+ case 'FunctionExpression': {
165
+ return mapFunction(node);
166
+ }
167
+ default: {
168
+ return node;
169
+ }
170
+ }
171
+ },
172
+ });
173
+ }
@@ -174,6 +174,7 @@ module.exports = {
174
174
  TypeAnnotation: ['typeAnnotation'],
175
175
  TypeCastExpression: ['expression', 'typeAnnotation'],
176
176
  TypeofTypeAnnotation: ['argument'],
177
+ TypeOperator: ['typeAnnotation'],
177
178
  TypeParameter: ['bound', 'variance', 'default'],
178
179
  TypeParameterDeclaration: ['params'],
179
180
  TypeParameterInstantiation: ['params'],
@@ -595,6 +595,9 @@ const HERMES_AST_VISITOR_KEYS = {
595
595
  TypeofTypeAnnotation: {
596
596
  argument: 'Node'
597
597
  },
598
+ TypeOperator: {
599
+ typeAnnotation: 'Node'
600
+ },
598
601
  TypeParameter: {
599
602
  bound: 'Node',
600
603
  variance: 'Node',
@@ -684,6 +687,9 @@ const HERMES_AST_VISITOR_KEYS = {
684
687
  OptionalMemberExpression: {
685
688
  object: 'Node',
686
689
  property: 'Node'
690
+ },
691
+ ExportNamespaceSpecifier: {
692
+ exported: 'Node'
687
693
  }
688
694
  };
689
695
  exports.HERMES_AST_VISITOR_KEYS = HERMES_AST_VISITOR_KEYS;