[79a0317] | 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 | /**
|
---|
| 14 | * @param {Expression | SpreadElement} expr expressions
|
---|
| 15 | * @returns {{fn: FunctionExpression | ArrowFunctionExpression, expressions: (Expression | SpreadElement)[], needThis: boolean | undefined } | undefined} function expression with additional information
|
---|
| 16 | */
|
---|
| 17 | module.exports = expr => {
|
---|
| 18 | // <FunctionExpression>
|
---|
| 19 | if (
|
---|
| 20 | expr.type === "FunctionExpression" ||
|
---|
| 21 | expr.type === "ArrowFunctionExpression"
|
---|
| 22 | ) {
|
---|
| 23 | return {
|
---|
| 24 | fn: expr,
|
---|
| 25 | expressions: [],
|
---|
| 26 | needThis: false
|
---|
| 27 | };
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | // <FunctionExpression>.bind(<Expression>)
|
---|
| 31 | if (
|
---|
| 32 | expr.type === "CallExpression" &&
|
---|
| 33 | expr.callee.type === "MemberExpression" &&
|
---|
| 34 | expr.callee.object.type === "FunctionExpression" &&
|
---|
| 35 | expr.callee.property.type === "Identifier" &&
|
---|
| 36 | expr.callee.property.name === "bind" &&
|
---|
| 37 | expr.arguments.length === 1
|
---|
| 38 | ) {
|
---|
| 39 | return {
|
---|
| 40 | fn: expr.callee.object,
|
---|
| 41 | expressions: [expr.arguments[0]],
|
---|
| 42 | needThis: undefined
|
---|
| 43 | };
|
---|
| 44 | }
|
---|
| 45 | // (function(_this) {return <FunctionExpression>})(this) (Coffeescript)
|
---|
| 46 | if (
|
---|
| 47 | expr.type === "CallExpression" &&
|
---|
| 48 | expr.callee.type === "FunctionExpression" &&
|
---|
| 49 | expr.callee.body.type === "BlockStatement" &&
|
---|
| 50 | expr.arguments.length === 1 &&
|
---|
| 51 | expr.arguments[0].type === "ThisExpression" &&
|
---|
| 52 | expr.callee.body.body &&
|
---|
| 53 | expr.callee.body.body.length === 1 &&
|
---|
| 54 | expr.callee.body.body[0].type === "ReturnStatement" &&
|
---|
| 55 | expr.callee.body.body[0].argument &&
|
---|
| 56 | expr.callee.body.body[0].argument.type === "FunctionExpression"
|
---|
| 57 | ) {
|
---|
| 58 | return {
|
---|
| 59 | fn: expr.callee.body.body[0].argument,
|
---|
| 60 | expressions: [],
|
---|
| 61 | needThis: true
|
---|
| 62 | };
|
---|
| 63 | }
|
---|
| 64 | };
|
---|