index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * Copyright 2013-present, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. */
  9. /*global exports:true*/
  10. 'use strict';
  11. var es6ArrowFunction = require('./es6-arrow-function-visitors');
  12. var es6CallSpread = require('./es6-call-spread-visitors');
  13. var es6Class = require('./es6-class-visitors');
  14. var es6Destructuring = require('./es6-destructuring-visitors');
  15. var es6ForOf = require('./es6-for-of-visitors');
  16. var es6ObjectComputedProperty =
  17. require('./es6-object-computed-property-visitors');
  18. var es6ObjectConciseMethod = require('./es6-object-concise-method-visitors');
  19. var es6ObjectShortNotation = require('./es6-object-short-notation-visitors');
  20. var es6RestParam = require('./es6-rest-param-visitors');
  21. var es6Template = require('./es6-template-visitors');
  22. var es7SpreadProperty = require('./es7-spread-property-visitors');
  23. var es7TrailingComma = require('./es7-trailing-comma-visitors');
  24. var reactDisplayName = require('./react-display-name-visitors');
  25. var reactJSX = require('./react-jsx-visitors');
  26. var reservedWords = require('./reserved-words-visitors');
  27. var trailingComma = require('./trailing-comma-visitors');
  28. var undefinedToVoid0 = require('./undefined-to-void-0-visitors');
  29. // Map from transformName => orderedListOfVisitors.
  30. var transformVisitors = {
  31. 'es6-arrow-function': es6ArrowFunction.visitorList,
  32. 'es6-call-spread': es6CallSpread.visitorList,
  33. 'es6-class': es6Class.visitorList,
  34. 'es6-destructuring': es6Destructuring.visitorList,
  35. 'es6-for-of': es6ForOf.visitorList,
  36. 'es6-object-computed-property': es6ObjectComputedProperty.visitorList,
  37. 'es6-object-concise-method': es6ObjectConciseMethod.visitorList,
  38. 'es6-object-short-notation': es6ObjectShortNotation.visitorList,
  39. 'es6-rest-param': es6RestParam.visitorList,
  40. 'es6-template': es6Template.visitorList,
  41. 'es7-spread-property': es7SpreadProperty.visitorList,
  42. 'es7-trailing-comma': es7TrailingComma.visitorList,
  43. 'react-display-name': reactDisplayName.visitorList,
  44. 'react-jsx': reactJSX.visitorList,
  45. 'reserved-words': reservedWords.visitorList,
  46. 'trailing-comma': trailingComma.visitorList,
  47. 'undefined-to-void-0': undefinedToVoid0.visitorList
  48. };
  49. // Sets of transforms. Useful for quickly building up with simple options.
  50. var transformSets = {
  51. 'es6': [
  52. 'es6-arrow-function',
  53. 'es6-call-spread',
  54. 'es6-class',
  55. 'es6-destructuring',
  56. 'es6-for-of',
  57. 'es6-object-computed-property',
  58. 'es6-object-concise-method',
  59. 'es6-object-short-notation',
  60. 'es6-rest-param',
  61. 'es6-template'
  62. ],
  63. 'es7': [
  64. 'es7-spread-property',
  65. 'es7-trailing-comma'
  66. ],
  67. 'react': [
  68. 'react-jsx',
  69. 'react-display-name'
  70. ],
  71. 'target:es3': [
  72. 'reserved-words',
  73. 'trailing-comma'
  74. ],
  75. 'utility': [
  76. 'undefined-to-void-0'
  77. ]
  78. };
  79. // harmony is all newer transforms. Define it here so we don't duplicate.
  80. transformSets.harmony = transformSets.es6.concat(transformSets.es7);
  81. // Specifies the order in which each transform should run.
  82. var transformRunOrder = [
  83. 'reserved-words',
  84. 'es6-destructuring',
  85. 'es6-arrow-function',
  86. // needs to be before concice-methods, short-notation, spread-property
  87. 'es6-object-computed-property',
  88. 'es6-object-concise-method',
  89. 'es6-object-short-notation',
  90. 'es6-class',
  91. 'es6-rest-param',
  92. 'es6-template',
  93. 'es6-call-spread',
  94. 'es6-for-of',
  95. 'es7-spread-property',
  96. // These are 2 distinct transforms - 'trailing-comma' handles array & object
  97. // literals. 'es7-trailing-comma' handles extra arguments in function calls.
  98. 'trailing-comma',
  99. 'es7-trailing-comma',
  100. 'react-jsx',
  101. 'react-display-name',
  102. 'undefined-to-void-0'
  103. ];
  104. /**
  105. * Given a list of transform names, return the ordered list of visitors to be
  106. * passed to the transform() function.
  107. *
  108. * @param {array?} excludes
  109. * @return {array}
  110. */
  111. function getAllVisitors(excludes) {
  112. var ret = [];
  113. for (var i = 0, il = transformRunOrder.length; i < il; i++) {
  114. if (!excludes || excludes.indexOf(transformRunOrder[i]) === -1) {
  115. ret = ret.concat(transformVisitors[transformRunOrder[i]]);
  116. }
  117. }
  118. return ret;
  119. }
  120. /**
  121. * Given a list of visitor set names, return the ordered list of visitors to be
  122. * passed to jstransform.
  123. *
  124. * @param {array}
  125. * @return {array}
  126. */
  127. function getVisitorsBySet(sets) {
  128. var visitorsToInclude = sets.reduce(function(visitors, set) {
  129. if (!transformSets.hasOwnProperty(set)) {
  130. throw new Error('Unknown visitor set: ' + set);
  131. }
  132. transformSets[set].forEach(function(visitor) {
  133. visitors[visitor] = true;
  134. });
  135. return visitors;
  136. }, {});
  137. var visitorList = [];
  138. for (var i = 0; i < transformRunOrder.length; i++) {
  139. if (visitorsToInclude.hasOwnProperty(transformRunOrder[i])) {
  140. visitorList = visitorList.concat(transformVisitors[transformRunOrder[i]]);
  141. }
  142. }
  143. return visitorList;
  144. }
  145. exports.getVisitorsBySet = getVisitorsBySet;
  146. exports.getAllVisitors = getAllVisitors;
  147. exports.transformVisitors = transformVisitors;