Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Inline & simplify
@babel/helper-define-map
(#15274)
* Move functions to inline file * Simplify code * Delete `@babel/helper-define-map` * `make code-quality`
- Loading branch information
1 parent
6757a60
commit 0e86d38
Showing
9 changed files
with
80 additions
and
289 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
packages/babel-plugin-transform-property-mutators/src/define-map.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { types as t } from "@babel/core"; | ||
|
||
type DefineMap = { | ||
_inherits: t.Node[]; | ||
_key: t.Expression; | ||
get?: t.Expression; | ||
set?: t.Expression; | ||
kind: "get" | "set"; | ||
}; | ||
|
||
export type MutatorMap = Record<string, DefineMap>; | ||
|
||
export function pushAccessor( | ||
mutatorMap: MutatorMap, | ||
node: t.ObjectMethod & { kind: "get" | "set"; computed: false }, | ||
) { | ||
const alias = t.toKeyAlias(node); | ||
const map = (mutatorMap[alias] ??= { | ||
_inherits: [], | ||
_key: node.key, | ||
} as DefineMap); | ||
|
||
map._inherits.push(node); | ||
|
||
const value = t.functionExpression( | ||
null, | ||
node.params, | ||
node.body, | ||
node.generator, | ||
node.async, | ||
); | ||
value.returnType = node.returnType; | ||
t.inheritsComments(value, node); | ||
map[node.kind] = value; | ||
|
||
return map; | ||
} | ||
|
||
export function toDefineObject(mutatorMap: any) { | ||
const objExpr = t.objectExpression([]); | ||
|
||
Object.keys(mutatorMap).forEach(function (mutatorMapKey) { | ||
const map = mutatorMap[mutatorMapKey]; | ||
map.configurable = t.booleanLiteral(true); | ||
map.enumerable = t.booleanLiteral(true); | ||
|
||
const mapNode = t.objectExpression([]); | ||
|
||
const propNode = t.objectProperty(map._key, mapNode, map._computed); | ||
|
||
Object.keys(map).forEach(function (key) { | ||
const node = map[key]; | ||
if (key[0] === "_") return; | ||
|
||
const prop = t.objectProperty(t.identifier(key), node); | ||
t.inheritsComments(prop, node); | ||
t.removeComments(node); | ||
|
||
mapNode.properties.push(prop); | ||
}); | ||
|
||
objExpr.properties.push(propNode); | ||
}); | ||
|
||
return objExpr; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.