Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing error when a component is used within a markdown list #279

Labels
🌊 blocked/upstream This cannot progress before something external happens first help wanted 🙏 This could use your insight or help 🐛 type/bug This is a problem 🙆 yes/confirmed This is confirmed and ready to be worked on

Comments

@LeBenLeBen
Copy link

Subject of the issue

The following code:

- <LinkTo kind="docs-packages-vuetify-preset" story="page">
    Vuetify preset
  </LinkTo>
  : some extra text describing the preset

Trigger an error on line 3:

  </LinkTo>
    ^^^^^^
    Parsing error: '>' expected.

Adding an empty line between the last two lines fixes the issue, but the rendered output is not the same. Also putting it all in a single line works, but it’s not "Prettier compliant" and gets reformatted on multiple lines. If the same code is placed at the root (not within a markdown list), it works as well.

It does pretty much the same with a regular element instead of a component, but the error is different:

Parsing error: Identifier expected

Note that the code compiles properly, the problem is only at EsLint level.

Your environment

  • OS: macOS 10.15.7
  • Packages: eslint-plugin-mdx 1.9.0
  • Env: Node 14.16.0, npm 6.14.11
@LeBenLeBen LeBenLeBen added 🐛 type/bug This is a problem 🙉 open/needs-info This needs some more info labels Mar 5, 2021
@JounQin
Copy link
Member

JounQin commented Mar 5, 2021

Please provide a minimal runnable reproduction.

@LeBenLeBen
Copy link
Author

Here you go: https://github.com/LeBenLeBen/eslint-mdx-repro-279

npm install
npm run validate

@JounQin
Copy link
Member

JounQin commented Mar 5, 2021

@wooorm I tried to parse the following:

- <div kind="docs-packages-vuetify-preset" story="page">
    Vuetify preset
  </div>
  : some extra text describing the preset

And logged console.log(jsxNodes[0], this.combineLeftJsxNodes(jsxNodes)) at https://github.com/mdx-js/eslint-mdx/blob/master/packages/eslint-mdx/src/traverse.ts#L117

And the log is following:

{
  type: 'jsx',
  data: undefined,
  value: '<div kind="docs-packages-vuetify-preset" story="page">\n' +
    '  Vuetify preset\n' +
    '</div>',
  position: {
    start: { line: 1, column: 0, offset: 2 },
    end: { line: 3, column: 3, offset: 80 }
  }
}

{
  type: 'jsx',
  data: undefined,
  value: '<div kind="docs-packages-vuetify-preset" story="page">\n' +
    '    Vuetify preset\n' +
    '  </',
  position: {
    start: { line: 1, column: 0, offset: 2 },
    end: { line: 3, column: 3, offset: 80 }
  }
}

/Users/JounQin/Workspaces/GitHub/eslint-mdx/test/fixtures/jsx-text.mdx
  3:4  error  Parsing error: /Users/JounQin/Workspaces/GitHub/eslint-mdx/test/fixtures/jsx-text.mdx: Unexpected token (3:4)

  1 | <$><div kind="docs-packages-vuetify-preset" story="page">
  2 |     Vuetify preset
> 3 |   </</$>
    |     ^

✖ 1 problem (1 error, 0 warnings)

So it seems to be an issue of remark-mdx which produces incorrect offset.

@JounQin JounQin added help wanted 🙏 This could use your insight or help 🌊 blocked/upstream This cannot progress before something external happens first 🐛 type/bug This is a problem 🙆 yes/confirmed This is confirmed and ready to be worked on and removed 🐛 type/bug This is a problem 🙉 open/needs-info This needs some more info labels Mar 5, 2021
@JounQin
Copy link
Member

JounQin commented Mar 5, 2021

@LeBenLeBen Temporary workaround:

<!-- prettier-ignore -->
- <div kind="docs-packages-vuetify-preset" story="page">
    Vuetify preset
</div>
  : some extra text describing the preset

Sorry, I can't fix it easily due to remark-mdx itself. Let's wait for @wooorm's response.

@JounQin
Copy link
Member

JounQin commented Mar 5, 2021

@wooorm I have to do something like following, I don't know if it is as expected:

combineLeftJsxNodes(jsxNodes: Node[], parent?: Parent): Node {
  const start = jsxNodes[0].position.start
  const end = { ...last(jsxNodes).position.end }
  // fix #279
  if (parent.type === 'listItem') {
    end.offset += parent.position.indent.reduce(
      (acc, indent, index) => acc + (index ? indent + 1 : 0),
      0,
    )
  }
  return {
    type: 'jsx',
    data: jsxNodes[0].data,
    value: this.code.slice(start.offset, end.offset),
    position: {
      start,
      end,
    },
  }
}

JounQin added a commit that referenced this issue Mar 6, 2021
@wooorm
Copy link
Member

wooorm commented Mar 6, 2021

Parsing it at least works on main, and maybe on the last v2 beta.

@JounQin posiiton.indent went away in the last remark btw.
Might it be time to investigate the new AST that’s on main already?

@JounQin
Copy link
Member

JounQin commented Mar 6, 2021

@wooorm I'm fine to start working on next version, but I'd like to fix this error on current stable version too. I want to confirm if my fix will always work, or, can you patch a fix on remark-mdx@1 for the incorrect position info.

@wooorm
Copy link
Member

wooorm commented Mar 6, 2021

I do not know if your fix will always work, maybe?
And I'm personally not interested in patching v1. I rewrote the parsing in april and in november, solving tons of issues. I'm not interested in again fixing things I already fixed

@JounQin
Copy link
Member

JounQin commented Mar 11, 2021

@LeBenLeBen Please try v1.9.1

@LeBenLeBen
Copy link
Author

Perfect, it works now. Thank you for looking into this so quickly 💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment