0.2.0 - Mid migration

This commit is contained in:
Daniel Mason 2022-04-25 14:47:15 +12:00
parent 139e6a915e
commit 7e38fdbd7d
42393 changed files with 5358157 additions and 62 deletions

View file

@ -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.

View file

@ -0,0 +1,19 @@
# @babel/plugin-proposal-optional-chaining
> Transform optional chaining operators into a series of nil checks
See our website [@babel/plugin-proposal-optional-chaining](https://babeljs.io/docs/en/babel-plugin-proposal-optional-chaining) for more information.
## Install
Using npm:
```sh
npm install --save-dev @babel/plugin-proposal-optional-chaining
```
or using yarn:
```sh
yarn add @babel/plugin-proposal-optional-chaining --dev
```

View file

@ -0,0 +1,256 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
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');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var syntaxOptionalChaining__default = /*#__PURE__*/_interopDefaultLegacy(syntaxOptionalChaining);
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)) return true;
maybeWrapped = p;
});
return maybeWrapped;
}
const {
ast
} = core.template.expression;
function isSimpleMemberExpression(expression) {
expression = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(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 childKey = optionalPath.isOptionalMemberExpression() ? "object" : "callee";
const childPath = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(optionalPath.get(childKey));
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 replaceKey = isCall ? "callee" : "object";
const chainWithTypes = node[replaceKey];
let chain = chainWithTypes;
while (helperSkipTransparentExpressionWrappers.isTransparentExprWrapper(chain)) {
chain = chain.expression;
}
let ref;
let check;
if (isCall && core.types.isIdentifier(chain, {
name: "eval"
})) {
check = ref = chain;
node[replaceKey] = core.types.sequenceExpression([core.types.numericLiteral(0), ref]);
} else if (pureGetters && isCall && isSimpleMemberExpression(chain)) {
check = ref = chainWithTypes;
} else {
ref = scope.maybeGenerateMemoised(chain);
if (ref) {
check = core.types.assignmentExpression("=", core.types.cloneNode(ref), chainWithTypes);
node[replaceKey] = 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.skipTransparentExprWrappers(replacementPath.get("object")).node;
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;
api.assertVersion(7);
const {
loose = false
} = options;
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: syntaxOptionalChaining__default['default'].default,
visitor: {
"OptionalCallExpression|OptionalMemberExpression"(path) {
transform(path, {
noDocumentAll,
pureGetters
});
}
}
};
});
exports.default = index;
exports.transform = transform;
//# sourceMappingURL=index.js.map

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,36 @@
{
"name": "@babel/plugin-proposal-optional-chaining",
"version": "7.14.5",
"description": "Transform optional chaining operators into a series of nil checks",
"repository": {
"type": "git",
"url": "https://github.com/babel/babel.git",
"directory": "packages/babel-plugin-proposal-optional-chaining"
},
"homepage": "https://babel.dev/docs/en/next/babel-plugin-proposal-optional-chaining",
"license": "MIT",
"publishConfig": {
"access": "public"
},
"main": "./lib/index.js",
"keywords": [
"babel-plugin"
],
"dependencies": {
"@babel/helper-plugin-utils": "^7.14.5",
"@babel/helper-skip-transparent-expression-wrappers": "^7.14.5",
"@babel/plugin-syntax-optional-chaining": "^7.8.3"
},
"peerDependencies": {
"@babel/core": "^7.0.0-0"
},
"devDependencies": {
"@babel/core": "7.14.5",
"@babel/helper-plugin-test-runner": "7.14.5",
"@babel/plugin-transform-block-scoping": "7.14.5"
},
"engines": {
"node": ">=6.9.0"
},
"author": "The Babel Team (https://babel.dev/team)"
}