index.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.CodeGenerator = undefined;
  4. var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck");
  5. var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
  6. var _possibleConstructorReturn2 = require("babel-runtime/helpers/possibleConstructorReturn");
  7. var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
  8. var _inherits2 = require("babel-runtime/helpers/inherits");
  9. var _inherits3 = _interopRequireDefault(_inherits2);
  10. exports.default = function (ast, opts, code) {
  11. var gen = new Generator(ast, opts, code);
  12. return gen.generate();
  13. };
  14. var _detectIndent = require("detect-indent");
  15. var _detectIndent2 = _interopRequireDefault(_detectIndent);
  16. var _sourceMap = require("./source-map");
  17. var _sourceMap2 = _interopRequireDefault(_sourceMap);
  18. var _babelMessages = require("babel-messages");
  19. var messages = _interopRequireWildcard(_babelMessages);
  20. var _printer = require("./printer");
  21. var _printer2 = _interopRequireDefault(_printer);
  22. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  23. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  24. var Generator = function (_Printer) {
  25. (0, _inherits3.default)(Generator, _Printer);
  26. function Generator(ast) {
  27. var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  28. var code = arguments[2];
  29. (0, _classCallCheck3.default)(this, Generator);
  30. var tokens = ast.tokens || [];
  31. var format = normalizeOptions(code, opts, tokens);
  32. var map = opts.sourceMaps ? new _sourceMap2.default(opts, code) : null;
  33. var _this = (0, _possibleConstructorReturn3.default)(this, _Printer.call(this, format, map, tokens));
  34. _this.ast = ast;
  35. return _this;
  36. }
  37. Generator.prototype.generate = function generate() {
  38. return _Printer.prototype.generate.call(this, this.ast);
  39. };
  40. return Generator;
  41. }(_printer2.default);
  42. function normalizeOptions(code, opts, tokens) {
  43. var style = " ";
  44. if (code && typeof code === "string") {
  45. var indent = (0, _detectIndent2.default)(code).indent;
  46. if (indent && indent !== " ") style = indent;
  47. }
  48. var format = {
  49. auxiliaryCommentBefore: opts.auxiliaryCommentBefore,
  50. auxiliaryCommentAfter: opts.auxiliaryCommentAfter,
  51. shouldPrintComment: opts.shouldPrintComment,
  52. retainLines: opts.retainLines,
  53. retainFunctionParens: opts.retainFunctionParens,
  54. comments: opts.comments == null || opts.comments,
  55. compact: opts.compact,
  56. minified: opts.minified,
  57. concise: opts.concise,
  58. quotes: opts.quotes || findCommonStringDelimiter(code, tokens),
  59. jsonCompatibleStrings: opts.jsonCompatibleStrings,
  60. indent: {
  61. adjustMultilineComment: true,
  62. style: style,
  63. base: 0
  64. },
  65. flowCommaSeparator: opts.flowCommaSeparator
  66. };
  67. if (format.minified) {
  68. format.compact = true;
  69. format.shouldPrintComment = format.shouldPrintComment || function () {
  70. return format.comments;
  71. };
  72. } else {
  73. format.shouldPrintComment = format.shouldPrintComment || function (value) {
  74. return format.comments || value.indexOf("@license") >= 0 || value.indexOf("@preserve") >= 0;
  75. };
  76. }
  77. if (format.compact === "auto") {
  78. format.compact = code.length > 500000;
  79. if (format.compact) {
  80. console.error("[BABEL] " + messages.get("codeGeneratorDeopt", opts.filename, "500KB"));
  81. }
  82. }
  83. if (format.compact) {
  84. format.indent.adjustMultilineComment = false;
  85. }
  86. return format;
  87. }
  88. function findCommonStringDelimiter(code, tokens) {
  89. var DEFAULT_STRING_DELIMITER = "double";
  90. if (!code) {
  91. return DEFAULT_STRING_DELIMITER;
  92. }
  93. var occurrences = {
  94. single: 0,
  95. double: 0
  96. };
  97. var checked = 0;
  98. for (var i = 0; i < tokens.length; i++) {
  99. var token = tokens[i];
  100. if (token.type.label !== "string") continue;
  101. var raw = code.slice(token.start, token.end);
  102. if (raw[0] === "'") {
  103. occurrences.single++;
  104. } else {
  105. occurrences.double++;
  106. }
  107. checked++;
  108. if (checked >= 3) break;
  109. }
  110. if (occurrences.single > occurrences.double) {
  111. return "single";
  112. } else {
  113. return "double";
  114. }
  115. }
  116. var CodeGenerator = exports.CodeGenerator = function () {
  117. function CodeGenerator(ast, opts, code) {
  118. (0, _classCallCheck3.default)(this, CodeGenerator);
  119. this._generator = new Generator(ast, opts, code);
  120. }
  121. CodeGenerator.prototype.generate = function generate() {
  122. return this._generator.generate();
  123. };
  124. return CodeGenerator;
  125. }();