Skip to content

Commit 7d855e6

Browse files
authoredSep 25, 2023
docs: Create a shared settings document (#115)
1 parent 150b34f commit 7d855e6

11 files changed

+298
-621
lines changed
 

‎.eslint-doc-generatorrc.js

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const config = {
1010
["flat/recommended", "☑️"],
1111
["flat/mixed-esm-and-cjs", "🟠"],
1212
],
13+
ruleDocSectionOptions: false,
1314
}
1415

1516
module.exports = config

‎docs/rules/file-extension-in-import.md

+6-30
Original file line numberDiff line numberDiff line change
@@ -92,39 +92,15 @@ import logo from "./logo.png"
9292

9393
### Shared Settings
9494

95-
The following options can be set by [shared settings](http://eslint.org/docs/user-guide/configuring.html#adding-shared-settings).
96-
Several rules have the same option, but we can set this option at once.
95+
#### tsconfigPath
9796

98-
#### typescriptExtensionMap
99-
100-
Adds the ability to change the extension mapping when converting between typescript and javascript
101-
102-
You can also use the [typescript compiler jsx options](https://www.typescriptlang.org/tsconfig#jsx) to automatically use the correct mapping.
97+
This can be configured in the shared settings [`settings.tsconfigPath`](../shared-settings.md#tsconfigpath).
98+
Please see the shared settings documentation for more information.
10399

104-
If this option is left undefined we:
105-
106-
1. Check your `tsconfig.json` `compilerOptions.jsx`
107-
2. Return the default mapping (jsx = `preserve`)
100+
#### typescriptExtensionMap
108101

109-
```js
110-
// .eslintrc.js
111-
module.exports = {
112-
"settings": {
113-
"node": {
114-
"typescriptExtensionMap": [
115-
[ "", ".js" ],
116-
[ ".ts", ".js" ],
117-
[ ".cts", ".cjs" ],
118-
[ ".mts", ".mjs" ],
119-
[ ".tsx", ".jsx" ],
120-
]
121-
}
122-
},
123-
"rules": {
124-
"n/file-extension-in-import": "error"
125-
}
126-
}
127-
```
102+
This can be configured in the shared settings [`settings.typescriptExtensionMap`](../shared-settings.md#typescriptextensionmap).
103+
Please see the shared settings documentation for more information.
128104

129105
## 🔎 Implementation
130106

‎docs/rules/no-extraneous-import.md

+6-45
Original file line numberDiff line numberDiff line change
@@ -26,57 +26,18 @@ This rule warns `import` declarations of extraneous modules.
2626

2727
#### allowModules
2828

29-
Some platforms have additional embedded modules.
30-
For example, Electron has `electron` module.
31-
32-
We can specify additional embedded modules with this option.
33-
This option is an array of strings as module names.
34-
35-
```json
36-
{
37-
"rules": {
38-
"n/no-extraneous-import": ["error", {
39-
"allowModules": ["electron"]
40-
}]
41-
}
42-
}
43-
```
29+
This can be configured in the rule options or as a shared setting [`settings.allowModules`](../shared-settings.md#allowmodules).
30+
Please see the shared settings documentation for more information.
4431

4532
#### resolvePaths
4633

47-
Adds additional paths to try for when resolving imports.
48-
If a path is relative, it will be resolved from CWD.
49-
50-
Default is `[]`
34+
This can be configured in the rule options or as a shared setting [`settings.resolvePaths`](../shared-settings.md#resolvepaths).
35+
Please see the shared settings documentation for more information.
5136

5237
#### convertPath
5338

54-
- `exclude`: TODO
55-
- `include`: TODO
56-
- `replace`: TODO
57-
58-
### Shared Settings
59-
60-
The following options can be set by [shared settings](http://eslint.org/docs/user-guide/configuring.html#adding-shared-settings).
61-
Several rules have the same option, but we can set this option at once.
62-
63-
- `allowModules`
64-
- `resolvePaths`
65-
66-
```js
67-
// .eslintrc.js
68-
module.exports = {
69-
"settings": {
70-
"node": {
71-
"allowModules": ["electron"],
72-
"resolvePaths": [__dirname],
73-
}
74-
},
75-
"rules": {
76-
"n/no-extraneous-import": "error"
77-
}
78-
}
79-
```
39+
This can be configured in the rule options or as a shared setting [`settings.convertPath`](../shared-settings.md#convertpath).
40+
Please see the shared settings documentation for more information.
8041

8142
## 🔎 Implementation
8243

‎docs/rules/no-extraneous-require.md

+10-53
Original file line numberDiff line numberDiff line change
@@ -27,66 +27,23 @@ This rule warns `require()` of extraneous modules.
2727

2828
#### allowModules
2929

30-
Some platforms have additional embedded modules.
31-
For example, Electron has `electron` module.
32-
33-
We can specify additional embedded modules with this option.
34-
This option is an array of strings as module names.
35-
36-
```json
37-
{
38-
"rules": {
39-
"n/no-extraneous-require": ["error", {
40-
"allowModules": ["electron"]
41-
}]
42-
}
43-
}
44-
```
30+
This can be configured in the rule options or as a shared setting [`settings.allowModules`](../shared-settings.md#allowmodules).
31+
Please see the shared settings documentation for more information.
4532

4633
#### resolvePaths
4734

48-
Adds additional paths to try for when resolving imports.
49-
If a path is relative, it will be resolved from CWD.
35+
This can be configured in the rule options or as a shared setting [`settings.resolvePaths`](../shared-settings.md#resolvepaths).
36+
Please see the shared settings documentation for more information.
5037

51-
Default is `[]`
52-
53-
#### tryExtensions
54-
55-
When an import path does not exist, this rule checks whether or not any of `path.js`, `path.json`, and `path.node` exists.
56-
`tryExtensions` option is the extension list this rule uses at the time.
38+
#### convertPath
5739

58-
Default is `[".js", ".json", ".node"]`.
40+
This can be configured in the rule options or as a shared setting [`settings.convertPath`](../shared-settings.md#convertpath).
41+
Please see the shared settings documentation for more information.
5942

60-
#### convertPath
43+
#### tryExtensions
6144

62-
- `exclude`: TODO
63-
- `include`: TODO
64-
- `replace`: TODO
65-
66-
### Shared Settings
67-
68-
The following options can be set by [shared settings](http://eslint.org/docs/user-guide/configuring.html#adding-shared-settings).
69-
Several rules have the same option, but we can set this option at once.
70-
71-
- `allowModules`
72-
- `resolvePaths`
73-
- `tryExtensions`
74-
75-
```js
76-
// .eslintrc.js
77-
module.exports = {
78-
"settings": {
79-
"node": {
80-
"allowModules": ["electron"],
81-
"resolvePaths": [__dirname],
82-
"tryExtensions": [".js", ".json", ".node"]
83-
}
84-
},
85-
"rules": {
86-
"n/no-extraneous-require": "error"
87-
}
88-
}
89-
```
45+
This can be configured in the rule options or as a shared setting [`settings.tryExtensions`](../shared-settings.md#tryextensions).
46+
Please see the shared settings documentation for more information.
9047

9148
## 🔎 Implementation
9249

‎docs/rules/no-missing-import.md

+9-73
Original file line numberDiff line numberDiff line change
@@ -46,87 +46,23 @@ import existingModule from "existing-module";
4646

4747
#### allowModules
4848

49-
Some platforms have additional embedded modules.
50-
For example, Electron has `electron` module.
51-
52-
We can specify additional embedded modules with this option.
53-
This option is an array of strings as module names.
54-
55-
```json
56-
{
57-
"rules": {
58-
"n/no-missing-import": ["error", {
59-
"allowModules": ["electron"]
60-
}]
61-
}
62-
}
63-
```
49+
This can be configured in the rule options or as a shared setting [`settings.allowModules`](../shared-settings.md#allowmodules).
50+
Please see the shared settings documentation for more information.
6451

6552
#### resolvePaths
6653

67-
Adds additional paths to try for when resolving imports.
68-
If a path is relative, it will be resolved from CWD.
69-
70-
Default is `[]`
71-
72-
#### typescriptExtensionMap
73-
74-
Adds the ability to change the extension mapping when converting between typescript and javascript
75-
76-
You can also use the [typescript compiler jsx options](https://www.typescriptlang.org/tsconfig#jsx) to automatically use the correct mapping.
77-
78-
If this option is left undefined we:
79-
80-
1. Check the Shared Settings
81-
2. Check your `tsconfig.json` `compilerOptions.jsx`
82-
3. Return the default mapping (jsx = `preserve`)
83-
84-
Default is:
85-
86-
```json
87-
[
88-
[ "", ".js" ],
89-
[ ".ts", ".js" ],
90-
[ ".cts", ".cjs" ],
91-
[ ".mts", ".mjs" ],
92-
[ ".tsx", ".jsx" ],
93-
]
94-
```
54+
This can be configured in the rule options or as a shared setting [`settings.resolvePaths`](../shared-settings.md#resolvepaths).
55+
Please see the shared settings documentation for more information.
9556

9657
#### tsconfigPath
9758

98-
Adds the ability to specify the tsconfig used by the typescriptExtensionMap tool.
99-
100-
### Shared Settings
59+
This can be configured in the rule options or as a shared setting [`settings.tsconfigPath`](../shared-settings.md#tsconfigpath).
60+
Please see the shared settings documentation for more information.
10161

102-
The following options can be set by [shared settings](http://eslint.org/docs/user-guide/configuring.html#adding-shared-settings).
103-
Several rules have the same option, but we can set this option at once.
104-
105-
- `allowModules`
106-
- `resolvePaths`
107-
- `typescriptExtensionMap`
62+
#### typescriptExtensionMap
10863

109-
```js
110-
// .eslintrc.js
111-
module.exports = {
112-
"settings": {
113-
"node": {
114-
"allowModules": ["electron"],
115-
"resolvePaths": [__dirname],
116-
"typescriptExtensionMap": [
117-
[ "", ".js" ],
118-
[ ".ts", ".js" ],
119-
[ ".cts", ".cjs" ],
120-
[ ".mts", ".mjs" ],
121-
[ ".tsx", ".js" ],
122-
]
123-
}
124-
},
125-
"rules": {
126-
"n/no-missing-import": "error"
127-
}
128-
}
129-
```
64+
This can be configured in the rule options or as a shared setting [`settings.typescriptExtensionMap`](../shared-settings.md#typescriptextensionmap).
65+
Please see the shared settings documentation for more information.
13066

13167
## 🔎 Implementation
13268

‎docs/rules/no-missing-require.md

+11-79
Original file line numberDiff line numberDiff line change
@@ -52,96 +52,28 @@ var foo = require(FOO_NAME);
5252

5353
#### allowModules
5454

55-
Some platforms have additional embedded modules.
56-
For example, Electron has `electron` module.
57-
58-
We can specify additional embedded modules with this option.
59-
This option is an array of strings as module names.
60-
61-
```json
62-
{
63-
"rules": {
64-
"n/no-missing-require": ["error", {
65-
"allowModules": ["electron"]
66-
}]
67-
}
68-
}
69-
```
55+
This can be configured in the rule options or as a shared setting [`settings.allowModules`](../shared-settings.md#allowmodules).
56+
Please see the shared settings documentation for more information.
7057

7158
#### resolvePaths
7259

73-
Adds additional paths to try for when resolving a require.
74-
If a path is relative, it will be resolved from CWD.
75-
76-
Default is `[]`
60+
This can be configured in the rule options or as a shared setting [`settings.resolvePaths`](../shared-settings.md#resolvepaths).
61+
Please see the shared settings documentation for more information.
7762

7863
#### tryExtensions
7964

80-
When an import path does not exist, this rule checks whether or not any of `path.js`, `path.json`, and `path.node` exists.
81-
`tryExtensions` option is the extension list this rule uses at the time.
82-
83-
Default is `[".js", ".json", ".node"]`.
84-
85-
#### typescriptExtensionMap
86-
87-
Adds the ability to change the extension mapping when converting between typescript and javascript
88-
89-
You can also use the [typescript compiler jsx options](https://www.typescriptlang.org/tsconfig#jsx) to automatically use the correct mapping.
90-
91-
If this option is left undefined we:
92-
93-
1. Check the Shared Settings
94-
2. Check your `tsconfig.json` `compilerOptions.jsx`
95-
3. Return the default mapping (jsx = `preserve`)
96-
97-
Default is:
98-
99-
```json
100-
[
101-
[ "", ".js" ],
102-
[ ".ts", ".js" ],
103-
[ ".cts", ".cjs" ],
104-
[ ".mts", ".mjs" ],
105-
[ ".tsx", ".jsx" ],
106-
]
107-
```
65+
This can be configured in the rule options or as a shared setting [`settings.tryExtensions`](../shared-settings.md#tryextensions).
66+
Please see the shared settings documentation for more information.
10867

10968
#### tsconfigPath
11069

111-
Adds the ability to specify the tsconfig used by the typescriptExtensionMap tool.
112-
113-
### Shared Settings
70+
This can be configured in the rule options or as a shared setting [`settings.tsconfigPath`](../shared-settings.md#tsconfigpath).
71+
Please see the shared settings documentation for more information.
11472

115-
The following options can be set by [shared settings](http://eslint.org/docs/user-guide/configuring.html#adding-shared-settings).
116-
Several rules have the same option, but we can set this option at once.
117-
118-
- `allowModules`
119-
- `resolvePaths`
120-
- `tryExtensions`
121-
- `typescriptExtensionMap`
73+
#### typescriptExtensionMap
12274

123-
```js
124-
// .eslintrc.js
125-
module.exports = {
126-
"settings": {
127-
"node": {
128-
"allowModules": ["electron"],
129-
"resolvePaths": [__dirname],
130-
"tryExtensions": [".js", ".json", ".node"],
131-
"typescriptExtensionMap": [
132-
[ "", ".js" ],
133-
[ ".ts", ".js" ],
134-
[ ".cts", ".cjs" ],
135-
[ ".mts", ".mjs" ],
136-
[ ".tsx", ".js" ],
137-
]
138-
}
139-
},
140-
"rules": {
141-
"n/no-missing-require": "error"
142-
}
143-
}
144-
```
75+
This can be configured in the rule options or as a shared setting [`settings.typescriptExtensionMap`](../shared-settings.md#typescriptextensionmap).
76+
Please see the shared settings documentation for more information.
14577

14678
## 🔎 Implementation
14779

‎docs/rules/no-unpublished-bin.md

+2-74
Original file line numberDiff line numberDiff line change
@@ -36,80 +36,8 @@ If `npm` ignores the files in `bin` field, this rule warns the files.
3636

3737
#### convertPath
3838

39-
If we use transpilers (e.g. Babel), perhaps the file path to a source code is never published.
40-
`convertPath` option tells to the rule, it needs to convert file paths.
41-
42-
For example:
43-
44-
```json
45-
{
46-
"rules": {
47-
"n/no-unpublished-bin": ["error", {
48-
"convertPath": {
49-
"src/bin/**/*.js": ["^src/(.+)$", "$1"]
50-
}
51-
}]
52-
}
53-
}
54-
```
55-
56-
This option has the following shape: `<targetFiles>: [<fromRegExp>, <toString>]`
57-
58-
`targetFiles` is a glob pattern.
59-
It converts paths which are matched to the pattern with the following way.
60-
61-
```js
62-
path.replace(new RegExp(fromRegExp), toString);
63-
```
64-
65-
So on this example, `src/bin/index.js` is handled as `bin/index.js`.
66-
67-
The `convertPath` option can be an array as well.
68-
69-
For example:
70-
71-
```json
72-
{
73-
"rules": {
74-
"n/no-unpublished-bin": ["error", {
75-
"convertPath": [
76-
{
77-
"include": ["src/bin/**/*.js"],
78-
"exclude": ["**/*.spec.js"],
79-
"replace": ["^src/(.+)$", "$1"]
80-
}
81-
]
82-
}]
83-
}
84-
}
85-
```
86-
87-
In this style, this option has the following shape as the same expression as above: `{include: [<targetFiles>], replace: [<fromRegExp>, <toString>]}`.
88-
In addition, we can specify glob patterns to exclude files.
89-
90-
### Shared Settings
91-
92-
The following options can be set by [shared settings](http://eslint.org/docs/user-guide/configuring.html#adding-shared-settings).
93-
Several rules have the same option, but we can set this option at once.
94-
95-
- `convertPath`
96-
97-
For Example:
98-
99-
```json
100-
{
101-
"settings": {
102-
"node": {
103-
"convertPath": {
104-
"src/bin/**/*.js": ["^src/bin/(.+)$", "bin/$1"]
105-
}
106-
}
107-
},
108-
"rules": {
109-
"n/no-unpublished-bin": "error"
110-
}
111-
}
112-
```
39+
This can be configured in the rule options or as a shared setting [`settings.convertPath`](../shared-settings.md#convertpath).
40+
Please see the shared settings documentation for more information.
11341

11442
## 🔎 Implementation
11543

‎docs/rules/no-unpublished-import.md

+7-93
Original file line numberDiff line numberDiff line change
@@ -35,78 +35,18 @@ Then this rule warns `import` declarations in \*published\* files if the `import
3535

3636
#### allowModules
3737

38-
Some platforms have additional embedded modules.
39-
For example, Electron has `electron` module.
38+
This can be configured in the rule options or as a shared setting [`settings.allowModules`](../shared-settings.md#allowmodules).
39+
Please see the shared settings documentation for more information.
4040

41-
We can specify additional embedded modules with this option.
42-
This option is an array of strings as module names.
41+
#### resolvePaths
4342

44-
```json
45-
{
46-
"rules": {
47-
"n/no-unpublished-import": ["error", {
48-
"allowModules": ["electron"]
49-
}]
50-
}
51-
}
52-
```
43+
This can be configured in the rule options or as a shared setting [`settings.resolvePaths`](../shared-settings.md#resolvepaths).
44+
Please see the shared settings documentation for more information.
5345

5446
#### convertPath
5547

56-
If we use transpilers (e.g. Babel), perhaps the file path to a source code is never published.
57-
`convertPath` option tells to the rule, it needs to convert file paths.
58-
59-
For example:
60-
61-
```json
62-
{
63-
"rules": {
64-
"n/no-unpublished-import": ["error", {
65-
"convertPath": {
66-
"src/**/*.jsx": ["^src/(.+?)\\.jsx$", "lib/$1.js"]
67-
}
68-
}]
69-
}
70-
}
71-
```
72-
73-
This option has the following shape: `<targetFiles>: [<fromRegExp>, <toString>]`
74-
75-
`targetFiles` is a glob pattern.
76-
It converts paths which are matched to the pattern with the following way.
77-
78-
```js
79-
path.replace(new RegExp(fromRegExp), toString);
80-
```
81-
82-
So on this example, `src/a/foo.jsx` is handled as `lib/a/foo.js`.
83-
84-
The `convertPath` option can be an array as well.
85-
86-
For example:
87-
88-
```json
89-
{
90-
"rules": {
91-
"n/no-unpublished-import": ["error", {
92-
"convertPath": [
93-
{
94-
"include": ["src/**/*.js"],
95-
"exclude": ["**/*.spec.js"],
96-
"replace": ["^src/(.+)$", "lib/$1"]
97-
}
98-
]
99-
}]
100-
}
101-
}
102-
```
103-
104-
In this style, this option has the following shape as the same expression as above: `{include: [<targetFiles>], replace: [<fromRegExp>, <toString>]}`.
105-
In addition, we can specify glob patterns to exclude files.
106-
107-
#### resolvePaths
108-
109-
TODO
48+
This can be configured in the rule options or as a shared setting [`settings.convertPath`](../shared-settings.md#convertpath).
49+
Please see the shared settings documentation for more information.
11050

11151
### ignoreTypeImport
11252

@@ -128,32 +68,6 @@ In this way, the following code will not be reported:
12868
import type foo from "foo";
12969
```
13070

131-
### Shared Settings
132-
133-
The following options can be set by [shared settings](http://eslint.org/docs/user-guide/configuring.html#adding-shared-settings).
134-
Several rules have the same option, but we can set this option at once.
135-
136-
- `allowModules`
137-
- `convertPath`
138-
139-
For Example:
140-
141-
```json
142-
{
143-
"settings": {
144-
"node": {
145-
"allowModules": ["electron"],
146-
"convertPath": {
147-
"src/**/*.jsx": ["^src/(.+?)\\.jsx$", "lib/$1.js"]
148-
}
149-
}
150-
},
151-
"rules": {
152-
"n/no-unpublished-import": "error"
153-
}
154-
}
155-
```
156-
15771
## 🔎 Implementation
15872

15973
- [Rule source](../../lib/rules/no-unpublished-import.js)

‎docs/rules/no-unpublished-require.md

+9-100
Original file line numberDiff line numberDiff line change
@@ -35,114 +35,23 @@ Then this rule warns `require()` expressions in \*published\* files if the `requ
3535

3636
#### allowModules
3737

38-
Some platforms have additional embedded modules.
39-
For example, Electron has `electron` module.
38+
This can be configured in the rule options or as a shared setting [`settings.allowModules`](../shared-settings.md#allowmodules).
39+
Please see the shared settings documentation for more information.
4040

41-
We can specify additional embedded modules with this option.
42-
This option is an array of strings as module names.
41+
#### resolvePaths
4342

44-
```json
45-
{
46-
"rules": {
47-
"n/no-unpublished-require": ["error", {
48-
"allowModules": ["electron"]
49-
}]
50-
}
51-
}
52-
```
43+
This can be configured in the rule options or as a shared setting [`settings.resolvePaths`](../shared-settings.md#resolvepaths).
44+
Please see the shared settings documentation for more information.
5345

5446
#### convertPath
5547

56-
If we use transpilers (e.g. Babel), perhaps the file path to a source code is never published.
57-
`convertPath` option tells to the rule, it needs to convert file paths.
58-
59-
For example:
60-
61-
```json
62-
{
63-
"rules": {
64-
"n/no-unpublished-require": ["error", {
65-
"convertPath": {
66-
"src/**/*.jsx": ["^src/(.+?)\\.jsx$", "lib/$1.js"]
67-
},
68-
"tryExtensions": [".js", ".jsx", ".json"]
69-
}]
70-
}
71-
}
72-
```
73-
74-
This option has the following shape: `<targetFiles>: [<fromRegExp>, <toString>]`
75-
76-
`targetFiles` is a glob pattern.
77-
It converts paths which are matched to the pattern with the following way.
78-
79-
```js
80-
path.replace(new RegExp(fromRegExp), toString);
81-
```
82-
83-
So on this example, `src/a/foo.jsx` is handled as `lib/a/foo.js`.
84-
85-
The `convertPath` option can be an array as well.
86-
87-
For example:
88-
89-
```json
90-
{
91-
"rules": {
92-
"n/no-unpublished-require": ["error", {
93-
"convertPath": [
94-
{
95-
"include": ["src/**/*.js"],
96-
"exclude": ["**/*.spec.js"],
97-
"replace": ["^src/(.+)$", "lib/$1"]
98-
}
99-
]
100-
}]
101-
}
102-
}
103-
```
104-
105-
In this style, this option has the following shape as the same expression as above: `{include: [<targetFiles>], replace: [<fromRegExp>, <toString>]}`.
106-
In addition, we can specify glob patterns to exclude files.
48+
This can be configured in the rule options or as a shared setting [`settings.convertPath`](../shared-settings.md#convertpath).
49+
Please see the shared settings documentation for more information.
10750

10851
#### tryExtensions
10952

110-
When an import path does not exist, this rule checks whether or not any of `path.js`, `path.json`, and `path.node` exists.
111-
`tryExtensions` option is the extension list this rule uses at the time.
112-
113-
Default is `[".js", ".json", ".node"]`.
114-
115-
#### resolvePaths
116-
117-
TODO
118-
119-
### Shared Settings
120-
121-
The following options can be set by [shared settings](http://eslint.org/docs/user-guide/configuring.html#adding-shared-settings).
122-
Several rules have the same option, but we can set this option at once.
123-
124-
- `allowModules`
125-
- `convertPath`
126-
- `tryExtensions`
127-
128-
For Example:
129-
130-
```json
131-
{
132-
"settings": {
133-
"node": {
134-
"allowModules": ["electron"],
135-
"convertPath": {
136-
"src/**/*.jsx": ["^src/(.+?)\\.jsx$", "lib/$1.js"]
137-
},
138-
"tryExtensions": [".js", ".jsx", ".json"]
139-
}
140-
},
141-
"rules": {
142-
"n/no-unpublished-require": "error"
143-
}
144-
}
145-
```
53+
This can be configured in the rule options or as a shared setting [`settings.tryExtensions`](../shared-settings.md#tryextensions).
54+
Please see the shared settings documentation for more information.
14655

14756
## 🔎 Implementation
14857

‎docs/rules/shebang.md

+2-74
Original file line numberDiff line numberDiff line change
@@ -67,80 +67,8 @@ console.log("hello");
6767

6868
#### convertPath
6969

70-
If we use transpilers (e.g. Babel), perhaps the file path to a source code is never handled as a bin file.
71-
`convertPath` option tells to the rule, it needs to convert file paths.
72-
73-
For example:
74-
75-
```json
76-
{
77-
"rules": {
78-
"n/shebang": ["error", {
79-
"convertPath": {
80-
"src/**/*.jsx": ["^src/(.+?)\\.jsx$", "lib/$1.js"]
81-
}
82-
}]
83-
}
84-
}
85-
```
86-
87-
This option has the following shape: `<targetFiles>: [<fromRegExp>, <toString>]`
88-
89-
`targetFiles` is a glob pattern.
90-
It converts paths which are matched to the pattern with the following way.
91-
92-
```js
93-
path.replace(new RegExp(fromRegExp), toString);
94-
```
95-
96-
So on this example, `src/a/foo.jsx` is handled as `lib/a/foo.js`.
97-
98-
The `convertPath` option can be an array as well.
99-
100-
For example:
101-
102-
```json
103-
{
104-
"rules": {
105-
"n/shebang": ["error", {
106-
"convertPath": [
107-
{
108-
"include": ["src/**/*.js"],
109-
"exclude": ["**/*.spec.js"],
110-
"replace": ["^src/(.+)$", "lib/$1"]
111-
}
112-
]
113-
}]
114-
}
115-
}
116-
```
117-
118-
In this style, this option has the following shape as the same expression as above: `{include: [<targetFiles>], replace: [<fromRegExp>, <toString>]}`.
119-
In addition, we can specify glob patterns to exclude files.
120-
121-
### Shared Settings
122-
123-
The following options can be set by [shared settings](http://eslint.org/docs/user-guide/configuring.html#adding-shared-settings).
124-
Several rules have the same option, but we can set this option at once.
125-
126-
- `convertPath`
127-
128-
For Example:
129-
130-
```json
131-
{
132-
"settings": {
133-
"node": {
134-
"convertPath": {
135-
"src/**/*.jsx": ["^src/(.+?)\\.jsx$", "lib/$1.js"]
136-
}
137-
}
138-
},
139-
"rules": {
140-
"n/shebang": "error"
141-
}
142-
}
143-
```
70+
This can be configured in the rule options or as a shared setting [`settings.convertPath`](../shared-settings.md#convertpath).
71+
Please see the shared settings documentation for more information.
14472

14573
## 🔎 Implementation
14674

‎docs/shared-settings.md

+235
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
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

Comments
 (0)
Please sign in to comment.