From e37096f7b2ed3efcc4aae271a7cf92c6cca091f2 Mon Sep 17 00:00:00 2001 From: Kelly Joseph Price Date: Thu, 5 Dec 2024 11:25:58 -0800 Subject: [PATCH] feat: allow spaces in variables --- __tests__/lib/mdast/index.test.ts | 8 +++ .../lib/mdast/variables-with-spaces/in.mdx | 1 + .../lib/mdast/variables-with-spaces/out.json | 72 +++++++++++++++++++ __tests__/lib/mdast/variables/in.mdx | 2 +- __tests__/lib/mdast/variables/out.json | 2 +- processor/transform/variables.ts | 18 +++-- 6 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 __tests__/lib/mdast/variables-with-spaces/in.mdx create mode 100644 __tests__/lib/mdast/variables-with-spaces/out.json diff --git a/__tests__/lib/mdast/index.test.ts b/__tests__/lib/mdast/index.test.ts index af5a711f1..fbf0b5d32 100644 --- a/__tests__/lib/mdast/index.test.ts +++ b/__tests__/lib/mdast/index.test.ts @@ -9,6 +9,9 @@ import tablesJson from './tables/out.json'; import variablesMdx from './variables/in.mdx?raw'; import variablesJson from './variables/out.json'; +import variablesWithSpacesMdx from './variables-with-spaces/in.mdx?raw'; +import variablesWithSpacesJson from './variables-with-spaces/out.json'; + import inlineImagesMdx from './images/inline/in.mdx?raw'; import inlineImagesJson from './images/inline/out.json'; @@ -28,6 +31,11 @@ describe('mdast transformer', async () => { expect(mdast(variablesMdx)).toStrictEqualExceptPosition(variablesJson); }); + it.only('parses variables with spaces', () => { + // @ts-ignore + expect(mdast(variablesWithSpacesMdx)).toStrictEqualExceptPosition(variablesWithSpacesJson); + }); + it('parses inline images', () => { // @ts-ignore expect(mdast(inlineImagesMdx)).toStrictEqualExceptPosition(inlineImagesJson); diff --git a/__tests__/lib/mdast/variables-with-spaces/in.mdx b/__tests__/lib/mdast/variables-with-spaces/in.mdx new file mode 100644 index 000000000..12b445c0a --- /dev/null +++ b/__tests__/lib/mdast/variables-with-spaces/in.mdx @@ -0,0 +1 @@ +Hello, { user['this is cursed'] } diff --git a/__tests__/lib/mdast/variables-with-spaces/out.json b/__tests__/lib/mdast/variables-with-spaces/out.json new file mode 100644 index 000000000..413361bb7 --- /dev/null +++ b/__tests__/lib/mdast/variables-with-spaces/out.json @@ -0,0 +1,72 @@ +{ + "children": [ + { + "children": [ + { + "position": { + "end": { + "column": 8, + "line": 1, + "offset": 7 + }, + "start": { + "column": 1, + "line": 1, + "offset": 0 + } + }, + "type": "text", + "value": "Hello, " + }, + { + "data": { + "hName": "Variable", + "hProperties": { + "name": "this is cursed" + } + }, + "position": { + "end": { + "column": 19, + "line": 1, + "offset": 18 + }, + "start": { + "column": 8, + "line": 1, + "offset": 7 + } + }, + "type": "readme-variable", + "value": "{ user['this is cursed'] }" + } + ], + "position": { + "end": { + "column": 19, + "line": 1, + "offset": 18 + }, + "start": { + "column": 1, + "line": 1, + "offset": 0 + } + }, + "type": "paragraph" + } + ], + "position": { + "end": { + "column": 1, + "line": 2, + "offset": 19 + }, + "start": { + "column": 1, + "line": 1, + "offset": 0 + } + }, + "type": "root" +} diff --git a/__tests__/lib/mdast/variables/in.mdx b/__tests__/lib/mdast/variables/in.mdx index e831b583b..65c2ff69c 100644 --- a/__tests__/lib/mdast/variables/in.mdx +++ b/__tests__/lib/mdast/variables/in.mdx @@ -1 +1 @@ -Hello, { user.name } +Hello, {user.name} diff --git a/__tests__/lib/mdast/variables/out.json b/__tests__/lib/mdast/variables/out.json index 5f7b0a48f..6f095bced 100644 --- a/__tests__/lib/mdast/variables/out.json +++ b/__tests__/lib/mdast/variables/out.json @@ -38,7 +38,7 @@ } }, "type": "readme-variable", - "value": "{ user.name }" + "value": "{user.name}" } ], "position": { diff --git a/processor/transform/variables.ts b/processor/transform/variables.ts index 6d99c6b2f..896b889cb 100644 --- a/processor/transform/variables.ts +++ b/processor/transform/variables.ts @@ -12,8 +12,18 @@ const variables = visit(tree, (node, index, parent) => { if (!['mdxFlowExpression', 'mdxTextExpression'].includes(node.type) || !('value' in node)) return; - const match = node.value.match(/^\s*user\.(?\S*)\s*$/); - if (!match) return; + // @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' || + expression.expression.property?.type !== 'Literal' + ) + return; + + const name = expression.expression.property.value; let variable = asMdx ? ({ @@ -23,7 +33,7 @@ const variables = { type: 'mdxJsxAttribute', name: 'name', - value: match.groups.value, + value: name, }, ], children: [], @@ -34,7 +44,7 @@ const variables = data: { hName: 'Variable', hProperties: { - name: match.groups.value, + name, }, }, value: `{${node.value}}`,