Skip to content

Commit

Permalink
latest live cycle handling
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed May 13, 2024
1 parent 2f24fff commit e58a065
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 30 deletions.
33 changes: 27 additions & 6 deletions test/fixtures/datatables/actual.js
Original file line number Diff line number Diff line change
@@ -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');
19 changes: 15 additions & 4 deletions test/fixtures/datatables/expected.js
Original file line number Diff line number Diff line change
@@ -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;
}
4 changes: 0 additions & 4 deletions test/fixtures/setimmediate-skip/expected.failure.js

This file was deleted.

50 changes: 34 additions & 16 deletions transform-cjs-dew.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
Expand All @@ -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]);
Expand All @@ -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';
Expand Down Expand Up @@ -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'));
}
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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')) {
Expand Down Expand Up @@ -1499,6 +1516,7 @@ module.exports = function ({ types: t }) {
}
}
if (path.node.operator === 'delete' && t.isIdentifier(path.node.argument)) {
path.skip();
path.remove();
}
}
Expand Down

0 comments on commit e58a065

Please sign in to comment.