source: trip-planner-front/node_modules/regenerator-transform/src/replaceShorthandObjectMethod.js@ 6c1585f

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

initial commit

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