Skip to content

Commit e58a065

Browse files
committed
latest live cycle handling
1 parent 2f24fff commit e58a065

File tree

4 files changed

+76
-30
lines changed

4 files changed

+76
-30
lines changed

test/fixtures/datatables/actual.js

+27-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
1-
var o, a;
2-
"function" == typeof define && define.amd
3-
? void 0
4-
: "object" == typeof exports
5-
? (module.exports = function (e, t) { return t; })
6-
: void 0;
1+
/*! DataTables Bootstrap 5 integration
2+
* 2020 SpryMedia Ltd - datatables.net/license
3+
*/
4+
!(function (n) {
5+
var o, a;
6+
"function" == typeof define && define.amd
7+
? define(["jquery", "datatables.net"], function (e) {
8+
return n(e, window, document);
9+
})
10+
: "object" == typeof exports
11+
? ((o = require("jquery")),
12+
(a = function (e, t) {
13+
t.fn.dataTable || require("datatables.net")(e, t);
14+
}),
15+
"undefined" == typeof window
16+
? (module.exports = function (e, t) {
17+
return (
18+
(e = e || window), (t = t || o(e)), a(e, t), n(t, 0, e.document)
19+
);
20+
})
21+
: (a(window, o), (module.exports = n(o, window, window.document))))
22+
: n(jQuery, window, document);
23+
})();
24+
25+
26+
27+
require('test');

test/fixtures/datatables/expected.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
1+
import { dew as _jqueryDew } from "jquery";
2+
import { dew as _datatablesDew } from "datatables.net";
3+
import { dew as _testDew } from "test";
14
var exports = {},
25
_dewExec = false;
36
export function dew() {
47
if (_dewExec) return exports;
58
_dewExec = true;
6-
var o, a;
7-
true ? exports = function (e, t) {
8-
return t;
9-
} : void 0;
9+
/*! DataTables Bootstrap 5 integration
10+
* 2020 SpryMedia Ltd - datatables.net/license
11+
*/
12+
!function (n) {
13+
var o, a;
14+
true ? (o = _jqueryDew(), a = function (e, t) {
15+
t.fn.dataTable || _datatablesDew()(e, t);
16+
}, "undefined" == typeof window ? exports = function (e, t) {
17+
return e = e || window, t = t || o(e), a(e, t), n(t, 0, e.document);
18+
} : (a(window, o), exports = n(o, window, window.document))) : n(jQuery, window, document);
19+
}();
20+
_testDew();
1021
return exports;
1122
}

test/fixtures/setimmediate-skip/expected.failure.js

-4
This file was deleted.

transform-cjs-dew.js

+34-16
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,20 @@ const strictReservedOrKeyword = Object.assign(
2222
});
2323

2424
function hasBinding (path, bindingName) {
25-
// Babel bug with replacement scoping creating circular scope
26-
// work around by calling the id check directly
27-
if (path.scope === path.scope.parentScope) {
28-
return path.scope.hasOwnBinding(bindingName);
29-
}
3025
return path.scope.hasBinding(bindingName);
3126
}
3227

