Skip to content

Commit

Permalink
feat: optional mergePros
Browse files Browse the repository at this point in the history
  • Loading branch information
Amour1688 committed Sep 9, 2020
1 parent 45290ca commit e16695d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
30 changes: 20 additions & 10 deletions packages/babel-plugin-jsx/src/buildProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,18 @@ const getJSXAttributeValue = (
const transformJSXSpreadAttribute = (
nodePath: NodePath,
path: NodePath<t.JSXSpreadAttribute>,
mergeArgs: (t.ObjectProperty | t.Expression)[],
mergeProps: boolean,
args: (t.ObjectProperty | t.Expression | t.SpreadElement)[],
) => {
const argument = path.get('argument') as NodePath<t.ObjectExpression>;
const { properties } = argument.node;
if (!properties) {
if (argument.isIdentifier()) {
walksScope(nodePath, (argument.node as t.Identifier).name, SlotFlags.DYNAMIC);
}
mergeArgs.push(argument.node);
args.push(mergeProps ? argument.node : t.spreadElement(argument.node));
} else {
mergeArgs.push(t.objectExpression(properties));
args.push(t.objectExpression(properties));
}
};

Expand All @@ -71,7 +72,10 @@ const mergeAsArray = (existing: t.ObjectProperty, incoming: t.ObjectProperty) =>
}
};

const dedupeProperties = (properties: t.ObjectProperty[] = []) => {
const dedupeProperties = (properties: t.ObjectProperty[] = [], mergeProps?: boolean) => {
if (!mergeProps) {
return properties;
}
const knownProps = new Map<string, t.ObjectProperty>();
const deduped: t.ObjectProperty[] = [];
properties.forEach((prop) => {
Expand Down Expand Up @@ -146,6 +150,7 @@ const buildProps = (path: NodePath<t.JSXElement>, state: State) => {
let hasDynamicKeys = false;

const mergeArgs: (t.CallExpression | t.ObjectExpression | t.Identifier)[] = [];
const { mergeProps = true } = state.opts;
props
.forEach((prop) => {
if (prop.isJSXAttribute()) {
Expand Down Expand Up @@ -273,8 +278,8 @@ const buildProps = (path: NodePath<t.JSXElement>, state: State) => {
));
}
} else {
if (properties.length) {
mergeArgs.push(t.objectExpression(dedupeProperties(properties)));
if (properties.length && mergeProps) {
mergeArgs.push(t.objectExpression(dedupeProperties(properties, mergeProps)));
properties = [];
}

Expand All @@ -283,7 +288,8 @@ const buildProps = (path: NodePath<t.JSXElement>, state: State) => {
transformJSXSpreadAttribute(
path as NodePath,
prop as NodePath<t.JSXSpreadAttribute>,
mergeArgs,
mergeProps,
mergeProps ? mergeArgs : properties,
);
}
});
Expand Down Expand Up @@ -314,10 +320,9 @@ const buildProps = (path: NodePath<t.JSXElement>, state: State) => {
}

let propsExpression: t.Expression | t.ObjectProperty | t.Literal = t.nullLiteral();

if (mergeArgs.length) {
if (properties.length) {
mergeArgs.push(t.objectExpression(dedupeProperties(properties)));
mergeArgs.push(t.objectExpression(dedupeProperties(properties, mergeProps)));
}
if (mergeArgs.length > 1) {
propsExpression = t.callExpression(
Expand All @@ -329,7 +334,12 @@ const buildProps = (path: NodePath<t.JSXElement>, state: State) => {
propsExpression = mergeArgs[0];
}
} else if (properties.length) {
propsExpression = t.objectExpression(dedupeProperties(properties));
// single no need for spread
if (properties.length === 1 && t.isSpreadElement(properties[0])) {
propsExpression = (properties[0] as unknown as t.SpreadElement).argument;
} else {
propsExpression = t.objectExpression(dedupeProperties(properties, mergeProps));
}
}

return {
Expand Down
1 change: 1 addition & 0 deletions packages/babel-plugin-jsx/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type State = {
interface Opts {
transformOn?: boolean;
optimize?: boolean;
mergeProps?: boolean;
isCustomElement?: (tag: string) => boolean;
}

Expand Down

0 comments on commit e16695d

Please sign in to comment.