Eslintconfig package json что это
Configuration Files
Configuration File Formats
ESLint supports configuration files in several formats:
If there are multiple configuration files in the same directory, ESLint will only use one. The priority order is as follows:
Using Configuration Files
There are two ways to use configuration files.
Here’s an example JSON configuration file that uses the typescript-eslint parser to support TypeScript syntax:
Comments in configuration files
Both the JSON and YAML configuration file formats support comments (package.json files should not include them). You can use JavaScript-style comments for JSON files and YAML-style comments for YAML files. ESLint safely ignores comments in configuration files. This allows your configuration files to be more human-friendly.
For JavaScript-style comments:
For YAML-style comments:
Adding Shared Settings
ESLint supports adding shared settings into configuration files. Plugins use settings to specify information that should be shared across all of its rules. You can add settings object to ESLint configuration file and it will be supplied to every rule being executed. This may be useful if you are adding custom rules and want them to have access to the same information and be easily configurable.
Cascading and Hierarchy
The complete configuration hierarchy, from highest to lowest precedence, is as follows:
Extending Configuration Files
A configuration file, once extended, can inherit all the traits of another configuration file (including rules, plugins, and language options) and modify all the options. As a result, there are three configurations, as defined below:
The extends property value is either:
ESLint extends configurations recursively, so a base configuration can also have an extends property. Relative paths and shareable config names in an extends property are resolved from the location of the config file where they appear.
The rules property can do any of the following to extend (or override) the set of rules:
Using a shareable configuration package
A sharable configuration is an npm package that exports a configuration object. Make sure that you have installed the package in your project root directory, so that ESLint can require it.
The extends property value can omit the eslint-config- prefix of the package name.
Example of a configuration file in YAML format:
Using eslint:recommended
Using «eslint:recommended» in the extends property enables a subset of core rules that report common problems (these rules are identified with a checkmark ✓ on the rules page).
Here’s an example of extending eslint:recommended and overriding some of the set configuration options:
Example of a configuration file in JavaScript format:
Using a configuration from a plugin
A plugin is an npm package that can add various extensions to ESLint. A plugin can perform numerous functions, including but not limited to adding new rules and exporting shareable configurations. Make sure the package has been installed in a directory where ESLint can require it.
The plugins property value can omit the eslint-plugin- prefix of the package name.
The extends property value can consist of:
Example of a configuration file in JSON format:
Using a configuration file
The extends property value can be an absolute or relative path to a base configuration file. ESLint resolves a relative path to a base configuration file relative to the configuration file that uses it.
Example of a configuration file in JSON format:
Using «eslint:all»
The extends property value can be «eslint:all» to enable all core rules in the currently installed version of ESLint. The set of core rules can change at any minor or major version of ESLint.
Important: This configuration is not recommended for production use because it changes with every minor and major version of ESLint. Use it at your own risk.
You might enable all core rules as a shortcut to explore rules and options while you decide on the configuration for a project, especially if you rarely override options or disable rules. The default options for rules are not endorsements by ESLint (for example, the default option for the quotes rule does not mean double quotes are better than single quotes).
Example of a configuration file in JavaScript format:
Configuration Based on Glob Patterns
v4.1.0+. Sometimes a more fine-controlled configuration is necessary, for example, if the configuration for files within the same directory has to be different. Therefore you can provide configurations under the overrides key that will only apply to files that match specific glob patterns, using the same format you would pass on the command line (e.g., app/**/*.test.js ).
How do overrides work?
It is possible to override settings based on file glob patterns in your configuration by using the overrides key. An example of using the overrides key is as follows:
Here is how overrides work in a configuration file:
Relative glob patterns
Specifying target files to lint
If you specified directories with CLI (e.g., eslint lib ), ESLint searches target files in the directory to lint. The target files are *.js or the files that match any of overrides entries (but exclude entries that are any of files end with * ).
Personal Configuration Files (deprecated)
/ refers to the home directory of the current user on your preferred operating system. The personal configuration file being referred to here is
/.eslintrc.* file, which is currently handled differently than other configuration files.
How does ESLint find personal configuration files?
If eslint could not find any configuration file in the project, eslint loads
If eslint could find configuration files in the project, eslint ignores
/.eslintrc.* file even if it’s in an ancestor directory of the project directory.
How do personal configuration files behave?
/.eslintrc.* files behave similarly to regular configuration files, with some exceptions:
/.eslintrc.* files load shareable configs and custom parsers from
/node_modules/ – similarly to require() – in the user’s home directory. Please note that it doesn’t load global-installed packages.
Configuring ESLint
ESLint is designed to be completely configurable, meaning you can turn off every rule and run only with basic syntax validation, or mix and match the bundled rules and your custom rules to make ESLint perfect for your project. There are two primary ways to configure ESLint:
There are several pieces of information that can be configured:
All of these options give you fine-grained control over how ESLint treats your code.
Specifying Parser Options
ESLint allows you to specify the JavaScript language options you want to support. By default, ESLint supports only ECMAScript 5 syntax. You can override that setting to enable support for ECMAScript 6 as well as JSX by using parser options.
Setting parser options helps ESLint determine what is a parsing error. All language options are false by default.
Specifying Parser
By default, ESLint uses Espree as its parser. You can optionally specify that a different parser should be used in your configuration file so long as the parser meets the following requirements:
Note that even with these compatibilities, there are no guarantees that an external parser will work correctly with ESLint and ESLint will not fix bugs related to incompatibilities with other parsers.
The following parsers are compatible with ESLint:
Note when using a custom parser, the parserOptions configuration property is still required for ESLint to work properly with features not in ECMAScript 5 by default. Parsers are all passed parserOptions and may or may not use them to determine which features to enable.
Specifying Environments
An environment defines global variables that are predefined. The available environments are:
These environments are not mutually exclusive, so you can define more than one at a time.
To specify environments using a comment inside of your JavaScript file, use the following format:
This enables Node.js and Mocha environments.
Or in a package.json file
If you want to use an environment from a plugin, be sure to specify the plugin name in the plugins array and the use the unprefixed plugin name, followed by a slash, followed by the environment name. For example:
Or in a package.json file
Specifying Globals
The no-undef rule will warn on variables that are accessed but not defined within the same file. If you are using global variables inside of a file then it’s worthwhile to define those globals so that ESLint will not warn about their usage. You can define global variables either using comments inside of a file or in the configuration file.
To specify globals using a comment inside of your JavaScript file, use the following format:
To configure global variables inside of a configuration file, use the globals key and indicate the global variables you want to use. Set each global variable name equal to true to allow the variable to be overwritten or false to disallow overwriting. For example:
Configuring Plugins
ESLint supports the use of third-party plugins. Before using the plugin you have to install it using npm.
To configure plugins inside of a configuration file, use the plugins key, which contains a list of plugin names. The eslint-plugin- prefix can be omitted from the plugin name.
Note: A globally-installed instance of ESLint can only use globally-installed ESLint plugins. A locally-installed ESLint can make use of both locally- and globally- installed ESLint plugins.
Configuring Rules
ESLint comes with a large number of rules. You can modify which rules your project uses either using configuration comments or configuration files. To change a rule setting, you must set the rule ID equal to one of these values:
To configure rules inside of a file using configuration comments, use a comment in the following format:
In this example, eqeqeq is turned off and curly is turned on as an error. If a rule has additional options, you can specify them using array literal syntax, such as:
This comment specifies the «double» option for the quotes rule.
To configure rules inside of a configuration file, use the rules key along with an error level and any options you want to use. For example:
To temporarily disable rule warnings in your file use the following format:
You can also disable or enable warnings for specific rules:
To disable all rules on a specific line:
To disable a specific rule on a specific line:
Note: Comments that disable warnings for a portion of a file tell ESLint not to report rule violations for the disabled code. ESLint parses the entire file, so disabled code still needs to be syntactically valid JavaScript.
Adding Shared Settings
ESLint supports adding shared settings into configuration file. You can add settings object to ESLint configuration file and it will be supplied to every rule that will be executed. This may be useful if you are adding custom rules and want them to have access to the same information and be easily configurable.
Using Configuration Files
In each case, the settings in the configuration file override default settings.
Configuration File Formats
ESLint supports configuration files in several formats:
If there are multiple configuration files in the same directory, ESLint will only use one. The priority order is:
Configuration Cascading and Hierarchy
Note: If you have a personal configuration file in your home directory (
/.eslintrc ), it will only be used if no other configuration files are found. Since a personal configuration would apply to everything inside of a user’s directory, including third-party code, this could cause problems when running ESLint.
The complete configuration hierarchy, from highest precedence to lowest precedence, is as follows:
Extending Configuration Files
If you want to extend a specific configuration file, you can use the extends property and specify the path to the file. The path can be either relative or absolute.
Configurations can be extended by using:
The extended configuration provides base rules, which can be overridden by the configuration that references it. For example:
Configurations may also be provided as an array, with additional files overriding any properties provided in the previous file. For example:
You can also extend configurations using shareable configuration packages. To do so, be sure to install the configuration package you want from npm and then use the package name, such as:
In this example, the eslint-config-myrules package will be loaded as an object and used as the parent of this configuration.
Note: You can omit eslint-config- and ESLint will automatically insert it for you, similar to how plugins work. See Shareable Configs for more information.
ESLint also supports extending configuration from plugins that provide configs:
Important: When you are extending from the configuration bundled with plugins, you need to start with plugin: prefix as well as specify configuration name after the slash. You may optionally omit the eslint-plugin- prefix.
Note: For configuration files in your home directory, or in any path that isn’t an ancestor to the location of ESLint (either globally or locally), extends is resolved from the path of the project using ESLint (typically the current working directory) rather than relative to the file itself.
Comments in Configuration Files
Both the JSON and YAML configuration file formats support comments ( package.json files should not include them). You can use JavaScript-style comments or YAML-style comments in either type of file and ESLint will safely ignore them. This allows your configuration files to be more human-friendly. For example:
Specifying File extensions to Lint
Ignoring Files and Directories
Globs are matched using node-ignore, so a number of features are available:
Using an Alternate File
Ignored File Warnings
You’ll see this warning:
Configuring ESLint
ESLint is designed to be completely configurable, meaning you can turn off every rule and run only with basic syntax validation, or mix and match the bundled rules and your custom rules to make ESLint perfect for your project. There are two primary ways to configure ESLint:
There are several pieces of information that can be configured:
All of these options give you fine-grained control over how ESLint treats your code.
Specifying Parser Options
ESLint allows you to specify the JavaScript language options you want to support. By default, ESLint supports only ECMAScript 5 syntax. You can override that setting to enable support for ECMAScript 6 and 7 as well as JSX by using parser options.
Please note that supporting JSX syntax is not the same as supporting React. React applies specific semantics to JSX syntax that ESLint doesn’t recognize. We recommend using eslint-plugin-react if you are using React and want React semantics.
Setting parser options helps ESLint determine what is a parsing error. All language options are false by default.
Specifying Parser
By default, ESLint uses Espree as its parser. You can optionally specify that a different parser should be used in your configuration file so long as the parser meets the following requirements:
Note that even with these compatibilities, there are no guarantees that an external parser will work correctly with ESLint and ESLint will not fix bugs related to incompatibilities with other parsers.
The following parsers are compatible with ESLint:
Note when using a custom parser, the parserOptions configuration property is still required for ESLint to work properly with features not in ECMAScript 5 by default. Parsers are all passed parserOptions and may or may not use them to determine which features to enable.
Specifying Environments
An environment defines global variables that are predefined. The available environments are:
These environments are not mutually exclusive, so you can define more than one at a time.
To specify environments using a comment inside of your JavaScript file, use the following format:
This enables Node.js and Mocha environments.
Or in a package.json file
If you want to use an environment from a plugin, be sure to specify the plugin name in the plugins array and then use the unprefixed plugin name, followed by a slash, followed by the environment name. For example:
Or in a package.json file
Specifying Globals
The no-undef rule will warn on variables that are accessed but not defined within the same file. If you are using global variables inside of a file then it’s worthwhile to define those globals so that ESLint will not warn about their usage. You can define global variables either using comments inside of a file or in the configuration file.
To specify globals using a comment inside of your JavaScript file, use the following format:
To configure global variables inside of a configuration file, use the globals key and indicate the global variables you want to use. Set each global variable name equal to true to allow the variable to be overwritten or false to disallow overwriting. For example:
Configuring Plugins
ESLint supports the use of third-party plugins. Before using the plugin you have to install it using npm.
To configure plugins inside of a configuration file, use the plugins key, which contains a list of plugin names. The eslint-plugin- prefix can be omitted from the plugin name.
Note: A globally-installed instance of ESLint can only use globally-installed ESLint plugins. A locally-installed ESLint can make use of both locally- and globally- installed ESLint plugins.
Configuring Rules
ESLint comes with a large number of rules. You can modify which rules your project uses either using configuration comments or configuration files. To change a rule setting, you must set the rule ID equal to one of these values:
To configure rules inside of a file using configuration comments, use a comment in the following format:
In this example, eqeqeq is turned off and curly is turned on as an error. You can also use the numeric equivalent for the rule severity:
This example is the same as the last example, only it uses the numeric codes instead of the string values. The eqeqeq rule is off and the curly rule is set to be an error.
If a rule has additional options, you can specify them using array literal syntax, such as:
This comment specifies the «double» option for the quotes rule. The first item in the array is always the rule severity (number or string).
To configure rules inside of a configuration file, use the rules key along with an error level and any options you want to use. For example:
Disabling Rules with Inline Comments
To temporarily disable rule warnings in your file, use block comments in the following format:
You can also disable or enable warnings for specific rules:
To disable rule warnings in an entire file, put a /* eslint-disable */ block comment at the top of the file:
You can also disable or enable specific rules for an entire file:
To disable all rules on a specific line, use a line comment in one of the following formats:
To disable a specific rule on a specific line:
To disable multiple rules on a specific line:
Note: Comments that disable warnings for a portion of a file tell ESLint not to report rule violations for the disabled code. ESLint still parses the entire file, however, so disabled code still needs to be syntactically valid JavaScript.
Adding Shared Settings
ESLint supports adding shared settings into configuration file. You can add settings object to ESLint configuration file and it will be supplied to every rule that will be executed. This may be useful if you are adding custom rules and want them to have access to the same information and be easily configurable.
Using Configuration Files
In each case, the settings in the configuration file override default settings.
Configuration File Formats
ESLint supports configuration files in several formats:
If there are multiple configuration files in the same directory, ESLint will only use one. The priority order is:
Configuration Cascading and Hierarchy
Note: If you have a personal configuration file in your home directory (
/.eslintrc ), it will only be used if no other configuration files are found. Since a personal configuration would apply to everything inside of a user’s directory, including third-party code, this could cause problems when running ESLint.
The complete configuration hierarchy, from highest precedence to lowest precedence, is as follows:
Extending Configuration Files
A configuration file can extend the set of enabled rules from base configurations.
The extends property value is either:
ESLint extends configurations recursively so a base configuration can also have an extends property.
The rules property can do any of the following to extend (or override) the set of rules:
Using «eslint:recommended»
An extends property value «eslint:recommended» enables a subset of core rules that report common problems, which have a check mark ✓ on the rules page. The recommended subset can change only at major versions of ESLint.
Example of a configuration file in JavaScript format:
Using a shareable configuration package
A sharable configuration is an npm package that exports a configuration object. Make sure the package has been installed to a directory where ESLint can require it.
The extends property value can omit the eslint-config- prefix of the package name.
Example of a configuration file in YAML format:
Using the configuration from a plugin
A plugin is an npm package that usually exports rules. Some plugins also export one or more named configurations. Make sure the package has been installed to a directory where ESLint can require it.
The plugins property value can omit the eslint-plugin- prefix of the package name.
The extends property value can consist of:
Example of a configuration file in JSON format:
Using a configuration file
The extends property value can be an absolute or relative path to a base configuration file.
ESLint resolves a relative path to a base configuration file relative to the configuration file that uses it unless that file is in your home directory or a directory that isn’t an ancestor to the directory in which ESLint is installed (either locally or globally). In those cases, ESLint resolves the relative path to the base file relative to the linted project directory (typically the current working directory).
Example of a configuration file in JSON format:
Using «eslint:all»
The extends property value can be «eslint:all» to enable all core rules in the currently installed version of ESLint. The set of core rules can change at any minor or major version of ESLint.
Important: This configuration is not recommended for production use because it changes with every minor and major version of ESLint. Use at your own risk.
If you configure ESLint to automatically enable new rules when you upgrade, ESLint can report new problems when there are no changes to source code, therefore any newer minor version of ESLint can behave as if it has breaking changes.
You might enable all core rules as a shortcut to explore rules and options while you decide on the configuration for a project, especially if you rarely override options or disable rules. The default options for rules are not endorsements by ESLint (for example, the default option for the quotes rule does not mean double quotes are better than single quotes).
Example of a configuration file in JavaScript format:
Comments in Configuration Files
Both the JSON and YAML configuration file formats support comments ( package.json files should not include them). You can use JavaScript-style comments or YAML-style comments in either type of file and ESLint will safely ignore them. This allows your configuration files to be more human-friendly. For example:
Specifying File extensions to Lint
Ignoring Files and Directories
Globs are matched using node-ignore, so a number of features are available:
Using an Alternate File
Ignored File Warnings
You’ll see this warning: