remcohaszing / rehype-mdx-title

A rehype MDX plugin for exposing the page title
MIT License
14 stars 1 forks source link

Potential idea: add error when `name` is already exported #2

Open wooorm opened 3 years ago

wooorm commented 3 years ago

Finding out which names are already exported is a bit complex, so this might be too much work. However, it could be nice to have a better error message than what would be given when the resulting file is imported:

~/src/about.server.mdx:4
export const title = 'About';
             ^

SyntaxError: Identifier 'title' has already been declared
    at Loader.moduleStrategy (node:internal/modules/esm/translators:146:18)
    at async link (node:internal/modules/esm/module_job:67:21)
remcohaszing commented 3 years ago

I think it’s very doable. The problem aren’t the exports, but the declared variables. In case of MDX these are imports and exports. Since they are always top-level, we just need to iterate over all MDX ESM nodes and find all imports and exports of their estree program body.

The same issue exists for remark-mdx-frontmatter, so I’ll create a reusable mdast utility for this.

wooorm commented 2 years ago

Some custom plugins might expose “normal” variables in the blocks btw. So it might be more complex, assuming that use case is covered

remcohaszing commented 2 years ago

Do you mean something like this?

export const { foo: { bar: baz } } = whatever

I think this could handled by concatenating the program data of all mdxEsm nodes and processing the resulting body using periscopic.

wooorm commented 2 years ago

No, I mean without the export, so adding:

const a = 1;
var b = 2;
let c = 3;
function d() {}

Indeed, periscopic

remcohaszing commented 2 years ago

I’m not entirely sure, but I think the only way to add such variables would be through mdxEsm nodes, right?

wooorm commented 2 years ago

Yep, correct!