|
| 1 | +# Shared Settings |
| 2 | + |
| 3 | +The following options can be set by [shared settings](http://eslint.org/docs/user-guide/configuring.html#adding-shared-settings). |
| 4 | +Several rules have the same option, but we can set this option at once. |
| 5 | + |
| 6 | +`eslint-plugin-n` reads both the `node`, and the `n` settings to allow for backward compatibility with `eslint-plugin-node`. |
| 7 | + |
| 8 | +## version |
| 9 | + |
| 10 | +This rule reads the [`engines`] field of `package.json` but, you can overwrite the version by `version` option. |
| 11 | + |
| 12 | +The `version` option accepts [the valid version range of `node-semver`](https://github.com/npm/node-semver#range-grammar). |
| 13 | + |
| 14 | +### Example version |
| 15 | + |
| 16 | +```json |
| 17 | +{ "version": ">= 14" } |
| 18 | +``` |
| 19 | + |
| 20 | +### Default version |
| 21 | + |
| 22 | +```json |
| 23 | +{ "version": ">= 16.0.0" } |
| 24 | +``` |
| 25 | + |
| 26 | +## allowModules |
| 27 | + |
| 28 | +Some platforms have additional embedded modules. |
| 29 | +For example, Electron has `electron` module. |
| 30 | + |
| 31 | +We can specify additional embedded modules with this option. |
| 32 | +This option is an array of strings as module names. |
| 33 | + |
| 34 | +### Example allowModules |
| 35 | + |
| 36 | +```json |
| 37 | +{ "allowModules": ["electron"] } |
| 38 | +``` |
| 39 | + |
| 40 | +### Default allowModules |
| 41 | + |
| 42 | +```json |
| 43 | +{ "allowModules": [] } |
| 44 | +``` |
| 45 | + |
| 46 | +## resolvePaths |
| 47 | + |
| 48 | +Adds additional paths to try for when resolving imports. |
| 49 | +If a path is relative, it will be resolved from CWD. |
| 50 | + |
| 51 | +### Example resolvePaths |
| 52 | + |
| 53 | +```json |
| 54 | +{ "resolvePaths": ["/path/to/somewhere", "../relative/path"] } |
| 55 | +``` |
| 56 | + |
| 57 | +### Default resolvePaths |
| 58 | + |
| 59 | +```json |
| 60 | +{ "resolvePaths": [] } |
| 61 | +``` |
| 62 | + |
| 63 | +## convertPath |
| 64 | + |
| 65 | +If we use transpilers (e.g. Babel), perhaps the file path to a source code is never published. |
| 66 | +`convertPath` option tells to the rule, it needs to convert file paths. |
| 67 | + |
| 68 | +This option has two shapes: |
| 69 | + |
| 70 | +```ts |
| 71 | +type ConvertPathObject = { |
| 72 | + [includedFiles: string]: [ pattern: string, replacement: string ] |
| 73 | +} |
| 74 | +``` |
| 75 | +
|
| 76 | +```ts |
| 77 | +type ConvertPathArray = { |
| 78 | + include: string[], |
| 79 | + exclude?: string[], |
| 80 | + replace: [ pattern: string, replacement: string ] |
| 81 | +}[] |
| 82 | +``` |
| 83 | +
|
| 84 | +All replacements use the following code: |
| 85 | +
|
| 86 | +```js |
| 87 | +path.replace(new RegExp(pattern), replacement); |
| 88 | +``` |
| 89 | + |
| 90 | +This means the following replacements are permitted: |
| 91 | + |
| 92 | +| Pattern | Inserts | |
| 93 | +| --- | --- | |
| 94 | +| `$$` | Inserts a "$". | |
| 95 | +| `$&` | Inserts the matched substring. | |
| 96 | +| `` $` `` | Inserts the portion of the string that precedes the matched substring. | |
| 97 | +| `$'` | Inserts the portion of the string that follows the matched substring. | |
| 98 | +| `$n` | Inserts the nth (1-indexed) capturing group where n is a positive integer less than 100. | |
| 99 | +| `$<Name>` | Inserts the named capturing group where Name is the group name. | |
| 100 | + |
| 101 | +### convertPath - Object |
| 102 | + |
| 103 | +This option has the following shape: `<targetFiles>: [<pattern>, <replacement>]` |
| 104 | + |
| 105 | +- `targetFiles` is a glob pattern matching linted files |
| 106 | +- `pattern` is a string escaped regex we pass to `new RegExp` |
| 107 | +- `replacement` is the replacement string. |
| 108 | + |
| 109 | +#### Example convertPath - Object |
| 110 | + |
| 111 | +So in this example, `src/bin/index.js` is handled as `bin/index.js`. |
| 112 | + |
| 113 | +```json |
| 114 | +{ "convertPath": { |
| 115 | + "src/bin/**/*.js": ["^src/bin/(.+)$", "bin/$1"] |
| 116 | +} } |
| 117 | +``` |
| 118 | + |
| 119 | +### convertPath - Array |
| 120 | + |
| 121 | +This option has the following shape: `{ include: <includedFiles>, exclude: <excludedFiles>, replace: [<pattern>, <replacement>] }` |
| 122 | + |
| 123 | +- `includedFiles` is a glob pattern matching linted files |
| 124 | +- `excludedFiles` is a glob pattern matching files in includedFiles that we want to exclude |
| 125 | +- `pattern` is a string escaped regex we pass to `new RegExp` |
| 126 | +- `replacement` is the replacement string. |
| 127 | + |
| 128 | +#### Example 1 - Basics |
| 129 | + |
| 130 | +So in this example, `src/bin/index.js` is handled as `bin/index.js`. |
| 131 | + |
| 132 | +```json |
| 133 | +{ "convertPath": [ |
| 134 | + { |
| 135 | + "include": ["src/bin/**/*.js"], |
| 136 | + "replace": ["^src/bin/(.+)$", "bin/$1"] |
| 137 | + } |
| 138 | +] } |
| 139 | +``` |
| 140 | + |
| 141 | +#### Example 2 - Exclude specs |
| 142 | + |
| 143 | +So in this example, `src/bin/index.js` is handled as `bin/index.js` but, we exclude all `.spec.js` from the conversion. |
| 144 | + |
| 145 | +```json |
| 146 | +{ "convertPath": [ |
| 147 | + { |
| 148 | + "include": ["src/bin/**/*.js"], |
| 149 | + "exclude": ["**/*.spec.js"], |
| 150 | + "replace": ["^src/bin/(.+)$", "bin/$1"] |
| 151 | + } |
| 152 | +] } |
| 153 | +``` |
| 154 | + |
| 155 | +## tryExtensions |
| 156 | + |
| 157 | +When an import path does not exist, this rule checks whether or not any of `path.js`, `path.json`, and `path.node` exists. |
| 158 | +`tryExtensions` option is the extension list this rule uses at the time. |
| 159 | + |
| 160 | +### Example tryExtensions |
| 161 | + |
| 162 | +In this example we only allow the .js, and .ts extensions to be tried. |
| 163 | + |
| 164 | +```json |
| 165 | +{ "tryExtensions": [ ".js", ".ts" ] } |
| 166 | +``` |
| 167 | + |
| 168 | +### Default tryExtensions |
| 169 | + |
| 170 | +```json |
| 171 | +{ "tryExtensions": [ ".js", ".json", ".node" ] } |
| 172 | +``` |
| 173 | + |
| 174 | +## tsconfigPath |
| 175 | + |
| 176 | +Adds the ability to specify the tsconfig used by the typescriptExtensionMap tool. |
| 177 | + |
| 178 | +### Example absolute tsconfigPath |
| 179 | + |
| 180 | +```json |
| 181 | +{ "tsconfigPath": "/path/to/tsconfig.json" } |
| 182 | +``` |
| 183 | + |
| 184 | +### Example relative tsconfigPath |
| 185 | + |
| 186 | +```json |
| 187 | +{ "tsconfigPath": "./.tsconfig/development.json" } |
| 188 | +``` |
| 189 | + |
| 190 | +### Default tsconfigPath |
| 191 | + |
| 192 | +By default the `tsconfigPath` is searched for up the file tree from the currently linted file. |
| 193 | + |
| 194 | +## typescriptExtensionMap |
| 195 | + |
| 196 | +Adds the ability to change the extension mapping when converting between typescript and javascript |
| 197 | + |
| 198 | +You can also use the [typescript compiler jsx options](https://www.typescriptlang.org/tsconfig#jsx) to automatically use the correct mapping. |
| 199 | + |
| 200 | +We perform the following checks to work out what your ts extension mappings should be: |
| 201 | + |
| 202 | +1. This checks `options.typescriptExtensionMap`, if its an array then it gets returned. |
| 203 | +2. This checks `options.typescriptExtensionMap`, if its a string, convert to the correct mapping. |
| 204 | +3. This checks `options.tsconfigFile`, if its set it check for a `compilerOptions.jsx` property |
| 205 | +4. This checks `settings.typescriptExtensionMap`, if its an array then it gets returned. |
| 206 | +5. This checks `settings.typescriptExtensionMap`, if its a string, convert to the correct mapping. |
| 207 | +6. This checks `settings.tsconfigFile`, if its set it check for a `compilerOptions.jsx` property |
| 208 | +7. This tries to find the closest `tsconfig.json`, then checks for a `compilerOptions.jsx` property |
| 209 | +8. This returns `PRESERVE_MAPPING`. |
| 210 | + |
| 211 | +### Example - Custom react mappings |
| 212 | + |
| 213 | +```json |
| 214 | +{ "typescriptExtensionMap": [ |
| 215 | + [ "", ".js" ], |
| 216 | + [ ".ts", ".js" ], |
| 217 | + [ ".cts", ".cjs" ], |
| 218 | + [ ".mts", ".mjs" ], |
| 219 | + [ ".tsx", ".js" ], |
| 220 | +] } |
| 221 | +``` |
| 222 | + |
| 223 | +### Example - Wordy react mappings |
| 224 | + |
| 225 | +```json |
| 226 | +{ "typescriptExtensionMap": "react" } |
| 227 | +``` |
| 228 | + |
| 229 | +### Default typescriptExtensionMap |
| 230 | + |
| 231 | +If we cannot find a tsconfig file, we fall back to using: |
| 232 | + |
| 233 | +```json |
| 234 | +{ "typescriptExtensionMap": "preserve" } |
| 235 | +``` |
0 commit comments