UNPKG

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