Skip to content

Commit

Permalink
Skip newlines around inline #__PURE__ annotations (#11133)
Browse files Browse the repository at this point in the history
* Skip newlines around #__PURE__ annotations

* Update tests
  • Loading branch information
nicolo-ribaudo committed Feb 13, 2020
1 parent 3c6a8ae commit d21e221
Show file tree
Hide file tree
Showing 274 changed files with 382 additions and 1,072 deletions.
@@ -1,6 +1,4 @@
var Foo =
/*#__PURE__*/
function (_Bar) {
var Foo = /*#__PURE__*/function (_Bar) {
"use strict";

babelHelpers.inherits(Foo, _Bar);
Expand Down
@@ -1,9 +1,7 @@
function f() {
var _this = this;

let g =
/*#__PURE__*/
function () {
let g = /*#__PURE__*/function () {
var _ref = babelHelpers.asyncToGenerator(function* () {
_this;
});
Expand All @@ -19,9 +17,7 @@ class Class {
var _this2 = this;

return babelHelpers.asyncToGenerator(function* () {
var c =
/*#__PURE__*/
function () {
var c = /*#__PURE__*/function () {
var _ref2 = babelHelpers.asyncToGenerator(function* (b) {
_this2;
});
Expand Down
Expand Up @@ -15,19 +15,15 @@ function _defineProperties(target, props) { for (var i = 0; i < props.length; i+

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

var Foo =
/*#__PURE__*/
function () {
var Foo = /*#__PURE__*/function () {
function Foo() {
_classCallCheck(this, Foo);
}

_createClass(Foo, [{
key: "bar",
value: function () {
var _bar = _asyncToGenerator(
/*#__PURE__*/
regeneratorRuntime.mark(function _callee() {
var _bar = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
var baz;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
Expand Down Expand Up @@ -61,19 +57,15 @@ function foo() {
}

function _foo() {
_foo = _asyncToGenerator(
/*#__PURE__*/
regeneratorRuntime.mark(function _callee3() {
_foo = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() {
var bar, _bar2;

return regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
_bar2 = function _ref2() {
_bar2 = _asyncToGenerator(
/*#__PURE__*/
regeneratorRuntime.mark(function _callee2() {
_bar2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
var baz;
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
Expand Down
@@ -1,6 +1,4 @@
var Test =
/*#__PURE__*/
function () {
var Test = /*#__PURE__*/function () {
"use strict";

function Test() {
Expand Down
Expand Up @@ -4,7 +4,7 @@
"column": 10
},
"generated": {
"line": 13,
"line": 11,
"column": 15
}
}]
42 changes: 29 additions & 13 deletions packages/babel-generator/src/printer.js
Expand Up @@ -9,6 +9,7 @@ import * as generatorFunctions from "./generators";
const SCIENTIFIC_NOTATION = /e/i;
const ZERO_DECIMAL_INTEGER = /\.0+$/;
const NON_DECIMAL_LITERAL = /^0[box]/;
const PURE_ANNOTATION_RE = /^\s*[@#]__PURE__\s*$/;

export type Format = {
shouldPrintComment: (comment: string) => boolean,
Expand Down Expand Up @@ -477,7 +478,11 @@ export default class Printer {
}

_printLeadingComments(node) {
this._printComments(this._getComments(true, node));
this._printComments(
this._getComments(true, node),
// Don't add leading/trailing new lines to #__PURE__ annotations
true,
);
}

printInnerComments(node, indent = true) {
Expand Down Expand Up @@ -532,7 +537,7 @@ export default class Printer {
);
}

_printComment(comment) {
_printComment(comment, skipNewLines?: boolean) {
if (!this.format.shouldPrintComment(comment.value)) return;

// Some plugins use this to mark comments as removed using the AST-root 'comments' property,
Expand All @@ -549,12 +554,12 @@ export default class Printer {

const isBlockComment = comment.type === "CommentBlock";

// Always add a newline before a block comment
this.newline(
this._buf.hasContent() && !this._noLineTerminator && isBlockComment
? 1
: 0,
);
// Add a newline before and after a block comment, unless explicitly
// disallowed
const printNewLines =
isBlockComment && !skipNewLines && !this._noLineTerminator;

if (printNewLines && this._buf.hasContent()) this.newline(1);

if (!this.endsWith("[") && !this.endsWith("{")) this.space();

Expand Down Expand Up @@ -584,15 +589,26 @@ export default class Printer {
this._append(val);
});

// Always add a newline after a block comment
this.newline(isBlockComment && !this._noLineTerminator ? 1 : 0);
if (printNewLines) this.newline(1);
}

_printComments(comments?: Array<Object>) {
_printComments(comments?: Array<Object>, inlinePureAnnotation?: boolean) {
if (!comments || !comments.length) return;

for (const comment of comments) {
this._printComment(comment);
if (
inlinePureAnnotation &&
comments.length === 1 &&
PURE_ANNOTATION_RE.test(comments[0].value)
) {
this._printComment(
comments[0],
// Keep newlines if the comment marks a standalone call
this._buf.hasContent() && !this.endsWith("\n"),
);
} else {
for (const comment of comments) {
this._printComment(comment);
}
}
}
}
Expand Down
@@ -1,8 +1,6 @@
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

let Foo =
/*#__PURE__*/
function () {
let Foo = /*#__PURE__*/function () {
"use strict";

function Foo() {
Expand Down
Expand Up @@ -3,9 +3,7 @@ function g() {
}

function _g() {
_g = babelHelpers.wrapAsyncGenerator(function* (x =
/*#__PURE__*/
babelHelpers.asyncToGenerator(function* () {
_g = babelHelpers.wrapAsyncGenerator(function* (x = /*#__PURE__*/babelHelpers.asyncToGenerator(function* () {
yield 1;
})) {
yield babelHelpers.awaitAsyncGenerator(2);
Expand Down
Expand Up @@ -10,9 +10,7 @@ let Hello = function Hello() {
};
};

let Outer =
/*#__PURE__*/
function (_Hello) {
let Outer = /*#__PURE__*/function (_Hello) {
babelHelpers.inherits(Outer, _Hello);

function Outer() {
Expand Down
@@ -1,8 +1,6 @@
"use strict";

let Hello =
/*#__PURE__*/
function () {
let Hello = /*#__PURE__*/function () {
function Hello() {
babelHelpers.classCallCheck(this, Hello);
}
Expand All @@ -16,9 +14,7 @@ function () {
return Hello;
}();

let Outer =
/*#__PURE__*/
function (_Hello) {
let Outer = /*#__PURE__*/function (_Hello) {
babelHelpers.inherits(Outer, _Hello);

function Outer() {
Expand Down
@@ -1,6 +1,4 @@
var Foo =
/*#__PURE__*/
function () {
var Foo = /*#__PURE__*/function () {
"use strict";

function Foo() {
Expand Down
@@ -1,6 +1,4 @@
var Foo =
/*#__PURE__*/
function () {
var Foo = /*#__PURE__*/function () {
"use strict";

function Foo() {
Expand Down
@@ -1,6 +1,4 @@
var Point =
/*#__PURE__*/
function () {
var Point = /*#__PURE__*/function () {
"use strict";

function Point(x = 0, y = 0) {
Expand Down
@@ -1,6 +1,4 @@
var Foo =
/*#__PURE__*/
function (_Bar) {
var Foo = /*#__PURE__*/function (_Bar) {
"use strict";

babelHelpers.inherits(Foo, _Bar);
Expand Down
Expand Up @@ -10,9 +10,7 @@ var Foo = function Foo() {

var _prop = babelHelpers.classPrivateFieldLooseKey("prop");

var Bar =
/*#__PURE__*/
function (_Foo) {
var Bar = /*#__PURE__*/function (_Foo) {
"use strict";

babelHelpers.inherits(Bar, _Foo);
Expand Down
@@ -1,6 +1,4 @@
var Child =
/*#__PURE__*/
function (_Parent) {
var Child = /*#__PURE__*/function (_Parent) {
"use strict";

babelHelpers.inherits(Child, _Parent);
Expand Down
Expand Up @@ -7,9 +7,7 @@ var Outer = function Outer() {
value: void 0
});

var Test =
/*#__PURE__*/
function (_babelHelpers$classPr) {
var Test = /*#__PURE__*/function (_babelHelpers$classPr) {
babelHelpers.inherits(Test, _babelHelpers$classPr);

function Test() {
Expand Down
@@ -1,6 +1,4 @@
var Foo =
/*#__PURE__*/
function () {
var Foo = /*#__PURE__*/function () {
"use strict";

function Foo() {
Expand Down
@@ -1,6 +1,4 @@
var Foo =
/*#__PURE__*/
function (_Bar) {
var Foo = /*#__PURE__*/function (_Bar) {
"use strict";

babelHelpers.inherits(Foo, _Bar);
Expand Down
@@ -1,6 +1,4 @@
var Foo =
/*#__PURE__*/
function (_Bar) {
var Foo = /*#__PURE__*/function (_Bar) {
"use strict";

babelHelpers.inherits(Foo, _Bar);
Expand Down
@@ -1,6 +1,4 @@
var Foo =
/*#__PURE__*/
function () {
var Foo = /*#__PURE__*/function () {
"use strict";

function Foo() {
Expand Down
@@ -1,6 +1,4 @@
var Foo =
/*#__PURE__*/
function () {
var Foo = /*#__PURE__*/function () {
"use strict";

function Foo() {
Expand Down
@@ -1,6 +1,4 @@
var Foo =
/*#__PURE__*/
function () {
var Foo = /*#__PURE__*/function () {
"use strict";

function Foo() {
Expand Down
@@ -1,6 +1,4 @@
var Point =
/*#__PURE__*/
function () {
var Point = /*#__PURE__*/function () {
"use strict";

function Point(x = 0, y = 0) {
Expand Down
@@ -1,6 +1,4 @@
var Foo =
/*#__PURE__*/
function (_Bar) {
var Foo = /*#__PURE__*/function (_Bar) {
"use strict";

babelHelpers.inherits(Foo, _Bar);
Expand Down
Expand Up @@ -11,9 +11,7 @@ var Foo = function Foo() {

var _prop = new WeakMap();

var Bar =
/*#__PURE__*/
function (_Foo) {
var Bar = /*#__PURE__*/function (_Foo) {
"use strict";

babelHelpers.inherits(Bar, _Foo);
Expand Down
@@ -1,6 +1,4 @@
var Child =
/*#__PURE__*/
function (_Parent) {
var Child = /*#__PURE__*/function (_Parent) {
"use strict";

babelHelpers.inherits(Child, _Parent);
Expand Down
Expand Up @@ -8,9 +8,7 @@ var Outer = function Outer() {
value: void 0
});

var Test =
/*#__PURE__*/
function (_babelHelpers$classPr) {
var Test = /*#__PURE__*/function (_babelHelpers$classPr) {
babelHelpers.inherits(Test, _babelHelpers$classPr);

function Test() {
Expand Down
@@ -1,6 +1,4 @@
var Foo =
/*#__PURE__*/
function () {
var Foo = /*#__PURE__*/function () {
"use strict";

function Foo() {
Expand Down

0 comments on commit d21e221

Please sign in to comment.