From e58a065a8c61b6f65685f510eadeea8f8bed75e2 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Sun, 12 May 2024 23:25:42 -0700 Subject: [PATCH] latest live cycle handling --- test/fixtures/datatables/actual.js | 33 +++++++++--- test/fixtures/datatables/expected.js | 19 +++++-- .../setimmediate-skip/expected.failure.js | 4 -- transform-cjs-dew.js | 50 +++++++++++++------ 4 files changed, 76 insertions(+), 30 deletions(-) delete mode 100644 test/fixtures/setimmediate-skip/expected.failure.js diff --git a/test/fixtures/datatables/actual.js b/test/fixtures/datatables/actual.js index 9f55199..8ef3c0b 100644 --- a/test/fixtures/datatables/actual.js +++ b/test/fixtures/datatables/actual.js @@ -1,6 +1,27 @@ -var o, a; -"function" == typeof define && define.amd - ? void 0 - : "object" == typeof exports - ? (module.exports = function (e, t) { return t; }) - : void 0; +/*! DataTables Bootstrap 5 integration + * 2020 SpryMedia Ltd - datatables.net/license + */ +!(function (n) { + var o, a; + "function" == typeof define && define.amd + ? define(["jquery", "datatables.net"], function (e) { + return n(e, window, document); + }) + : "object" == typeof exports + ? ((o = require("jquery")), + (a = function (e, t) { + t.fn.dataTable || require("datatables.net")(e, t); + }), + "undefined" == typeof window + ? (module.exports = function (e, t) { + return ( + (e = e || window), (t = t || o(e)), a(e, t), n(t, 0, e.document) + ); + }) + : (a(window, o), (module.exports = n(o, window, window.document)))) + : n(jQuery, window, document); +})(); + + + +require('test'); \ No newline at end of file diff --git a/test/fixtures/datatables/expected.js b/test/fixtures/datatables/expected.js index 1b2a36d..6c6332d 100644 --- a/test/fixtures/datatables/expected.js +++ b/test/fixtures/datatables/expected.js @@ -1,11 +1,22 @@ +import { dew as _jqueryDew } from "jquery"; +import { dew as _datatablesDew } from "datatables.net"; +import { dew as _testDew } from "test"; var exports = {}, _dewExec = false; export function dew() { if (_dewExec) return exports; _dewExec = true; - var o, a; - true ? exports = function (e, t) { - return t; - } : void 0; + /*! DataTables Bootstrap 5 integration + * 2020 SpryMedia Ltd - datatables.net/license + */ + !function (n) { + var o, a; + true ? (o = _jqueryDew(), a = function (e, t) { + t.fn.dataTable || _datatablesDew()(e, t); + }, "undefined" == typeof window ? exports = function (e, t) { + return e = e || window, t = t || o(e), a(e, t), n(t, 0, e.document); + } : (a(window, o), exports = n(o, window, window.document))) : n(jQuery, window, document); + }(); + _testDew(); return exports; } \ No newline at end of file diff --git a/test/fixtures/setimmediate-skip/expected.failure.js b/test/fixtures/setimmediate-skip/expected.failure.js deleted file mode 100644 index 078a463..0000000 --- a/test/fixtures/setimmediate-skip/expected.failure.js +++ /dev/null @@ -1,4 +0,0 @@ -import _process from "process"; -var process = _process; -const maybeSetImmediate = "undefined" !== typeof process.nextTick ? process.nextTick : null; -export default {}; \ No newline at end of file diff --git a/transform-cjs-dew.js b/transform-cjs-dew.js index a5929e3..9c9198f 100644 --- a/transform-cjs-dew.js +++ b/transform-cjs-dew.js @@ -22,14 +22,20 @@ const strictReservedOrKeyword = Object.assign( }); function hasBinding (path, bindingName) { - // Babel bug with replacement scoping creating circular scope - // work around by calling the id check directly - if (path.scope === path.scope.parentScope) { - return path.scope.hasOwnBinding(bindingName); - } return path.scope.hasBinding(bindingName); } +function isLive(path) { + const seen = new Set(); + let curPath = path; + while (curPath) { + if (seen.has(curPath)) return false; + seen.add(curPath); + curPath = curPath.parentPath; + } + return true; +} + const topLevelReserved = Object.assign( Object.create(null), { "global":1, "self":1, "globalThis":1 @@ -134,6 +140,14 @@ module.exports = function ({ types: t }) { return t.isStringLiteral(node) || t.isTemplateLiteral(node) && node.quasis[0].value.cooked.length || t.isBinaryExpression(node) || t.isIdentifier(node) && node.name === '__dirname'; } + // typeof setImmediate [!=]== + function isLogicalOrConditionalBinaryExpression (path) { + return t.isBinaryExpression(path.node) && ( + t.isConditionalExpression(path.parentPath.node) && path.parentPath.node.test === path.node || + t.isLogicalExpression(path.parentPath.node) && path.parentPath.node.left === path.node + ); + } + function addDependency (path, state, depModuleArg, optional = false) { if (path.parentPath && path.parentPath.data === 'dead') return null; @@ -307,11 +321,13 @@ module.exports = function ({ types: t }) { let consequent = path.parentPath.get('consequent'); let alternate = path.parentPath.get('alternate'); - path.stop(); + path.skip(); if (truthy) { - if (alternate.node) + if (alternate.node) { + alternate.skip(); alternate.remove(); + } if (t.isBlockStatement(consequent.node)) { if (consequent.node.body.length === 1 && t.isExpressionStatement(consequent.node.body[0])) { path.parentPath.replaceWith(consequent.node.body[0]); @@ -322,8 +338,10 @@ module.exports = function ({ types: t }) { } } else { - if (consequent.node) + if (consequent.node) { + consequent.skip(); consequent.remove(); + } if (t.isBlockStatement(alternate.node)) { if (alternate.node.body.length === 1 && t.isExpressionStatement(alternate.node.body[0])) { path.parentPath.replaceWith(alternate.node.body[0]); @@ -343,7 +361,7 @@ module.exports = function ({ types: t }) { let consequent = path.parentPath.get('consequent'); let alternate = path.parentPath.get('alternate'); - path.stop(); + path.skip(); if (parentNode.test.value) { alternate.data = 'dead'; @@ -1118,6 +1136,7 @@ module.exports = function ({ types: t }) { } else if (path.node.init === null) { const name = path.node.id.name; + path.skip(); path.remove(); path.parentPath.scope.registerBinding(name, newBinding.get('id')); } @@ -1208,7 +1227,7 @@ module.exports = function ({ types: t }) { * detect usage of module */ ReferencedIdentifier (path, state) { - if (state.inserting) + if (state.inserting || !isLive(path)) return; let identifierName = path.node.name; @@ -1385,12 +1404,10 @@ module.exports = function ({ types: t }) { state.usesExports = true; } else if (identifierName === 'setImmediate' && !hasBinding(path, 'setImmediate')) { - // typeof setImmediate [!=]== - if (t.isUnaryExpression(path.parentPath.node, { operator: 'typeof' }) && - (t.isBinaryExpression(path.parentPath.parentPath.node) || t.isBinaryExpression(path.parentPath.parentPath.node)) && - (t.isConditionalExpression(path.parentPath.parentPath.parentPath.node) && path.parentPath.parentPath.parentPath.node.test === path.parentPath.parentPath.node || - t.isLogicalExpression(path.parentPath.parentPath.parentPath.node) && path.parentPath.parentPath.parentPath.node.left === path.parentPath.parentPath.node)) { - path.stop(); + if (t.isUnaryExpression(path.parentPath.node, { operator: 'typeof' }) && isLogicalOrConditionalBinaryExpression(path.parentPath.parentPath)) { + return; + } + if (t.isConditionalExpression(path.parentPath) && isLogicalOrConditionalBinaryExpression(path.parentPath.get('test')) && t.isUnaryExpression(path.parentPath.node.test.right, { operator: 'typeof' }) && t.isIdentifier(path.parentPath.node.test.right.argument, { name: 'setImmediate' })) { return; } if (hasBinding(path, 'process')) { @@ -1499,6 +1516,7 @@ module.exports = function ({ types: t }) { } } if (path.node.operator === 'delete' && t.isIdentifier(path.node.argument)) { + path.skip(); path.remove(); } }