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