Skip to content

Commit

Permalink
Inline & simplify @babel/helper-define-map (#15274)
Browse files Browse the repository at this point in the history
* Move functions to inline file

* Simplify code

* Delete `@babel/helper-define-map`

* `make code-quality`
  • Loading branch information
nicolo-ribaudo committed Dec 14, 2022
1 parent 6757a60 commit 0e86d38
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 289 deletions.
3 changes: 0 additions & 3 deletions packages/babel-helper-define-map/.npmignore

This file was deleted.

19 changes: 0 additions & 19 deletions packages/babel-helper-define-map/README.md

This file was deleted.

43 changes: 0 additions & 43 deletions packages/babel-helper-define-map/package.json

This file was deleted.

197 changes: 0 additions & 197 deletions packages/babel-helper-define-map/src/index.ts

This file was deleted.

Expand Up @@ -17,7 +17,6 @@
"babel-plugin"
],
"dependencies": {
"@babel/helper-define-map": "workspace:^",
"@babel/helper-plugin-utils": "workspace:^"
},
"peerDependencies": {
Expand Down
@@ -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;
}
26 changes: 14 additions & 12 deletions packages/babel-plugin-transform-property-mutators/src/index.ts
@@ -1,5 +1,5 @@
import { declare } from "@babel/helper-plugin-utils";
import * as defineMap from "@babel/helper-define-map";
import { type MutatorMap, pushAccessor, toDefineObject } from "./define-map";
import { types as t } from "@babel/core";

export default declare(api => {
Expand All @@ -9,18 +9,20 @@ export default declare(api => {
name: "transform-property-mutators",

visitor: {
ObjectExpression(path, { file }) {
ObjectExpression(path) {
const { node } = path;
let mutatorMap: defineMap.MutatorMap | void;
let mutatorMap: MutatorMap | undefined;
const newProperties = node.properties.filter(function (prop) {
if (t.isObjectMethod(prop)) {
if (prop.kind === "get" || prop.kind === "set") {
mutatorMap ??= {};
if (!prop.computed) {
defineMap.push(mutatorMap, prop, null, file);
return false;
}
}
if (
t.isObjectMethod(prop) &&
!prop.computed &&
(prop.kind === "get" || prop.kind === "set")
) {
pushAccessor(
(mutatorMap ??= {}),
prop as t.ObjectMethod & { kind: "get" | "set"; computed: false },
);
return false;
}
return true;
});
Expand All @@ -37,7 +39,7 @@ export default declare(api => {
t.identifier("Object"),
t.identifier("defineProperties"),
),
[node, defineMap.toDefineObject(mutatorMap)],
[node, toDefineObject(mutatorMap)],
),
);
},
Expand Down

0 comments on commit 0e86d38

Please sign in to comment.