ESLint 配置中的"parser"和"parserOptions.parser"有什么区别?



我已经为TypeScript和Vue使用了很长时间的以下预设。它是有效的,但我还没有理解每一个选项,现在我将理解它。首先:parser@typescript-eslint/parser之间有什么区别?

parser: vue-eslint-parser
parserOptions:
parser: "@typescript-eslint/parser"
sourceType: module
project: tsconfig.json
tsconfigRootDir: ./
extraFileExtensions: [ ".vue" ]
env:
es6: true
browser: true
node: true
plugins:
- "@typescript-eslint"
- vue

实验数据

如果没有parser: "vue-eslint-parser",我们在TypeScript文件的第行中有[unknown]: Parsing error: Unexpected token :

(async function executeApplication(): Promise<void> {})()

.vue文件中的Parsing error: Unexpected token <,行:

<template lang="pug">

如果我们删除或注释掉parserOptions.parser: "@typescript-eslint/parser"

  • [unknown]: Parsing error: Unexpected token :将保留
  • Parsing error: Unexpected token <将消失,而Parsing error: Unexpected character '@'将出现在@Component export default class extends Vue {行中

是否需要parser@typescript-eslint/parser

vue-eslint-parser是要使用的主解析器,而不是默认解析器(espree(。它将处理.vue SFC文件,尤其是<template>标记。

在这个解析器中,您有一个自定义选项来指定要使用哪个解析器来整理.vue文件中的<script>标记。

因此,基本上,您是在告诉esint使用vue-eslint-parser来解析.vue文件,并且在这个解析器中,使用@typescript-eslint/parser作为<script>标记。

最新更新