Skip to content

Typescript + React + Context.Provider = TypeError: Cannot read property 'variables' of null #2882

Closed
@stevematney

Description

@stevematney

eslint is failing when using @typescript-eslint/eslint-plugin and eslint-react-plugin together. This failure seems to only happen when attempting to render a Provider from a Context as jsx.

If I disable react/jsx-no-undef, it works, and I can fall back to tsc or tsserver to catch JSX undefined errors, but then eslint itself becomes silent on the issue. Here's the configuration I've got in a stripped down project to produce the error. All the relevant files are listed below, and here is a repo which demonstrates the issue: https://github.com/stevematney/eslint-clash

tsconfig.json

{
  "compilerOptions": {
    "target": "ES2019",
    "lib": ["dom", "dom.iterable", "ES2019"],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "commonjs",
    "moduleResolution": "Node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "downlevelIteration": true,
    "jsx": "react"
  },
  "include": ["*", "./.eslintrc.js"]
}

.eslintrc.js

module.exports = {
  root: true,
  env: {
    browser: true,
    node: true,
  },
  extends: [
    "plugin:react/recommended",
    "plugin:@typescript-eslint/recommended",
  ],
  parser: "@typescript-eslint/parser",
  parserOptions: {
    sourceType: "module",
    ecmaFeatures: {
      jsx: true,
      modules: true,
    },
    ecmaVersion: "es2020",
    tsconfigRootDir: __dirname,
    project: `./tsconfig.json`,
  },
  settings: {
    react: {
      version: "detect",
    },
  },
};

App.tsx

import React, { createContext } from "react";

const basicContext = { yes: "yes", no: "no" };
const AContext = createContext(basicContext);

export default (function () {
  return (
    <AContext.Provider value={basicContext}>
      {/* put stuff in here if you want */}
    </AContext.Provider>
  );
} as React.FC);

package.json

{
  "name": "eslint-clash",
  "version": "1.0.0",
  "description": "",
  "main": ".eslintrc.js",
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@typescript-eslint/eslint-plugin": "^4.11.0",
    "@typescript-eslint/parser": "^4.11.0",
    "eslint": "^7.16.0",
    "eslint-plugin-react": "^7.21.5",
    "react": "^16.14.0"
  }
}

Error Output:

TypeError: Cannot read property 'variables' of null
Occurred while linting /Users/me/MacsideProjects/eslint-clash/hello.tsx:8
    at checkIdentifierInJSX (/Users/me/node_modules/eslint-plugin-react/lib/rules/jsx-no-undef.js:63:27)
    at JSXOpeningElement (/Users/me/node_modules/eslint-plugin-react/lib/rules/jsx-no-undef.js:106:9)
    at /Users/me/MacsideProjects/eslint-clash/node_modules/eslint/lib/linter/safe-emitter.js:45:58
    at Array.forEach (<anonymous>)
    at Object.emit (/Users/me/MacsideProjects/eslint-clash/node_modules/eslint/lib/linter/safe-emitter.js:45:38)
    at NodeEventGenerator.applySelector (/Users/me/MacsideProjects/eslint-clash/node_modules/eslint/lib/linter/node-event-generator.js:254:26)
    at NodeEventGenerator.applySelectors (/Users/me/MacsideProjects/eslint-clash/node_modules/eslint/lib/linter/node-event-generator.js:283:22)
    at NodeEventGenerator.enterNode (/Users/me/MacsideProjects/eslint-clash/node_modules/eslint/lib/linter/node-event-generator.js:297:14)
    at CodePathAnalyzer.enterNode (/Users/me/MacsideProjects/eslint-clash/node_modules/eslint/lib/linter/code-path-analysis/code-path-analyzer.js:711:23)
    at /Users/me/MacsideProjects/eslint-clash/node_modules/eslint/lib/linter/linter.js:952:32

I would be happy to hear if I'm just doing something incorrectly, but this does seem to be a bug.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions