source: trip-planner-front/node_modules/regenerator-transform/lib/replaceShorthandObjectMethod.js@ 188ee53

Last change on this file since 188ee53 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.6 KB
Line 
1"use strict";
2
3var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
4
5exports.__esModule = true;
6exports["default"] = replaceShorthandObjectMethod;
7
8var util = _interopRequireWildcard(require("./util"));
9
10/**
11 * Copyright (c) 2014-present, Facebook, Inc.
12 *
13 * This source code is licensed under the MIT license found in the
14 * LICENSE file in the root directory of this source tree.
15 */
16// this function converts a shorthand object generator method into a normal
17// (non-shorthand) object property which is a generator function expression. for
18// example, this:
19//
20// var foo = {
21// *bar(baz) { return 5; }
22// }
23//
24// should be replaced with:
25//
26// var foo = {
27// bar: function*(baz) { return 5; }
28// }
29//
30// to do this, it clones the parameter array and the body of the object generator
31// method into a new FunctionExpression.
32//
33// this method can be passed any Function AST node path, and it will return
34// either:
35// a) the path that was passed in (iff the path did not need to be replaced) or
36// b) the path of the new FunctionExpression that was created as a replacement
37// (iff the path did need to be replaced)
38//
39// In either case, though, the caller can count on the fact that the return value
40// is a Function AST node path.
41//
42// If this function is called with an AST node path that is not a Function (or with an
43// argument that isn't an AST node path), it will throw an error.
44function replaceShorthandObjectMethod(path) {
45 var t = util.getTypes();
46
47 if (!path.node || !t.isFunction(path.node)) {
48 throw new Error("replaceShorthandObjectMethod can only be called on Function AST node paths.");
49 } // this function only replaces shorthand object methods (called ObjectMethod
50 // in Babel-speak).
51
52
53 if (!t.isObjectMethod(path.node)) {
54 return path;
55 } // this function only replaces generators.
56
57
58 if (!path.node.generator) {
59 return path;
60 }
61
62 var parameters = path.node.params.map(function (param) {
63 return t.cloneDeep(param);
64 });
65 var functionExpression = t.functionExpression(null, // id
66 parameters, // params
67 t.cloneDeep(path.node.body), // body
68 path.node.generator, path.node.async);
69 util.replaceWithOrRemove(path, t.objectProperty(t.cloneDeep(path.node.key), // key
70 functionExpression, //value
71 path.node.computed, // computed
72 false // shorthand
73 )); // path now refers to the ObjectProperty AST node path, but we want to return a
74 // Function AST node path for the function expression we created. we know that
75 // the FunctionExpression we just created is the value of the ObjectProperty,
76 // so return the "value" path off of this path.
77
78 return path.get("value");
79}
Note: See TracBrowser for help on using the repository browser.