2014-08-21 20:32:24 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2018-07-30 23:08:51 +08:00
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2024-03-18 23:28:40 +08:00
|
|
|
/** @typedef {import("estree").ArrowFunctionExpression} ArrowFunctionExpression */
|
2023-05-22 04:31:30 +08:00
|
|
|
/** @typedef {import("estree").Expression} Expression */
|
2024-03-18 23:28:40 +08:00
|
|
|
/** @typedef {import("estree").FunctionExpression} FunctionExpression */
|
2023-05-22 04:31:30 +08:00
|
|
|
/** @typedef {import("estree").SpreadElement} SpreadElement */
|
|
|
|
|
|
|
|
/**
|
2024-01-27 01:38:18 +08:00
|
|
|
* @param {Expression | SpreadElement} expr expressions
|
2024-03-18 23:28:40 +08:00
|
|
|
* @returns {{fn: FunctionExpression | ArrowFunctionExpression, expressions: (Expression | SpreadElement)[], needThis: boolean | undefined } | undefined} function expression with additional information
|
2023-05-22 04:31:30 +08:00
|
|
|
*/
|
2025-07-17 00:13:14 +08:00
|
|
|
module.exports = (expr) => {
|
2014-08-21 20:32:24 +08:00
|
|
|
// <FunctionExpression>
|
2018-02-25 09:00:20 +08:00
|
|
|
if (
|
|
|
|
expr.type === "FunctionExpression" ||
|
|
|
|
expr.type === "ArrowFunctionExpression"
|
|
|
|
) {
|
2014-08-21 20:32:24 +08:00
|
|
|
return {
|
|
|
|
fn: expr,
|
2016-02-23 01:47:16 +08:00
|
|
|
expressions: [],
|
|
|
|
needThis: false
|
2014-08-21 20:32:24 +08:00
|
|
|
};
|
|
|
|
}
|
2016-12-22 04:09:59 +08:00
|
|
|
|
2014-08-21 20:32:24 +08:00
|
|
|
// <FunctionExpression>.bind(<Expression>)
|
2018-02-25 09:00:20 +08:00
|
|
|
if (
|
|
|
|
expr.type === "CallExpression" &&
|
2014-08-21 20:32:24 +08:00
|
|
|
expr.callee.type === "MemberExpression" &&
|
|
|
|
expr.callee.object.type === "FunctionExpression" &&
|
|
|
|
expr.callee.property.type === "Identifier" &&
|
|
|
|
expr.callee.property.name === "bind" &&
|
2018-02-25 09:00:20 +08:00
|
|
|
expr.arguments.length === 1
|
|
|
|
) {
|
2014-08-21 20:32:24 +08:00
|
|
|
return {
|
|
|
|
fn: expr.callee.object,
|
2018-04-11 03:18:12 +08:00
|
|
|
expressions: [expr.arguments[0]],
|
|
|
|
needThis: undefined
|
2014-08-21 20:32:24 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
// (function(_this) {return <FunctionExpression>})(this) (Coffeescript)
|
2018-02-25 09:00:20 +08:00
|
|
|
if (
|
|
|
|
expr.type === "CallExpression" &&
|
2014-08-21 20:32:24 +08:00
|
|
|
expr.callee.type === "FunctionExpression" &&
|
|
|
|
expr.callee.body.type === "BlockStatement" &&
|
|
|
|
expr.arguments.length === 1 &&
|
|
|
|
expr.arguments[0].type === "ThisExpression" &&
|
|
|
|
expr.callee.body.body &&
|
|
|
|
expr.callee.body.body.length === 1 &&
|
|
|
|
expr.callee.body.body[0].type === "ReturnStatement" &&
|
|
|
|
expr.callee.body.body[0].argument &&
|
2018-02-25 09:00:20 +08:00
|
|
|
expr.callee.body.body[0].argument.type === "FunctionExpression"
|
|
|
|
) {
|
2014-08-21 20:32:24 +08:00
|
|
|
return {
|
|
|
|
fn: expr.callee.body.body[0].argument,
|
2016-02-23 01:47:16 +08:00
|
|
|
expressions: [],
|
|
|
|
needThis: true
|
2014-08-21 20:32:24 +08:00
|
|
|
};
|
|
|
|
}
|
2015-04-24 05:55:50 +08:00
|
|
|
};
|