Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,20 +235,33 @@ function createStream (opts, api) {
// Here if `exports.a` is removed we need to make sure the `function(){}` is still
// an expression, by prepending `void 0,` to result in:
// `void 0,function(){},exports.b=function(){}`
var isPossiblyAmbiguousExpression = node.right.type === 'FunctionExpression' || node.right.type === 'ClassExpression'
if (isPossiblyAmbiguousExpression && node.parent.type === 'SequenceExpression' ||
// In the case of non-function/class expressions, we can void the whole thing
// eg: `exports.a={},exports.b=''`
// becomes: `void {},void ''`
var isFunction = node.right.type === 'FunctionExpression'
var isAssignment = node.right.type === 'AssignmentExpression'
var isArrowFunction = node.right.type === 'ArrowFunctionExpression'
var isVariableDeclarator = node.parent.parent.type === 'VariableDeclarator'
if (
// persist sequential variable declarations
// eg: `var a = (0, exports.a = function(){})`
(!isVariableDeclarator && node.parent.type === 'SequenceExpression') ||
// without this, `exports.a = exports.b = xyz` eliminating exports.a becomes `void exports.b = xyz`
// which is invalid.
node.right.type === 'AssignmentExpression' ||
isAssignment ||
// Don't output a statement containing only `void () => {}`
node.right.type === 'ArrowFunctionExpression') {
isArrowFunction
) {
// ignore alias assignment expression `exports.a = exports.b = exports.c`
// unless the last argument is noname function
var isAliasAssignment = node.right.type === 'AssignmentExpression' && node.right.left.type === 'MemberExpression' && node.right.left.object.name === 'exports'
var isFunction = isAliasAssignment && node.right.right.type === 'FunctionExpression'
var isClass = isAliasAssignment && node.right.right.type === 'ClassExpression'
if (!isAliasAssignment || isFunction || isClass) {
prefix += 'void 0, '
var isAliasAssignment = isAssignment && node.right.left.type === 'MemberExpression' && node.right.left.object.name === 'exports'
var isAliasFunction = isAliasAssignment && node.right.right.type === 'FunctionExpression'
var isAliasClass = isAliasAssignment && node.right.right.type === 'ClassExpression'
if (!isAliasAssignment || isAliasFunction || isAliasClass) {
prefix += 'void '
if (isAssignment || isArrowFunction || isFunction || isAliasFunction || isAliasClass) {
prefix += '0, '
}
}
}
// Make sure we can't accidentally continue a previous statement.
Expand Down
2 changes: 2 additions & 0 deletions test/funny-exports/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ var c = (0, /* common-shake removed: exports.c = */ 'beep boop')

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

/* 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 () {}

},{}]},{},[1]);
2 changes: 2 additions & 0 deletions test/funny-exports/funny.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ function a() {} exports.b = function () { return a }, console.log(a)
var c = (0, exports.c = 'beep boop')

exports.d = exports.e = null

exports.f = {},exports.g = 'g',exports.h = () => {},exports.i = function () {}