forked from daren.hsu/line_push
update
This commit is contained in:
+1
-1
@@ -2,7 +2,7 @@
|
||||
|
||||
> Transform optional chaining operators into a series of nil checks
|
||||
|
||||
See our website [@babel/plugin-proposal-optional-chaining](https://babeljs.io/docs/en/next/babel-plugin-proposal-optional-chaining.html) for more information.
|
||||
See our website [@babel/plugin-proposal-optional-chaining](https://babeljs.io/docs/en/babel-plugin-proposal-optional-chaining) for more information.
|
||||
|
||||
## Install
|
||||
|
||||
|
||||
+228
-129
@@ -1,150 +1,249 @@
|
||||
"use strict";
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = void 0;
|
||||
Object.defineProperty(exports, '__esModule', { value: true });
|
||||
|
||||
var _helperPluginUtils = require("@babel/helper-plugin-utils");
|
||||
var helperPluginUtils = require('@babel/helper-plugin-utils');
|
||||
var syntaxOptionalChaining = require('@babel/plugin-syntax-optional-chaining');
|
||||
var core = require('@babel/core');
|
||||
var helperSkipTransparentExpressionWrappers = require('@babel/helper-skip-transparent-expression-wrappers');
|
||||
|
||||
var _pluginSyntaxOptionalChaining = _interopRequireDefault(require("@babel/plugin-syntax-optional-chaining"));
|
||||
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
||||
|
||||
var _core = require("@babel/core");
|
||||
var syntaxOptionalChaining__default = /*#__PURE__*/_interopDefaultLegacy(syntaxOptionalChaining);
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
function willPathCastToBoolean(path) {
|
||||
const maybeWrapped = findOutermostTransparentParent(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
|
||||
});
|
||||
}
|
||||
function findOutermostTransparentParent(path) {
|
||||
let maybeWrapped = path;
|
||||
path.findParent(p => {
|
||||
if (!helperSkipTransparentExpressionWrappers.isTransparentExprWrapper(p.node)) return true;
|
||||
maybeWrapped = p;
|
||||
});
|
||||
return maybeWrapped;
|
||||
}
|
||||
|
||||
const {
|
||||
ast
|
||||
} = core.template.expression;
|
||||
|
||||
function isSimpleMemberExpression(expression) {
|
||||
expression = helperSkipTransparentExpressionWrappers.skipTransparentExprWrapperNodes(expression);
|
||||
return core.types.isIdentifier(expression) || core.types.isSuper(expression) || core.types.isMemberExpression(expression) && !expression.computed && isSimpleMemberExpression(expression.object);
|
||||
}
|
||||
|
||||
function needsMemoize(path) {
|
||||
let optionalPath = path;
|
||||
const {
|
||||
scope
|
||||
} = path;
|
||||
|
||||
while (optionalPath.isOptionalMemberExpression() || optionalPath.isOptionalCallExpression()) {
|
||||
const {
|
||||
node
|
||||
} = optionalPath;
|
||||
const childPath = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(optionalPath.isOptionalMemberExpression() ? optionalPath.get("object") : optionalPath.get("callee"));
|
||||
|
||||
if (node.optional) {
|
||||
return !scope.isStatic(childPath.node);
|
||||
}
|
||||
|
||||
optionalPath = childPath;
|
||||
}
|
||||
}
|
||||
|
||||
function transform(path, {
|
||||
pureGetters,
|
||||
noDocumentAll
|
||||
}) {
|
||||
const {
|
||||
scope
|
||||
} = path;
|
||||
const maybeWrapped = findOutermostTransparentParent(path);
|
||||
const {
|
||||
parentPath
|
||||
} = maybeWrapped;
|
||||
const willReplacementCastToBoolean = willPathCastToBoolean(maybeWrapped);
|
||||
let isDeleteOperation = false;
|
||||
const parentIsCall = parentPath.isCallExpression({
|
||||
callee: maybeWrapped.node
|
||||
}) && path.isOptionalMemberExpression();
|
||||
const optionals = [];
|
||||
let optionalPath = path;
|
||||
|
||||
if (scope.path.isPattern() && needsMemoize(optionalPath)) {
|
||||
path.replaceWith(core.template.ast`(() => ${path.node})()`);
|
||||
return;
|
||||
}
|
||||
|
||||
while (optionalPath.isOptionalMemberExpression() || optionalPath.isOptionalCallExpression()) {
|
||||
const {
|
||||
node
|
||||
} = optionalPath;
|
||||
|
||||
if (node.optional) {
|
||||
optionals.push(node);
|
||||
}
|
||||
|
||||
if (optionalPath.isOptionalMemberExpression()) {
|
||||
optionalPath.node.type = "MemberExpression";
|
||||
optionalPath = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(optionalPath.get("object"));
|
||||
} else if (optionalPath.isOptionalCallExpression()) {
|
||||
optionalPath.node.type = "CallExpression";
|
||||
optionalPath = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(optionalPath.get("callee"));
|
||||
}
|
||||
}
|
||||
|
||||
let replacementPath = path;
|
||||
|
||||
if (parentPath.isUnaryExpression({
|
||||
operator: "delete"
|
||||
})) {
|
||||
replacementPath = parentPath;
|
||||
isDeleteOperation = true;
|
||||
}
|
||||
|
||||
for (let i = optionals.length - 1; i >= 0; i--) {
|
||||
const node = optionals[i];
|
||||
const isCall = core.types.isCallExpression(node);
|
||||
const chainWithTypes = isCall ? node.callee : node.object;
|
||||
const chain = helperSkipTransparentExpressionWrappers.skipTransparentExprWrapperNodes(chainWithTypes);
|
||||
let ref;
|
||||
let check;
|
||||
|
||||
if (isCall && core.types.isIdentifier(chain, {
|
||||
name: "eval"
|
||||
})) {
|
||||
check = ref = chain;
|
||||
node.callee = core.types.sequenceExpression([core.types.numericLiteral(0), ref]);
|
||||
} else if (pureGetters && isCall && isSimpleMemberExpression(chain)) {
|
||||
check = ref = node.callee;
|
||||
} else {
|
||||
ref = scope.maybeGenerateMemoised(chain);
|
||||
|
||||
if (ref) {
|
||||
check = core.types.assignmentExpression("=", core.types.cloneNode(ref), chainWithTypes);
|
||||
isCall ? node.callee = ref : node.object = ref;
|
||||
} else {
|
||||
check = ref = chainWithTypes;
|
||||
}
|
||||
}
|
||||
|
||||
if (isCall && core.types.isMemberExpression(chain)) {
|
||||
if (pureGetters && isSimpleMemberExpression(chain)) {
|
||||
node.callee = chainWithTypes;
|
||||
} else {
|
||||
const {
|
||||
object
|
||||
} = chain;
|
||||
let context = scope.maybeGenerateMemoised(object);
|
||||
|
||||
if (context) {
|
||||
chain.object = core.types.assignmentExpression("=", context, object);
|
||||
} else if (core.types.isSuper(object)) {
|
||||
context = core.types.thisExpression();
|
||||
} else {
|
||||
context = object;
|
||||
}
|
||||
|
||||
node.arguments.unshift(core.types.cloneNode(context));
|
||||
node.callee = core.types.memberExpression(node.callee, core.types.identifier("call"));
|
||||
}
|
||||
}
|
||||
|
||||
let replacement = replacementPath.node;
|
||||
|
||||
if (i === 0 && parentIsCall) {
|
||||
var _baseRef;
|
||||
|
||||
const object = helperSkipTransparentExpressionWrappers.skipTransparentExprWrapperNodes(replacement.object);
|
||||
let baseRef;
|
||||
|
||||
if (!pureGetters || !isSimpleMemberExpression(object)) {
|
||||
baseRef = scope.maybeGenerateMemoised(object);
|
||||
|
||||
if (baseRef) {
|
||||
replacement.object = core.types.assignmentExpression("=", baseRef, object);
|
||||
}
|
||||
}
|
||||
|
||||
replacement = core.types.callExpression(core.types.memberExpression(replacement, core.types.identifier("bind")), [core.types.cloneNode((_baseRef = baseRef) != null ? _baseRef : object)]);
|
||||
}
|
||||
|
||||
if (willReplacementCastToBoolean) {
|
||||
const nonNullishCheck = noDocumentAll ? ast`${core.types.cloneNode(check)} != null` : ast`
|
||||
${core.types.cloneNode(check)} !== null && ${core.types.cloneNode(ref)} !== void 0`;
|
||||
replacementPath.replaceWith(core.types.logicalExpression("&&", nonNullishCheck, replacement));
|
||||
replacementPath = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(replacementPath.get("right"));
|
||||
} else {
|
||||
const nullishCheck = noDocumentAll ? ast`${core.types.cloneNode(check)} == null` : ast`
|
||||
${core.types.cloneNode(check)} === null || ${core.types.cloneNode(ref)} === void 0`;
|
||||
const returnValue = isDeleteOperation ? ast`true` : ast`void 0`;
|
||||
replacementPath.replaceWith(core.types.conditionalExpression(nullishCheck, returnValue, replacement));
|
||||
replacementPath = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(replacementPath.get("alternate"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var index = helperPluginUtils.declare((api, options) => {
|
||||
var _api$assumption, _api$assumption2;
|
||||
|
||||
var _default = (0, _helperPluginUtils.declare)((api, options) => {
|
||||
api.assertVersion(7);
|
||||
const {
|
||||
loose = false
|
||||
} = options;
|
||||
|
||||
function isSimpleMemberExpression(expression) {
|
||||
return _core.types.isIdentifier(expression) || _core.types.isSuper(expression) || _core.types.isMemberExpression(expression) && !expression.computed && isSimpleMemberExpression(expression.object);
|
||||
}
|
||||
|
||||
const noDocumentAll = (_api$assumption = api.assumption("noDocumentAll")) != null ? _api$assumption : loose;
|
||||
const pureGetters = (_api$assumption2 = api.assumption("pureGetters")) != null ? _api$assumption2 : loose;
|
||||
return {
|
||||
name: "proposal-optional-chaining",
|
||||
inherits: _pluginSyntaxOptionalChaining.default,
|
||||
inherits: syntaxOptionalChaining__default["default"].default,
|
||||
visitor: {
|
||||
"OptionalCallExpression|OptionalMemberExpression"(path) {
|
||||
const {
|
||||
scope
|
||||
} = path;
|
||||
let maybeParenthesized = path;
|
||||
const parentPath = path.findParent(p => {
|
||||
if (!p.isParenthesizedExpression()) return true;
|
||||
maybeParenthesized = p;
|
||||
transform(path, {
|
||||
noDocumentAll,
|
||||
pureGetters
|
||||
});
|
||||
let isDeleteOperation = false;
|
||||
const parentIsCall = parentPath.isCallExpression({
|
||||
callee: maybeParenthesized.node
|
||||
}) && path.isOptionalMemberExpression();
|
||||
const optionals = [];
|
||||
let optionalPath = path;
|
||||
|
||||
while (optionalPath.isOptionalMemberExpression() || optionalPath.isOptionalCallExpression() || optionalPath.isParenthesizedExpression() || optionalPath.isTSNonNullExpression()) {
|
||||
const {
|
||||
node
|
||||
} = optionalPath;
|
||||
|
||||
if (node.optional) {
|
||||
optionals.push(node);
|
||||
}
|
||||
|
||||
if (optionalPath.isOptionalMemberExpression()) {
|
||||
optionalPath.node.type = "MemberExpression";
|
||||
optionalPath = optionalPath.get("object");
|
||||
} else if (optionalPath.isOptionalCallExpression()) {
|
||||
optionalPath.node.type = "CallExpression";
|
||||
optionalPath = optionalPath.get("callee");
|
||||
} else {
|
||||
optionalPath = optionalPath.get("expression");
|
||||
}
|
||||
}
|
||||
|
||||
let replacementPath = path;
|
||||
|
||||
if (parentPath.isUnaryExpression({
|
||||
operator: "delete"
|
||||
})) {
|
||||
replacementPath = parentPath;
|
||||
isDeleteOperation = true;
|
||||
}
|
||||
|
||||
for (let i = optionals.length - 1; i >= 0; i--) {
|
||||
const node = optionals[i];
|
||||
|
||||
const isCall = _core.types.isCallExpression(node);
|
||||
|
||||
const replaceKey = isCall ? "callee" : "object";
|
||||
const chain = node[replaceKey];
|
||||
let ref;
|
||||
let check;
|
||||
|
||||
if (loose && isCall && isSimpleMemberExpression(chain)) {
|
||||
check = ref = chain;
|
||||
} else {
|
||||
ref = scope.maybeGenerateMemoised(chain);
|
||||
|
||||
if (ref) {
|
||||
check = _core.types.assignmentExpression("=", _core.types.cloneNode(ref), chain);
|
||||
node[replaceKey] = ref;
|
||||
} else {
|
||||
check = ref = chain;
|
||||
}
|
||||
}
|
||||
|
||||
if (isCall && _core.types.isMemberExpression(chain)) {
|
||||
if (loose && isSimpleMemberExpression(chain)) {
|
||||
node.callee = chain;
|
||||
} else {
|
||||
const {
|
||||
object
|
||||
} = chain;
|
||||
let context = scope.maybeGenerateMemoised(object);
|
||||
|
||||
if (context) {
|
||||
chain.object = _core.types.assignmentExpression("=", context, object);
|
||||
} else if (_core.types.isSuper(object)) {
|
||||
context = _core.types.thisExpression();
|
||||
} else {
|
||||
context = object;
|
||||
}
|
||||
|
||||
node.arguments.unshift(_core.types.cloneNode(context));
|
||||
node.callee = _core.types.memberExpression(node.callee, _core.types.identifier("call"));
|
||||
}
|
||||
}
|
||||
|
||||
let replacement = replacementPath.node;
|
||||
|
||||
if (i === 0 && parentIsCall) {
|
||||
var _baseRef;
|
||||
|
||||
const {
|
||||
object
|
||||
} = replacement;
|
||||
let baseRef;
|
||||
|
||||
if (!loose || !isSimpleMemberExpression(object)) {
|
||||
baseRef = scope.maybeGenerateMemoised(object);
|
||||
|
||||
if (baseRef) {
|
||||
replacement.object = _core.types.assignmentExpression("=", baseRef, object);
|
||||
}
|
||||
}
|
||||
|
||||
replacement = _core.types.callExpression(_core.types.memberExpression(replacement, _core.types.identifier("bind")), [_core.types.cloneNode((_baseRef = baseRef) != null ? _baseRef : object)]);
|
||||
}
|
||||
|
||||
replacementPath.replaceWith(_core.types.conditionalExpression(loose ? _core.types.binaryExpression("==", _core.types.cloneNode(check), _core.types.nullLiteral()) : _core.types.logicalExpression("||", _core.types.binaryExpression("===", _core.types.cloneNode(check), _core.types.nullLiteral()), _core.types.binaryExpression("===", _core.types.cloneNode(ref), scope.buildUndefinedNode())), isDeleteOperation ? _core.types.booleanLiteral(true) : scope.buildUndefinedNode(), replacement));
|
||||
replacementPath = replacementPath.get("alternate");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
exports.default = _default;
|
||||
exports["default"] = index;
|
||||
exports.transform = transform;
|
||||
//# sourceMappingURL=index.js.map
|
||||
|
||||
+1
File diff suppressed because one or more lines are too long
+32
-21
@@ -1,53 +1,63 @@
|
||||
{
|
||||
"_args": [
|
||||
[
|
||||
"@babel/plugin-proposal-optional-chaining@7.10.4",
|
||||
"/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series"
|
||||
"@babel/plugin-proposal-optional-chaining@7.18.6",
|
||||
"/home/node/nuxt"
|
||||
]
|
||||
],
|
||||
"_from": "@babel/plugin-proposal-optional-chaining@7.10.4",
|
||||
"_id": "@babel/plugin-proposal-optional-chaining@7.10.4",
|
||||
"_from": "@babel/plugin-proposal-optional-chaining@7.18.6",
|
||||
"_id": "@babel/plugin-proposal-optional-chaining@7.18.6",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-ZIhQIEeavTgouyMSdZRap4VPPHqJJ3NEs2cuHs5p0erH+iz6khB0qfgU8g7UuJkG88+fBMy23ZiU+nuHvekJeQ==",
|
||||
"_integrity": "sha512-PatI6elL5eMzoypFAiYDpYQyMtXTn+iMhuxxQt5mAXD4fEmKorpSI3PHd+i3JXBJN3xyA6MvJv7at23HffFHwA==",
|
||||
"_location": "/@babel/plugin-proposal-optional-chaining",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "version",
|
||||
"registry": true,
|
||||
"raw": "@babel/plugin-proposal-optional-chaining@7.10.4",
|
||||
"raw": "@babel/plugin-proposal-optional-chaining@7.18.6",
|
||||
"name": "@babel/plugin-proposal-optional-chaining",
|
||||
"escapedName": "@babel%2fplugin-proposal-optional-chaining",
|
||||
"scope": "@babel",
|
||||
"rawSpec": "7.10.4",
|
||||
"rawSpec": "7.18.6",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "7.10.4"
|
||||
"fetchSpec": "7.18.6"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/@babel/preset-env"
|
||||
"/@babel/plugin-bugfix-v8-spread-parameters-in-optional-chaining",
|
||||
"/@babel/preset-env",
|
||||
"/@nuxt/babel-preset-app"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/@babel/plugin-proposal-optional-chaining/-/plugin-proposal-optional-chaining-7.10.4.tgz",
|
||||
"_spec": "7.10.4",
|
||||
"_where": "/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series",
|
||||
"_resolved": "https://registry.npmjs.org/@babel/plugin-proposal-optional-chaining/-/plugin-proposal-optional-chaining-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-plugin-utils": "^7.10.4",
|
||||
"@babel/plugin-syntax-optional-chaining": "^7.8.0"
|
||||
"@babel/helper-plugin-utils": "^7.18.6",
|
||||
"@babel/helper-skip-transparent-expression-wrappers": "^7.18.6",
|
||||
"@babel/plugin-syntax-optional-chaining": "^7.8.3"
|
||||
},
|
||||
"description": "Transform optional chaining operators into a series of nil checks",
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.10.4",
|
||||
"@babel/helper-plugin-test-runner": "^7.10.4",
|
||||
"@babel/plugin-transform-block-scoping": "^7.10.4"
|
||||
"@babel/core": "^7.18.6",
|
||||
"@babel/helper-plugin-test-runner": "^7.18.6",
|
||||
"@babel/plugin-transform-block-scoping": "^7.18.6",
|
||||
"@babel/traverse": "^7.18.6"
|
||||
},
|
||||
"gitHead": "7fd40d86a0d03ff0e9c3ea16b29689945433d4df",
|
||||
"homepage": "https://github.com/babel/babel#readme",
|
||||
"engines": {
|
||||
"node": ">=6.9.0"
|
||||
},
|
||||
"homepage": "https://babel.dev/docs/en/next/babel-plugin-proposal-optional-chaining",
|
||||
"keywords": [
|
||||
"babel-plugin"
|
||||
],
|
||||
"license": "MIT",
|
||||
"main": "lib/index.js",
|
||||
"main": "./lib/index.js",
|
||||
"name": "@babel/plugin-proposal-optional-chaining",
|
||||
"peerDependencies": {
|
||||
"@babel/core": "^7.0.0-0"
|
||||
@@ -60,5 +70,6 @@
|
||||
"url": "git+https://github.com/babel/babel.git",
|
||||
"directory": "packages/babel-plugin-proposal-optional-chaining"
|
||||
},
|
||||
"version": "7.10.4"
|
||||
"type": "commonjs",
|
||||
"version": "7.18.6"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user