index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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, opts, code) {
  27. (0, _classCallCheck3.default)(this, Generator);
  28. opts = opts || {};
  29. var tokens = ast.tokens || [];
  30. var format = normalizeOptions(code, opts, tokens);
  31. var map = opts.sourceMaps ? new _sourceMap2.default(opts, code) : null;
  32. var _this = (0, _possibleConstructorReturn3.default)(this, _Printer.call(this, format, map, tokens));
  33. _this.ast = ast;
  34. return _this;
  35. }
  36. Generator.prototype.generate = function generate() {
  37. return _Printer.prototype.generate.call(this, this.ast);
  38. };
  39. return Generator;
  40. }(_printer2.default);
  41. function normalizeOptions(code, opts, tokens) {
  42. var style = " ";
  43. if (code && typeof code === "string") {
  44. var indent = (0, _detectIndent2.default)(code).indent;
  45. if (indent && indent !== " ") style = indent;
  46. }
  47. var format = {
  48. auxiliaryCommentBefore: opts.auxiliaryCommentBefore,
  49. auxiliaryCommentAfter: opts.auxiliaryCommentAfter,
  50. shouldPrintComment: opts.shouldPrintComment,
  51. retainLines: opts.retainLines,
  52. comments: opts.comments == null || opts.comments,
  53. compact: opts.compact,
  54. minified: opts.minified,
  55. concise: opts.concise,
  56. quotes: opts.quotes || findCommonStringDelimiter(code, tokens),
  57. indent: {
  58. adjustMultilineComment: true,
  59. style: style,
  60. base: 0
  61. }
  62. };
  63. if (format.minified) {
  64. format.compact = true;
  65. format.shouldPrintComment = format.shouldPrintComment || function () {
  66. return format.comments;
  67. };
  68. } else {
  69. format.shouldPrintComment = format.shouldPrintComment || function (value) {
  70. return format.comments || value.indexOf("@license") >= 0 || value.indexOf("@preserve") >= 0;
  71. };
  72. }
  73. if (format.compact === "auto") {
  74. format.compact = code.length > 100000;
  75. if (format.compact) {
  76. console.error("[BABEL] " + messages.get("codeGeneratorDeopt", opts.filename, "100KB"));
  77. }
  78. }
  79. if (format.compact) {
  80. format.indent.adjustMultilineComment = false;
  81. }
  82. return format;
  83. }
  84. function findCommonStringDelimiter(code, tokens) {
  85. var occurences = {
  86. single: 0,
  87. double: 0
  88. };
  89. var checked = 0;
  90. for (var i = 0; i < tokens.length; i++) {
  91. var token = tokens[i];
  92. if (token.type.label !== "string") continue;
  93. var raw = code.slice(token.start, token.end);
  94. if (raw[0] === "'") {
  95. occurences.single++;
  96. } else {
  97. occurences.double++;
  98. }
  99. checked++;
  100. if (checked >= 3) break;
  101. }
  102. if (occurences.single > occurences.double) {
  103. return "single";
  104. } else {
  105. return "double";
  106. }
  107. }
  108. var CodeGenerator = exports.CodeGenerator = function () {
  109. function CodeGenerator(ast, opts, code) {
  110. (0, _classCallCheck3.default)(this, CodeGenerator);
  111. this._generator = new Generator(ast, opts, code);
  112. }
  113. CodeGenerator.prototype.generate = function generate() {
  114. return this._generator.generate();
  115. };
  116. return CodeGenerator;
  117. }();