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
.
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
.
{ "version": ">= 14" }
{ "version": ">= 16.0.0" }
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.
{ "allowModules": ["electron"] }
{ "allowModules": [] }
Adds additional paths to try for when resolving imports. If a path is relative, it will be resolved from CWD.
{ "resolvePaths": ["/path/to/somewhere", "../relative/path"] }
{ "resolvePaths": [] }
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. |
This option has the following shape: <targetFiles>: [<pattern>, <replacement>]
targetFiles
is a glob pattern matching linted filespattern
is a string escaped regex we pass tonew RegExp
replacement
is the replacement string.
So in this example, src/bin/index.js
is handled as bin/index.js
.
{ "convertPath": {
"src/bin/**/*.js": ["^src/bin/(.+)$", "bin/$1"]
} }
This option has the following shape: { include: <includedFiles>, exclude: <excludedFiles>, replace: [<pattern>, <replacement>] }
includedFiles
is a glob pattern matching linted filesexcludedFiles
is a glob pattern matching files in includedFiles that we want to excludepattern
is a string escaped regex we pass tonew RegExp
replacement
is the replacement string.
So in this example, src/bin/index.js
is handled as bin/index.js
.
{ "convertPath": [
{
"include": ["src/bin/**/*.js"],
"replace": ["^src/bin/(.+)$", "bin/$1"]
}
] }
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"]
}
] }
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.
In this example we only allow the .js, and .ts extensions to be tried.
{ "tryExtensions": [ ".js", ".ts" ] }
{ "tryExtensions": [ ".js", ".json", ".node" ] }
Adds the ability to specify the tsconfig used by the typescriptExtensionMap tool.
{ "tsconfigPath": "/path/to/tsconfig.json" }
{ "tsconfigPath": "./.tsconfig/development.json" }
By default the tsconfigPath
is searched for up the file tree from the currently linted file.
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.jsx
property - 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.jsx
property - This tries to find the closest
tsconfig.json
, then checks for acompilerOptions.jsx
property - This returns
PRESERVE_MAPPING
.
{ "typescriptExtensionMap": [
[ "", ".js" ],
[ ".ts", ".js" ],
[ ".cts", ".cjs" ],
[ ".mts", ".mjs" ],
[ ".tsx", ".js" ],
] }
{ "typescriptExtensionMap": "react" }
If we cannot find a tsconfig file, we fall back to using:
{ "typescriptExtensionMap": "preserve" }