28+
function isLive(path) {
29+
const seen = new Set();
30+
let curPath = path;
31+
while (curPath) {
32+
if (seen.has(curPath)) return false;
33+
seen.add(curPath);
34+
curPath = curPath.parentPath;
35+
}
36+
return true;
37+
}
38+
3339
const topLevelReserved = Object.assign(
3440
Object.create(null), {
3541
"global":1, "self":1, "globalThis":1
@@ -134,6 +140,14 @@ module.exports = function ({ types: t }) {
134140
return t.isStringLiteral(node) || t.isTemplateLiteral(node) && node.quasis[0].value.cooked.length || t.isBinaryExpression(node) || t.isIdentifier(node) && node.name === '__dirname';
135141
}
136142

143+
// typeof setImmediate [!=]==
144+
function isLogicalOrConditionalBinaryExpression (path) {
145+
return t.isBinaryExpression(path.node) && (
146+
t.isConditionalExpression(path.parentPath.node) && path.parentPath.node.test === path.node ||
147+
t.isLogicalExpression(path.parentPath.node) && path.parentPath.node.left === path.node
148+
);
149+
}
150+
137151
function addDependency (path, state, depModuleArg, optional = false) {
138152
if (path.parentPath && path.parentPath.data === 'dead')
139153
return null;
@@ -307,11 +321,13 @@ module.exports = function ({ types: t }) {
307321
let consequent = path.parentPath.get('consequent');
308322
let alternate = path.parentPath.get('alternate');
309323

310-
path.stop();
324+
path.skip();
311325

312326
if (truthy) {
313-
if (alternate.node)
327+
if (alternate.node) {
328+
alternate.skip();
314329
alternate.remove();
330+
}
315331
if (t.isBlockStatement(consequent.node)) {
316332
if (consequent.node.body.length === 1 && t.isExpressionStatement(consequent.node.body[0])) {
317333
path.parentPath.replaceWith(consequent.node.body[0]);
@@ -322,8 +338,10 @@ module.exports = function ({ types: t }) {
322338
}
323339
}
324340
else {
325-
if (consequent.node)
341+
if (consequent.node) {
342+
consequent.skip();
326343
consequent.remove();
344+
}
327345
if (t.isBlockStatement(alternate.node)) {
328346
if (alternate.node.body.length === 1 && t.isExpressionStatement(alternate.node.body[0])) {
329347
path.parentPath.replaceWith(alternate.node.body[0]);
@@ -343,7 +361,7 @@ module.exports = function ({ types: t }) {
343361
let consequent = path.parentPath.get('consequent');
344362
let alternate = path.parentPath.get('alternate');
345363

346-
path.stop();
364+
path.skip();
347365

348366
if (parentNode.test.value) {
349367
alternate.data = 'dead';
@@ -1118,6 +1136,7 @@ module.exports = function ({ types: t }) {
11181136
}
11191137
else if (path.node.init === null) {
11201138
const name = path.node.id.name;
1139+
path.skip();
11211140
path.remove();
11221141
path.parentPath.scope.registerBinding(name, newBinding.get('id'));
11231142
}
@@ -1208,7 +1227,7 @@ module.exports = function ({ types: t }) {
12081227
* detect usage of module
12091228
*/
12101229
ReferencedIdentifier (path, state) {
1211-
if (state.inserting)
1230+
if (state.inserting || !isLive(path))
12121231
return;
12131232
let identifierName = path.node.name;
12141233

@@ -1385,12 +1404,10 @@ module.exports = function ({ types: t }) {
13851404
state.usesExports = true;
13861405
}
13871406
else if (identifierName === 'setImmediate' && !hasBinding(path, 'setImmediate')) {
1388-
// typeof setImmediate [!=]==
1389-
if (t.isUnaryExpression(path.parentPath.node, { operator: 'typeof' }) &&
1390-
(t.isBinaryExpression(path.parentPath.parentPath.node) || t.isBinaryExpression(path.parentPath.parentPath.node)) &&
1391-
(t.isConditionalExpression(path.parentPath.parentPath.parentPath.node) && path.parentPath.parentPath.parentPath.node.test === path.parentPath.parentPath.node ||
1392-
t.isLogicalExpression(path.parentPath.parentPath.parentPath.node) && path.parentPath.parentPath.parentPath.node.left === path.parentPath.parentPath.node)) {
1393-
path.stop();
1407+
if (t.isUnaryExpression(path.parentPath.node, { operator: 'typeof' }) && isLogicalOrConditionalBinaryExpression(path.parentPath.parentPath)) {
1408+
return;
1409+
}
1410+
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' })) {
13941411
return;
13951412
}
13961413
if (hasBinding(path, 'process')) {
@@ -1499,6 +1516,7 @@ module.exports = function ({ types: t }) {
14991516
}
15001517
}
15011518
if (path.node.operator === 'delete' && t.isIdentifier(path.node.argument)) {
1519+
path.skip();
15021520
path.remove();
15031521
}
15041522
}

0 commit comments

Comments
 (0)