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

fix: allow spaces in variables #1035

Merged
merged 7 commits into from
Dec 5, 2024
Merged

Conversation

kellyjosephprice
Copy link
Collaborator

@kellyjosephprice kellyjosephprice commented Dec 5, 2024

PR App Fix RM-XYZ

🧰 Changes

Allows spaces in user variables.

Previous, the transformer was validating user variables with a regex that didn't allow spaces. This PR changes the regex to allow spaces. This also allows spaces in the variables names, which would have crashed the parser before.

All of the following are now valid:

{user.name}

{ user.name }

{user['dont do this']}

🧬 QA & Testing

@kellyjosephprice kellyjosephprice marked this pull request as ready for review December 5, 2024 19:03
Copy link
Contributor

@rafegoldberg rafegoldberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couple of quick things

@@ -1 +1 @@
Hello, {user.name}
Hello, { user.name }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe both just in case?

Suggested change
Hello, { user.name }
**With Spaces:** { user.name }
**Without Spaces:** {user.name}

@@ -12,7 +12,7 @@ const variables =
visit(tree, (node, index, parent) => {
if (!['mdxFlowExpression', 'mdxTextExpression'].includes(node.type) || !('value' in node)) return;

const match = node.value.match(/^user\.(?<value>.*)$/);
const match = node.value.match(/^\s*user\.(?<value>\S*)\s*$/);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think variable names can include spaces though? So this should still be .* right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh no, that's so cursed

@kellyjosephprice
Copy link
Collaborator Author

Ok, fixed this up so we should be able to handle variables with spaces in the name.

Copy link
Contributor

@rafegoldberg rafegoldberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice; approved save for one question!

Comment on lines 15 to 29
// @ts-expect-error - estree is not defined on our mdx types?!
if (node.data.estree.type !== 'Program') return;
// @ts-expect-error - estree is not defined on our mdx types?!
const [expression] = node.data.estree.body;
if (
!expression ||
expression.type !== 'ExpressionStatement' ||
!['Literal', 'Identifier'].includes(expression.expression.property?.type)
)
return;

const name =
expression.expression.property.type === 'Identifier'
? expression.expression.property.name
: expression.expression.property.value;
Copy link
Contributor

@rafegoldberg rafegoldberg Dec 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this a lot more than using regex, but it seems like this new logic will apply to all {vars} (not just user.-prefixed ones…) Does that matter at all?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OH shit, I forget to check the module is named user. Good catch!

@kellyjosephprice kellyjosephprice merged commit 5733446 into next Dec 5, 2024
13 checks passed
@kellyjosephprice kellyjosephprice deleted the fix/spaces-in-variables branch December 5, 2024 20:45
rafegoldberg pushed a commit that referenced this pull request Dec 5, 2024
## Version 7.10.4
### 🛠 Fixes & Updates

* allow spaces in variables ([#1035](#1035)) ([5733446](5733446))
* remove old parser ([#1034](#1034)) ([47aca89](47aca89))

<!--SKIP CI-->
@rafegoldberg
Copy link
Contributor

This PR was released!

🚀 Changes included in v7.10.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants