Befor generating
This commit is contained in:
1 parent
3383f4bf4a
commit
1aa1b5f625
6756 files changed
+649946
-1
No files matched your search
+22
@@ -0,0 +1,22 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2014-present Sebastian McKenzie and other contributors
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
"Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
# @babel/helper-replace-supers
|
||||
|
||||
> Helper function to replace supers
|
||||
|
||||
See our website [@babel/helper-replace-supers](https://babeljs.io/docs/babel-helper-replace-supers) for more information.
|
||||
|
||||
## Install
|
||||
|
||||
Using npm:
|
||||
|
||||
```sh
|
||||
npm install --save @babel/helper-replace-supers
|
||||
```
|
||||
|
||||
or using yarn:
|
||||
|
||||
```sh
|
||||
yarn add @babel/helper-replace-supers
|
||||
```
|
||||
+303
@@ -0,0 +1,303 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = void 0;
|
||||
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,
|
||||
callExpression,
|
||||
cloneNode,
|
||||
identifier,
|
||||
memberExpression,
|
||||
sequenceExpression,
|
||||
stringLiteral,
|
||||
thisExpression
|
||||
} = _core.types;
|
||||
exports.environmentVisitor = _traverse.visitors.environmentVisitor({});
|
||||
exports.skipAllButComputedKey = function skipAllButComputedKey(path) {
|
||||
path.skip();
|
||||
if (path.node.computed) {
|
||||
path.context.maybeQueue(path.get("key"));
|
||||
}
|
||||
};
|
||||
const visitor = _traverse.visitors.environmentVisitor({
|
||||
Super(path, state) {
|
||||
const {
|
||||
node,
|
||||
parentPath
|
||||
} = path;
|
||||
if (!parentPath.isMemberExpression({
|
||||
object: node
|
||||
})) return;
|
||||
state.handle(parentPath);
|
||||
}
|
||||
});
|
||||
const unshadowSuperBindingVisitor = _traverse.visitors.environmentVisitor({
|
||||
Scopable(path, {
|
||||
refName
|
||||
}) {
|
||||
const binding = path.scope.getOwnBinding(refName);
|
||||
if ((binding == null ? void 0 : binding.identifier.name) === refName) {
|
||||
path.scope.rename(refName);
|
||||
}
|
||||
}
|
||||
});
|
||||
const specHandlers = {
|
||||
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) {
|
||||
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)])]);
|
||||
},
|
||||
_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]);
|
||||
},
|
||||
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 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())]);
|
||||
},
|
||||
destructureSet(superMember) {
|
||||
throw superMember.buildCodeFrameError(`Destructuring to a super field is not supported yet.`);
|
||||
},
|
||||
call(superMember, args) {
|
||||
return (0, _helperOptimiseCallExpression.default)(this._get(superMember), thisExpression(), args, false);
|
||||
},
|
||||
optionalCall(superMember, args) {
|
||||
return (0, _helperOptimiseCallExpression.default)(this._get(superMember), cloneNode(thisExpression()), 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 looseHandlers = Object.assign({}, specHandlers, {
|
||||
prop(superMember) {
|
||||
const {
|
||||
property
|
||||
} = superMember.node;
|
||||
if (this.memoiser.has(property)) {
|
||||
return cloneNode(this.memoiser.get(property));
|
||||
}
|
||||
return cloneNode(property);
|
||||
},
|
||||
get(superMember) {
|
||||
const {
|
||||
isStatic,
|
||||
getSuperRef
|
||||
} = this;
|
||||
const {
|
||||
computed
|
||||
} = superMember.node;
|
||||
const prop = this.prop(superMember);
|
||||
let object;
|
||||
if (isStatic) {
|
||||
var _getSuperRef;
|
||||
object = (_getSuperRef = getSuperRef()) != null ? _getSuperRef : memberExpression(identifier("Function"), identifier("prototype"));
|
||||
} else {
|
||||
var _getSuperRef2;
|
||||
object = memberExpression((_getSuperRef2 = getSuperRef()) != null ? _getSuperRef2 : identifier("Object"), identifier("prototype"));
|
||||
}
|
||||
return memberExpression(object, prop, computed);
|
||||
},
|
||||
set(superMember, value) {
|
||||
const {
|
||||
computed
|
||||
} = superMember.node;
|
||||
const prop = this.prop(superMember);
|
||||
return assignmentExpression("=", memberExpression(thisExpression(), prop, computed), value);
|
||||
},
|
||||
destructureSet(superMember) {
|
||||
const {
|
||||
computed
|
||||
} = superMember.node;
|
||||
const prop = this.prop(superMember);
|
||||
return memberExpression(thisExpression(), prop, computed);
|
||||
},
|
||||
call(superMember, args) {
|
||||
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 || (path.isStaticBlock == null ? void 0 : path.isStaticBlock());
|
||||
this.isPrivateMethod = path.isPrivate() && path.isMethod();
|
||||
this.file = opts.file;
|
||||
this.constantSuper = (_opts$constantSuper = opts.constantSuper) != null ? _opts$constantSuper : opts.isLoose;
|
||||
this.opts = 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 {
|
||||
methodPath
|
||||
} = this;
|
||||
if (this.opts.refToPreserve) {
|
||||
methodPath.traverse(unshadowSuperBindingVisitor, {
|
||||
refName: this.opts.refToPreserve.name
|
||||
});
|
||||
}
|
||||
const handler = this.constantSuper ? looseHandlers : this.file.availableHelper("superPropSet") ? specHandlers : specHandlers_old;
|
||||
visitor.shouldSkip = path => {
|
||||
if (path.parentPath === methodPath) {
|
||||
if (path.parentKey === "decorators" || path.parentKey === "key") {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
};
|
||||
(0, _helperMemberExpressionToFunctions.default)(methodPath, visitor, Object.assign({
|
||||
file: this.file,
|
||||
scope: this.methodPath.scope,
|
||||
isDerivedConstructor: this.isDerivedConstructor,
|
||||
isStatic: this.isStatic,
|
||||
isPrivateMethod: this.isPrivateMethod,
|
||||
getObjectRef: this.getObjectRef.bind(this),
|
||||
getSuperRef: this.getSuperRef.bind(this),
|
||||
boundGet: handler.get
|
||||
}, handler));
|
||||
}
|
||||
}
|
||||
exports.default = ReplaceSupers;
|
||||
|
||||
//# sourceMappingURL=index.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"names":["_helperMemberExpressionToFunctions","require","_helperOptimiseCallExpression","_core","_traverse","assignmentExpression","callExpression","cloneNode","identifier","memberExpression","sequenceExpression","stringLiteral","thisExpression","t","exports","environmentVisitor","visitors","skipAllButComputedKey","path","skip","node","computed","context","maybeQueue","get","visitor","Super","state","parentPath","isMemberExpression","object","handle","unshadowSuperBindingVisitor","Scopable","refName","binding","scope","getOwnBinding","name","rename","specHandlers","memoise","superMember","count","property","memo","maybeGenerateMemoised","memoiser","set","prop","has","_getPrototypeOfExpression","objectRef","getObjectRef","targetRef","isStatic","isPrivateMethod","file","addHelper","isDerivedConstructor","numericLiteral","_call","args","optional","argsNode","length","isSpreadElement","isIdentifier","argument","isArrayExpression","arrayExpression","call","optionalCallExpression","value","isInStrictMode","destructureSet","buildCodeFrameError","optionalCall","delete","template","expression","ast","specHandlers_old","_get","proto","booleanLiteral","optimiseCall","looseHandlers","Object","assign","getSuperRef","_getSuperRef","_getSuperRef2","ReplaceSupers","constructor","opts","_opts$constantSuper","methodPath","isClassMethod","kind","superRef","isObjectMethod","static","isStaticBlock","isPrivate","isMethod","constantSuper","isLoose","replace","refToPreserve","traverse","handler","availableHelper","shouldSkip","parentKey","memberExpressionToFunctions","bind","boundGet","default"],"sources":["../src/index.ts"],"sourcesContent":["import type { File, NodePath, Scope } from \"@babel/core\";\nimport memberExpressionToFunctions from \"@babel/helper-member-expression-to-functions\";\nimport type { HandlerState } from \"@babel/helper-member-expression-to-functions\";\nimport optimiseCall from \"@babel/helper-optimise-call-expression\";\nimport { template, types as t } from \"@babel/core\";\nimport { visitors } from \"@babel/traverse\";\nconst {\n assignmentExpression,\n callExpression,\n cloneNode,\n identifier,\n memberExpression,\n sequenceExpression,\n stringLiteral,\n thisExpression,\n} = t;\n\nif (!process.env.BABEL_8_BREAKING && !USE_ESM && !IS_STANDALONE) {\n // eslint-disable-next-line no-restricted-globals\n exports.environmentVisitor = visitors.environmentVisitor({});\n // eslint-disable-next-line no-restricted-globals\n exports.skipAllButComputedKey = function skipAllButComputedKey(\n path: NodePath<t.Method | t.ClassProperty>,\n ) {\n path.skip();\n if (path.node.computed) {\n path.context.maybeQueue(path.get(\"key\"));\n }\n };\n}\n\nconst visitor = visitors.environmentVisitor<\n HandlerState<ReplaceState> & ReplaceState\n>({\n Super(path, state) {\n const { node, parentPath } = path;\n if (!parentPath.isMemberExpression({ object: node })) return;\n state.handle(parentPath);\n },\n});\n\nconst unshadowSuperBindingVisitor = visitors.environmentVisitor<{\n refName: string;\n}>({\n Scopable(path, { refName }) {\n // https://github.com/Zzzen/babel/pull/1#pullrequestreview-564833183\n const binding = path.scope.getOwnBinding(refName);\n if (binding?.identifier.name === refName) {\n path.scope.rename(refName);\n }\n },\n});\n\ntype SharedState = {\n file: File;\n scope: Scope;\n isDerivedConstructor: boolean;\n isStatic: boolean;\n isPrivateMethod: boolean;\n getObjectRef: () => t.Identifier;\n getSuperRef: () => t.Identifier;\n // we dont need boundGet here, but memberExpressionToFunctions handler needs it.\n boundGet: HandlerState[\"get\"];\n};\n\ntype Handler = HandlerState<SharedState> & SharedState;\ntype SuperMember = NodePath<\n t.MemberExpression & {\n object: t.Super;\n property: Exclude<t.MemberExpression[\"property\"], t.PrivateName>;\n }\n>;\n\nconst enum Flags {\n Prototype = 0b1,\n Call = 0b10,\n}\n\ninterface SpecHandler\n extends Pick<\n Handler,\n | \"memoise\"\n | \"get\"\n | \"set\"\n | \"destructureSet\"\n | \"call\"\n | \"optionalCall\"\n | \"delete\"\n > {\n _get?(\n this: Handler & SpecHandler,\n superMember: SuperMember,\n ): t.CallExpression;\n _call?(\n this: Handler & SpecHandler,\n superMember: SuperMember,\n args: t.CallExpression[\"arguments\"],\n optional: boolean,\n ): t.CallExpression | t.OptionalCallExpression;\n _getPrototypeOfExpression(this: Handler & SpecHandler): t.CallExpression;\n prop(this: Handler & SpecHandler, superMember: SuperMember): t.Expression;\n}\n\nconst specHandlers: SpecHandler = {\n memoise(\n this: Handler & SpecHandler,\n superMember: SuperMember,\n count: number,\n ) {\n const { scope, node } = superMember;\n const { computed, property } = node;\n if (!computed) {\n return;\n }\n\n const memo = scope.maybeGenerateMemoised(property);\n if (!memo) {\n return;\n }\n\n this.Line truncated
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
{
|
||||
"name": "@babel/helper-replace-supers",
|
||||
"version": "7.29.7",
|
||||
"description": "Helper function to replace supers",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/babel/babel.git",
|
||||
"directory": "packages/babel-helper-replace-supers"
|
||||
},
|
||||
"homepage": "https://babel.dev/docs/en/next/babel-helper-replace-supers",
|
||||
"license": "MIT",
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"main": "./lib/index.js",
|
||||
"dependencies": {
|
||||
"@babel/helper-member-expression-to-functions": "^7.29.7",
|
||||
"@babel/helper-optimise-call-expression": "^7.29.7",
|
||||
"@babel/traverse": "^7.29.7"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.29.7"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@babel/core": "^7.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=6.9.0"
|
||||
},
|
||||
"author": "The Babel Team (https://babel.dev/team)",
|
||||
"type": "commonjs"
|
||||
}
|
||||
Reference in new issue
Block a user