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 = [];\n while (\n optionalPath.isOptionalMemberExpression() ||\n optionalPath.isOptionalCallExpression()\n ) {\n const { node } = optionalPath;\n chains.push(node);\n\n if (optionalPath.isOptionalMemberExpression()) {\n optionalPath = skipTransparentExprWrappers(optionalPath.get(\"object\"));\n } else if (optionalPath.isOptionalCallExpression()) {\n optionalPath = skipTransparentExprWrappers(optionalPath.get(\"callee\"));\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) as boolean;\n const pureGetters = (api.assumption(\"pureGetters\") ?? false) as boolean;\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","isOptionalCallExpression","push","skipTransparentExprWrappers","get","i","arguments","optional","callee","declare","api","assertVersion","noDocumentAll","assumption","pureGetters","name","visitor","transform"],"mappings":";;;;;;;;;AAQA,SAASA,sBAAT,CAAgCC,aAAhC,EAA8E;AAC5E,EAAA,MAAMC,WAAW,GAAGD,aAAa,CAACE,SAAd,CAAwBC,IAAI,IAAIC,UAAC,CAACC,eAAF,CAAkBF,IAAlB,CAAhC,CAApB,CAAA;EACA,OAAOF,WAAW,IAAI,CAAf,IAAoBA,WAAW,KAAKD,aAAa,CAACM,MAAd,GAAuB,CAAlE,CAAA;AACD,CAAA;;AAUM,SAASC,eAAT,CACLC,IADK,EAEI;EACT,IAAIC,YAAoC,GAAGD,IAA3C,CAAA;EACA,MAAME,MAAM,GAAG,EAAf,CAAA;;EACA,OACED,YAAY,CAACE,0BAAb,EAAA,IACAF,YAAY,CAACG,wBAAb,EAFF,EAGE;IACA,MAAM;AAAET,MAAAA,IAAAA;AAAF,KAAA,GAAWM,YAAjB,CAAA;IACAC,MAAM,CAACG,IAAP,CAAYV,IAAZ,CAAA,CAAA;;AAEA,IAAA,IAAIM,YAAY,CAACE,0BAAb,EAAJ,EAA+C;MAC7CF,YAAY,GAAGK,mEAA2B,CAAC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