From 9011dcbfebe5673ae3bce2e0e40de5501a613047 Mon Sep 17 00:00:00 2001 From: Amjad Masad Date: Tue, 27 Oct 2015 17:01:09 -0700 Subject: [PATCH] No need for semicolons at the end of the block in compact mode --- packages/babel/src/generation/buffer.js | 9 ++++++++- .../fixtures/generation/compact/no-semicolon/actual.js | 6 ++++++ .../fixtures/generation/compact/no-semicolon/expected.js | 1 + .../expected.js | 2 +- 4 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 packages/babel/test/fixtures/generation/compact/no-semicolon/actual.js create mode 100644 packages/babel/test/fixtures/generation/compact/no-semicolon/expected.js diff --git a/packages/babel/src/generation/buffer.js b/packages/babel/src/generation/buffer.js index 0262bb5d77..ba51ede6da 100644 --- a/packages/babel/src/generation/buffer.js +++ b/packages/babel/src/generation/buffer.js @@ -84,6 +84,7 @@ export default class Buffer { rightBrace() { this.newline(true); + if (this.format.compact) this._removeLast(";"); this.push("}"); } @@ -114,8 +115,11 @@ export default class Buffer { removeLast(cha) { if (this.format.compact) return; - if (!this.isLast(cha)) return; + return this._removeLast(cha); + } + _removeLast(cha) { + if (!this._isLast(cha)) return; this.buf = this.buf.substr(0, this.buf.length - 1); this.position.unshift(cha); } @@ -298,7 +302,10 @@ export default class Buffer { isLast(cha) { if (this.format.compact) return false; + return this._isLast(cha); + } + _isLast(cha) { var buf = this.buf; var last = buf[buf.length - 1]; diff --git a/packages/babel/test/fixtures/generation/compact/no-semicolon/actual.js b/packages/babel/test/fixtures/generation/compact/no-semicolon/actual.js new file mode 100644 index 0000000000..85240b60d1 --- /dev/null +++ b/packages/babel/test/fixtures/generation/compact/no-semicolon/actual.js @@ -0,0 +1,6 @@ +function foo() { + if (bar) { + baz(); + } + return; +} diff --git a/packages/babel/test/fixtures/generation/compact/no-semicolon/expected.js b/packages/babel/test/fixtures/generation/compact/no-semicolon/expected.js new file mode 100644 index 0000000000..5773cd637c --- /dev/null +++ b/packages/babel/test/fixtures/generation/compact/no-semicolon/expected.js @@ -0,0 +1 @@ +function foo(){if(bar){baz()}return} diff --git a/packages/babel/test/fixtures/generation/edgecase/return-with-retainlines-and-compact-option/expected.js b/packages/babel/test/fixtures/generation/edgecase/return-with-retainlines-and-compact-option/expected.js index 042f71356d..e853654162 100644 --- a/packages/babel/test/fixtures/generation/edgecase/return-with-retainlines-and-compact-option/expected.js +++ b/packages/babel/test/fixtures/generation/edgecase/return-with-retainlines-and-compact-option/expected.js @@ -1,4 +1,4 @@ function foo(l){ return ( -l);} +l)}