Skip to content

Commit 2604a39

Browse files
josephggoto-bus-stop
authored andcommitted
Fix code gen when removed expression is wrapped in parens (#20)
This fixes #18, which you can run into very easily using tinyify because uglify runs before common-shakeify and it loves to wrap expressions in parenthesis. Note as per the issue, there's a slightly simpler & cleaner solution to this by modifying [common-shake](https://github.com/goto-bus-stop/common-shake) to allow parenthesis support in the walked tree.
1 parent 4d58258 commit 2604a39

5 files changed

Lines changed: 31 additions & 1 deletion

File tree

index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,15 @@ function createStream (opts) {
194194
else if (node.parent.type === 'ExpressionStatement') {
195195
prefix += 'void '
196196
}
197-
string.overwrite(node.start, node.right.start, prefix)
197+
198+
// Acorn silently strips parens, and node.right.start might be after one
199+
// or more parenthesis that we need to keep. Eg, exports.a = (1+1)
200+
if (node.right.end !== node.end) {
201+
// Replace entire expression. node.start - node.end will wrap the entire expression.
202+
string.overwrite(node.start, node.end, prefix + '(' + node.right.getSource() + ')')
203+
} else {
204+
string.overwrite(node.start, node.right.start, prefix)
205+
}
198206
return
199207
} else if (node.type === 'Property') {
200208
// We may have to also overwrite a comma here, eg in `module.exports = {a, b, c}`

test/paren-exports/a.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
exports.a = a => a
2+
3+
exports.z = (1+1)
4+
exports.b = ((a, b) => a ? b : !b)
5+
exports.c = (function (a, b) {})
6+
exports.d = (async function (a, b) {})

test/paren-exports/app.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log(require('./a').a)

test/paren-exports/expected.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
2+
exports.a = a => a
3+
4+
/* common-shake removed: exports.z = */ void (1+1)
5+
/* common-shake removed: exports.b = */ void 0, ((a, b) => a ? b : !b)
6+
/* common-shake removed: exports.c = */ void (function (a, b) {})
7+
/* common-shake removed: exports.d = */ void (async function (a, b) {})
8+
9+
},{}],2:[function(require,module,exports){
10+
console.log(require('./a').a)
11+
12+
},{"./a":1}]},{},[2]);

test/test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ test('semi', function (t) {
6363
test('simple', function (t) {
6464
runTest(t, 'simple')
6565
})
66+
test('paren-exports', function (t) {
67+
runTest(t, 'paren-exports')
68+
})
6669

6770
test('external', function (t) {
6871
var b = browserify({

0 commit comments

Comments
 (0)