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.

AST node name changes in next.9

See original GitHub issue

Subject of the issue

The new node types in v2-next.9 renames the types

  • export
  • import
  • jsx

into

  • mdxjsEsm
  • mdxJsxFlowElement

I believe a full list of the 5 new node types can be found here

The new node types for import and export specifically seem to mostly be additive changes (specifically, inserting the new estree AST as data on the node), but the name change will break gatsby-plugin-mdx and anyone else who is depending on the export or import nodes (as well as the jsx node).

Also note that the behavior here is slightly different. An import and an export next to each other are now combined into a single mdxjsEsm node.

Your environment

Steps to reproduce

const mdx = require("@mdx-js/mdx");

main();

async function main() {
  const t = mdx.createCompiler().parse(string);

  const f = await mdx.createCompiler().parse(`
import Thing from 'somewhere';
export const meta = {};

<Test/>
`);
  console.log(f);
}

Expected behaviour

Expected import node and export node to be separate and include one import or export per node.

Actual behaviour

both import and export are combined into a single new node type, even though the .value string still exists and would provide backwards compatibility.

{
  type: 'root',
  children: [
    {
      type: 'mdxjsEsm',
      value: "import Thing from 'somewhere';\nexport const meta = {};",
      position: [Object],
      data: [Object]
    },
    {
      type: 'mdxJsxFlowElement',
      name: 'Test',
      attributes: [],
      children: [],
      position: [Object]
    }
  ],
  position: {
    start: { line: 1, column: 1, offset: 0 },
    end: { line: 6, column: 1, offset: 65 }
  }
}

Additional questions

Is it also possible to provide backwards compatibility for the jsx node?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
johnocommented, Apr 3, 2021

Ah I see, I suppose we can get close to current support by exposing some APIs for walking the estree for folks that want to do things like plucking/removing imports and specific types of exports.

1reaction
wooormcommented, Apr 3, 2021

The reason for having a single node that contains arbitrary JavaScript is a) in alignment with how it’s parsed, but importantly b), to allow extensions to what’s allowed. Such as var/let/const (GH-1046) without adding new nodes (so, in a minor release).

That single arbitrary JS node also allows MDX to be extended to support:

```js eval
var number = Math.PI
```

The number is {number}.

…through a plugin that walks mdast and replaces code[eval] with such an mdxjsEsm node.

Another idea is to allow more vue/svelte like syntax:

<script>
  var number = Math.PI
</script>

The number is {number}.

To answer “they’re semantically […] different” — On the JavaScript level they are. And they are separately available as a subtree. Folks can inspect and transform those subtrees, which is in my opinion preferable over trying to handle a serialized value (such as what remark-mdx-frontmatter does), Exposing the JavaScript in a plugin mechanism is now also a minor release away in @mdx-js/mdx. This would allow arbitrary JavaScript transforms, such as minification (terser) or bundling (what mdx-bundler does currently).

Read more comments on GitHub >

github_iconTop Results From Across the Web

ast — Abstract Syntax Trees — Python 3.11.1 documentation
A named expression. This AST node is produced by the assignment expressions operator (also known as the walrus operator). As opposed to the...
Read more >
Chapter 8. Building AST. Part 1 - Andrew Shitov
In this chapter, we will be working on implementing AST, the abstract syntax tree, that represents the program in the form of connected...
Read more >
Mutating an AST - Practical Abstract Syntax Trees
Currently, the node's name ( node.name.name ) is button , but now we need to change it to our Button component. The first...
Read more >
AST Matcher Reference - Clang
Return type Name Parameters Matcher attr Matcher... Matcher cxxBaseSpecifier Matcher... Matcher cxxCtorInitializer Matcher...
Read more >
Node (javaparser-core 3.15.9 API) - javadoc.io
Any change in the tree is sent as an event to any observers watching. ... Walks the AST with pre-order traversal, returning the...
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