-
Notifications
You must be signed in to change notification settings - Fork 9
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
Conversation
There was a problem hiding this 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
__tests__/lib/mdast/variables/in.mdx
Outdated
@@ -1 +1 @@ | |||
Hello, {user.name} | |||
Hello, { user.name } |
There was a problem hiding this comment.
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?
Hello, { user.name } | |
**With Spaces:** { user.name } | |
**Without Spaces:** {user.name} |
processor/transform/variables.ts
Outdated
@@ -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*$/); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
Ok, fixed this up so we should be able to handle variables with spaces in the name. |
There was a problem hiding this 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!
processor/transform/variables.ts
Outdated
// @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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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!
This PR was released!🚀 Changes included in v7.10.4 |
🧰 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:
🧬 QA & Testing