简体   繁体   English

将 ESLint 添加到 Meteor + Vue + Typescript 项目

[英]Add ESLint to a Meteor + Vue + Typescript project

I've been having trouble setting up ESLint with Meteor, Vue, Typescript and prettier.我在使用 Meteor、Vue、Typescript 和更漂亮的设置 ESLint 时遇到了麻烦。 I can get it successfully parsing and formatting Typescript files but it is throwing syntax errors for the .vue files.我可以成功解析和格式化 Typescript 文件,但它会引发.vue文件的语法错误。

ESLint related dependencies ESLint 相关依赖

"@babel/plugin-transform-typescript": "^7.12.1",
"@meteorjs/eslint-config-meteor": "^1.0.5",
"@types/meteor": "^1.4.64",
"@types/mocha": "^8.0.3",
"@typescript-eslint/eslint-plugin": "^4.22.0",
"@typescript-eslint/parser": "^4.22.0",
"eslint-config-prettier": "^8.3.0",
"eslint-config-vue-typescript-eslint": "^1.1.7",
"eslint-plugin-prettier": "^3.4.0",
"eslint-plugin-vue": "^7.9.0",

.eslinrc.js .eslinrc.js

module.exports = {
    root: true,
    env: {
        node: true,
        webextensions: true
    },
    parser: '@typescript-eslint/parser', // Specifies the ESLint parser
    parserOptions: {
        ecmaVersion: 2020, // Allows for the parsing of modern ECMAScript features
        sourceType: 'module', // Allows for the use of imports
        ecmaFeatures: {
            jsx: true // Allows for the parsing of JSX
        }
    },
    settings: {
        vue: {
            version: 'detect' // Tells eslint-plugin-vue to automatically detect the version of Vue to use
        }
    },
    extends: [
        'plugin:vue/recommended',
        'eslint:recommended',
        'vue-typescript-eslint',
        'plugin:@typescript-eslint/recommended', // Uses the recommended rules from the @typescript-eslint/eslint-plugin
        'plugin:prettier/recommended' // Enables eslint-plugin-prettier and eslint-config-prettier. This will display prettier errors as ESLint errors. Make sure this is always the last configuration in the extends array.
    ],
    rules: {
        // Place to specify ESLint rules. Can be used to overwrite rules specified from the extended configs
        // e.g. "@typescript-eslint/explicit-function-return-type": "off",
        'no-unused-vars': 'warn'
    }
};

.prettierrc.js .prettierrc.js

module.exports = {
    semi: true,
    trailingComma: "all",
    singleQuote: true,
    printWidth: 120,
    tabWidth: 4
};

SamplePageContent.vue示例页面内容.vue

<template>
  <v-row>
    <h4>Sample page content</h4>
  </v-row>
</template>

<script lang="ts">

import Vue from "vue";

export default Vue.extend( {
  components: {},
  props: {
    giftList: {
      type: Object
    }
  },
});
</script>

<style scoped>
</style>

I get an ESLint: Parsing error: '}' expected.我得到一个ESLint: Parsing error: '}' expected. occur on the components section.发生在components部分。

How can I get it to parse/format my .vue files correctly?如何让它正确解析/格式化我的.vue文件?

Update - Setup Info更新 - 设置信息

Here is my question showing the commands used to set up my project initially.这是我的问题,显示了最初用于设置我的项目的命令。 https://forums.meteor.com/t/creating-a-meteor-vue-typescript-project-that-uses-class-style-components/55778 https://forums.meteor.com/t/creating-a-meteor-vue-typescript-project-that-uses-class-style-components/55778

meteor create --vue gift-list-app
meteor add typescript
meteor npm install --save-dev @types/meteor
meteor add nathantreid:vue-typescript-babel
meteor npm install --save-dev @babel/plugin-transform-typescript

Add these dev dependencies if they are missing.如果缺少这些开发依赖项,请添加它们。

"devDependencies": {
    "@babel/plugin-transform-typescript": "^7.12.1",
    "@types/meteor": "^1.4.67",
    "@babel/core": "^7.4.4",
    "@babel/plugin-syntax-decorators": "^7.2.0",
    "@babel/plugin-syntax-jsx": "^7.2.0",
    "@babel/preset-typescript": "^7.3.3",
    "@babel/plugin-syntax-dynamic-import": "^7.2.0"
}

Here is a Meteor + Vue + Typescript example project I created.这是我创建的Meteor + Vue + Typescript示例项目。 If ESLint can be correctly added to this it would be perfect.如果 ESLint 可以正确添加到这将是完美的。

https://github.com/Michael2109/meteor-vue-typescript-examplehttps://github.com/Michael2109/meteor-vue-typescript-example

I did follow the tutorial from [1] for these parts:对于这些部分,我确实遵循了 [1] 中的教程:

  1. Download the ESLint and Prettier extensions for VSCode.下载 VSCode 的 ESLint 和 Prettier 扩展。
  2. Install the ESLint and Prettier libraries into our project.将 ESLint 和 Prettier 库安装到我们的项目中。 In your project's root directory, you will want to run: npm install -D eslint prettier在项目的根目录中,您需要运行: npm install -D eslint prettier
  3. Install eslint-config-prettier (disables formatting for ESLint) and eslint-plugin-prettier (allows ESLint to show formatting errors as we type) npm install -D eslint-config-prettier eslint-plugin-prettier安装 eslint-config-prettier(禁用 ESLint 格式化)和 eslint-plugin-prettier(允许 ESLint 在我们键入时显示格式化错误) npm install -D eslint-config-prettier eslint-plugin-prettier
  4. The last step is to make sure Prettier formats on save.最后一步是确保保存时更漂亮的格式。 Insert "editor.formatOnSave": true into your User Settings in VSCode.在 VSCode 的用户设置中插入 "editor.formatOnSave": true 。

