| [9af201e] | 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | /** @typedef {import("estree").ArrowFunctionExpression} ArrowFunctionExpression */
|
|---|
| 9 | /** @typedef {import("estree").Expression} Expression */
|
|---|
| 10 | /** @typedef {import("estree").FunctionExpression} FunctionExpression */
|
|---|
| 11 | /** @typedef {import("estree").SpreadElement} SpreadElement */
|
|---|
| 12 |
|
|---|
| 13 | /** @typedef {{ fn: FunctionExpression | ArrowFunctionExpression, expressions: (Expression | SpreadElement)[], needThis: boolean | undefined }} FunctionExpressionResult */
|
|---|
| 14 |
|
|---|
| 15 | /**
|
|---|
| 16 | * Returns function expression with additional information.
|
|---|
| 17 | * @param {Expression | SpreadElement} expr expressions
|
|---|
| 18 | * @returns {FunctionExpressionResult | undefined} function expression with additional information
|
|---|
| 19 | */
|
|---|
| 20 | module.exports = (expr) => {
|
|---|
| 21 | // <FunctionExpression>
|
|---|
| 22 | if (
|
|---|
| 23 | expr.type === "FunctionExpression" ||
|
|---|
| 24 | expr.type === "ArrowFunctionExpression"
|
|---|
| 25 | ) {
|
|---|
| 26 | return {
|
|---|
| 27 | fn: expr,
|
|---|
| 28 | expressions: [],
|
|---|
| 29 | needThis: false
|
|---|
| 30 | };
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | // <FunctionExpression>.bind(<Expression>)
|
|---|
| 34 | if (
|
|---|
| 35 | expr.type === "CallExpression" &&
|
|---|
| 36 | expr.callee.type === "MemberExpression" &&
|
|---|
| 37 | expr.callee.object.type === "FunctionExpression" &&
|
|---|
| 38 | expr.callee.property.type === "Identifier" &&
|
|---|
| 39 | expr.callee.property.name === "bind" &&
|
|---|
| 40 | expr.arguments.length === 1
|
|---|
| 41 | ) {
|
|---|
| 42 | return {
|
|---|
| 43 | fn: expr.callee.object,
|
|---|
| 44 | expressions: [expr.arguments[0]],
|
|---|
| 45 | needThis: undefined
|
|---|
| 46 | };
|
|---|
| 47 | }
|
|---|
| 48 | // (function(_this) {return <FunctionExpression>})(this) (Coffeescript)
|
|---|
| 49 | if (
|
|---|
| 50 | expr.type === "CallExpression" &&
|
|---|
| 51 | expr.callee.type === "FunctionExpression" &&
|
|---|
| 52 | expr.callee.body.type === "BlockStatement" &&
|
|---|
| 53 | expr.arguments.length === 1 &&
|
|---|
| 54 | expr.arguments[0].type === "ThisExpression" &&
|
|---|
| 55 | expr.callee.body.body &&
|
|---|
| 56 | expr.callee.body.body.length === 1 &&
|
|---|
| 57 | expr.callee.body.body[0].type === "ReturnStatement" &&
|
|---|
| 58 | expr.callee.body.body[0].argument &&
|
|---|
| 59 | expr.callee.body.body[0].argument.type === "FunctionExpression"
|
|---|
| 60 | ) {
|
|---|
| 61 | return {
|
|---|
| 62 | fn: expr.callee.body.body[0].argument,
|
|---|
| 63 | expressions: [],
|
|---|
| 64 | needThis: true
|
|---|
| 65 | };
|
|---|
| 66 | }
|
|---|
| 67 | };
|
|---|