Shared Settings
December 10, 2024 ยท View on GitHub
The following options can be set by shared settings. Several rules have the same option, but we can set this option at once.
eslint-plugin-n reads both the node, and the n settings to allow for backward compatibility with eslint-plugin-node.
version
This rule reads the [engines] field of package.json but, you can overwrite the version by version option.
The version option accepts the valid version range of node-semver.
Example version
{ "version": ">= 14" }
Default version
{ "version": ">= 16.0.0" }
allowModules
Some platforms have additional embedded modules.
For example, Electron has electron module.
We can specify additional embedded modules with this option. This option is an array of strings as module names.
Example allowModules
{ "allowModules": ["electron"] }
Default allowModules
{ "allowModules": [] }
resolvePaths
Adds additional paths to try for when resolving imports. If a path is relative, it will be resolved from CWD.
Example resolvePaths
{ "resolvePaths": ["/path/to/somewhere", "../relative/path"] }
Default resolvePaths
{ "resolvePaths": [] }
resolverConfig
Override the options generated by this plugin for resolving require and import statements.
While these options are passed down to enhanced-resolve's factory method, we only support a subset of the options they allow. These are documented below.
The options you define here are assigned over the default options generated by the plugin.
Supported options:
resolverConfig.modules: A list of directories to resolve modules from, can be absolute path or folder name
Example resolverConfig
{ "resolverConfig": { "modules": ["node_modules", "bower_components"] } }
Default resolverConfig
{ "resolverConfig": {} }
convertPath
If we use transpilers (e.g. Babel), perhaps the file path to a source code is never published.
convertPath option tells to the rule, it needs to convert file paths.
This option has two shapes:
type ConvertPathObject = {
[includedFiles: string]: [ pattern: string, replacement: string ]
}
type ConvertPathArray = {
include: string[],
exclude?: string[],
replace: [ pattern: string, replacement: string ]
}[]
All replacements use the following code:
path.replace(new RegExp(pattern), replacement);
This means the following replacements are permitted:
| Pattern | Inserts |
|---|---|
$$ | Inserts a "$". |
$& | Inserts the matched substring. |
$` | Inserts the portion of the string that precedes the matched substring. |
$' | Inserts the portion of the string that follows the matched substring. |
$n | Inserts the nth (1-indexed) capturing group where n is a positive integer less than 100. |
$<Name> | Inserts the named capturing group where Name is the group name. |
convertPath - Object
This option has the following shape: <targetFiles>: [<pattern>, <replacement>]
targetFilesis a glob pattern matching linted filespatternis a string escaped regex we pass tonew RegExpreplacementis the replacement string.
Example convertPath - Object
So in this example, src/bin/index.js is handled as bin/index.js.
{ "convertPath": {
"src/bin/**/*.js": ["^src/bin/(.+)$", "bin/\$1"]
} }
convertPath - Array
This option has the following shape: { include: <includedFiles>, exclude: <excludedFiles>, replace: [<pattern>, <replacement>] }
includedFilesis a glob pattern matching linted filesexcludedFilesis a glob pattern matching files in includedFiles that we want to excludepatternis a string escaped regex we pass tonew RegExpreplacementis the replacement string.
Example 1 - Basics
So in this example, src/bin/index.js is handled as bin/index.js.
{ "convertPath": [
{
"include": ["src/bin/**/*.js"],
"replace": ["^src/bin/(.+)$", "bin/\$1"]
}
] }
Example 2 - Exclude specs
So in this example, src/bin/index.js is handled as bin/index.js but, we exclude all .spec.js from the conversion.
{ "convertPath": [
{
"include": ["src/bin/**/*.js"],
"exclude": ["**/*.spec.js"],
"replace": ["^src/bin/(.+)$", "bin/\$1"]
}
] }
tryExtensions
When an import path does not exist, this rule checks whether or not any of path.js, path.json, and path.node exists.
tryExtensions option is the extension list this rule uses at the time.
Example tryExtensions
In this example we only allow the .js, and .ts extensions to be tried.
{ "tryExtensions": [ ".js", ".ts" ] }
Default tryExtensions
{ "tryExtensions": [ ".js", ".json", ".node" ] }
tsconfigPath
Adds the ability to specify the tsconfig used by the typescriptExtensionMap tool.
Example absolute tsconfigPath
{ "tsconfigPath": "/path/to/tsconfig.json" }
Example relative tsconfigPath
{ "tsconfigPath": "./.tsconfig/development.json" }
Default tsconfigPath
By default the tsconfigPath is searched for up the file tree from the currently linted file.
typescriptExtensionMap
Adds the ability to change the extension mapping when converting between typescript and javascript
You can also use the typescript compiler jsx options to automatically use the correct mapping.
We perform the following checks to work out what your ts extension mappings should be:
- This checks
options.typescriptExtensionMap, if its an array then it gets returned. - This checks
options.typescriptExtensionMap, if its a string, convert to the correct mapping. - This checks
options.tsconfigFile, if its set it check for acompilerOptions.jsxproperty - This checks
settings.typescriptExtensionMap, if its an array then it gets returned. - This checks
settings.typescriptExtensionMap, if its a string, convert to the correct mapping. - This checks
settings.tsconfigFile, if its set it check for acompilerOptions.jsxproperty - This tries to find the closest
tsconfig.json, then checks for acompilerOptions.jsxproperty - This returns
PRESERVE_MAPPING.
Example - Custom react mappings
{ "typescriptExtensionMap": [
[ "", ".js" ],
[ ".ts", ".js" ],
[ ".cts", ".cjs" ],
[ ".mts", ".mjs" ],
[ ".tsx", ".js" ],
] }
Example - Wordy react mappings
{ "typescriptExtensionMap": "react" }
Default typescriptExtensionMap
If we cannot find a tsconfig file, we fall back to using:
{ "typescriptExtensionMap": "preserve" }