I also installed Eslint as global to my Ubuntu and applied the dev-deps mentioned in the question.我还将 Eslint 作为全局安装到我的 Ubuntu 并应用了问题中提到的 dev-deps。 I also added Vetur plugin to VSCode and updated the VSCode to newest edition.我还在 VSCode 中添加了 Vetur 插件,并将 VSCode 更新到最新版本。

At .eslint.js file I have the following:.eslint.js文件中,我有以下内容:

  parserOptions: {
      ecmaVersion: 2020, // Allows for the parsing of modern ECMAScript features
      sourceType: 'module', // Allows for the use of imports
      extraFileExtensions: ['.vue'], // ADDED
      ecmaFeatures: {
          jsx: true // Allows for the parsing of JSX
      }
  },

From VSCode Settings.json I had this:从 VSCode Settings.json 我有这个:

  {
    "editor.formatOnSave": true,
    "eslint.alwaysShowStatus": true,
    "editor.codeActionsOnSave": {
      "source.fixAll.eslint": true
    }
  }

Just before all started to work, I got a question from VS Code, that lint is not active on some file.就在一切开始工作之前,我从 VS Code 收到了一个问题,即 lint 在某些文件上没有激活。 I activated with "do that for all files" and now lint warnings show just fine.我激活了“对所有文件都这样做”,现在 lint 警告显示得很好。

I tried to tell here every step I made, but cannot say 100% which made the trick.我试图在这里告诉我我所做的每一步,但不能说 100% 成功了。

Disclaimer免责声明

I could not verify why the mentioned curly bracket problem is on components part of constructor.我无法验证为什么提到的大括号问题出现在构造函数的组件上。 I treat it as a bug but cannot say on which side: on code or lint.我把它当作一个错误,但不能说在哪一边:在代码或 lint 上。 On other problems prettier makes good stuff and esLint finds many bugs and warnings.在其他问题上, prettier的东西是好东西,而esLint发现了许多错误和警告。 That said, I think this is enough to answer the question.也就是说,我认为这足以回答这个问题。

EDIT:编辑:

You told you use Eslint in WebStorm.你告诉过你在 WebStorm 中使用 Eslint。 There are some settings [2] where you can activate them after adding the relevant plugins.有一些设置 [2] 您可以在添加相关插件后激活它们。

  • To configure ESLint automatically in the current project, open the Settings/Preferences dialog Ctrl+Alt+S , go to Languages and Frameworks | JavaScript | Code Quality Tools | ESLint要在当前项目中自动配置 ESLint,打开 Settings/Preferences 对话框Ctrl+Alt+S , go to Languages and Frameworks | JavaScript | Code Quality Tools | ESLint Languages and Frameworks | JavaScript | Code Quality Tools | ESLint Languages and Frameworks | JavaScript | Code Quality Tools | ESLint , and select the Automatic ESLint configuration option. Languages and Frameworks | JavaScript | Code Quality Tools | ESLint和 select Automatic ESLint configuration选项。

  • To configure ESLint automatically in all new projects, open the Settings for New Projects dialog ( File | New Projects Settings | Settings for New Projects ), go to Languages and Frameworks | JavaScript | Code Quality Tools | ESLint要在所有新项目中自动配置 ESLint,请打开 Settings for New Projects 对话框( File | New Projects Settings | Settings for New Projects ),go to Languages and Frameworks | JavaScript | Code Quality Tools | ESLint Languages and Frameworks | JavaScript | Code Quality Tools | ESLint Languages and Frameworks | JavaScript | Code Quality Tools | ESLint , and select the Automatic ESLint configuration option. Languages and Frameworks | JavaScript | Code Quality Tools | ESLint和 select Automatic ESLint configuration选项。

To auto fix on save:要在保存时自动修复:

  • Open the Settings/Preferences dialog Ctrl+Alt+S , go to Languages and Frameworks | JavaScript | Code Quality Tools | ESLint打开设置/首选项对话框Ctrl+Alt+S , go 到Languages and Frameworks | JavaScript | Code Quality Tools | ESLint Languages and Frameworks | JavaScript | Code Quality Tools | ESLint Languages and Frameworks | JavaScript | Code Quality Tools | ESLint , and select the Run eslint --fix on save checkbox. Languages and Frameworks | JavaScript | Code Quality Tools | ESLint和 select Run eslint --fix on save复选框。

On my source [2] are also other things but these are the things that "make it work", at least some visible way.在我的来源 [2] 上还有其他东西,但这些是“让它工作”的东西,至少是一些可见的方式。

Sources:资料来源:

[1] How to configure Vue CLI 4 with ESLint + Prettier + Airbnb rules + TypeScript + Vetur? [1] 如何使用 ESLint + Prettier + Airbnb 规则 + TypeScript + Vetur 配置 Vue CLI 4?

[2] https://www.jetbrains.com/help/webstorm/eslint.html#ws_js_eslint_verify_code [2] https://www.jetbrains.com/help/webstorm/eslint.html#ws_js_eslint_verify_code

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM