Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline & simplify @babel/helper-define-map #15274

Merged
merged 4 commits into from Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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