mirror of https://github.com/jkjoy/sunpeiwen.git
1 line
48 KiB
Plaintext
1 line
48 KiB
Plaintext
|
{"version":3,"file":"index.js","sources":["../src/shouldStoreRHSInTemporaryVariable.ts","../src/index.ts"],"sourcesContent":["import { types as t } from \"@babel/core\";\n\nconst {\n isObjectProperty,\n isArrayPattern,\n isObjectPattern,\n isAssignmentPattern,\n isRestElement,\n isIdentifier,\n} = t;\n/**\n * This is a helper function to determine if we should create an intermediate variable\n * such that the RHS of an assignment is not duplicated.\n *\n * See https://github.com/babel/babel/pull/13711#issuecomment-914388382 for discussion\n * on further optimizations.\n */\nexport default function shouldStoreRHSInTemporaryVariable(\n node: t.LVal,\n): boolean {\n if (isArrayPattern(node)) {\n const nonNullElements = node.elements.filter(element => element !== null);\n if (nonNullElements.length > 1) return true;\n else return shouldStoreRHSInTemporaryVariable(nonNullElements[0]);\n } else if (isObjectPattern(node)) {\n const { properties } = node;\n if (properties.length > 1) return true;\n else if (properties.length === 0) return false;\n else {\n const firstProperty = properties[0];\n if (isObjectProperty(firstProperty)) {\n // the value of the property must be an LVal\n return shouldStoreRHSInTemporaryVariable(firstProperty.value as t.LVal);\n } else {\n return shouldStoreRHSInTemporaryVariable(firstProperty);\n }\n }\n } else if (isAssignmentPattern(node)) {\n return shouldStoreRHSInTemporaryVariable(node.left);\n } else if (isRestElement(node)) {\n if (isIdentifier(node.argument)) return true;\n return shouldStoreRHSInTemporaryVariable(node.argument);\n } else {\n // node is Identifier or MemberExpression\n return false;\n }\n}\n","import { declare } from \"@babel/helper-plugin-utils\";\nimport syntaxObjectRestSpread from \"@babel/plugin-syntax-object-rest-spread\";\nimport { types as t } from \"@babel/core\";\nimport type { PluginPass } from \"@babel/core\";\nimport type { NodePath, Scope } from \"@babel/traverse\";\nimport { convertFunctionParams } from \"@babel/plugin-transform-parameters\";\nimport { isRequired } from \"@babel/helper-compilation-targets\";\nimport compatData from \"@babel/compat-data/corejs2-built-ins\";\nimport shouldStoreRHSInTemporaryVariable from \"./shouldStoreRHSInTemporaryVariable\";\n\nconst { isAssignmentPattern, isObjectProperty } = t;\n// @babel/types <=7.3.3 counts FOO as referenced in var { x: FOO }.\n// We need to detect this bug to know if \"unused\" means 0 or 1 references.\nif (!process.env.BABEL_8_BREAKING) {\n const node = t.identifier(\"a\");\n const property = t.objectProperty(t.identifier(\"key\"), node);\n const pattern = t.objectPattern([property]);\n\n // eslint-disable-next-line no-var\n var ZERO_REFS = t.isReferenced(node, property, pattern) ? 1 : 0;\n}\n\ntype Param = NodePath<t.Function[\"params\"][number]>;\nexport interface Options {\n useBuiltIns?: boolean;\n loose?: boolean;\n}\n\nexport default declare((api, opts: Options) => {\n api.assertVersion(7);\n\n const targets = api.targets();\n const supportsObjectAssign = !isRequired(\"es6.object.assign\", targets, {\n compatData,\n });\n\n const { useBuiltIns = supportsObjectAssign, loose = false } = opts;\n\n if (typeof loose !== \"boolean\") {\n throw new Error(\".loose must be a boolean, or undefined\");\n }\n\n const ignoreFunctionLength = api.assumption(\"ignoreFunctionLength\") ?? loose;\n const objectRestNoSymbols = api.assumption(\"objectRestNoSymbols\") ?? loose;\n const pureGetters = api.assumption(\"pureGetters\") ?? loose;\n const setSpreadProperties = api.assumption(\"setSpreadProperties\") ?? loose;\n\n function getExtendsHelper(\n file: PluginPass,\n ): t.MemberExpression | t.Identifier {\n return useBuiltIns\n ? t.memberExpression(t.identifier(\"Object\"), t.identifier(\"assign\"))\n : file.addHelper(\"extends\");\n }\n\n function hasRestElement(path: Param) {\n let foundRestElement = false;\n visitRestElements(path, restElement => {\n foundRestElement =
|