Skip to content

Commit c46ca2c

Browse files
authored
Merge pull request #247 from brettz9/check-types-prefer-types
`preferredTypes`
2 parents df07683 + d8c3e6e commit c46ca2c

File tree

9 files changed

+934
-37
lines changed

9 files changed

+934
-37
lines changed

.README/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,31 @@ The format of the configuration is as follows:
198198
}
199199
```
200200

201+
### Settings to Configure `check-types` and `no-undefined-types`
202+
203+
- `settings.jsdoc.preferredTypes` An option map to indicate preferred
204+
or forbidden types (if default types are indicated here, these will
205+
have precedence over the default recommendations for `check-types`).
206+
The keys of this map are the types to be replaced (or forbidden) and
207+
can include the "ANY" type, `*`.
208+
The values can be:
209+
- `false` to forbid the type
210+
- a string to indicate the type that should be preferred in its place
211+
(and which `fix` mode can replace)
212+
- an object with the key `message` to provide a specific error message
213+
when encountering the discouraged type and, if a type is to be preferred
214+
in its place, the key `replacement` to indicate the type that should be
215+
used in its place (and which `fix` mode can replace) or `false` if
216+
forbidding the type. The message string will have the following
217+
substrings with special meaning replaced with their corresponding
218+
value (`{{tagName}}`, `{{tagValue}}`, `{{badType}}`, and
219+
`{{preferredType}}`, noting that the latter is of no use when one is
220+
merely forbidding a type).
221+
222+
If `no-undefined-types` has the option key `preferredTypesDefined` set to
223+
`true`, the preferred types indicated in the `settings.jsdoc.preferredTypes`
224+
map will be assumed to be defined.
225+
201226
### Settings to Configure `valid-types`
202227

203228
* `settings.jsdoc.allowEmptyNamepaths` - Set to `false` to disallow

.README/rules/check-types.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Reports invalid types.
44

5-
Ensures that case of native types is the same as in this list:
5+
By default, ensures that case of native types is the same as in this list:
66

77
```
88
undefined
@@ -16,6 +16,17 @@ Date
1616
RegExp
1717
```
1818

19+
#### Options
20+
21+
`check-types` allows one option:
22+
23+
- An option object with the key `noDefaults` to insist that only the
24+
supplied option type map is to be used, and that the default preferences
25+
(such as "string" over "String") will not be enforced.
26+
27+
See also the documentation on `settings.jsdoc.preferredTypes` which impacts
28+
the behavior of `check-types`.
29+
1930
#### Why not capital case everything?
2031

2132
Why are `boolean`, `number` and `string` exempt from starting with a capital letter? Let's take `string` as an example. In Javascript, everything is an object. The string Object has prototypes for string functions such as `.toUpperCase()`.

.README/rules/no-undefined-types.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,30 @@ unimported types.
66
When enabling this rule, types in jsdoc comments will resolve as used
77
variables, i.e. will not be marked as unused by `no-unused-vars`.
88

9-
The following tags will be checked for name(paths) definitions to also serve
10-
as a potential "type" for checking the tag types in the table below:
9+
In addition to considering globals found in code (or in ESLint-indicated
10+
`globals`) as defined, the following tags will also be checked for
11+
name(path) definitions to also serve as a potential "type" for checking
12+
the tag types in the table below:
1113

1214
`callback`, `class` (or `constructor`), `constant` (or `const`), `event`, `external` (or `host`), `function` (or `func` or `method`), `interface`, `member` (or `var`), `mixin`, `name`, `namespace`, `typedef`.
1315

16+
The following types are always considered defined.
17+
18+
- `null`, `undefined`, `string`, `boolean`
19+
- `number`, `NaN`, `Infinity`
20+
- `any`, `*`
21+
- `Array`, `Object`, `RegExp`, `Date`, `Function`
22+
23+
#### Options
24+
25+
An option object may have the following keys:
26+
27+
- `preferredTypesDefined` - If this option is set to `true` and preferred
28+
types are indicated within `settings.jsdoc.preferredTypes`, any such
29+
types will be assumed to be defined as well.
30+
- `definedTypes` - This array can be populated to indicate other types which
31+
are automatically considered as defined (in addition to globals, etc.)
32+
1433
|||
1534
|---|---|
1635
|Context|`ArrowFunctionExpression`, `FunctionDeclaration`, `FunctionExpression`|

0 commit comments

Comments
 (0)