es6-arrow-function-visitors.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * Copyright 2013 Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*global exports:true*/
  17. /**
  18. * Desugars ES6 Arrow functions to ES3 function expressions.
  19. * If the function contains `this` expression -- automatically
  20. * binds the funciton to current value of `this`.
  21. *
  22. * Single parameter, simple expression:
  23. *
  24. * [1, 2, 3].map(x => x * x);
  25. *
  26. * [1, 2, 3].map(function(x) { return x * x; });
  27. *
  28. * Several parameters, complex block:
  29. *
  30. * this.users.forEach((user, idx) => {
  31. * return this.isActive(idx) && this.send(user);
  32. * });
  33. *
  34. * this.users.forEach(function(user, idx) {
  35. * return this.isActive(idx) && this.send(user);
  36. * }.bind(this));
  37. *
  38. */
  39. var restParamVisitors = require('./es6-rest-param-visitors');
  40. var Syntax = require('esprima-fb').Syntax;
  41. var utils = require('../src/utils');
  42. /**
  43. * @public
  44. */
  45. function visitArrowFunction(traverse, node, path, state) {
  46. // Prologue.
  47. utils.append('function', state);
  48. renderParams(node, state);
  49. // Skip arrow.
  50. utils.catchupWhiteSpace(node.body.range[0], state);
  51. var renderBody = node.body.type == Syntax.BlockStatement
  52. ? renderStatementBody
  53. : renderExpressionBody;
  54. path.unshift(node);
  55. renderBody(traverse, node, path, state);
  56. path.shift();
  57. // Bind the function only if `this` value is used
  58. // inside it or inside any sub-expression.
  59. if (utils.containsChildOfType(node.body, Syntax.ThisExpression)) {
  60. utils.append('.bind(this)', state);
  61. }
  62. return false;
  63. }
  64. function renderParams(node, state) {
  65. // To preserve inline typechecking directives, we
  66. // distinguish between parens-free and paranthesized single param.
  67. if (isParensFreeSingleParam(node, state) || !node.params.length) {
  68. utils.append('(', state);
  69. }
  70. if (node.params.length !== 0) {
  71. utils.catchup(node.params[node.params.length - 1].range[1], state);
  72. }
  73. utils.append(')', state);
  74. }
  75. function isParensFreeSingleParam(node, state) {
  76. return node.params.length === 1 &&
  77. state.g.source[state.g.position] !== '(';
  78. }
  79. function renderExpressionBody(traverse, node, path, state) {
  80. // Wrap simple expression bodies into a block
  81. // with explicit return statement.
  82. utils.append('{', state);
  83. if (node.rest) {
  84. utils.append(
  85. restParamVisitors.renderRestParamSetup(node),
  86. state
  87. );
  88. }
  89. utils.append('return ', state);
  90. renderStatementBody(traverse, node, path, state);
  91. utils.append(';}', state);
  92. }
  93. function renderStatementBody(traverse, node, path, state) {
  94. traverse(node.body, path, state);
  95. utils.catchup(node.body.range[1], state);
  96. }
  97. visitArrowFunction.test = function(node, path, state) {
  98. return node.type === Syntax.ArrowFunctionExpression;
  99. };
  100. exports.visitorList = [
  101. visitArrowFunction
  102. ];