From c474fd48e178ca0ae00b6b3524988ff689a0d8ea Mon Sep 17 00:00:00 2001 From: Buu Nguyen Date: Fri, 19 May 2017 13:54:51 -0700 Subject: [PATCH] Fix issue semi-colon gets inserted unnecessarily (#5749) * Fix issue semi-colon gets inserted unnecessarily * Simplify if condition a bit --- .../test/fixtures/regression/T7537/expected.js | 7 ++----- .../test/fixtures/destructuring/issue-5744/actual.js | 1 + .../test/fixtures/destructuring/issue-5744/expected.js | 6 ++++++ packages/babel-traverse/src/path/modification.js | 5 ++++- 4 files changed, 13 insertions(+), 6 deletions(-) create mode 100644 packages/babel-plugin-transform-es2015-destructuring/test/fixtures/destructuring/issue-5744/actual.js create mode 100644 packages/babel-plugin-transform-es2015-destructuring/test/fixtures/destructuring/issue-5744/expected.js diff --git a/packages/babel-plugin-transform-es2015-classes/test/fixtures/regression/T7537/expected.js b/packages/babel-plugin-transform-es2015-classes/test/fixtures/regression/T7537/expected.js index 7b672beec4..6289d6305f 100644 --- a/packages/babel-plugin-transform-es2015-classes/test/fixtures/regression/T7537/expected.js +++ b/packages/babel-plugin-transform-es2015-classes/test/fixtures/regression/T7537/expected.js @@ -17,14 +17,11 @@ var A = function (_B) { _classCallCheck(this, A); if (track !== undefined) { - ; - var _this = _possibleConstructorReturn(this, (A.__proto__ || Object.getPrototypeOf(A)).call(this, track)); } else { - ; - var _this = _possibleConstructorReturn(this, (A.__proto__ || Object.getPrototypeOf(A)).call(this)); - }return _possibleConstructorReturn(_this); + } + return _possibleConstructorReturn(_this); } return A; diff --git a/packages/babel-plugin-transform-es2015-destructuring/test/fixtures/destructuring/issue-5744/actual.js b/packages/babel-plugin-transform-es2015-destructuring/test/fixtures/destructuring/issue-5744/actual.js new file mode 100644 index 0000000000..e54b00a3fe --- /dev/null +++ b/packages/babel-plugin-transform-es2015-destructuring/test/fixtures/destructuring/issue-5744/actual.js @@ -0,0 +1 @@ +if (true) [a, b] = [b, a]; diff --git a/packages/babel-plugin-transform-es2015-destructuring/test/fixtures/destructuring/issue-5744/expected.js b/packages/babel-plugin-transform-es2015-destructuring/test/fixtures/destructuring/issue-5744/expected.js new file mode 100644 index 0000000000..96368d6909 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-destructuring/test/fixtures/destructuring/issue-5744/expected.js @@ -0,0 +1,6 @@ +if (true) { + var _ref = [b, a]; + a = _ref[0]; + b = _ref[1]; + _ref; +} diff --git a/packages/babel-traverse/src/path/modification.js b/packages/babel-traverse/src/path/modification.js index 052cd1afec..4984d45916 100644 --- a/packages/babel-traverse/src/path/modification.js +++ b/packages/babel-traverse/src/path/modification.js @@ -126,7 +126,10 @@ export function insertAfter(nodes) { if (Array.isArray(this.container)) { return this._containerInsertAfter(nodes); } else if (this.isStatementOrBlock()) { - if (this.node) nodes.unshift(this.node); + // Unshift current node if it's not an empty expression + if (this.node && (!this.isExpressionStatement() || this.node.expression != null)) { + nodes.unshift(this.node); + } this._replaceWith(t.blockStatement(nodes)); } else { throw new Error("We don't know what to do with this node type. " +