From 009a428723549b31891b1756a4bb3d5ec47b4e1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl=20T=C3=B6rnqvist?= Date: Fri, 14 Oct 2022 11:39:56 +0200 Subject: [PATCH 1/2] Fix sequential expressions Handle values other than functions and class expressions --- index.js | 31 ++++++++++++++++++++++--------- test/funny-exports/expected.js | 2 ++ test/funny-exports/funny.js | 2 ++ 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index f5b9425..8281e9e 100644 --- a/index.js +++ b/index.js @@ -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. diff --git a/test/funny-exports/expected.js b/test/funny-exports/expected.js index 1c3f9e4..440d837 100644 --- a/test/funny-exports/expected.js +++ b/test/funny-exports/expected.js @@ -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' + },{}]},{},[1]); diff --git a/test/funny-exports/funny.js b/test/funny-exports/funny.js index e03a2a7..3eaf6ee 100644 --- a/test/funny-exports/funny.js +++ b/test/funny-exports/funny.js @@ -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' From f70852fa002d6a841682428a7d847342a84c9f5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl=20T=C3=B6rnqvist?= Date: Fri, 14 Oct 2022 12:11:03 +0200 Subject: [PATCH 2/2] Add more sequential test cases --- test/funny-exports/expected.js | 2 +- test/funny-exports/funny.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/funny-exports/expected.js b/test/funny-exports/expected.js index 440d837..f645ae5 100644 --- a/test/funny-exports/expected.js +++ b/test/funny-exports/expected.js @@ -13,6 +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.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]); diff --git a/test/funny-exports/funny.js b/test/funny-exports/funny.js index 3eaf6ee..a2b3be6 100644 --- a/test/funny-exports/funny.js +++ b/test/funny-exports/funny.js @@ -7,4 +7,4 @@ var c = (0, exports.c = 'beep boop') exports.d = exports.e = null -exports.f = {},exports.g = 'g' +exports.f = {},exports.g = 'g',exports.h = () => {},exports.i = function () {}