1 line
50 KiB
Plaintext
1 line
50 KiB
Plaintext
|
{"version":3,"file":"index.js","sources":["../src/shouldStoreRHSInTemporaryVariable.ts","../src/compat-data.ts","../src/index.ts"],"sourcesContent":["import type { types as t } from \"@babel/core\";\n\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 (!node) return false;\n if (node.type === \"ArrayPattern\") {\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 (node.type === \"ObjectPattern\") {\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 (firstProperty.type === \"ObjectProperty\") {\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 (node.type === \"AssignmentPattern\") {\n return shouldStoreRHSInTemporaryVariable(node.left);\n } else if (node.type === \"RestElement\") {\n if (node.argument.type === \"Identifier\") return true;\n return shouldStoreRHSInTemporaryVariable(node.argument);\n } else {\n // node is Identifier or MemberExpression\n return false;\n }\n}\n","export default {\n \"Object.assign\": {\n chrome: \"49\",\n opera: \"36\",\n edge: \"13\",\n firefox: \"36\",\n safari: \"10\",\n node: \"6\",\n deno: \"1\",\n ios: \"10\",\n samsung: \"5\",\n opera_mobile: \"36\",\n electron: \"0.37\",\n },\n};\n","import { declare } from \"@babel/helper-plugin-utils\";\nimport { types as t } from \"@babel/core\";\nimport type { PluginPass, NodePath, Scope } from \"@babel/core\";\nimport { convertFunctionParams } from \"@babel/plugin-transform-parameters\";\nimport { isRequired } from \"@babel/helper-compilation-targets\";\nimport shouldStoreRHSInTemporaryVariable from \"./shouldStoreRHSInTemporaryVariable.ts\";\nimport compatData from \"./compat-data.ts\";\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(REQUIRED_VERSION(7));\n\n const targets = api.targets();\n const supportsObjectAssign = !isRequired(\"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 functi
|