hermes-parser 0.11.0 → 0.12.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,116 @@
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
+ Object.defineProperty(exports, "__esModule", {
13
+ value: true
14
+ });
15
+ exports.default = mutate;
16
+
17
+ var _SimpleTransform = require("../transform/SimpleTransform");
18
+
19
+ // https://github.com/prettier/prettier/blob/d962466a828f8ef51435e3e8840178d90b7ec6cd/src/language-js/parse/postprocess/index.js#L161-L182
20
+ function transformChainExpression(node) {
21
+ switch (node.type) {
22
+ case 'CallExpression':
23
+ // $FlowExpectedError[cannot-spread-interface]
24
+ return { ...node,
25
+ type: 'OptionalCallExpression',
26
+ callee: transformChainExpression(node.callee)
27
+ };
28
+
29
+ case 'MemberExpression':
30
+ // $FlowExpectedError[cannot-spread-interface]
31
+ return { ...node,
32
+ type: 'OptionalMemberExpression',
33
+ object: transformChainExpression(node.object)
34
+ };
35
+ // No default
36
+ }
37
+
38
+ return node;
39
+ }
40
+
41
+ function mutate(rootNode, visitorKeys) {
42
+ // Since we don't return the result of `transform` we need to be careful not to replace the Program root node.
43
+ _SimpleTransform.SimpleTransform.transform(rootNode, {
44
+ transform(node) {
45
+ // prettier fully expects the parent pointers are NOT set and
46
+ // certain cases can crash due to prettier infinite-looping
47
+ // whilst naively traversing the parent property
48
+ // https://github.com/prettier/prettier/issues/11793
49
+ // Note: Only needed for prettier V2, this is supported in V3
50
+ if (node.parent) {
51
+ // $FlowExpectedError[cannot-write]
52
+ delete node.parent;
53
+ } // prettier currently relies on the AST being in the old-school, deprecated AST format for optional chaining
54
+ // so we have to apply their transform to our AST so it can actually format it.
55
+ // Note: Only needed for prettier V2, this is supported in V3
56
+
57
+
58
+ if (node.type === 'ChainExpression') {
59
+ return transformChainExpression(node.expression);
60
+ } // Prettier currently relies on comparing the `node` vs `node.value` start positions to know if an
61
+ // `ObjectTypeProperty` is a method or not (instead of using the `node.method` boolean). To correctly print
62
+ // the node when its not a method we need the start position to be different from the `node.value`s start
63
+ // position.
64
+
65
+
66
+ if (node.type === 'ObjectTypeProperty') {
67
+ if (node.method === false && node.kind === 'init' && node.range[0] === 1 && node.value.range[0] === 1) {
68
+ // $FlowExpectedError[cannot-write]
69
+ // $FlowExpectedError[cannot-spread-interface]
70
+ node.value = { ...node.value,
71
+ range: [2, node.value.range[1]]
72
+ };
73
+ }
74
+
75
+ return node;
76
+ } // Prettier currently relies on comparing the the start positions to know if the import/export specifier should have a
77
+ // rename (eg `Name` vs `Name as Name`) when the name is exactly the same
78
+ // So we need to ensure that the range is always the same to avoid the useless code printing
79
+
80
+
81
+ if (node.type === 'ImportSpecifier') {
82
+ if (node.local.name === node.imported.name) {
83
+ if (node.local.range == null) {
84
+ // for our TS-ast printing which has no locs
85
+ // $FlowExpectedError[cannot-write]
86
+ node.local.range = [0, 0];
87
+ } // $FlowExpectedError[cannot-write]
88
+
89
+
90
+ node.imported.range = [...node.local.range];
91
+ }
92
+
93
+ return node;
94
+ }
95
+
96
+ if (node.type === 'ExportSpecifier') {
97
+ if (node.local.name === node.exported.name) {
98
+ if (node.local.range == null) {
99
+ // for our TS-ast printing which has no locs
100
+ // $FlowExpectedError[cannot-write]
101
+ node.local.range = [0, 0];
102
+ } // $FlowExpectedError[cannot-write]
103
+
104
+
105
+ node.exported.range = [...node.local.range];
106
+ }
107
+
108
+ return node;
109
+ }
110
+
111
+ return node;
112
+ },
113
+
114
+ visitorKeys
115
+ });
116
+ }
@@ -0,0 +1,119 @@
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 {ESNode, Program} from 'hermes-estree';
14
+ import type {VisitorKeysType} from '../traverse/getVisitorKeys';
15
+ import {SimpleTransform} from '../transform/SimpleTransform';
16
+
17
+ // https://github.com/prettier/prettier/blob/d962466a828f8ef51435e3e8840178d90b7ec6cd/src/language-js/parse/postprocess/index.js#L161-L182
18
+ function transformChainExpression(node: ESNode): ESNode {
19
+ switch (node.type) {
20
+ case 'CallExpression':
21
+ // $FlowExpectedError[cannot-spread-interface]
22
+ return {
23
+ ...node,
24
+ type: 'OptionalCallExpression',
25
+ callee: transformChainExpression(node.callee),
26
+ };
27
+
28
+ case 'MemberExpression':
29
+ // $FlowExpectedError[cannot-spread-interface]
30
+ return {
31
+ ...node,
32
+ type: 'OptionalMemberExpression',
33
+ object: transformChainExpression(node.object),
34
+ };
35
+ // No default
36
+ }
37
+
38
+ return node;
39
+ }
40
+
41
+ export default function mutate(
42
+ rootNode: Program,
43
+ visitorKeys: ?VisitorKeysType,
44
+ ): void {
45
+ // Since we don't return the result of `transform` we need to be careful not to replace the Program root node.
46
+ SimpleTransform.transform(rootNode, {
47
+ transform(node): ESNode | null {
48
+ // prettier fully expects the parent pointers are NOT set and
49
+ // certain cases can crash due to prettier infinite-looping
50
+ // whilst naively traversing the parent property
51
+ // https://github.com/prettier/prettier/issues/11793
52
+ // Note: Only needed for prettier V2, this is supported in V3
53
+ if (node.parent) {
54
+ // $FlowExpectedError[cannot-write]
55
+ delete node.parent;
56
+ }
57
+
58
+ // prettier currently relies on the AST being in the old-school, deprecated AST format for optional chaining
59
+ // so we have to apply their transform to our AST so it can actually format it.
60
+ // Note: Only needed for prettier V2, this is supported in V3
61
+ if (node.type === 'ChainExpression') {
62
+ return transformChainExpression(node.expression);
63
+ }
64
+
65
+ // Prettier currently relies on comparing the `node` vs `node.value` start positions to know if an
66
+ // `ObjectTypeProperty` is a method or not (instead of using the `node.method` boolean). To correctly print
67
+ // the node when its not a method we need the start position to be different from the `node.value`s start
68
+ // position.
69
+ if (node.type === 'ObjectTypeProperty') {
70
+ if (
71
+ node.method === false &&
72
+ node.kind === 'init' &&
73
+ node.range[0] === 1 &&
74
+ node.value.range[0] === 1
75
+ ) {
76
+ // $FlowExpectedError[cannot-write]
77
+ // $FlowExpectedError[cannot-spread-interface]
78
+ node.value = {
79
+ ...node.value,
80
+ range: [2, node.value.range[1]],
81
+ };
82
+ }
83
+ return node;
84
+ }
85
+
86
+ // Prettier currently relies on comparing the the start positions to know if the import/export specifier should have a
87
+ // rename (eg `Name` vs `Name as Name`) when the name is exactly the same
88
+ // So we need to ensure that the range is always the same to avoid the useless code printing
89
+ if (node.type === 'ImportSpecifier') {
90
+ if (node.local.name === node.imported.name) {
91
+ if (node.local.range == null) {
92
+ // for our TS-ast printing which has no locs
93
+ // $FlowExpectedError[cannot-write]
94
+ node.local.range = [0, 0];
95
+ }
96
+ // $FlowExpectedError[cannot-write]
97
+ node.imported.range = [...node.local.range];
98
+ }
99
+ return node;
100
+ }
101
+
102
+ if (node.type === 'ExportSpecifier') {
103
+ if (node.local.name === node.exported.name) {
104
+ if (node.local.range == null) {
105
+ // for our TS-ast printing which has no locs
106
+ // $FlowExpectedError[cannot-write]
107
+ node.local.range = [0, 0];
108
+ }
109
+ // $FlowExpectedError[cannot-write]
110
+ node.exported.range = [...node.local.range];
111
+ }
112
+ return node;
113
+ }
114
+
115
+ return node;
116
+ },
117
+ visitorKeys,
118
+ });
119
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hermes-parser",
3
- "version": "0.11.0",
3
+ "version": "0.12.0",
4
4
  "description": "A JavaScript parser built from the Hermes engine",
5
5
  "main": "dist/index.js",
6
6
  "license": "MIT",
@@ -9,7 +9,7 @@
9
9
  "url": "git@github.com:facebook/hermes.git"
10
10
  },
11
11
  "dependencies": {
12
- "hermes-estree": "0.11.0"
12
+ "hermes-estree": "0.12.0"
13
13
  },
14
14
  "devDependencies": {
15
15
  "@babel/parser": "7.7.4",