Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu committed Jan 24, 2024
1 parent 5ad89e1 commit ec83450
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,71 @@ export default (
return callProvider({ kind: "property", object, key, placement }, path);
}

const polyfilled = new WeakSet();
function maybeMark(path: NodePath, oldNode: t.Node) {
if (path.node !== oldNode) {
polyfilled.add(path.node);
}
}

return {
LogicalExpression: {
exit(path: NodePath<t.LogicalExpression>) {
const { node } = path;
if (node.operator !== "||") return;
if (polyfilled.has(node.left)) {
path.get("right").remove();
} else if (
t.isUnaryExpression(node.left) &&
node.left.operator === "!" &&
polyfilled.has(node.left.argument)
) {
path.replaceWith(node.right);
}
},
},
IfStatement: {
exit(path: NodePath<t.IfStatement>) {
const { node } = path;
if (polyfilled.has(node.test)) {
path.replaceWith(node.consequent);
} else if (
t.isUnaryExpression(node.test) &&
node.test.operator === "!"
) {
if (polyfilled.has(node.test.argument)) {
path.replaceWith(node.alternate);
}
}
},
},
ConditionalExpression: {
exit(path: NodePath<t.ConditionalExpression>) {
const { node } = path;
if (polyfilled.has(node.test)) {
path.replaceWith(node.consequent);
} else if (
t.isUnaryExpression(node.test) &&
node.test.operator === "!"
) {
if (polyfilled.has(node.test.argument)) {
path.replaceWith(node.alternate);
}
}
},
},
// Symbol(), new Promise
ReferencedIdentifier(path: NodePath<t.Identifier>) {
const {
node: { name },
node,
scope,
} = path;
if (scope.getBindingIdentifier(name)) return;

callProvider({ kind: "global", name }, path);

maybeMark(path, node);
},

MemberExpression(path: NodePath<t.MemberExpression>) {
Expand All @@ -33,8 +88,12 @@ export default (
if (binding && binding.path.isImportNamespaceSpecifier()) return;
}

const { node } = path;

const source = resolveSource(object);
return property(source.id, key, source.placement, path);
property(source.id, key, source.placement, path);

maybeMark(path, node);
},

ObjectPattern(path: NodePath<t.ObjectPattern>) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
String.prototype.padStart || err;
!String.prototype.padStart || ok;


String.prototype.padStart ? ok : err;
!String.prototype.padStart ? err : ok;

if (String.prototype.padStart) {
ok;
} else {
err;
}
if (!String.prototype.padStart) {
err;
} else {
ok;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"plugins": [
[
"@@/polyfill-corejs3",
{
"method": "usage-pure",
"version": "3.34"
}
]
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import _padStartInstanceProperty from "core-js-pure/stable/instance/pad-start.js";
_padStartInstanceProperty(String.prototype);
ok;
ok;
ok;
{
ok;
}
{
ok;
}

0 comments on commit ec83450

Please sign in to comment.