为何ESLint对空值合并运算符??报错?
ESLint无法识别空值合并运算符(??)的解决办法
问题详情
我的ESLint配置:
globals: require: true global: true $: true env: browser: true es6: true extends: 'eslint:recommended' parserOptions: ecmaVersion: 2018 sourceType: module
JavaScript代码语句:
let checkboxChecked = JSON.parse(localStorage.getItem('previouslyChecked')) ?? {};
ESLint报错:error Parsing error: Unexpected token ?,不想使用// eslint-disable-line,求解决思路。
解决步骤
- 问题根源:空值合并运算符
??是ES2020(ES11)引入的新语法,而当前配置里的ecmaVersion设为2018,ESLint的解析器只能识别到ES2018及更早的语法,自然无法识别??。 - 核心修复:修改
parserOptions中的ecmaVersion为2020,或者直接设置为latest(自动适配最新ECMAScript语法),修改后的配置如下:
globals: require: true global: true $: true env: browser: true es6: true extends: 'eslint:recommended' parserOptions: ecmaVersion: 2020 sourceType: module
- 额外排查:如果修改后仍报错,检查ESLint版本是否过旧——部分早期版本的ESLint对ES2020语法支持不完善,建议升级到最新稳定版。
内容的提问来源于stack exchange,提问作者michouël
相关产品推荐
相关产品推荐

