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