| 1 |
import syntaxObjectRestSpread from "@babel/plugin-syntax-object-rest-spread"; |
| 2 |
import type { PluginAPI, PluginObject } from "@babel/core"; |
| 3 |
|
| 4 |
export default function ({ types: t }: PluginAPI): PluginObject { |
| 5 |
return { |
| 6 |
inherits: syntaxObjectRestSpread.default, |
| 7 |
|
| 8 |
visitor: { |
| 9 |
CallExpression(path) { |
| 10 |
if (!path.get("callee").matchesPattern("Object.assign")) return; |
| 11 |
|
| 12 |
const args = path.get("arguments"); |
| 13 |
if (args.length === 0) return; |
| 14 |
|
| 15 |
const [objPath] = args; |
| 16 |
if (!objPath.isObjectExpression()) return; |
| 17 |
|
| 18 |
const obj = objPath.node; |
| 19 |
const { properties } = obj; |
| 20 |
|
| 21 |
for (let i = 1; i < args.length; i++) { |
| 22 |
const arg = args[i]; |
| 23 |
const { node } = arg; |
| 24 |
|
| 25 |
if (t.isObjectExpression(node)) { |
| 26 |
properties.push(...node.properties); |
| 27 |
} else { |
| 28 |
properties.push( |
| 29 |
t.spreadElement( |
| 30 |
// @ts-expect-error fixme |
| 31 |
node, |
| 32 |
), |
| 33 |
); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
path.replaceWith(obj); |
| 38 |
}, |
| 39 |
}, |
| 40 |
}; |
| 41 |
} |