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

[Bug]: parserOptions.ecmaFeatures.jsx not set when using presets #3523

Closed
2 tasks done
CleyFaye opened this issue Jan 18, 2023 · 6 comments
Closed
2 tasks done

[Bug]: parserOptions.ecmaFeatures.jsx not set when using presets #3523

CleyFaye opened this issue Jan 18, 2023 · 6 comments
Labels

Comments

@CleyFaye
Copy link

Is there an existing issue for this?

  • I have searched the existing issues and my issue is unique
  • My issue appears in the command-line and not only in the text editor

Description Overview

When extending eslint configuration with "extends": ["plugin:react/recommended"], the parserOptions are not set to enable JSX support.

According to the README, parserOptions.ecmaFeatures.jsx should be set to true by all presets.

% cat .eslintrc.cjs
module.exports = {
  parserOptions: {
    ecmaVersion: "latest",
    sourceType: "module"
  },
  extends: [
    "eslint:recommended",
    "plugin:react/all"
  ],
  settings: {
    react: {
      version: "detect"
    }
  }
}

% npx eslint --print-config somefile.js | jq .parserOptions
{
  "ecmaVersion": "latest",
  "sourceType": "module"
}

This property not being set breaks parsing of JavaScript files containing JSX.

Expected Behavior

According to the documentation:

Note: These configurations will import eslint-plugin-react and enable JSX in parser options.

But using the presets (all and recommended) do not set this property.

It used to work in v7.31.11 but is broken in v7.32.1, which is actually tagged "latest" on npmjs.

With v7.31.11, the example in the overview above yields this result:

% npx eslint --print-config somefile.js | jq .parserOptions
{
  "ecmaVersion": "latest",
  "sourceType": "module",
  "ecmaFeatures": {
    "jsx": true
  }
}

and eslint works fine with files containing JSX.

eslint-plugin-react version

v7.32.1

eslint version

v8.32.0

node version

v18.13.0

@CleyFaye CleyFaye added the bug label Jan 18, 2023
@ljharb
Copy link
Member

ljharb commented Jan 18, 2023

I would suggest using the airbnb config, instead of the "recommended" config (which isn't actually recommended, because it's very out of date due to semver constraints).

@ljharb
Copy link
Member

ljharb commented Jan 18, 2023

That said, it's currently set here: https://github.com/jsx-eslint/eslint-plugin-react/blob/master/configs/all.js#L29-L35 for the flat config, so we should also be setting it for the legacy config, assuming the flat config won't error on it.

@ljharb ljharb closed this as completed in 0479acd Jan 18, 2023
@lkraav
Copy link

lkraav commented Mar 9, 2024

7.34.0: somehow this flat config only applies languageOptions.parserOptions.ecmaFeatures.jsx as a standalone config object, but not when spreading inside another. Is this a bug or a feature?

// @see https://eslint.org/docs/latest/use/configure/migration-guide#predefined-and-shareable-configs
import js from "@eslint/js";
import reactRecommended from "eslint-plugin-react/configs/recommended.js";

export default [
  js.configs.recommended,
  // reactRecommended, // `languageOptions.parserOptions.ecmaFeatures.jsx` is set, but I don't want it as a global config objet?
  {
    files: ["apps/admin/**/*.{js,jsx}"],
    ...reactRecommended, // `languageOptions.parserOptions.ecmaFeatures.jsx` is missing.
  },
];

@ljharb
Copy link
Member

ljharb commented Mar 9, 2024

That's likely because languageOptions is non-enumerable, so it won't spread.

@lkraav
Copy link

lkraav commented Mar 9, 2024

That's likely because languageOptions is non-enumerable, so it won't spread.

Yeah I suspected this being related after inspecting the code.

For a new person, this distinction might be a bit confusing, because

You can of course add/override some properties.
**Note**: Our shareable configs does not preconfigure `files` or [`languageOptions.globals`](https://eslint.org/docs/latest/user-guide/configuring/configuration-files-new#configuration-objects).
For most of the cases, you probably want to configure some properties by yourself.
```js
const reactRecommended = require('eslint-plugin-react/configs/recommended');
const globals = require('globals');
module.exports = [
{
files: ['**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}'],
...reactRecommended,
languageOptions: {
...reactRecommended.languageOptions,
globals: {
...globals.serviceworker,
...globals.browser,
},
},
},
];
```
doesn't really say it will be mandatory to also spread languageOptions manually to get jsx parsed.

"You probably want to" doesn't sound like "You really have to".

@ljharb
Copy link
Member

ljharb commented Mar 9, 2024

That's true. I think long term we need separate entry points for flat config, and until we have those, this plugin should only be used with flat config in concert with eslint's flat compat wrapper.

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

No branches or pull requests

3 participants