update
This commit is contained in:
+100
-69
@@ -3,61 +3,49 @@
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.skipAllButComputedKey = skipAllButComputedKey;
|
||||
exports.default = exports.environmentVisitor = void 0;
|
||||
exports.default = void 0;
|
||||
Object.defineProperty(exports, "environmentVisitor", {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return _helperEnvironmentVisitor.default;
|
||||
}
|
||||
});
|
||||
Object.defineProperty(exports, "skipAllButComputedKey", {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return _helperEnvironmentVisitor.skipAllButComputedKey;
|
||||
}
|
||||
});
|
||||
|
||||
var _traverse = _interopRequireDefault(require("@babel/traverse"));
|
||||
var _traverse = require("@babel/traverse");
|
||||
|
||||
var _helperMemberExpressionToFunctions = _interopRequireDefault(require("@babel/helper-member-expression-to-functions"));
|
||||
var _helperMemberExpressionToFunctions = require("@babel/helper-member-expression-to-functions");
|
||||
|
||||
var _helperOptimiseCallExpression = _interopRequireDefault(require("@babel/helper-optimise-call-expression"));
|
||||
var _helperOptimiseCallExpression = require("@babel/helper-optimise-call-expression");
|
||||
|
||||
var t = _interopRequireWildcard(require("@babel/types"));
|
||||
var _helperEnvironmentVisitor = require("@babel/helper-environment-visitor");
|
||||
|
||||
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
||||
var _t = require("@babel/types");
|
||||
|
||||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
const {
|
||||
assignmentExpression,
|
||||
booleanLiteral,
|
||||
callExpression,
|
||||
cloneNode,
|
||||
identifier,
|
||||
memberExpression,
|
||||
sequenceExpression,
|
||||
stringLiteral,
|
||||
thisExpression
|
||||
} = _t;
|
||||
|
||||
function getPrototypeOfExpression(objectRef, isStatic, file, isPrivateMethod) {
|
||||
objectRef = t.cloneNode(objectRef);
|
||||
const targetRef = isStatic || isPrivateMethod ? objectRef : t.memberExpression(objectRef, t.identifier("prototype"));
|
||||
return t.callExpression(file.addHelper("getPrototypeOf"), [targetRef]);
|
||||
objectRef = cloneNode(objectRef);
|
||||
const targetRef = isStatic || isPrivateMethod ? objectRef : memberExpression(objectRef, identifier("prototype"));
|
||||
return callExpression(file.addHelper("getPrototypeOf"), [targetRef]);
|
||||
}
|
||||
|
||||
function skipAllButComputedKey(path) {
|
||||
if (!path.node.computed) {
|
||||
path.skip();
|
||||
return;
|
||||
}
|
||||
|
||||
const keys = t.VISITOR_KEYS[path.type];
|
||||
|
||||
for (const key of keys) {
|
||||
if (key !== "key") path.skipKey(key);
|
||||
}
|
||||
}
|
||||
|
||||
const environmentVisitor = {
|
||||
TypeAnnotation(path) {
|
||||
path.skip();
|
||||
},
|
||||
|
||||
Function(path) {
|
||||
if (path.isMethod()) return;
|
||||
if (path.isArrowFunctionExpression()) return;
|
||||
path.skip();
|
||||
},
|
||||
|
||||
"Method|ClassProperty|ClassPrivateProperty"(path) {
|
||||
skipAllButComputedKey(path);
|
||||
}
|
||||
|
||||
};
|
||||
exports.environmentVisitor = environmentVisitor;
|
||||
|
||||
const visitor = _traverse.default.visitors.merge([environmentVisitor, {
|
||||
const visitor = _traverse.default.visitors.merge([_helperEnvironmentVisitor.default, {
|
||||
Super(path, state) {
|
||||
const {
|
||||
node,
|
||||
@@ -71,6 +59,19 @@ const visitor = _traverse.default.visitors.merge([environmentVisitor, {
|
||||
|
||||
}]);
|
||||
|
||||
const unshadowSuperBindingVisitor = _traverse.default.visitors.merge([_helperEnvironmentVisitor.default, {
|
||||
Scopable(path, {
|
||||
refName
|
||||
}) {
|
||||
const binding = path.scope.getOwnBinding(refName);
|
||||
|
||||
if (binding && binding.identifier.name === refName) {
|
||||
path.scope.rename(refName);
|
||||
}
|
||||
}
|
||||
|
||||
}]);
|
||||
|
||||
const specHandlers = {
|
||||
memoise(superMember, count) {
|
||||
const {
|
||||
@@ -102,14 +103,14 @@ const specHandlers = {
|
||||
} = superMember.node;
|
||||
|
||||
if (this.memoiser.has(property)) {
|
||||
return t.cloneNode(this.memoiser.get(property));
|
||||
return cloneNode(this.memoiser.get(property));
|
||||
}
|
||||
|
||||
if (computed) {
|
||||
return t.cloneNode(property);
|
||||
return cloneNode(property);
|
||||
}
|
||||
|
||||
return t.stringLiteral(property.name);
|
||||
return stringLiteral(property.name);
|
||||
},
|
||||
|
||||
get(superMember) {
|
||||
@@ -118,20 +119,20 @@ const specHandlers = {
|
||||
|
||||
_get(superMember, thisRefs) {
|
||||
const proto = getPrototypeOfExpression(this.getObjectRef(), this.isStatic, this.file, this.isPrivateMethod);
|
||||
return t.callExpression(this.file.addHelper("get"), [thisRefs.memo ? t.sequenceExpression([thisRefs.memo, proto]) : proto, this.prop(superMember), thisRefs.this]);
|
||||
return callExpression(this.file.addHelper("get"), [thisRefs.memo ? sequenceExpression([thisRefs.memo, proto]) : proto, this.prop(superMember), thisRefs.this]);
|
||||
},
|
||||
|
||||
_getThisRefs() {
|
||||
if (!this.isDerivedConstructor) {
|
||||
return {
|
||||
this: t.thisExpression()
|
||||
this: thisExpression()
|
||||
};
|
||||
}
|
||||
|
||||
const thisRef = this.scope.generateDeclaredUidIdentifier("thisSuper");
|
||||
return {
|
||||
memo: t.assignmentExpression("=", thisRef, t.thisExpression()),
|
||||
this: t.cloneNode(thisRef)
|
||||
memo: assignmentExpression("=", thisRef, thisExpression()),
|
||||
this: cloneNode(thisRef)
|
||||
};
|
||||
},
|
||||
|
||||
@@ -139,7 +140,7 @@ const specHandlers = {
|
||||
const thisRefs = this._getThisRefs();
|
||||
|
||||
const proto = getPrototypeOfExpression(this.getObjectRef(), this.isStatic, this.file, this.isPrivateMethod);
|
||||
return t.callExpression(this.file.addHelper("set"), [thisRefs.memo ? t.sequenceExpression([thisRefs.memo, proto]) : proto, this.prop(superMember), value, thisRefs.this, t.booleanLiteral(superMember.isInStrictMode())]);
|
||||
return callExpression(this.file.addHelper("set"), [thisRefs.memo ? sequenceExpression([thisRefs.memo, proto]) : proto, this.prop(superMember), value, thisRefs.this, booleanLiteral(superMember.isInStrictMode())]);
|
||||
},
|
||||
|
||||
destructureSet(superMember) {
|
||||
@@ -149,7 +150,13 @@ const specHandlers = {
|
||||
call(superMember, args) {
|
||||
const thisRefs = this._getThisRefs();
|
||||
|
||||
return (0, _helperOptimiseCallExpression.default)(this._get(superMember, thisRefs), t.cloneNode(thisRefs.this), args, false);
|
||||
return (0, _helperOptimiseCallExpression.default)(this._get(superMember, thisRefs), cloneNode(thisRefs.this), args, false);
|
||||
},
|
||||
|
||||
optionalCall(superMember, args) {
|
||||
const thisRefs = this._getThisRefs();
|
||||
|
||||
return (0, _helperOptimiseCallExpression.default)(this._get(superMember, thisRefs), cloneNode(thisRefs.this), args, true);
|
||||
}
|
||||
|
||||
};
|
||||
@@ -160,16 +167,16 @@ const looseHandlers = Object.assign({}, specHandlers, {
|
||||
} = superMember.node;
|
||||
|
||||
if (this.memoiser.has(property)) {
|
||||
return t.cloneNode(this.memoiser.get(property));
|
||||
return cloneNode(this.memoiser.get(property));
|
||||
}
|
||||
|
||||
return t.cloneNode(property);
|
||||
return cloneNode(property);
|
||||
},
|
||||
|
||||
get(superMember) {
|
||||
const {
|
||||
isStatic,
|
||||
superRef
|
||||
getSuperRef
|
||||
} = this;
|
||||
const {
|
||||
computed
|
||||
@@ -178,12 +185,16 @@ const looseHandlers = Object.assign({}, specHandlers, {
|
||||
let object;
|
||||
|
||||
if (isStatic) {
|
||||
object = superRef ? t.cloneNode(superRef) : t.memberExpression(t.identifier("Function"), t.identifier("prototype"));
|
||||
var _getSuperRef;
|
||||
|
||||
object = (_getSuperRef = getSuperRef()) != null ? _getSuperRef : memberExpression(identifier("Function"), identifier("prototype"));
|
||||
} else {
|
||||
object = superRef ? t.memberExpression(t.cloneNode(superRef), t.identifier("prototype")) : t.memberExpression(t.identifier("Object"), t.identifier("prototype"));
|
||||
var _getSuperRef2;
|
||||
|
||||
object = memberExpression((_getSuperRef2 = getSuperRef()) != null ? _getSuperRef2 : identifier("Object"), identifier("prototype"));
|
||||
}
|
||||
|
||||
return t.memberExpression(object, prop, computed);
|
||||
return memberExpression(object, prop, computed);
|
||||
},
|
||||
|
||||
set(superMember, value) {
|
||||
@@ -191,7 +202,7 @@ const looseHandlers = Object.assign({}, specHandlers, {
|
||||
computed
|
||||
} = superMember.node;
|
||||
const prop = this.prop(superMember);
|
||||
return t.assignmentExpression("=", t.memberExpression(t.thisExpression(), prop, computed), value);
|
||||
return assignmentExpression("=", memberExpression(thisExpression(), prop, computed), value);
|
||||
},
|
||||
|
||||
destructureSet(superMember) {
|
||||
@@ -199,36 +210,55 @@ const looseHandlers = Object.assign({}, specHandlers, {
|
||||
computed
|
||||
} = superMember.node;
|
||||
const prop = this.prop(superMember);
|
||||
return t.memberExpression(t.thisExpression(), prop, computed);
|
||||
return memberExpression(thisExpression(), prop, computed);
|
||||
},
|
||||
|
||||
call(superMember, args) {
|
||||
return (0, _helperOptimiseCallExpression.default)(this.get(superMember), t.thisExpression(), args, false);
|
||||
return (0, _helperOptimiseCallExpression.default)(this.get(superMember), thisExpression(), args, false);
|
||||
},
|
||||
|
||||
optionalCall(superMember, args) {
|
||||
return (0, _helperOptimiseCallExpression.default)(this.get(superMember), thisExpression(), args, true);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
class ReplaceSupers {
|
||||
constructor(opts) {
|
||||
var _opts$constantSuper;
|
||||
|
||||
const path = opts.methodPath;
|
||||
this.methodPath = path;
|
||||
this.isDerivedConstructor = path.isClassMethod({
|
||||
kind: "constructor"
|
||||
}) && !!opts.superRef;
|
||||
this.isStatic = path.isObjectMethod() || path.node.static;
|
||||
this.isStatic = path.isObjectMethod() || path.node.static || (path.isStaticBlock == null ? void 0 : path.isStaticBlock());
|
||||
this.isPrivateMethod = path.isPrivate() && path.isMethod();
|
||||
this.file = opts.file;
|
||||
this.superRef = opts.superRef;
|
||||
this.isLoose = opts.isLoose;
|
||||
this.constantSuper = (_opts$constantSuper = opts.constantSuper) != null ? _opts$constantSuper : opts.isLoose;
|
||||
this.opts = opts;
|
||||
}
|
||||
|
||||
getObjectRef() {
|
||||
return t.cloneNode(this.opts.objectRef || this.opts.getObjectRef());
|
||||
return cloneNode(this.opts.objectRef || this.opts.getObjectRef());
|
||||
}
|
||||
|
||||
getSuperRef() {
|
||||
if (this.opts.superRef) return cloneNode(this.opts.superRef);
|
||||
|
||||
if (this.opts.getSuperRef) {
|
||||
return cloneNode(this.opts.getSuperRef());
|
||||
}
|
||||
}
|
||||
|
||||
replace() {
|
||||
const handler = this.isLoose ? looseHandlers : specHandlers;
|
||||
if (this.opts.refToPreserve) {
|
||||
this.methodPath.traverse(unshadowSuperBindingVisitor, {
|
||||
refName: this.opts.refToPreserve.name
|
||||
});
|
||||
}
|
||||
|
||||
const handler = this.constantSuper ? looseHandlers : specHandlers;
|
||||
(0, _helperMemberExpressionToFunctions.default)(this.methodPath, visitor, Object.assign({
|
||||
file: this.file,
|
||||
scope: this.methodPath.scope,
|
||||
@@ -236,7 +266,8 @@ class ReplaceSupers {
|
||||
isStatic: this.isStatic,
|
||||
isPrivateMethod: this.isPrivateMethod,
|
||||
getObjectRef: this.getObjectRef.bind(this),
|
||||
superRef: this.superRef
|
||||
getSuperRef: this.getSuperRef.bind(this),
|
||||
boundGet: handler.get
|
||||
}, handler));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user