Skip to content

Commit f80ef52

Browse files
authored
Fix sequential expressions (#43)
* Fix sequential expressions Handle values other than functions and class expressions * Add more sequential test cases
1 parent bce2021 commit f80ef52

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

index.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -235,20 +235,33 @@ function createStream (opts, api) {
235235
// Here if `exports.a` is removed we need to make sure the `function(){}` is still
236236
// an expression, by prepending `void 0,` to result in:
237237
// `void 0,function(){},exports.b=function(){}`
238-
var isPossiblyAmbiguousExpression = node.right.type === 'FunctionExpression' || node.right.type === 'ClassExpression'
239-
if (isPossiblyAmbiguousExpression && node.parent.type === 'SequenceExpression' ||
238+
// In the case of non-function/class expressions, we can void the whole thing
239+
// eg: `exports.a={},exports.b=''`
240+
// becomes: `void {},void ''`
241+
var isFunction = node.right.type === 'FunctionExpression'
242+
var isAssignment = node.right.type === 'AssignmentExpression'
243+
var isArrowFunction = node.right.type === 'ArrowFunctionExpression'
244+
var isVariableDeclarator = node.parent.parent.type === 'VariableDeclarator'
245+
if (
246+
// persist sequential variable declarations
247+
// eg: `var a = (0, exports.a = function(){})`
248+
(!isVariableDeclarator && node.parent.type === 'SequenceExpression') ||
240249
// without this, `exports.a = exports.b = xyz` eliminating exports.a becomes `void exports.b = xyz`
241250
// which is invalid.
242-
node.right.type === 'AssignmentExpression' ||
251+
isAssignment ||
243252
// Don't output a statement containing only `void () => {}`
244-
node.right.type === 'ArrowFunctionExpression') {
253+
isArrowFunction
254+
) {
245255
// ignore alias assignment expression `exports.a = exports.b = exports.c`
246256
// unless the last argument is noname function
247-
var isAliasAssignment = node.right.type === 'AssignmentExpression' && node.right.left.type === 'MemberExpression' && node.right.left.object.name === 'exports'
248-
var isFunction = isAliasAssignment && node.right.right.type === 'FunctionExpression'
249-
var isClass = isAliasAssignment && node.right.right.type === 'ClassExpression'
250-
if (!isAliasAssignment || isFunction || isClass) {
251-
prefix += 'void 0, '
257+
var isAliasAssignment = isAssignment && node.right.left.type === 'MemberExpression' && node.right.left.object.name === 'exports'
258+
var isAliasFunction = isAliasAssignment && node.right.right.type === 'FunctionExpression'
259+
var isAliasClass = isAliasAssignment && node.right.right.type === 'ClassExpression'
260+
if (!isAliasAssignment || isAliasFunction || isAliasClass) {
261+
prefix += 'void '
262+
if (isAssignment || isArrowFunction || isFunction || isAliasFunction || isAliasClass) {
263+
prefix += '0, '
264+
}
252265
}
253266
}
254267
// Make sure we can't accidentally continue a previous statement.

test/funny-exports/expected.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ var c = (0, /* common-shake removed: exports.c = */ 'beep boop')
1313

1414
/* common-shake removed: exports.d = */ exports.e = null
1515

16+
/* common-shake removed: exports.f = */ void {},/* common-shake removed: exports.g = */ void 'g',/* common-shake removed: exports.h = */ void 0, () => {},/* common-shake removed: exports.i = */ void 0, function () {}
17+
1618
},{}]},{},[1]);

test/funny-exports/funny.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ function a() {} exports.b = function () { return a }, console.log(a)
66
var c = (0, exports.c = 'beep boop')
77

88
exports.d = exports.e = null
9+
10+
exports.f = {},exports.g = 'g',exports.h = () => {},exports.i = function () {}

0 commit comments

Comments
 (0)