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

feat(macro): support transforming member expressions to named args #1874

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/macro/src/macroJs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
SourceLocation,
StringLiteral,
TemplateLiteral,
MemberExpression,
} from "@babel/types"
import { NodePath } from "@babel/traverse"

Expand Down Expand Up @@ -367,11 +368,31 @@ export default class MacroJs {
}
}

memberExpressionToArgument(exp: MemberExpression): string {
let parts: string[] = []

if (this.types.isThisExpression(exp.object)) {
// ignore this
} else {
parts.unshift(this.expressionToArgument(exp.object))
}

parts.push(
this.expressionToArgument(
this.types.isPrivateName(exp.property) ? exp.property.id : exp.property
)
)

return parts.join("_")
}

expressionToArgument(exp: Expression): string {
if (this.types.isIdentifier(exp)) {
return exp.name
} else if (this.types.isStringLiteral(exp)) {
return exp.value
} else if (this.types.isMemberExpression(exp)) {
return this.memberExpressionToArgument(exp)
} else {
return String(this._expressionIndex())
}
Expand Down
31 changes: 30 additions & 1 deletion packages/macro/src/macroJsx.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as babelTypes from "@babel/types"
import {
ConditionalExpression,
MemberExpression,
Expression,
JSXAttribute,
JSXElement,
Expand Down Expand Up @@ -285,6 +286,7 @@ export default class MacroJSX {
if (exp.isJSXElement()) {
return this.tokenizeNode(exp)
}

return [this.tokenizeExpression(exp)]
} else if (path.isJSXElement()) {
return this.tokenizeNode(path)
Expand Down Expand Up @@ -464,8 +466,35 @@ export default class MacroJSX {
}
}

memberExpressionToArgument(path: NodePath<MemberExpression>): string {
let parts: string[] = []

if (this.types.isThisExpression(path.node.object)) {
// ignore this
} else {
parts.unshift(this.expressionToArgument(path.get("object")))
}

const propertyPath = path.get("property")

parts.push(
this.expressionToArgument(
propertyPath.isPrivateName() ? propertyPath.get("id") : propertyPath
)
)
return parts.join("_")
}

expressionToArgument(path: NodePath<Expression | Node>): string {
return path.isIdentifier() ? path.node.name : String(this.expressionIndex())
if (this.types.isIdentifier(path.node)) {
return path.node.name
} else if (this.types.isStringLiteral(path.node)) {
return path.node.value
} else if (path.isMemberExpression()) {
return this.memberExpressionToArgument(path)
} else {
return String(this.expressionIndex())
}
}

/**
Expand Down
Loading