diff --git a/src/babel/generation/buffer.js b/src/babel/generation/buffer.js index bb47b6a753..21d89e2c02 100644 --- a/src/babel/generation/buffer.js +++ b/src/babel/generation/buffer.js @@ -70,7 +70,7 @@ export default class Buffer { } newline(i, removeLast) { - if (this.format.compact) return; + if (this.format.compact || this.format.retainLines) return; if (this.format.concise) { this.space(); @@ -99,7 +99,7 @@ export default class Buffer { this._newline(removeLast); } - _newline(removeLast) { + _newline(removeLast, force) { // never allow more than two lines if (this.endsWith("\n\n")) return; diff --git a/src/babel/generation/index.js b/src/babel/generation/index.js index 7e35ed694a..fa68180c23 100644 --- a/src/babel/generation/index.js +++ b/src/babel/generation/index.js @@ -34,6 +34,7 @@ class CodeGenerator { } var format = { + retainLines: opts.retainLines, comments: opts.comments == null || opts.comments, compact: opts.compact, quotes: CodeGenerator.findCommonStringDelimeter(code, tokens), @@ -149,6 +150,13 @@ class CodeGenerator { print(node, parent, opts = {}) { if (!node) return; + // catch up to this nodes newline if we're behind + if (node.loc && this.format.retainLines) { + while (this.position.line < node.loc.start.line) { + this._push("\n"); + } + } + if (parent && parent._compact) { node._compact = true; } diff --git a/src/babel/transformation/file/options.json b/src/babel/transformation/file/options.json index 6b31c4c10e..e8e3498761 100644 --- a/src/babel/transformation/file/options.json +++ b/src/babel/transformation/file/options.json @@ -25,6 +25,13 @@ "type": "string" }, + "retainLines": { + "hidden": true, + "type": "boolean", + "default": false, + "description": "retain line numbers - will result in really ugly code" + }, + "nonStandard": { "type": "boolean", "default": true,