update
This commit is contained in:
+3
-3
@@ -2,18 +2,18 @@
|
||||
|
||||
> Helper function to replace supers
|
||||
|
||||
See our website [@babel/helper-replace-supers](https://babeljs.io/docs/en/next/babel-helper-replace-supers.html) for more information.
|
||||
See our website [@babel/helper-replace-supers](https://babeljs.io/docs/en/babel-helper-replace-supers) for more information.
|
||||
|
||||
## Install
|
||||
|
||||
Using npm:
|
||||
|
||||
```sh
|
||||
npm install --save-dev @babel/helper-replace-supers
|
||||
npm install --save @babel/helper-replace-supers
|
||||
```
|
||||
|
||||
or using yarn:
|
||||
|
||||
```sh
|
||||
yarn add @babel/helper-replace-supers --dev
|
||||
yarn add @babel/helper-replace-supers
|
||||
```
|
||||
|
||||
+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));
|
||||
}
|
||||
|
||||
|
||||
+28
-20
@@ -1,50 +1,57 @@
|
||||
{
|
||||
"_args": [
|
||||
[
|
||||
"@babel/helper-replace-supers@7.10.4",
|
||||
"/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series"
|
||||
"@babel/helper-replace-supers@7.18.6",
|
||||
"/home/node/nuxt"
|
||||
]
|
||||
],
|
||||
"_from": "@babel/helper-replace-supers@7.10.4",
|
||||
"_id": "@babel/helper-replace-supers@7.10.4",
|
||||
"_from": "@babel/helper-replace-supers@7.18.6",
|
||||
"_id": "@babel/helper-replace-supers@7.18.6",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-sPxZfFXocEymYTdVK1UNmFPBN+Hv5mJkLPsYWwGBxZAxaWfFu+xqp7b6qWD0yjNuNL2VKc6L5M18tOXUP7NU0A==",
|
||||
"_integrity": "sha512-fTf7zoXnUGl9gF25fXCWE26t7Tvtyn6H4hkLSYhATwJvw2uYxd3aoXplMSe0g9XbwK7bmxNes7+FGO0rB/xC0g==",
|
||||
"_location": "/@babel/helper-replace-supers",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "version",
|
||||
"registry": true,
|
||||
"raw": "@babel/helper-replace-supers@7.10.4",
|
||||
"raw": "@babel/helper-replace-supers@7.18.6",
|
||||
"name": "@babel/helper-replace-supers",
|
||||
"escapedName": "@babel%2fhelper-replace-supers",
|
||||
"scope": "@babel",
|
||||
"rawSpec": "7.10.4",
|
||||
"rawSpec": "7.18.6",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "7.10.4"
|
||||
"fetchSpec": "7.18.6"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/@babel/helper-create-class-features-plugin",
|
||||
"/@babel/helper-module-transforms",
|
||||
"/@babel/plugin-proposal-decorators",
|
||||
"/@babel/plugin-transform-classes",
|
||||
"/@babel/plugin-transform-object-super"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/@babel/helper-replace-supers/-/helper-replace-supers-7.10.4.tgz",
|
||||
"_spec": "7.10.4",
|
||||
"_where": "/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series",
|
||||
"_resolved": "https://registry.npmjs.org/@babel/helper-replace-supers/-/helper-replace-supers-7.18.6.tgz",
|
||||
"_spec": "7.18.6",
|
||||
"_where": "/home/node/nuxt",
|
||||
"author": {
|
||||
"name": "The Babel Team",
|
||||
"url": "https://babel.dev/team"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/babel/babel/issues"
|
||||
},
|
||||
"dependencies": {
|
||||
"@babel/helper-member-expression-to-functions": "^7.10.4",
|
||||
"@babel/helper-optimise-call-expression": "^7.10.4",
|
||||
"@babel/traverse": "^7.10.4",
|
||||
"@babel/types": "^7.10.4"
|
||||
"@babel/helper-environment-visitor": "^7.18.6",
|
||||
"@babel/helper-member-expression-to-functions": "^7.18.6",
|
||||
"@babel/helper-optimise-call-expression": "^7.18.6",
|
||||
"@babel/traverse": "^7.18.6",
|
||||
"@babel/types": "^7.18.6"
|
||||
},
|
||||
"description": "Helper function to replace supers",
|
||||
"gitHead": "7fd40d86a0d03ff0e9c3ea16b29689945433d4df",
|
||||
"homepage": "https://github.com/babel/babel#readme",
|
||||
"engines": {
|
||||
"node": ">=6.9.0"
|
||||
},
|
||||
"homepage": "https://babel.dev/docs/en/next/babel-helper-replace-supers",
|
||||
"license": "MIT",
|
||||
"main": "lib/index.js",
|
||||
"main": "./lib/index.js",
|
||||
"name": "@babel/helper-replace-supers",
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
@@ -54,5 +61,6 @@
|
||||
"url": "git+https://github.com/babel/babel.git",
|
||||
"directory": "packages/babel-helper-replace-supers"
|
||||
},
|
||||
"version": "7.10.4"
|
||||
"type": "commonjs",
|
||||
"version": "7.18.6"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user