できない.dev

ESLint「context.getSource is not a function」の原因と直し方

context.getSource is not a function は、ESLint 9 で古い版のプラグインのルールが走ると出る。
プラグインを 9 に対応した版に上げるか、@eslint/compat の fixupPluginRules() で包めば直る。

公開:

症状できない · exit 2
npx eslint src
…
TypeError: context.getSource is not a function
Occurred while linting /tmp/DuFD/src/Counter.js:5
Rule: "react-hooks/exhaustive-deps"
    at visitFunctionWithDependencies (/tmp/DuFD/node_modules/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.development.js:1704:42)
解決後できた · exit 0
npx eslint src
/tmp/DuFD/src/Counter.js
  8:6  warning  React Hook useEffect has a missing dependency: 'step'. Either include it or remove the dependency array. If 'setCount' needs the current value of 'step', you can also switch to useReducer instead of useState and read 'step' in the reducer  react-hooks/exhaustive-deps

✖ 1 problem (0 errors, 1 warning)

2026-11-07 に node:24(ESLint 9.39.5 / Node.js 24.22.2) で実際に打って取った出力。検証環境の詳細

要約

TypeError: context.getSource is not a function は、ESLint 9 で古い版のプラグインのルールが走ったときに出るエラーだ。
9.0 で、ルールに渡す context から getSource()・getScope() などのメソッドが消え、context.sourceCode の側に移った。
9 に対応する前のプラグインは消えたメソッドを呼ぶので、そのルールが走ったところで ESLint ごと止まり、終了コードは 2 になる。

エラーの下の Rule: "react-hooks/exhaustive-deps" の行に、落ちたルールの名前が出る。
/ の前がプラグインの名前で、そのプラグインを 9 に対応した版に上げれば直る。

実行例

node:24 のコンテナで、eslint@8 と eslint-plugin-react-hooks 4.6.0 のプロジェクトの ESLint だけを 9 に上げると、exhaustive-deps が context.getSource is not a function で落ちた。
/opt/v10 は同じプロジェクトを ESLint 10 に上げたもので、fixupPluginRules() で包むと終了コード 0 で通った。

