eslint-mdx icon indicating copy to clipboard operation
eslint-mdx copied to clipboard

Parse error for multiline object props on React components

Open mike-fam opened this issue 3 years ago • 2 comments
trafficstars

Initial checklist

Affected packages and versions

2.0.5

Link to runnable example

https://github.com/mike-fam/eslint-mdx-multiline-prop-error

Steps to reproduce

  1. Create a new workspace with npm and install eslint with eslint-plugin-mdx
  2. Eslint configuration:
{
	"overrides": [
		{
			"files": ["*.mdx"],
			"extends": "plugin:mdx/recommended"
		}
	],
	"rules": {}
}
  1. Create an mdx file with a react component with a multiline object prop e.g.
<button
	style={{
		display: 'block'
	}}
>
	Test
</button>
  1. Lint the file, note the error: Parsing error: Expected value to be truthy

Expected behavior

The file should pass linting without any problems, as this is valid mdx syntax.

Actual behavior

Linting produces the following error: 0:0 error Parsing error: Expected value to be truthy

Runtime

Node v16, Node v14

Package manager

yarn v1

OS

macOS

Build and bundle tools

webpack

mike-fam avatar Oct 05 '22 05:10 mike-fam

Thanks for reporting issue by using the template, I'm on a vocation these days, I'll check later soon.

JounQin avatar Oct 05 '22 09:10 JounQin

It seems related to tab, if I change the source codes with spaces there will be no issue:

<button
  style={{
    display: "block",
  }}
>
  Test
</button>

The assertion error is thrown at https://github.com/mdx-js/eslint-mdx/blob/a1998c787f0049ae0cd705fc51f079c63ded75b2/packages/eslint-mdx/src/worker.ts#L491

I'll check how to fix this with tab


For text:

<button
	style={{
		display: 'block'
	}}
>
	Test
</button>

attrValue.data.estree.range is [16, 40], but it should be [16, 39] IMO, could it be an issue from micromark-extension-mdx-expression? @wooorm

I'll try to draft a simple reproduction for it.

JounQin avatar Oct 09 '22 10:10 JounQin

Hi @JounQin, thanks for all your great work and investigating this. Is there any new update on this? For now, do you recommend replacing all the tabs in our stories with spaces or waiting for a new fix to be released?

mike-fam avatar Oct 27 '22 22:10 mike-fam

Hey @mike-fam did you manage to work around this one? I'm facing the same thing.

morganfeeney avatar Jan 10 '23 11:01 morganfeeney

I investigated a bit this issue.

With the following code:

-   list

    <Components data={`
    `} />

I also have the error Parsing error: Expected value to be truthy

This crashes because of this line: https://github.com/mdx-js/eslint-mdx/blob/9331895e902e4eb08cf2343bd7ccac2adf83af40/packages/eslint-mdx/src/worker.ts#L413.

When adding console.logs before and after attrValStart = prevCharOffset(attrValStart - 1); attrValEnd = nextCharOffset(attrValEnd);, here is what I have:

image

I think that the reason of the offset is because when writing <Component data={`\n `} />, acorn returns this node: image

But the offsets start & end on the data attribute (32, 35) don't match the right start & end (end should have been 32, 38): image

I'm not sure why the spaces are dropped in this estree (which comes from this processor https://github.com/mdx-js/eslint-mdx/blob/9331895e902e4eb08cf2343bd7ccac2adf83af40/packages/eslint-mdx/src/worker.ts#L79).

Ayc0 avatar Jan 14 '23 03:01 Ayc0

Hi @JounQin @wooorm sorry for bothering you. Is there any update on this issue?

mike-fam avatar Mar 03 '23 05:03 mike-fam

If I’m correct, this repo is mainly maintained by @JounQin. However, we haven’t heard from him in a while.

Maybe we should create and pin a Looking for maintainers issue?

remcohaszing avatar Mar 03 '23 13:03 remcohaszing

As I and @Ayc0 discovered above, it is not an internal issue but due to acorn return incorrect location info, but I'm not sure if it comes from acorn itself or micromark-extension-mdx-expression instead because the location info is restored at

https://github.com/micromark/micromark-extension-mdx-expression/blob/88c175f9ac684efc9306e23642807657df71d6a6/packages/micromark-util-events-to-acorn/dev/index.js#L216

cc @wooorm

JounQin avatar Apr 01 '23 16:04 JounQin

@JounQin @wooorm Would you guys mind creating an issue in either acorn or micromark-extension-mdx-expression, since I know little context to how the eslint-mdx plugin is implemented and how it uses those dependencies

mike-fam avatar Apr 26 '23 04:04 mike-fam

@wooorm Sorry to ping again, but I think we need your help here.

JounQin avatar May 04 '23 13:05 JounQin

I dunno where this problem is, I’d appreciate a reduced test case. If it isn’t in eslint-mdx, I’d appreciate a test case that doesn’t use eslint-mdx?

wooorm avatar May 04 '23 13:05 wooorm

Working on a minimal reproduction without eslint-mdx.

JounQin avatar May 04 '23 14:05 JounQin

@wooorm Please check https://github.com/JounQin/test/blob/mdx-tab/test.mjs#L29-L32

Besides, https://github.com/mdx-js/eslint-mdx/issues/435#issuecomment-1382644367 could be another range issue not related to tab but list.


I tried acorn-jsx directly, there is no range issue:

/**
 * @typedef {import('estree').Program} Program
 * @typedef {import('estree-jsx').JSXElement} JSXElement
 * @typedef {import('estree-jsx').JSXAttribute} JSXAttribute
 */

const root =
  // @ts-expect-error
  /** @type {Program} */ (acorn.Parser.extend(acornJsx()).parse(contents, {
    ecmaVersion: 2020,
  }))
const statement = root.body[0]
if (typeof statement === 'object' && statement.type === 'ExpressionStatement') {
  const jsxElement = /** @type {JSXElement} */ (statement.expression)
  const attribute =
    /** @type {JSXAttribute} */ (jsxElement.openingElement.attributes[0])
  console.log(attribute.value)
}
Node {
  type: 'JSXExpressionContainer',
  start: 15,
  end: 21,
  expression: Node {
    type: 'TemplateLiteral',
    start: 16,
    end: 20,  // correct
    expressions: [],
    quasis: [ [Node] ]
  }
}

JounQin avatar May 04 '23 14:05 JounQin

@JounQin did you see my above commit?

wooorm avatar May 17 '23 14:05 wooorm

@wooorm Thanks! It seems working to me now. @mike-fam @morganfeeney @Ayc0 Can you help to confirm? You may need to delete your npm-lock.json or yarn.lock, etc to upgrade all related packages to confirm.

JounQin avatar May 17 '23 16:05 JounQin

I think that this is working for me too! Thank you very much! 🎉

(with yarn 2+, you can run yarn up -R micromark-util-events-to-acorn to upgrade micromark-util-events-to-acorn in sub-dependencies without having to remove the whole yarn.lock)

Ayc0 avatar May 17 '23 18:05 Ayc0