Skip to content

Generates empty const statement #2112

Closed
@calebeby

Description

Initial checklist

Affected packages and versions

@mdx-js/mdx@2.1.3

Link to runnable example

No response

Steps to reproduce

My actual use case is that I have an astro site with an mdx file that uses shiki twoslash to highlight code snippets in the mdx. I haven't been able to narrow it down to a smaller reproducible code example, but I do have a fix!

https://github.com/mdx-js/mdx/blob/main/packages/mdx/lib/plugin/recma-jsx-rewrite.js#L390
This line is generating an empty const statement, which is invalid JS. It should not generate a const statement if there are no declarations.

This fix works:

if (declarations.length > 0) {
  statements.push({
    type: 'VariableDeclaration',
    kind: 'const',
    declarations
  })
}

I can send a PR with this change, but I'm not sure how to write a test for this, since I'm not sure how to narrow it down. Would you be able to help with a test, or would you accept a PR without a test?

Expected behavior

Don't emit an empty const declaration

function MDXContent(props = {}) {
  return <MDXLayout {...props}><_createMdxContent {...props} /></MDXLayout>;
}

Actual behavior

It is generating this :(

function MDXContent(props = {}) {
  const ;
  return <MDXLayout {...props}><_createMdxContent {...props} /></MDXLayout>;
}

Runtime

Node v16

Package manager

npm v8

OS

macOS

Build and bundle tools

Astro

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions