update
This commit is contained in:
+3
-3
@@ -2,18 +2,18 @@
|
||||
|
||||
> Helper function to replace certain member expressions with function calls
|
||||
|
||||
See our website [@babel/helper-member-expression-to-functions](https://babeljs.io/docs/en/next/babel-helper-member-expression-to-functions.html) for more information.
|
||||
See our website [@babel/helper-member-expression-to-functions](https://babeljs.io/docs/en/babel-helper-member-expression-to-functions) for more information.
|
||||
|
||||
## Install
|
||||
|
||||
Using npm:
|
||||
|
||||
```sh
|
||||
npm install --save-dev @babel/helper-member-expression-to-functions
|
||||
npm install --save @babel/helper-member-expression-to-functions
|
||||
```
|
||||
|
||||
or using yarn:
|
||||
|
||||
```sh
|
||||
yarn add @babel/helper-member-expression-to-functions --dev
|
||||
yarn add @babel/helper-member-expression-to-functions
|
||||
```
|
||||
|
||||
+186
-62
@@ -1,18 +1,94 @@
|
||||
"use strict";
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = memberExpressionToFunctions;
|
||||
Object.defineProperty(exports, '__esModule', { value: true });
|
||||
|
||||
var t = _interopRequireWildcard(require("@babel/types"));
|
||||
var _t = require('@babel/types');
|
||||
|
||||
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
||||
function _interopNamespace(e) {
|
||||
if (e && e.__esModule) return e;
|
||||
var n = Object.create(null);
|
||||
if (e) {
|
||||
Object.keys(e).forEach(function (k) {
|
||||
if (k !== 'default') {
|
||||
var d = Object.getOwnPropertyDescriptor(e, k);
|
||||
Object.defineProperty(n, k, d.get ? d : {
|
||||
enumerable: true,
|
||||
get: function () { return e[k]; }
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
n["default"] = e;
|
||||
return Object.freeze(n);
|
||||
}
|
||||
|
||||
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; }
|
||||
var _t__namespace = /*#__PURE__*/_interopNamespace(_t);
|
||||
|
||||
function willPathCastToBoolean(path) {
|
||||
const maybeWrapped = path;
|
||||
const {
|
||||
node,
|
||||
parentPath
|
||||
} = maybeWrapped;
|
||||
|
||||
if (parentPath.isLogicalExpression()) {
|
||||
const {
|
||||
operator,
|
||||
right
|
||||
} = parentPath.node;
|
||||
|
||||
if (operator === "&&" || operator === "||" || operator === "??" && node === right) {
|
||||
return willPathCastToBoolean(parentPath);
|
||||
}
|
||||
}
|
||||
|
||||
if (parentPath.isSequenceExpression()) {
|
||||
const {
|
||||
expressions
|
||||
} = parentPath.node;
|
||||
|
||||
if (expressions[expressions.length - 1] === node) {
|
||||
return willPathCastToBoolean(parentPath);
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return parentPath.isConditional({
|
||||
test: node
|
||||
}) || parentPath.isUnaryExpression({
|
||||
operator: "!"
|
||||
}) || parentPath.isLoop({
|
||||
test: node
|
||||
});
|
||||
}
|
||||
|
||||
const {
|
||||
LOGICAL_OPERATORS,
|
||||
arrowFunctionExpression,
|
||||
assignmentExpression,
|
||||
binaryExpression,
|
||||
booleanLiteral,
|
||||
callExpression,
|
||||
cloneNode,
|
||||
conditionalExpression,
|
||||
identifier,
|
||||
isMemberExpression,
|
||||
isOptionalCallExpression,
|
||||
isOptionalMemberExpression,
|
||||
isUpdateExpression,
|
||||
logicalExpression,
|
||||
memberExpression,
|
||||
nullLiteral,
|
||||
optionalCallExpression,
|
||||
optionalMemberExpression,
|
||||
sequenceExpression,
|
||||
updateExpression
|
||||
} = _t__namespace;
|
||||
|
||||
class AssignmentMemoiser {
|
||||
constructor() {
|
||||
this._map = void 0;
|
||||
this._map = new WeakMap();
|
||||
}
|
||||
|
||||
@@ -31,7 +107,7 @@ class AssignmentMemoiser {
|
||||
record.count--;
|
||||
|
||||
if (record.count === 0) {
|
||||
return t.assignmentExpression("=", value, key);
|
||||
return assignmentExpression("=", value, key);
|
||||
}
|
||||
|
||||
return value;
|
||||
@@ -51,23 +127,21 @@ function toNonOptional(path, base) {
|
||||
node
|
||||
} = path;
|
||||
|
||||
if (path.isOptionalMemberExpression()) {
|
||||
return t.memberExpression(base, node.property, node.computed);
|
||||
if (isOptionalMemberExpression(node)) {
|
||||
return memberExpression(base, node.property, node.computed);
|
||||
}
|
||||
|
||||
if (path.isOptionalCallExpression()) {
|
||||
const callee = path.get("callee");
|
||||
|
||||
if (path.node.optional && callee.isOptionalMemberExpression()) {
|
||||
const {
|
||||
object
|
||||
} = callee.node;
|
||||
const context = path.scope.maybeGenerateMemoised(object) || object;
|
||||
callee.get("object").replaceWith(t.assignmentExpression("=", context, object));
|
||||
return t.callExpression(t.memberExpression(base, t.identifier("call")), [context, ...node.arguments]);
|
||||
const object = callee.node.object;
|
||||
const context = path.scope.maybeGenerateMemoised(object);
|
||||
callee.get("object").replaceWith(assignmentExpression("=", context, object));
|
||||
return callExpression(memberExpression(base, identifier("call")), [context, ...path.node.arguments]);
|
||||
}
|
||||
|
||||
return t.callExpression(base, node.arguments);
|
||||
return callExpression(base, path.node.arguments);
|
||||
}
|
||||
|
||||
return path.node;
|
||||
@@ -84,7 +158,9 @@ function isInDetachedTree(path) {
|
||||
const parentNode = parentPath.node;
|
||||
|
||||
if (listKey) {
|
||||
if (container !== parentNode[listKey]) return true;
|
||||
if (container !== parentNode[listKey]) {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
if (container !== parentNode) return true;
|
||||
}
|
||||
@@ -98,30 +174,37 @@ function isInDetachedTree(path) {
|
||||
const handle = {
|
||||
memoise() {},
|
||||
|
||||
handle(member) {
|
||||
handle(member, noDocumentAll) {
|
||||
const {
|
||||
node,
|
||||
parent,
|
||||
parentPath
|
||||
parentPath,
|
||||
scope
|
||||
} = member;
|
||||
|
||||
if (member.isOptionalMemberExpression()) {
|
||||
if (isInDetachedTree(member)) return;
|
||||
const endPath = member.find(({
|
||||
node,
|
||||
parent,
|
||||
parentPath
|
||||
parent
|
||||
}) => {
|
||||
if (parentPath.isOptionalMemberExpression()) {
|
||||
if (isOptionalMemberExpression(parent)) {
|
||||
return parent.optional || parent.object !== node;
|
||||
}
|
||||
|
||||
if (parentPath.isOptionalCallExpression()) {
|
||||
if (isOptionalCallExpression(parent)) {
|
||||
return node !== member.node && parent.optional || parent.callee !== node;
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
if (scope.path.isPattern()) {
|
||||
endPath.replaceWith(callExpression(arrowFunctionExpression([], endPath.node), []));
|
||||
return;
|
||||
}
|
||||
|
||||
const willEndPathCastToBoolean = willPathCastToBoolean(endPath);
|
||||
const rootParentPath = endPath.parentPath;
|
||||
|
||||
if (rootParentPath.isUpdateExpression({
|
||||
@@ -156,22 +239,21 @@ const handle = {
|
||||
throw new Error(`Internal error: unexpected ${startingOptional.node.type}`);
|
||||
}
|
||||
|
||||
const {
|
||||
scope
|
||||
} = member;
|
||||
const startingProp = startingOptional.isOptionalMemberExpression() ? "object" : "callee";
|
||||
const startingNode = startingOptional.node[startingProp];
|
||||
const startingNode = startingOptional.isOptionalMemberExpression() ? startingOptional.node.object : startingOptional.node.callee;
|
||||
const baseNeedsMemoised = scope.maybeGenerateMemoised(startingNode);
|
||||
const baseRef = baseNeedsMemoised != null ? baseNeedsMemoised : startingNode;
|
||||
const parentIsOptionalCall = parentPath.isOptionalCallExpression({
|
||||
callee: node
|
||||
});
|
||||
|
||||
const isOptionalCall = parent => parentIsOptionalCall;
|
||||
|
||||
const parentIsCall = parentPath.isCallExpression({
|
||||
callee: node
|
||||
});
|
||||
startingOptional.replaceWith(toNonOptional(startingOptional, baseRef));
|
||||
|
||||
if (parentIsOptionalCall) {
|
||||
if (isOptionalCall()) {
|
||||
if (parent.optional) {
|
||||
parentPath.replaceWith(this.optionalCall(member, parent.arguments));
|
||||
} else {
|
||||
@@ -186,11 +268,9 @@ const handle = {
|
||||
let regular = member.node;
|
||||
|
||||
for (let current = member; current !== endPath;) {
|
||||
const {
|
||||
parentPath
|
||||
} = current;
|
||||
const parentPath = current.parentPath;
|
||||
|
||||
if (parentPath === endPath && parentIsOptionalCall && parent.optional) {
|
||||
if (parentPath === endPath && isOptionalCall() && parent.optional) {
|
||||
regular = parentPath.node;
|
||||
break;
|
||||
}
|
||||
@@ -202,7 +282,7 @@ const handle = {
|
||||
let context;
|
||||
const endParentPath = endPath.parentPath;
|
||||
|
||||
if (t.isMemberExpression(regular) && endParentPath.isOptionalCallExpression({
|
||||
if (isMemberExpression(regular) && endParentPath.isOptionalCallExpression({
|
||||
callee: endPath.node,
|
||||
optional: true
|
||||
})) {
|
||||
@@ -212,7 +292,7 @@ const handle = {
|
||||
context = member.scope.maybeGenerateMemoised(object);
|
||||
|
||||
if (context) {
|
||||
regular.object = t.assignmentExpression("=", context, object);
|
||||
regular.object = assignmentExpression("=", context, object);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -223,17 +303,39 @@ const handle = {
|
||||
regular = endParentPath.node;
|
||||
}
|
||||
|
||||
replacementPath.replaceWith(t.conditionalExpression(t.logicalExpression("||", t.binaryExpression("===", baseNeedsMemoised ? t.assignmentExpression("=", baseRef, startingNode) : baseRef, t.nullLiteral()), t.binaryExpression("===", t.cloneNode(baseRef), scope.buildUndefinedNode())), isDeleteOperation ? t.booleanLiteral(true) : scope.buildUndefinedNode(), regular));
|
||||
const baseMemoised = baseNeedsMemoised ? assignmentExpression("=", cloneNode(baseRef), cloneNode(startingNode)) : cloneNode(baseRef);
|
||||
|
||||
if (willEndPathCastToBoolean) {
|
||||
let nonNullishCheck;
|
||||
|
||||
if (noDocumentAll) {
|
||||
nonNullishCheck = binaryExpression("!=", baseMemoised, nullLiteral());
|
||||
} else {
|
||||
nonNullishCheck = logicalExpression("&&", binaryExpression("!==", baseMemoised, nullLiteral()), binaryExpression("!==", cloneNode(baseRef), scope.buildUndefinedNode()));
|
||||
}
|
||||
|
||||
replacementPath.replaceWith(logicalExpression("&&", nonNullishCheck, regular));
|
||||
} else {
|
||||
let nullishCheck;
|
||||
|
||||
if (noDocumentAll) {
|
||||
nullishCheck = binaryExpression("==", baseMemoised, nullLiteral());
|
||||
} else {
|
||||
nullishCheck = logicalExpression("||", binaryExpression("===", baseMemoised, nullLiteral()), binaryExpression("===", cloneNode(baseRef), scope.buildUndefinedNode()));
|
||||
}
|
||||
|
||||
replacementPath.replaceWith(conditionalExpression(nullishCheck, isDeleteOperation ? booleanLiteral(true) : scope.buildUndefinedNode(), regular));
|
||||
}
|
||||
|
||||
if (context) {
|
||||
const endParent = endParentPath.node;
|
||||
endParentPath.replaceWith(t.optionalCallExpression(t.optionalMemberExpression(endParent.callee, t.identifier("call"), false, true), [context, ...endParent.arguments], false));
|
||||
endParentPath.replaceWith(optionalCallExpression(optionalMemberExpression(endParent.callee, identifier("call"), false, true), [cloneNode(context), ...endParent.arguments], false));
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (parentPath.isUpdateExpression({
|
||||
if (isUpdateExpression(parent, {
|
||||
argument: node
|
||||
})) {
|
||||
if (this.simpleSet) {
|
||||
@@ -246,23 +348,27 @@ const handle = {
|
||||
prefix
|
||||
} = parent;
|
||||
this.memoise(member, 2);
|
||||
const value = t.binaryExpression(operator[0], t.unaryExpression("+", this.get(member)), t.numericLiteral(1));
|
||||
const ref = scope.generateUidIdentifierBasedOnNode(node);
|
||||
scope.push({
|
||||
id: ref
|
||||
});
|
||||
const seq = [assignmentExpression("=", cloneNode(ref), this.get(member))];
|
||||
|
||||
if (prefix) {
|
||||
seq.push(updateExpression(operator, cloneNode(ref), prefix));
|
||||
const value = sequenceExpression(seq);
|
||||
parentPath.replaceWith(this.set(member, value));
|
||||
return;
|
||||
} else {
|
||||
const {
|
||||
scope
|
||||
} = member;
|
||||
const ref = scope.generateUidIdentifierBasedOnNode(node);
|
||||
const ref2 = scope.generateUidIdentifierBasedOnNode(node);
|
||||
scope.push({
|
||||
id: ref
|
||||
id: ref2
|
||||
});
|
||||
value.left = t.assignmentExpression("=", t.cloneNode(ref), value.left);
|
||||
parentPath.replaceWith(t.sequenceExpression([this.set(member, value), t.cloneNode(ref)]));
|
||||
seq.push(assignmentExpression("=", cloneNode(ref2), updateExpression(operator, cloneNode(ref), prefix)), cloneNode(ref));
|
||||
const value = sequenceExpression(seq);
|
||||
parentPath.replaceWith(sequenceExpression([this.set(member, value), cloneNode(ref2)]));
|
||||
return;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (parentPath.isAssignmentExpression({
|
||||
@@ -275,30 +381,42 @@ const handle = {
|
||||
|
||||
const {
|
||||
operator,
|
||||
right
|
||||
} = parent;
|
||||
let value = right;
|
||||
right: value
|
||||
} = parentPath.node;
|
||||
|
||||
if (operator !== "=") {
|
||||
this.memoise(member, 2);
|
||||
value = t.binaryExpression(operator.slice(0, -1), this.get(member), value);
|
||||
if (operator === "=") {
|
||||
parentPath.replaceWith(this.set(member, value));
|
||||
} else {
|
||||
const operatorTrunc = operator.slice(0, -1);
|
||||
|
||||
if (LOGICAL_OPERATORS.includes(operatorTrunc)) {
|
||||
this.memoise(member, 1);
|
||||
parentPath.replaceWith(logicalExpression(operatorTrunc, this.get(member), this.set(member, value)));
|
||||
} else {
|
||||
this.memoise(member, 2);
|
||||
parentPath.replaceWith(this.set(member, binaryExpression(operatorTrunc, this.get(member), value)));
|
||||
}
|
||||
}
|
||||
|
||||
parentPath.replaceWith(this.set(member, value));
|
||||
return;
|
||||
}
|
||||
|
||||
if (parentPath.isCallExpression({
|
||||
callee: node
|
||||
})) {
|
||||
parentPath.replaceWith(this.call(member, parent.arguments));
|
||||
parentPath.replaceWith(this.call(member, parentPath.node.arguments));
|
||||
return;
|
||||
}
|
||||
|
||||
if (parentPath.isOptionalCallExpression({
|
||||
callee: node
|
||||
})) {
|
||||
parentPath.replaceWith(this.optionalCall(member, parent.arguments));
|
||||
if (scope.path.isPattern()) {
|
||||
parentPath.replaceWith(callExpression(arrowFunctionExpression([], parentPath.node), []));
|
||||
return;
|
||||
}
|
||||
|
||||
parentPath.replaceWith(this.optionalCall(member, parentPath.node.arguments));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -317,13 +435,19 @@ const handle = {
|
||||
return;
|
||||
}
|
||||
|
||||
member.replaceWith(this.get(member));
|
||||
if (parentPath.isTaggedTemplateExpression()) {
|
||||
member.replaceWith(this.boundGet(member));
|
||||
} else {
|
||||
member.replaceWith(this.get(member));
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
function memberExpressionToFunctions(path, visitor, state) {
|
||||
path.traverse(visitor, Object.assign({}, handle, state, {
|
||||
memoiser: new AssignmentMemoiser()
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
exports["default"] = memberExpressionToFunctions;
|
||||
//# sourceMappingURL=index.js.map
|
||||
|
||||
+1
File diff suppressed because one or more lines are too long
+24
-18
@@ -1,49 +1,54 @@
|
||||
{
|
||||
"_args": [
|
||||
[
|
||||
"@babel/helper-member-expression-to-functions@7.10.4",
|
||||
"/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series"
|
||||
"@babel/helper-member-expression-to-functions@7.18.6",
|
||||
"/home/node/nuxt"
|
||||
]
|
||||
],
|
||||
"_from": "@babel/helper-member-expression-to-functions@7.10.4",
|
||||
"_id": "@babel/helper-member-expression-to-functions@7.10.4",
|
||||
"_from": "@babel/helper-member-expression-to-functions@7.18.6",
|
||||
"_id": "@babel/helper-member-expression-to-functions@7.18.6",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-m5j85pK/KZhuSdM/8cHUABQTAslV47OjfIB9Cc7P+PvlAoBzdb79BGNfw8RhT5Mq3p+xGd0ZfAKixbrUZx0C7A==",
|
||||
"_integrity": "sha512-CeHxqwwipekotzPDUuJOfIMtcIHBuc7WAzLmTYWctVigqS5RktNMQ5bEwQSuGewzYnCtTWa3BARXeiLxDTv+Ng==",
|
||||
"_location": "/@babel/helper-member-expression-to-functions",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "version",
|
||||
"registry": true,
|
||||
"raw": "@babel/helper-member-expression-to-functions@7.10.4",
|
||||
"raw": "@babel/helper-member-expression-to-functions@7.18.6",
|
||||
"name": "@babel/helper-member-expression-to-functions",
|
||||
"escapedName": "@babel%2fhelper-member-expression-to-functions",
|
||||
"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-replace-supers"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/@babel/helper-member-expression-to-functions/-/helper-member-expression-to-functions-7.10.4.tgz",
|
||||
"_spec": "7.10.4",
|
||||
"_where": "/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series",
|
||||
"_resolved": "https://registry.npmjs.org/@babel/helper-member-expression-to-functions/-/helper-member-expression-to-functions-7.18.6.tgz",
|
||||
"_spec": "7.18.6",
|
||||
"_where": "/home/node/nuxt",
|
||||
"author": {
|
||||
"name": "Justin Ridgewell",
|
||||
"email": "justin@ridgewell.name"
|
||||
"name": "The Babel Team",
|
||||
"url": "https://babel.dev/team"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/babel/babel/issues"
|
||||
},
|
||||
"dependencies": {
|
||||
"@babel/types": "^7.10.4"
|
||||
"@babel/types": "^7.18.6"
|
||||
},
|
||||
"description": "Helper function to replace certain member expressions with function calls",
|
||||
"gitHead": "7fd40d86a0d03ff0e9c3ea16b29689945433d4df",
|
||||
"homepage": "https://github.com/babel/babel#readme",
|
||||
"devDependencies": {
|
||||
"@babel/traverse": "^7.18.6"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=6.9.0"
|
||||
},
|
||||
"homepage": "https://babel.dev/docs/en/next/babel-helper-member-expression-to-functions",
|
||||
"license": "MIT",
|
||||
"main": "lib/index.js",
|
||||
"main": "./lib/index.js",
|
||||
"name": "@babel/helper-member-expression-to-functions",
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
@@ -53,5 +58,6 @@
|
||||
"url": "git+https://github.com/babel/babel.git",
|
||||
"directory": "packages/babel-helper-member-expression-to-functions"
|
||||
},
|
||||
"version": "7.10.4"
|
||||
"type": "commonjs",
|
||||
"version": "7.18.6"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user