[6a3a178] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, '__esModule', { value: true });
|
---|
| 4 |
|
---|
| 5 | var helperPluginUtils = require('@babel/helper-plugin-utils');
|
---|
| 6 | var pluginProposalOptionalChaining = require('@babel/plugin-proposal-optional-chaining');
|
---|
| 7 | var helperSkipTransparentExpressionWrappers = require('@babel/helper-skip-transparent-expression-wrappers');
|
---|
| 8 | var core = require('@babel/core');
|
---|
| 9 |
|
---|
| 10 | function matchAffectedArguments(argumentNodes) {
|
---|
| 11 | const spreadIndex = argumentNodes.findIndex(node => core.types.isSpreadElement(node));
|
---|
| 12 | return spreadIndex >= 0 && spreadIndex !== argumentNodes.length - 1;
|
---|
| 13 | }
|
---|
| 14 |
|
---|
| 15 | function shouldTransform(path) {
|
---|
| 16 | let optionalPath = path;
|
---|
| 17 | const chains = [];
|
---|
| 18 |
|
---|
| 19 | while (optionalPath.isOptionalMemberExpression() || optionalPath.isOptionalCallExpression()) {
|
---|
| 20 | const {
|
---|
| 21 | node
|
---|
| 22 | } = optionalPath;
|
---|
| 23 | chains.push(node);
|
---|
| 24 |
|
---|
| 25 | if (optionalPath.isOptionalMemberExpression()) {
|
---|
| 26 | optionalPath = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(optionalPath.get("object"));
|
---|
| 27 | } else if (optionalPath.isOptionalCallExpression()) {
|
---|
| 28 | optionalPath = helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers(optionalPath.get("callee"));
|
---|
| 29 | }
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | for (let i = 0; i < chains.length; i++) {
|
---|
| 33 | const node = chains[i];
|
---|
| 34 |
|
---|
| 35 | if (core.types.isOptionalCallExpression(node) && matchAffectedArguments(node.arguments)) {
|
---|
| 36 | if (node.optional) {
|
---|
| 37 | return true;
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | const callee = chains[i + 1];
|
---|
| 41 |
|
---|
| 42 | if (core.types.isOptionalMemberExpression(callee, {
|
---|
| 43 | optional: true
|
---|
| 44 | })) {
|
---|
| 45 | return true;
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | return false;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | var index = helperPluginUtils.declare(api => {
|
---|
| 54 | api.assertVersion(7);
|
---|
| 55 | const noDocumentAll = api.assumption("noDocumentAll");
|
---|
| 56 | const pureGetters = api.assumption("pureGetters");
|
---|
| 57 | return {
|
---|
| 58 | name: "bugfix-v8-spread-parameters-in-optional-chaining",
|
---|
| 59 | visitor: {
|
---|
| 60 | "OptionalCallExpression|OptionalMemberExpression"(path) {
|
---|
| 61 | if (shouldTransform(path)) {
|
---|
| 62 | pluginProposalOptionalChaining.transform(path, {
|
---|
| 63 | noDocumentAll,
|
---|
| 64 | pureGetters
|
---|
| 65 | });
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | }
|
---|
| 70 | };
|
---|
| 71 | });
|
---|
| 72 |
|
---|
| 73 | exports.default = index;
|
---|
| 74 | //# sourceMappingURL=index.js.map
|
---|