question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generates empty `const` statement

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
wooormcommented, Oct 11, 2022

Released in 2.1.5!

2reactions
calebebycommented, Sep 22, 2022

FWIW I have a PR open that is close… I just started school so I’m super busy right now but I’m planning to get back to it soon

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to declare 'const' as an empty variable - Stack Overflow
I wonder if it is possible to declare 'const exch' as empty first since I need that variable to be global for a...
Read more >
const - JavaScript - MDN Web Docs - Mozilla
The const declaration creates block-scoped constants, much like variables declared using the let keyword. The value of a constant can't be ...
Read more >
JavaScript Const - GeeksforGeeks
Creates only read-only references to value. Syntax: const const_name; const x;. Example 1: It describes that the const variable cannot be ...
Read more >
In Javascript how to empty an array - Tutorialspoint
There are multiple ways to clear/empty an array in JavaScript. You need to use them based on the context. Let us look at...
Read more >
7 Tips to Handle undefined in JavaScript - Dmitri Pavlutin
2. Scenarios that create undefined · Tip 1: Favor const , otherwise use let , but say goodbye to var · Tip 2:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found