hermes-estree 0.10.1 → 0.11.1

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.
Files changed (39) hide show
  1. package/dist/generated/HermesESTreeSelectorTypes.js.flow +445 -0
  2. package/dist/generated/predicates.js +70 -0
  3. package/dist/generated/predicates.js.flow +70 -0
  4. package/dist/types.js.flow +132 -5
  5. package/package.json +1 -1
  6. package/dist/HermesAST.js.flow +0 -57
  7. package/dist/HermesASTAdapter.js +0 -186
  8. package/dist/HermesASTAdapter.js.flow +0 -183
  9. package/dist/HermesParser.js +0 -83
  10. package/dist/HermesParser.js.flow +0 -123
  11. package/dist/HermesParserDecodeUTF8String.js +0 -68
  12. package/dist/HermesParserDecodeUTF8String.js.flow +0 -65
  13. package/dist/HermesParserDeserializer.js +0 -243
  14. package/dist/HermesParserDeserializer.js.flow +0 -261
  15. package/dist/HermesParserNodeDeserializers.js +0 -2008
  16. package/dist/HermesParserNodeDeserializers.js.flow +0 -16
  17. package/dist/HermesParserWASM.js +0 -6
  18. package/dist/HermesParserWASM.js.flow +0 -75
  19. package/dist/HermesToBabelAdapter.js +0 -514
  20. package/dist/HermesToBabelAdapter.js.flow +0 -490
  21. package/dist/HermesToESTreeAdapter.js +0 -439
  22. package/dist/HermesToESTreeAdapter.js.flow +0 -422
  23. package/dist/ParserOptions.js.flow +0 -18
  24. package/dist/generated/ESTreeVisitorKeys.js +0 -180
  25. package/dist/generated/ESTreeVisitorKeys.js.flow +0 -15
  26. package/dist/generated/ParserVisitorKeys.js +0 -622
  27. package/dist/generated/ParserVisitorKeys.js.flow +0 -17
  28. package/dist/getModuleDocblock.js +0 -112
  29. package/dist/getModuleDocblock.js.flow +0 -118
  30. package/dist/transform/SimpleTransform.js +0 -92
  31. package/dist/transform/SimpleTransform.js.flow +0 -104
  32. package/dist/transform/astArrayMutationHelpers.js +0 -62
  33. package/dist/transform/astArrayMutationHelpers.js.flow +0 -71
  34. package/dist/transform/astNodeMutationHelpers.js +0 -186
  35. package/dist/transform/astNodeMutationHelpers.js.flow +0 -205
  36. package/dist/traverse/SimpleTraverser.js +0 -138
  37. package/dist/traverse/SimpleTraverser.js.flow +0 -132
  38. package/dist/traverse/getVisitorKeys.js +0 -35
  39. package/dist/traverse/getVisitorKeys.js.flow +0 -36
