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