1 line
5.2 KiB
Plaintext
1 line
5.2 KiB
Plaintext
{"version":3,"file":"index.js","sources":["../src/util.ts","../src/index.ts"],"sourcesContent":["import { skipTransparentExprWrappers } from \"@babel/helper-skip-transparent-expression-wrappers\";\nimport type { NodePath } from \"@babel/traverse\";\nimport { types as t } from \"@babel/core\";\n// https://crbug.com/v8/11558\n\n// check if there is a spread element followed by another argument.\n// (...[], 0) or (...[], ...[])\n\nfunction matchAffectedArguments(argumentNodes: t.CallExpression[\"arguments\"]) {\n const spreadIndex = argumentNodes.findIndex(node => t.isSpreadElement(node));\n return spreadIndex >= 0 && spreadIndex !== argumentNodes.length - 1;\n}\n\n/**\n * Check whether the optional chain is affected by https://crbug.com/v8/11558.\n * This routine MUST not manipulate NodePath\n *\n * @export\n * @param {(NodePath<t.OptionalMemberExpression | t.OptionalCallExpression>)} path\n * @returns {boolean}\n */\nexport function shouldTransform(\n path: NodePath<t.OptionalMemberExpression | t.OptionalCallExpression>,\n): boolean {\n let optionalPath: NodePath<t.Expression> = path;\n const chains: (t.OptionalCallExpression | t.OptionalMemberExpression)[] = [];\n for (;;) {\n if (optionalPath.isOptionalMemberExpression()) {\n chains.push(optionalPath.node);\n optionalPath = skipTransparentExprWrappers(optionalPath.get(\"object\"));\n } else if (optionalPath.isOptionalCallExpression()) {\n chains.push(optionalPath.node);\n optionalPath = skipTransparentExprWrappers(optionalPath.get(\"callee\"));\n } else {\n break;\n }\n }\n for (let i = 0; i < chains.length; i++) {\n const node = chains[i];\n if (\n t.isOptionalCallExpression(node) &&\n matchAffectedArguments(node.arguments)\n ) {\n // f?.(...[], 0)\n if (node.optional) {\n return true;\n }\n // o?.m(...[], 0)\n // when node.optional is false, chains[i + 1] is always well defined\n const callee = chains[i + 1];\n if (t.isOptionalMemberExpression(callee, { optional: true })) {\n return true;\n }\n }\n }\n return false;\n}\n","import { declare } from \"@babel/helper-plugin-utils\";\nimport { transform } from \"@babel/plugin-proposal-optional-chaining\";\nimport { shouldTransform } from \"./util\";\nimport type { NodePath } from \"@babel/traverse\";\nimport type * as t from \"@babel/types\";\n\nexport default declare(api => {\n api.assertVersion(7);\n\n const noDocumentAll = api.assumption(\"noDocumentAll\") ?? false;\n const pureGetters = api.assumption(\"pureGetters\") ?? false;\n\n return {\n name: \"bugfix-v8-spread-parameters-in-optional-chaining\",\n\n visitor: {\n \"OptionalCallExpression|OptionalMemberExpression\"(\n path: NodePath<t.OptionalCallExpression | t.OptionalMemberExpression>,\n ) {\n if (shouldTransform(path)) {\n transform(path, { noDocumentAll, pureGetters });\n }\n },\n },\n };\n});\n"],"names":["matchAffectedArguments","argumentNodes","spreadIndex","findIndex","node","t","isSpreadElement","length","shouldTransform","path","optionalPath","chains","isOptionalMemberExpression","push","skipTransparentExprWrappers","get","isOptionalCallExpression","i","arguments","optional","callee","declare","api","assertVersion","noDocumentAll","assumption","pureGetters","name","visitor","transform"],"mappings":";;;;;;;;;AAQA,SAASA,sBAAsB,CAACC,aAA4C,EAAE;AAC5E,EAAA,MAAMC,WAAW,GAAGD,aAAa,CAACE,SAAS,CAACC,IAAI,IAAIC,UAAC,CAACC,eAAe,CAACF,IAAI,CAAC,CAAC,CAAA;EAC5E,OAAOF,WAAW,IAAI,CAAC,IAAIA,WAAW,KAAKD,aAAa,CAACM,MAAM,GAAG,CAAC,CAAA;AACrE,CAAA;;AAUO,SAASC,eAAe,CAC7BC,IAAqE,EAC5D;EACT,IAAIC,YAAoC,GAAGD,IAAI,CAAA;EAC/C,MAAME,MAAiE,GAAG,EAAE,CAAA;EAC5E,SAAS;AACP,IAAA,IAAID,YAAY,CAACE,0BAA0B,EAAE,EAAE;AAC7CD,MAAAA,MAAM,CAACE,IAAI,CAACH,YAAY,CAACN,IAAI,CAAC,CAAA;MAC9BM,YAAY,GAAGI,mEAA2B,CAACJ,YAAY,CAACK,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAA;AACxE,KAAC,MAAM,IAAIL,YAAY,CAACM,wBAAwB,EAAE,EAAE;AAClDL,MAAA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