You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.3 KiB

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