micromark / micromark-extension-mdxjs-esm

micromark extension to support MDX JS import/exports
https://unifiedjs.com
MIT License
14 stars 0 forks source link

Crash when exporting something imported in another ESM block #1

Closed JounQin closed 3 years ago

JounQin commented 3 years ago

Subject of the issue

/Users/JounQin/Workspaces/Local/test/node_modules/micromark-extension-mdxjs-esm/index.js:138
        throw new VMessage(
        ^
[5:1: Could not parse import/exports with acorn: SyntaxError: Unexpected character '#'] {
  reason: "Could not parse import/exports with acorn: SyntaxError: Unexpected character '#'",
  line: 5,
  column: 1,
  location: { start: { line: 5, column: 1 }, end: { line: null, column: null } },
  source: 'micromark-extension-mdxjs-esm',
  ruleId: 'acorn'
}

Your environment

Steps to reproduce

import React from "react";

export { React };

# MDX

Expected behavior

No error

Actual behavior

Error

JounQin commented 3 years ago

Besides, if we only have the following

import React from "react";

export { React };

Another error is thrown:

/Users/JounQin/Workspaces/Local/test/node_modules/micromark/dist/util/slice-chunks.js:11
    view = [chunks[startIndex].slice(startBufferIndex, endBufferIndex)]
                               ^

TypeError: Cannot read property 'slice' of null
    at sliceChunks (/Users/JounQin/Workspaces/Local/test/node_modules/micromark/dist/util/slice-chunks.js:11:32)
    at sliceStream (/Users/JounQin/Workspaces/Local/test/node_modules/micromark/dist/util/create-tokenizer.js:89:12)
    at Object.sliceSerialize (/Users/JounQin/Workspaces/Local/test/node_modules/micromark/dist/util/create-tokenizer.js:85:28)
    at atEnd (/Users/JounQin/Workspaces/Local/test/node_modules/micromark-extension-mdxjs-esm/index.js:123:24)
    at rest (/Users/JounQin/Workspaces/Local/test/node_modules/micromark-extension-mdxjs-esm/index.js:86:16)
    at lineStart (/Users/JounQin/Workspaces/Local/test/node_modules/micromark-extension-mdxjs-esm/index.js:111:14)
    at go (/Users/JounQin/Workspaces/Local/test/node_modules/micromark/dist/util/create-tokenizer.js:136:13)
    at main (/Users/JounQin/Workspaces/Local/test/node_modules/micromark/dist/util/create-tokenizer.js:130:9)
    at Object.write (/Users/JounQin/Workspaces/Local/test/node_modules/micromark/dist/util/create-tokenizer.js:70:5)
    at exitContainers (/Users/JounQin/Workspaces/Local/test/node_modules/micromark/dist/initialize/document.js:126:17)
wooorm commented 3 years ago

Huh, looks weird that this is happening (there are a ton of tests). Could you post the code you're using to run this / their versions too? Or attaxh the mdx sample as a file instead (maybe line endings is the problem?)

wooorm commented 3 years ago

I’m now getting, for: import a from "b";\n\nexport {a};\n\nc, the error: "Could not parse import/exports with acorn: SyntaxError: Export 'a' is not defined".

I guess acorn is failing because I’m parsing them separately. And the second is failing because it doesn’t “know” there was an a imported before.

But this is still a different error from what you’re seeing

JounQin commented 3 years ago

My testing code, all installed latest.

const fs = require("fs");

const fromMarkdown = require("mdast-util-from-markdown");
const toHast = require("mdast-util-to-hast");
const mdxFromMarkdown = require("mdast-util-mdx").fromMarkdown;
const mdxjs = require("micromark-extension-mdxjs");
const toEstree = require("hast-util-to-estree");

const doc = fs.readFileSync("./test.mdx", "utf8");

const mdast = fromMarkdown(doc, {
  extensions: [mdxjs()],
  mdastExtensions: [mdxFromMarkdown],
});

const hast = toHast(mdast, {
  passThrough: [
    "mdxFlowExpression",
    "mdxJsxFlowElement",
    "mdxJsxTextElement",
    "mdxTextExpression",
    "mdxjsEsm",
  ],
});

fs.writeFileSync("./test.json", JSON.stringify(toEstree(hast), null, 2));
wooorm commented 3 years ago

Thanks for that repro! Removing the blank line between the import/export for now works.

Weird stuff!

wooorm commented 3 years ago

Fixed!