1 | ;
|
2 | /**
|
3 | * @license
|
4 | * Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
|
5 | * This code may only be used under the BSD style license found at
|
6 | * http://polymer.github.io/LICENSE.txt
|
7 | * The complete set of authors may be found at
|
8 | * http://polymer.github.io/AUTHORS.txt
|
9 | * The complete set of contributors may be found at
|
10 | * http://polymer.github.io/CONTRIBUTORS.txt
|
11 | * Code distributed by Google as part of the polymer project is also
|
12 | * subject to an additional IP rights grant found at
|
13 | * http://polymer.github.io/PATENTS.txt
|
14 | */
|
15 | Object.defineProperty(exports, "__esModule", { value: true });
|
16 | /**
|
17 | * TODO(usergenic): Immediately deprecate this. This `estraverse-shim` module
|
18 | * exists solely to make the transition from `espree/estree` JavaScript parser
|
19 | * to `babylon/babel` and tooling a gradual/piecemeal affair. It is intended as
|
20 | * a temporary shim that will eventually be removed once all dependent
|
21 | * scanners/visitors and utility functions which use the old `estraverse` style
|
22 | * methods are converted to `babel-traverse` etc.
|
23 | */
|
24 | const traverse_1 = require("@babel/traverse");
|
25 | const babel = require("@babel/types");
|
26 | /**
|
27 | * These enum options mimic the estraverse enums that are returned by their
|
28 | * `enterX`/`leaveX` visitor methods to advise flow of the visitor.
|
29 | */
|
30 | var VisitorOption;
|
31 | (function (VisitorOption) {
|
32 | VisitorOption["Skip"] = "skip";
|
33 | VisitorOption["Break"] = "break";
|
34 | VisitorOption["Remove"] = "remove";
|
35 | })(VisitorOption = exports.VisitorOption || (exports.VisitorOption = {}));
|
36 | /**
|
37 | * This method mirrors the API of `estraverse`'s `traverse` function. It uses
|
38 | * `babel-traverse` to perform a traversal of an AST, but does so with `noScope:
|
39 | * true` which turns off the scoping logic and enables it to traverse from any
|
40 | * node; whereasc `babel-traverse`'s scopes require traversal from the root
|
41 | * node, the `File` type, which we don't yet even store on our JavaScript
|
42 | * documents.
|
43 | */
|
44 | function traverse(ast, visitor) {
|
45 | traverse_1.default(ast, {
|
46 | enter(path) {
|
47 | dispatchVisitMethods(['enter', `enter${path.type}`], path, visitor);
|
48 | },
|
49 | exit(path) {
|
50 | dispatchVisitMethods(['leave', `leave${path.type}`], path, visitor);
|
51 | },
|
52 | noScope: !babel.isFile(ast),
|
53 | });
|
54 | }
|
55 | exports.traverse = traverse;
|
56 | /**
|
57 | * Calls into visitor methods for visited node types using `estraverse` API of
|
58 | * providing the node and the parent node, and translates the `VisitorOption`
|
59 | * return value into directives on the `path` object used by `babel-traverse` to
|
60 | * advise visitor control flow, i.e. `stop`, `skip`, and `remove`.
|
61 | */
|
62 | function dispatchVisitMethods(methodNames, path, visitor) {
|
63 | for (const methodName of methodNames) {
|
64 | if (typeof visitor[methodName] === 'function') {
|
65 | // TODO(rictic): can maybe remove this cast in TS 2.8
|
66 | const result = visitor[methodName](path.node, path.parent, path);
|
67 | switch (result) {
|
68 | case VisitorOption.Break:
|
69 | return path.stop();
|
70 | case VisitorOption.Skip:
|
71 | return path.skip();
|
72 | case VisitorOption.Remove:
|
73 | return path.remove();
|
74 | }
|
75 | }
|
76 | }
|
77 | }
|
78 | //# sourceMappingURL=estraverse-shim.js.map |
\ | No newline at end of file |