TypeError:context.getScope 不是一个函数
症状
¥Symptoms
当将 ESLint v9.0.0 或更高版本与插件一起使用时,你可能会看到以下错误之一:
¥When using ESLint v9.0.0 or later with a plugin, you may see one of the following errors:
TypeError: context.getScope is not a function
TypeError: context.getAncestors is not a function
TypeError: context.markVariableAsUsed is not a function
TypeError: context.getDeclaredVariables is not a function
原因
¥Cause
ESLint v9.0.0 引入了插件规则使用的 规则 API 的更改,其中包括将一些方法从 context
对象移动到 sourceCode
对象。如果你看到这些错误之一,则意味着该插件尚未更新为使用新规则 API。
¥ESLint v9.0.0 introduces changes to the rules API that plugin rules use, which included moving some methods from the context
object to the sourceCode
object. If you’re seeing one of these errors, that means the plugin has not yet been updated to use the new rules API.
解析
¥Resolution
此问题的常见解决方案包括:
¥Common resolutions for this issue include:
-
将插件升级到最新版本
¥Upgrade the plugin to the latest version
-
使用 兼容性实用程序 修补配置文件中的插件
¥Use the compatibility utilities to patch the plugin in your config file
资源
¥Resources
有关更多信息,请参阅:
¥For more information, see:
-
配置插件 有关如何配置插件的文档
¥Configure Plugins for documentation on how to configure plugins
-
创建插件 有关如何定义插件的文档
¥Create Plugins for documentation on how to define plugins