mdx-js / eslint-mdx

ESLint Parser/Plugin for MDX
https://npmjs.org/eslint-plugin-mdx
MIT License
264 stars 32 forks source link

Parsing error when parserOptions.ecmaVersion is not set #366

Closed PaulTondeur closed 2 years ago

PaulTondeur commented 2 years ago

Initial checklist

Affected packages and versions

latest

Link to runnable example

No response

Steps to reproduce

A very basic example following the instructions within this repo is already failing for me with a parsing error:

  0:0  error  Parsing error: ImportDeclaration should appear when the mode is ES6 and in the module context

I've reduced my .eslintrc.json to the simplest possible.

{
  "plugins": ["@typescript-eslint"],
  "extends": [
    "next/core-web-vitals",
    "plugin:prettier/recommended",
    "plugin:mdx/recommended"
  ],
  "settings": {
    "mdx/code-blocks": true,
    "mdx/language-mapper": {}
  }
}

When I add the following to the .eslintrc.json it works as expected

  "parserOptions": {
    "ecmaVersion": 12
  }

If this isn't a bug, it should be documented somewhere. It took a lot of time to figure this out.

Expected behavior

My MDX file should be linted without errors

Actual behavior

0:0 error Parsing error: ImportDeclaration should appear when the mode is ES6 and in the module context

Runtime

Node v16

Package manager

yarn v2, yarn v1

OS

macOS

Build and bundle tools

Next.js

JounQin commented 2 years ago

Please provide a runnable reproduction.

There is a default ecmaVersion for parsing at https://github.com/mdx-js/eslint-mdx/blob/master/packages/eslint-mdx/src/parser.ts#L48

PaulTondeur commented 2 years ago

What else would you like to have to reproduce? As detailed above with the basic eslint config. All that might be missing is another simplest mdx file with an import or jsx code.

JounQin commented 2 years ago

A demo GitHub repository for example.

hisapy commented 2 years ago

Hi everyone,

In my case I ended up with the following .eslintrc.json config to get rid of this error:

{
  "plugins": ["@typescript-eslint"],
  "extends": [
    "next/core-web-vitals",
    "plugin:@typescript-eslint/recommended",
    "plugin:storybook/recommended",
    "prettier"
  ],
  "rules": {
    "@typescript-eslint/no-unused-vars": "error",
    "@typescript-eslint/no-explicit-any": "error",
    "semi": [1, "never"]
  },
  "overrides": [
    {
      "files": ["*.mdx", "*.md"],
      "extends": "plugin:mdx/recommended",
      "parserOptions": {
        "ecmaVersion": "latest"
      }
    }
  ]
}

What I added for mdx is in overrides.

I had to add *.md in files because # was being reported as invalid character in VSCode

JounQin commented 2 years ago
# yarn 1
yarn add https://pkg.csb.dev/mdx-js/eslint-mdx/commit/abdc9db9/eslint-mdx https://pkg.csb.dev/mdx-js/eslint-mdx/commit/abdc9db9/eslint-plugin-mdx
# yarn 2, 3
yarn add eslint-mdx@https://pkg.csb.dev/mdx-js/eslint-mdx/commit/abdc9db9/eslint-mdx/_pkg.tgz eslint-plugin-mdx@https://pkg.csb.dev/mdx-js/eslint-mdx/commit/abdc9db9/eslint-plugin-mdx/_pkg.tgz
# npm
npm i https://pkg.csb.dev/mdx-js/eslint-mdx/commit/abdc9db9/eslint-mdx https://pkg.csb.dev/mdx-js/eslint-mdx/commit/abdc9db9/eslint-plugin-mdx

Please help us to test the fresh new version.

JounQin commented 2 years ago

Close in favor of #402, wait for a release from release-v1 branch @wooorm.

sneko commented 1 year ago

I can't explain why but this issue is still happening with latest version. I had to make a mix of multiple suggestions above:

  overrides: [
    {
      files: ['*.md', '*.mdx'],
      extends: 'plugin:mdx/recommended',
      parserOptions: {
        ecmaVersion: 12,
      },
    },
  ],

(using latest for ecmaVersion was doing nothing, maybe that's the 13 (latest) that is problematic?)