| 1 |
import { declare } from "@babel/helper-plugin-utils"; |
| 2 |
import { type MutatorMap, pushAccessor, toDefineObject } from "./define-map"; |
| 3 |
import { types as t } from "@babel/core"; |
| 4 |
|
| 5 |
export default declare(api => { |
| 6 |
api.assertVersion(7); |
| 7 |
|
| 8 |
return { |
| 9 |
name: "transform-property-mutators", |
| 10 |
|
| 11 |
visitor: { |
| 12 |
ObjectExpression(path) { |
| 13 |
const { node } = path; |
| 14 |
let mutatorMap: MutatorMap | undefined; |
| 15 |
const newProperties = node.properties.filter(function (prop) { |
| 16 |
if ( |
| 17 |
t.isObjectMethod(prop) && |
| 18 |
!prop.computed && |
| 19 |
(prop.kind === "get" || prop.kind === "set") |
| 20 |
) { |
| 21 |
pushAccessor( |
| 22 |
(mutatorMap ??= {}), |
| 23 |
prop as t.ObjectMethod & { kind: "get" | "set"; computed: false }, |
| 24 |
); |
| 25 |
return false; |
| 26 |
} |
| 27 |
return true; |
| 28 |
}); |
| 29 |
|
| 30 |
if (mutatorMap === undefined) { |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
node.properties = newProperties; |
| 35 |
|
| 36 |
path.replaceWith( |
| 37 |
t.callExpression( |
| 38 |
t.memberExpression( |
| 39 |
t.identifier("Object"), |
| 40 |
t.identifier("defineProperties"), |
| 41 |
), |
| 42 |
[node, toDefineObject(mutatorMap)], |
| 43 |
), |
| 44 |
); |
| 45 |
}, |
| 46 |
}, |
| 47 |
}; |
| 48 |
}); |