如何在VS Code中配置ESLint(Setting up ESLint in your JavaScript Project with VS Code)

我曾兩次遇到這個問題,兩次花時間去解決這個問題,爲了避免以後遇到同樣的問題,我將此記錄於此。

1 必須執行下面這個文章的配置步驟

https://dev.to/iamdammak/setting-up-eslint-in-your-javascript-project-with-vs-code-2amf 這個文章不僅講述如何在VS Code中配置ESLINT(HOW),也講述其中一些步驟的原因(WHY)。

2 我目前在用的配置文件,大家可以自己配置:

module.exports = {
  'parser': 'babel-eslint',
  'env': {
    'browser': true,
    'mocha': true,
    'es6': true,
    'jquery': true,
  },
  'extends': 'airbnb-base/legacy',
  'rules': {
    'indent': ['error', 4],
    'no-underscore-dangle': 'off',
    'max-len': ['error', 150, 4, {
      ignoreUrls: true,
      ignoreComments: false,
      ignoreRegExpLiterals: true,
      ignoreStrings: true,
      ignoreTemplateLiterals: true,
    }],
    'one-var': 'off',
    'default-case':'off',
    'no-multi-assign': 'off',
    'vars-on-top': 'off',
    'func-names': 'off',
    'no-eval': 'off',
    'global-require': 'off',
    'radix': ['error', 'as-needed'],
    'no-plusplus': 'off',
    'no-param-reassign': 'off',
    'camelcase': 'off',
    'spaced-comment': 'off',
    'no-continue': 'off',
    'eol-last': 'off',
    'no-bitwise': 'off',
  },
  'globals': {
    'mstrmojo': true,
    'mstrApp': true,
    'mstrConfig': true,
    'self': true,
    'ActiveXObject': true,
    'microstrategy': true,
    'mstrMobileApp': true,
    'mstr_profile': true,
    'global': true,
  },
};

另外:你必需知道VS Code可以根據其偏好設置讀取ESlint規則文件,從而標紅那些違反規則的代碼。
其偏好設置有兩者,分別是User preference和Workspace preference,入口是:Code - preference - 搜索ESLint - Edit in settings.json
在這裏插入圖片描述
eslint.options指定了VSCode會讀取的規則文件的位置。
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章