@@ -1,112 +0,0 @@
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.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
- }
@@ -1,118 +0,0 @@
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
- }
@@ -1,92 +0,0 @@
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.SimpleTransform = void 0;
16
-
17
- var _SimpleTraverser = require("../traverse/SimpleTraverser");
18
-
19
- var _astNodeMutationHelpers = require("./astNodeMutationHelpers");
20
-
21
- /**
22
- * A simple class to recursively tranform AST trees.
23
- */
24
- class SimpleTransform {
25
- /**
26
- * Transform the given AST tree.
27
- * @param rootNode The root node to traverse.
28
- * @param options The option object.
29
- * @return The modified rootNode or a new node if the rootNode was transformed directly.
30
- */
31
- transform(rootNode, options) {
32
- let resultRootNode = rootNode;
33
-
34
- _SimpleTraverser.SimpleTraverser.traverse(rootNode, {
35
- enter: (node, parent) => {
36
- const resultNode = options.transform(node);
37
-
38
- if (resultNode !== node) {
39
- const traversedResultNode = resultNode != null ? this.transform(resultNode, options) : null;
40
-
41
- if (parent == null) {
42
- if (node !== rootNode) {
43
- throw new Error('SimpleTransform infra error: Parent not set on non root node, this should not be possible');
44
- }
45
-
46
- resultRootNode = traversedResultNode;
47
- } else if (traversedResultNode == null) {
48
- (0, _astNodeMutationHelpers.removeNodeOnParent)(node, parent);
49
- } else {
50
- (0, _astNodeMutationHelpers.replaceNodeOnParent)(node, parent, traversedResultNode);
51
- }
52
-
53
- throw _SimpleTraverser.SimpleTraverser.Skip;
54
- }
55
- },
56
-
57
- leave(_node) {}
58
-
59
- });
60
-
61
- return resultRootNode;
62
- }
63
- /**
64
- * Transform the given AST tree.
65
- * @param node The root node to traverse.
66
- * @param options The option object.
67
- */
68
-
69
-
70
- static transform(node, options) {
71
- return new SimpleTransform().transform(node, options);
72
- }
73
- /**
74
- * Return a new AST node with the given properties overrided if needed.
75
- *
76
- * This function takes care to only create new nodes when needed. Referential equality of nodes
77
- * is important as its used to know if a node should be re-traversed.
78
- *
79
- * @param node The base AST node.
80
- * @param overrideProps New properties to apply to the node.
81
- * @return Either the orginal node if the properties matched the existing node or a new node with
82
- * the new properties.
83
- */
84
-
85
-
86
- static nodeWith(node, overrideProps) {
87
- return (0, _astNodeMutationHelpers.nodeWith)(node, overrideProps);
88
- }
89
-
90
- }
91
-
92
- exports.SimpleTransform = SimpleTransform;
@@ -1,104 +0,0 @@
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 {VisitorKeysType} from '../traverse/getVisitorKeys';
14
- import type {ESNode} from 'hermes-estree';
15
-
16
- import {SimpleTraverser} from '../traverse/SimpleTraverser';
17
- import {
18
- nodeWith,
19
- removeNodeOnParent,
20
- replaceNodeOnParent,
21
- } from './astNodeMutationHelpers';
22
-
23
- /**
24
- * Transform callback
25
- * @param node The node we are visiting
26
- * @returns
27
- * - return input node, signals no changes were made will continue to the next node.
28
- * - return new node, the old node will be replaced in the AST. The new node and its
29
- * children are then traversed.
30
- * - return null, signals the node should be deleted from the AST.
31
- */
32
- export type TransformCallback = (node: ESNode) => ESNode | null;
33
-
34
- export type TransformOptions = $ReadOnly<{
35
- /** The callback function which is called on entering each node. */
36
- transform: TransformCallback,
37
-
38
- /** The set of visitor keys to use for traversal. Defaults to the Flow ESTree visitor keys */
39
- visitorKeys?: ?VisitorKeysType,
40
- }>;
41
-
42
- /**
43
- * A simple class to recursively tranform AST trees.
44
- */
45
- export class SimpleTransform {
46
- /**
47
- * Transform the given AST tree.
48
- * @param rootNode The root node to traverse.
49
- * @param options The option object.
50
- * @return The modified rootNode or a new node if the rootNode was transformed directly.
51
- */
52
- transform(rootNode: ESNode, options: TransformOptions): ESNode | null {
53
- let resultRootNode: ESNode | null = rootNode;
54
- SimpleTraverser.traverse(rootNode, {
55
- enter: (node: ESNode, parent: ?ESNode) => {
56
- const resultNode = options.transform(node);
57
- if (resultNode !== node) {
58
- const traversedResultNode =
59
- resultNode != null ? this.transform(resultNode, options) : null;
60
- if (parent == null) {
61
- if (node !== rootNode) {
62
- throw new Error(
63
- 'SimpleTransform infra error: Parent not set on non root node, this should not be possible',
64
- );
65
- }
66
- resultRootNode = traversedResultNode;
67
- } else if (traversedResultNode == null) {
68
- removeNodeOnParent(node, parent);
69
- } else {
70
- replaceNodeOnParent(node, parent, traversedResultNode);
71
- }
72
-
73
- throw SimpleTraverser.Skip;
74
- }
75
- },
76
- leave(_node: ESNode) {},
77
- });
78
- return resultRootNode;
79
- }
80
-
81
- /**
82
- * Transform the given AST tree.
83
- * @param node The root node to traverse.
84
- * @param options The option object.
85
- */
86
- static transform(node: ESNode, options: TransformOptions): ESNode | null {
87
- return new SimpleTransform().transform(node, options);
88
- }
89
-
90
- /**
91
- * Return a new AST node with the given properties overrided if needed.
92
- *
93
- * This function takes care to only create new nodes when needed. Referential equality of nodes
94
- * is important as its used to know if a node should be re-traversed.
95
- *
96
- * @param node The base AST node.
97
- * @param overrideProps New properties to apply to the node.
98
- * @return Either the orginal node if the properties matched the existing node or a new node with
99
- * the new properties.
100
- */
101
- static nodeWith<T: ESNode>(node: T, overrideProps: Partial<T>): T {
102
- return nodeWith<T>(node, overrideProps);
103
- }
104
- }
@@ -1,62 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.arrayIsEqual = arrayIsEqual;
7
- exports.insertInArray = insertInArray;
8
- exports.removeFromArray = removeFromArray;
9
- exports.replaceInArray = replaceInArray;
10
-
11
- /**
12
- * Copyright (c) Meta Platforms, Inc. and affiliates.
13
- *
14
- * This source code is licensed under the MIT license found in the
15
- * LICENSE file in the root directory of this source tree.
16
- *
17
- *
18
- * @format
19
- */
20
- function assertArrayBounds(array, index) {
21
- if (index < 0 || index >= array.length) {
22
- throw new Error(`Invalid Mutation: Tried to mutate an elements array with an out of bounds index. Index: ${index}, Array Size: ${array.length}`);
23
- }
24
- }
25
-
26
- function arrayIsEqual(a1, a2) {
27
- if (a1 === a2) {
28
- return true;
29
- }
30
-
31
- if (a1.length !== a2.length) {
32
- return false;
33
- }
34
-
35
- for (let i = 0; i < a1.length; i++) {
36
- if (a1[i] !== a2[i]) {
37
- return false;
38
- }
39
- }
40
-
41
- return true;
42
- }
43
-
44
- function insertInArray(array, index, elements) {
45
- if (index === array.length) {
46
- // Support the insert at end of array case.
47
- return array.concat(elements);
48
- }
49
-
50
- assertArrayBounds(array, index);
51
- return array.slice(0, index).concat(elements).concat(array.slice(index));
52
- }
53
-
54
- function removeFromArray(array, index) {
55
- assertArrayBounds(array, index);
56
- return [...array.slice(0, index), ...array.slice(index + 1)];
57
- }
58
-
59
- function replaceInArray(array, index, elements) {
60
- assertArrayBounds(array, index);
61
- return array.slice(0, index).concat(elements).concat(array.slice(index + 1));
62
- }
@@ -1,71 +0,0 @@
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
- function assertArrayBounds<T>(array: $ReadOnlyArray<T>, index: number): void {
12
- if (index < 0 || index >= array.length) {
13
- throw new Error(
14
- `Invalid Mutation: Tried to mutate an elements array with an out of bounds index. Index: ${index}, Array Size: ${array.length}`,
15
- );
16
- }
17
- }
18
-
19
- export function arrayIsEqual(
20
- a1: $ReadOnlyArray<mixed>,
21
- a2: $ReadOnlyArray<mixed>,
22
- ): boolean {
23
- if (a1 === a2) {
24
- return true;
25
- }
26
-
27
- if (a1.length !== a2.length) {
28
- return false;
29
- }
30
-
31
- for (let i = 0; i < a1.length; i++) {
32
- if (a1[i] !== a2[i]) {
33
- return false;
34
- }
35
- }
36
-
37
- return true;
38
- }
39
-
40
- export function insertInArray<T>(
41
- array: $ReadOnlyArray<T>,
42
- index: number,
43
- elements: $ReadOnlyArray<T>,
44
- ): Array<T> {
45
- if (index === array.length) {
46
- // Support the insert at end of array case.
47
- return array.concat(elements);
48
- }
49
- assertArrayBounds(array, index);
50
- return array.slice(0, index).concat(elements).concat(array.slice(index));
51
- }
52
-
53
- export function removeFromArray<T>(
54
- array: $ReadOnlyArray<T>,
55
- index: number,
56
- ): Array<T> {
57
- assertArrayBounds(array, index);
58
- return [...array.slice(0, index), ...array.slice(index + 1)];
59
- }
60
-
61
- export function replaceInArray<T>(
62
- array: $ReadOnlyArray<T>,
63
- index: number,
64
- elements: $ReadOnlyArray<T>,
65
- ): Array<T> {
66
- assertArrayBounds(array, index);
67
- return array
68
- .slice(0, index)
69
- .concat(elements)
70
- .concat(array.slice(index + 1));
71
- }