yaodebian / vscode-eslint

VSCode extension to integrate eslint into VSCode

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

VS Code ESLint extension

Build Status

Integrates ESLint into VS Code. If you are new to ESLint check the documentation.

The extension uses the ESLint library installed in the opened workspace folder. If the folder doesn't provide one the extension looks for a global install version. If you haven't installed ESLint either locally or globally do so by running npm install eslint in the workspace folder for a local install or npm install -g eslint for a global install.

On new folders you might also need to create a .eslintrc configuration file. You can do this by either using the VS Code command Create ESLint configuration or by running the eslint command in a terminal. If you have installed ESLint globally (see above) then run eslint --init in a terminal. If you have installed ESLint locally then run .\node_modules\.bin\eslint --init under Windows and ./node_modules/.bin/eslint --init under Linux and Mac.

Settings Options

This extension contributes the following variables to the settings:

  • eslint.enable: enable/disable ESLint. Is enabled by default.

  • eslint.lintTask.enable: whether the extension contributes a lint task to lint a whole workspace folder.

  • eslint.lintTask.options: Command line options applied when running the task for linting the whole workspace (https://eslint.org/docs/user-guide/command-line-interface). An example to point to a custom .eslintrc.json file and a custom .eslintignore is:

    {
      "eslint.lintTask.options": "-c C:/mydirectory/.eslintrc.json --ignore-path C:/mydirectory/.eslintignore ."
    }
  • eslint.packageManager: controls the package manager to be used to resolve the ESLint library. This has only an influence if the ESLint library is resolved globally. Valid values are "npm" or "yarn" or "pnpm".

  • eslint.options: options to configure how ESLint is started using the ESLint CLI Engine API. Defaults to an empty option bag. An example to point to a custom .eslintrc.json file is:

    {
      "eslint.options": { "configFile": "C:/mydirectory/.eslintrc.json" }
    }
  • eslint.run - run the linter onSave or onType, default is onType. either off, onFocusChange or onWindowChange. It will not work with afterDelay.

  • eslint.quiet - ignore warnings.

  • eslint.runtime - use this setting to set the path of the node runtime to run ESLint under.

  • eslint.nodePath - use this setting if an installed ESLint package can't be detected, for example /myGlobalNodePackages/node_modules.

  • eslint.validate - an array of language identifiers specifying the files to be validated. Something like "eslint.validate": [ "javascript", "javascriptreact", "html" ]. If the setting is missing, it defaults to ["javascript", "javascriptreact"].

  • eslint.workingDirectories - an array for working directories to be used. ESLint resolves configuration files (e.g. eslintrc) relative to a working directory. This new settings allows users to control which working directory is used for which files (see also CLIEngine options#cwd). Example:

    root/
      client/
        .eslintrc.json
        client.js
      server/
        .eslintignore
        .eslintrc.json
        server.js
    

    Then using the setting:

      "eslint.workingDirectories": [
        "./client", "./server"
      ]

    will validate files inside the server directory with the server directory as the current eslint working directory. Same for files in the client directory.

    ESLint also considers the process's working directory when resolving .eslintignore files or when validating relative import statements like import A from 'components/A'; for which no base URI can be found. To make this work correctly the eslint validation process needs to switch the process's working directory as well. Since changing the processes`s working directory needs to be handled with care it must be explicitly enabled. To do so use the object literal syntax as show below for the server directory:

     "eslint.workingDirectories": [
       "./client", // Does not change the process's working directory
       { "directory": "./server", "changeProcessCWD": true }
     ]

    This validates files in the client folder with the process's working directory set to the workspace folder and files in the server folder with the process's working directory set to the server folder. This is like switching to the server folder in a terminal if ESLint is used as a shell command.

    If the workingDirectories setting is omitted the eslint working directory and the process's working directory is the workspace folder.

  • eslint.codeAction.disableRuleComment - object with properties:

    • enable - show disable lint rule in the quick fix menu. true by default.
    • location - choose to either add the eslint-disable comment on the separateLine or sameLine. separateLine is the default. Example:
    { "enable": true, "location": "sameLine" }
  • eslint.codeAction.showDocumentation - object with properties:

    • enable - show open lint rule documentation web page in the quick fix menu. true by default.
  • eslint.format.enable (@since 2.0.0): uses ESlint as a formatter for files that are validated by ESLint. If enabled please ensure to disable other formatters if you want to make this the default.

  • editor.codeActionsOnSave (@since 2.0.0): this setting now supports an entry source.fixAll.eslint. If set to true all auto-fixable ESLint errors from all plugins will be fixed on save. You can also selectively enable and disabled specific languages using VS Code's language scoped settings. To disable codeActionsOnSave for HTML files use the following setting:

  "[html]": {
    "editor.codeActionsOnSave": {
      "source.fixAll.eslint": false
    }
  }

Commands:

This extension contributes the following commands to the Command palette.

  • Create '.eslintrc.json' file: creates a new .eslintrc.json file.
  • Fix all auto-fixable problems: applies ESLint auto-fix resolutions to all fixable problems.
  • Disable ESLint for this Workspace: disables ESLint extension for this workspace.
  • Enable ESLint for this Workspace: enable ESLint extension for this workspace.

Using the extension with VS Code's task running

The extension is linting an individual file only on typing. If you want to lint the whole workspace set eslint.lintTask.enable to true and the extension will also contribute the eslint: lint whole folder task. There is no need anymore to define a custom task in tasks.json.

Using ESLint to validate TypeScript files

A great introduction on how to lint TypeScript using ESlint can be found in the TypeScript - ESLint. Please make yourself familiar with the introduction before using the VS Code ESLint extension in a TypeScript setup. Especially make sure that you can validate TypeScript files successfully in a terminal using the eslint command.

This project itself uses ESLint to validate its TypeScript files. So it can be used as a blueprint to get started.

Enable TypeScript file validation

To enable TypeScript file validation in the ESLint extension please add the following to your VS Code settings (either user or workspace):

	"eslint.validate": [
		{ "language": "typescript", "autoFix": true },
		{ "language": "typescriptreact", "autoFix": true }
	]

To avoid validation from any TSLint installation disable TSLint using "tslint.enable": false.

Mono repository setup

As with JavaScript validating TypeScript in a mono repository requires that you tell the VS Code ESLint extension what the current working directories are. Use the eslint.workingDirectories setting to do so. For this repository the working directory setup looks as follows:

	"eslint.workingDirectories": [
		{ "directory": "./client", "changeProcessCWD": true },
		{ "directory": "./server", "changeProcessCWD": true }
	]

ESLint 6.x

Migrating from ESLint 5.x to ESLint 6.x might need some adaption (see the ESLint Migration Guide for details). Before filing an issue against the VS Code ESLint extension please ensure that you can successfully validate your files in a terminal using the eslint command.

About

VSCode extension to integrate eslint into VSCode

License:MIT License


Languages

Language:TypeScript 97.3%Language:JavaScript 2.6%Language:HTML 0.1%Language:Vue 0.0%