@babel/helper-replace-supers
Advanced tools
Comparing version 7.24.7 to 7.25.0
136
lib/index.js
@@ -7,9 +7,8 @@ "use strict"; | ||
exports.default = void 0; | ||
var _helperEnvironmentVisitor = require("@babel/helper-environment-visitor"); | ||
var _helperMemberExpressionToFunctions = require("@babel/helper-member-expression-to-functions"); | ||
var _helperOptimiseCallExpression = require("@babel/helper-optimise-call-expression"); | ||
var _core = require("@babel/core"); | ||
var _traverse = require("@babel/traverse"); | ||
const { | ||
assignmentExpression, | ||
booleanLiteral, | ||
callExpression, | ||
@@ -24,12 +23,11 @@ cloneNode, | ||
{ | ||
const ns = require("@babel/helper-environment-visitor"); | ||
exports.environmentVisitor = ns.default; | ||
exports.skipAllButComputedKey = ns.skipAllButComputedKey; | ||
exports.environmentVisitor = _traverse.visitors.environmentVisitor({}); | ||
exports.skipAllButComputedKey = function skipAllButComputedKey(path) { | ||
path.skip(); | ||
if (path.node.computed) { | ||
path.context.maybeQueue(path.get("key")); | ||
} | ||
}; | ||
} | ||
function getPrototypeOfExpression(objectRef, isStatic, file, isPrivateMethod) { | ||
objectRef = cloneNode(objectRef); | ||
const targetRef = isStatic || isPrivateMethod ? objectRef : memberExpression(objectRef, identifier("prototype")); | ||
return callExpression(file.addHelper("getPrototypeOf"), [targetRef]); | ||
} | ||
const visitor = _core.traverse.visitors.merge([_helperEnvironmentVisitor.default, { | ||
const visitor = _traverse.visitors.environmentVisitor({ | ||
Super(path, state) { | ||
@@ -45,4 +43,4 @@ const { | ||
} | ||
}]); | ||
const unshadowSuperBindingVisitor = _core.traverse.visitors.merge([_helperEnvironmentVisitor.default, { | ||
}); | ||
const unshadowSuperBindingVisitor = _traverse.visitors.environmentVisitor({ | ||
Scopable(path, { | ||
@@ -56,3 +54,3 @@ refName | ||
} | ||
}]); | ||
}); | ||
const specHandlers = { | ||
@@ -90,19 +88,97 @@ memoise(superMember, count) { | ||
}, | ||
_getPrototypeOfExpression() { | ||
const objectRef = cloneNode(this.getObjectRef()); | ||
const targetRef = this.isStatic || this.isPrivateMethod ? objectRef : memberExpression(objectRef, identifier("prototype")); | ||
return callExpression(this.file.addHelper("getPrototypeOf"), [targetRef]); | ||
}, | ||
get(superMember) { | ||
return this._get(superMember, this._getThisRefs()); | ||
const objectRef = cloneNode(this.getObjectRef()); | ||
return callExpression(this.file.addHelper("superPropGet"), [this.isDerivedConstructor ? sequenceExpression([thisExpression(), objectRef]) : objectRef, this.prop(superMember), thisExpression(), ...(this.isStatic || this.isPrivateMethod ? [] : [_core.types.numericLiteral(1)])]); | ||
}, | ||
_get(superMember, thisRefs) { | ||
const proto = getPrototypeOfExpression(this.getObjectRef(), this.isStatic, this.file, this.isPrivateMethod); | ||
return callExpression(this.file.addHelper("get"), [thisRefs.needAccessFirst ? sequenceExpression([thisRefs.this, proto]) : proto, this.prop(superMember), thisRefs.this]); | ||
_call(superMember, args, optional) { | ||
const objectRef = cloneNode(this.getObjectRef()); | ||
let argsNode; | ||
if (args.length === 1 && _core.types.isSpreadElement(args[0]) && (_core.types.isIdentifier(args[0].argument) || _core.types.isArrayExpression(args[0].argument))) { | ||
argsNode = args[0].argument; | ||
} else { | ||
argsNode = _core.types.arrayExpression(args); | ||
} | ||
const call = _core.types.callExpression(this.file.addHelper("superPropGet"), [this.isDerivedConstructor ? sequenceExpression([thisExpression(), objectRef]) : objectRef, this.prop(superMember), thisExpression(), _core.types.numericLiteral(2 | (this.isStatic || this.isPrivateMethod ? 0 : 1))]); | ||
if (optional) { | ||
return _core.types.optionalCallExpression(call, [argsNode], true); | ||
} | ||
return callExpression(call, [argsNode]); | ||
}, | ||
_getThisRefs() { | ||
return { | ||
needAccessFirst: this.isDerivedConstructor, | ||
this: thisExpression() | ||
}; | ||
set(superMember, value) { | ||
const objectRef = cloneNode(this.getObjectRef()); | ||
return callExpression(this.file.addHelper("superPropSet"), [this.isDerivedConstructor ? sequenceExpression([thisExpression(), objectRef]) : objectRef, this.prop(superMember), value, thisExpression(), _core.types.numericLiteral(superMember.isInStrictMode() ? 1 : 0), ...(this.isStatic || this.isPrivateMethod ? [] : [_core.types.numericLiteral(1)])]); | ||
}, | ||
destructureSet(superMember) { | ||
throw superMember.buildCodeFrameError(`Destructuring to a super field is not supported yet.`); | ||
}, | ||
call(superMember, args) { | ||
return this._call(superMember, args, false); | ||
}, | ||
optionalCall(superMember, args) { | ||
return this._call(superMember, args, true); | ||
}, | ||
delete(superMember) { | ||
if (superMember.node.computed) { | ||
return sequenceExpression([callExpression(this.file.addHelper("toPropertyKey"), [cloneNode(superMember.node.property)]), _core.template.expression.ast` | ||
function () { throw new ReferenceError("'delete super[expr]' is invalid"); }() | ||
`]); | ||
} else { | ||
return _core.template.expression.ast` | ||
function () { throw new ReferenceError("'delete super.prop' is invalid"); }() | ||
`; | ||
} | ||
} | ||
}; | ||
const specHandlers_old = { | ||
memoise(superMember, count) { | ||
const { | ||
scope, | ||
node | ||
} = superMember; | ||
const { | ||
computed, | ||
property | ||
} = node; | ||
if (!computed) { | ||
return; | ||
} | ||
const memo = scope.maybeGenerateMemoised(property); | ||
if (!memo) { | ||
return; | ||
} | ||
this.memoiser.set(property, memo, count); | ||
}, | ||
prop(superMember) { | ||
const { | ||
computed, | ||
property | ||
} = superMember.node; | ||
if (this.memoiser.has(property)) { | ||
return cloneNode(this.memoiser.get(property)); | ||
} | ||
if (computed) { | ||
return cloneNode(property); | ||
} | ||
return stringLiteral(property.name); | ||
}, | ||
_getPrototypeOfExpression() { | ||
const objectRef = cloneNode(this.getObjectRef()); | ||
const targetRef = this.isStatic || this.isPrivateMethod ? objectRef : memberExpression(objectRef, identifier("prototype")); | ||
return callExpression(this.file.addHelper("getPrototypeOf"), [targetRef]); | ||
}, | ||
get(superMember) { | ||
return this._get(superMember); | ||
}, | ||
_get(superMember) { | ||
const proto = this._getPrototypeOfExpression(); | ||
return callExpression(this.file.addHelper("get"), [this.isDerivedConstructor ? sequenceExpression([thisExpression(), proto]) : proto, this.prop(superMember), thisExpression()]); | ||
}, | ||
set(superMember, value) { | ||
const thisRefs = this._getThisRefs(); | ||
const proto = getPrototypeOfExpression(this.getObjectRef(), this.isStatic, this.file, this.isPrivateMethod); | ||
return callExpression(this.file.addHelper("set"), [thisRefs.needAccessFirst ? sequenceExpression([thisRefs.this, proto]) : proto, this.prop(superMember), value, thisRefs.this, booleanLiteral(superMember.isInStrictMode())]); | ||
const proto = this._getPrototypeOfExpression(); | ||
return callExpression(this.file.addHelper("set"), [this.isDerivedConstructor ? sequenceExpression([thisExpression(), proto]) : proto, this.prop(superMember), value, thisExpression(), _core.types.booleanLiteral(superMember.isInStrictMode())]); | ||
}, | ||
@@ -113,8 +189,6 @@ destructureSet(superMember) { | ||
call(superMember, args) { | ||
const thisRefs = this._getThisRefs(); | ||
return (0, _helperOptimiseCallExpression.default)(this._get(superMember, thisRefs), cloneNode(thisRefs.this), args, false); | ||
return (0, _helperOptimiseCallExpression.default)(this._get(superMember), thisExpression(), args, false); | ||
}, | ||
optionalCall(superMember, args) { | ||
const thisRefs = this._getThisRefs(); | ||
return (0, _helperOptimiseCallExpression.default)(this._get(superMember, thisRefs), cloneNode(thisRefs.this), args, true); | ||
return (0, _helperOptimiseCallExpression.default)(this._get(superMember), cloneNode(thisExpression()), args, true); | ||
}, | ||
@@ -215,3 +289,3 @@ delete(superMember) { | ||
} | ||
const handler = this.constantSuper ? looseHandlers : specHandlers; | ||
const handler = this.constantSuper ? looseHandlers : this.file.availableHelper("superPropSet") ? specHandlers : specHandlers_old; | ||
visitor.shouldSkip = path => { | ||
@@ -218,0 +292,0 @@ if (path.parentPath === methodPath) { |
{ | ||
"name": "@babel/helper-replace-supers", | ||
"version": "7.24.7", | ||
"version": "7.25.0", | ||
"description": "Helper function to replace supers", | ||
@@ -17,8 +17,8 @@ "repository": { | ||
"dependencies": { | ||
"@babel/helper-environment-visitor": "^7.24.7", | ||
"@babel/helper-member-expression-to-functions": "^7.24.7", | ||
"@babel/helper-optimise-call-expression": "^7.24.7" | ||
"@babel/helper-member-expression-to-functions": "^7.24.8", | ||
"@babel/helper-optimise-call-expression": "^7.24.7", | ||
"@babel/traverse": "^7.25.0" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.24.7" | ||
"@babel/core": "^7.24.9" | ||
}, | ||
@@ -25,0 +25,0 @@ "peerDependencies": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
106233
328
+ Added@babel/traverse@^7.25.0
- Removed@babel/helper-environment-visitor@7.24.7(transitive)