$ npx eslint src
Oops! Something went wrong! :(
 
ESLint: 9.39.5
 
TypeError: context.getSource is not a function
Occurred while linting /tmp/DuFD/src/Counter.js:5
Rule: "react-hooks/exhaustive-deps"
    at visitFunctionWithDependencies (/tmp/DuFD/node_modules/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.development.js:1704:42)
    at visitCallExpression (/tmp/DuFD/node_modules/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.development.js:1759:11)
    at ruleErrorHandler (/tmp/DuFD/node_modules/eslint/lib/linter/linter.js:1173:33)
    at /tmp/DuFD/node_modules/eslint/lib/linter/source-code-visitor.js:76:46
    at Array.forEach (<anonymous>)
    at SourceCodeVisitor.callSync (/tmp/DuFD/node_modules/eslint/lib/linter/source-code-visitor.js:76:30)
    at /tmp/DuFD/node_modules/eslint/lib/linter/source-code-traverser.js:291:18
    at Array.forEach (<anonymous>)
    at SourceCodeTraverser.traverseSync (/tmp/DuFD/node_modules/eslint/lib/linter/source-code-traverser.js:290:10)
    at runRules (/tmp/DuFD/node_modules/eslint/lib/linter/linter.js:1214:12)
$ echo $?
2
$ npm ls eslint-plugin-react-hooks
dufd@1.0.0 /tmp/DuFD
`-- eslint-plugin-react-hooks@4.6.0
$ npm install -D eslint-plugin-react-hooks@latest
added 41 packages, and changed 1 package in 3s
$ echo $?
0
$ npm ls eslint-plugin-react-hooks
dufd@1.0.0 /tmp/DuFD
`-- eslint-plugin-react-hooks@7.1.1
$ npx eslint src
/tmp/DuFD/src/Counter.js
  8:6  warning  React Hook useEffect has a missing dependency: 'step'. Either include it or remove the dependency array. If 'setCount' needs the current value of 'step', you can also switch to useReducer instead of useState and read 'step' in the reducer  react-hooks/exhaustive-deps
 
✖ 1 problem (0 errors, 1 warning)
$ echo $?
0
$ npx eslint src
Oops! Something went wrong! :(
 
ESLint: 10.14.0
 
TypeError: Error while loading rule 'react-hooks/exhaustive-deps': context.getSourceCode is not a function
Occurred while linting /opt/v10/src/Counter.js
    at Object.create (/opt/v10/node_modules/eslint-plugin-react-hooks/cjs/eslint-plugin-react-hooks.development.js:763:32)
    at createRuleListeners (/opt/v10/node_modules/eslint/lib/linter/linter.js:502:15)
    at /opt/v10/node_modules/eslint/lib/linter/linter.js:628:7
    at Array.forEach (<anonymous>)
    at runRules (/opt/v10/node_modules/eslint/lib/linter/linter.js:562:31)
    at #flatVerifyWithoutProcessors (/opt/v10/node_modules/eslint/lib/linter/linter.js:1297:4)
    at Linter._verifyWithFlatConfigArrayAndWithoutProcessors (/opt/v10/node_modules/eslint/lib/linter/linter.js:1382:43)
    at Linter._verifyWithFlatConfigArray (/opt/v10/node_modules/eslint/lib/linter/linter.js:1449:15)
    at Linter.verify (/opt/v10/node_modules/eslint/lib/linter/linter.js:894:9)
    at Linter.verifyAndFix (/opt/v10/node_modules/eslint/lib/linter/linter.js:1569:20)
$ echo $?
2
$ npm install -D --legacy-peer-deps @eslint/compat
added 1 package in 547ms
$ echo $?
0
$ cat eslint.config.mjs
import { fixupPluginRules } from "@eslint/compat";
import { defineConfig } from "eslint/config";
import reactHooks from "eslint-plugin-react-hooks";
 
export default defineConfig([
  {
    plugins: { "react-hooks": fixupPluginRules(reactHooks) },
    rules: {
      "react-hooks/rules-of-hooks": "error",
      "react-hooks/exhaustive-deps": "warn",
    },
  },
]);
$ npx eslint src
/opt/v10/src/Counter.js
  8:6  warning  React Hook useEffect has a missing dependency: 'step'. Either include it or remove the dependency array. If 'setCount' needs the current value of 'step', you can also switch to useReducer instead of useState and read 'step' in the reducer  react-hooks/exhaustive-deps
 
✖ 1 problem (0 errors, 1 warning)
$ echo $?
0

— 2026-11-07 時点の出力

検証環境Node.js 24.22.2 / npm 11.19.1 / Python 3.11.2 / 2026-11-07 検証(ほか 2 件)
検証日
実行環境
node:24 Debian GNU/Linux 12 (bookworm)
バージョン
  • Node.js 24.22.2
  • npm 11.19.1
  • Python 3.11.2
  • Git 2.39.5
  • ESLint 9.39.5

この記事の「実行例」は、上記の環境で実際にコマンドを実行して得られた出力をそのまま掲載しています。 再現手順はリポジトリの検証スクリプトとして管理し、定期的に再実行して出力を更新しています。

解決策

1. プラグインを上げる

npm ls で落ちたプラグインの版を確かめ、ESLint 9 に対応した版に上げる。

npm ls eslint-plugin-react-hooks
npm install -D eslint-plugin-react-hooks@latest

eslint-plugin-react-hooks は 4.6.0 から 7.1.1 に上がり、設定を変えずに exhaustive-deps の警告を出した。
7.1.1 の peerDependencies は ESLint 10 まで含む。

2. fixupPluginRules() で包む

更新が止まったプラグインは、@eslint/compat の fixupPluginRules() で包む。
消えたメソッドが補われ、古いルールのまま動く。

npm install -D --legacy-peer-deps @eslint/compat
import { fixupPluginRules } from "@eslint/compat";
import { defineConfig } from "eslint/config";
import reactHooks from "eslint-plugin-react-hooks";
 
export default defineConfig([
  {
    plugins: { "react-hooks": fixupPluginRules(reactHooks) },
    rules: {
      "react-hooks/rules-of-hooks": "error",
      "react-hooks/exhaustive-deps": "warn",
    },
  },
]);

ESLint 10 で context.getSourceCode is not a function と出ていた 4.6.0 も、包むと exhaustive-deps の警告を出した。
古いプラグインの peerDependencies は ESLint 8 までなので、--legacy-peer-deps を付けないと npm install が ERESOLVE で止まる(移行ページで確かめた)。

3. 自作のルールを書き換える

自作のルールなら、context.getSource(node) を context.sourceCode.getText(node) に、context.getScope() を context.sourceCode.getScope(node) に書き換える。
消えたメソッドと置き換え先の一覧は、9.0 の移行ガイド(新しいタブで開く)にある。
10.0 で消えた getSourceCode() は context.sourceCode に、getFilename() は context.filename に替える。

よくある原因

  1. プラグインの版が古い。
    eslint-plugin-react-hooks 4.6.0 の exhaustive-deps は context.getSource() を呼び、ESLint 9 で TypeError になった。
    公式の告知(新しいタブで開く)は、context.getScope・context.getAncestors・context.markVariableAsUsed で落ちる例も挙げている。
    メソッドの名前が違うだけで、原因は同じである。
  2. ESLint だけを上げた。
    ESLint 8 から 9 への移行ページの実測では、npm install -D eslint@9 はプラグインの peerDependencies(ESLint 8 まで)に合わなくても、警告を出すだけで終了コード 0 で入った。
  3. ESLint 10 に上げた。
    10.0 では context.getSourceCode() や context.getFilename() も消えた。
    eslint-plugin-react-hooks 4.6.0 は、10 では context.getSourceCode is not a function と出て、ルールを読み込む段階で落ちた。