Skip to content

Commit 82ca868

Browse files
committed
feat(check-types): allow two types (set one to the other in preferredTypes); make this the default for typescript with "object"/"Object"
1 parent 96dcdce commit 82ca868

File tree

5 files changed

+164
-1
lines changed

5 files changed

+164
-1
lines changed

.README/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,11 @@ how the keys of `preferredTypes` may have `<>` or `.<>` (or just `.`)
361361
appended and its bearing on whether types are checked as parents/children
362362
only (e.g., to match `Array` if the type is `Array` vs. `Array.<string>`).
363363

364+
Note that if a value is present both as a key and as a value, neither the
365+
key nor the value will be reported. Thus in `check-types`, this fact can
366+
be used to allow both `object` and `Object` if one has a `preferredTypes`
367+
key `object: 'Object'` and `Object: 'object'`.
368+
364369
## Advanced
365370

366371
### AST and Selectors

.README/rules/check-types.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ RegExp
5555
`string[]` specifically as distinct from say `number[]`, but you can
5656
target both with `[]` or the child types `number` or `string`.
5757

58+
If a value is present both as a key and as a value, neither the key nor the
59+
value will be reported. Thus one can use this fact to allow both `object`
60+
and `Object`, for example. Note that in "typescript" mode, this is the default
61+
behavior.
62+
5863
See also the documentation on `settings.jsdoc.preferredTypes` which impacts
5964
the behavior of `check-types`.
6065

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,11 @@ how the keys of `preferredTypes` may have `<>` or `.<>` (or just `.`)
423423
appended and its bearing on whether types are checked as parents/children
424424
only (e.g., to match `Array` if the type is `Array` vs. `Array.<string>`).
425425

426+
Note that if a value is present both as a key and as a value, neither the
427+
key nor the value will be reported. Thus in `check-types`, this fact can
428+
be used to allow both `object` and `Object` if one has a `preferredTypes`
429+
key `object: 'Object'` and `Object: 'object'`.
430+
426431
<a name="eslint-plugin-jsdoc-advanced"></a>
427432
## Advanced
428433

@@ -3382,6 +3387,11 @@ RegExp
33823387
`string[]` specifically as distinct from say `number[]`, but you can
33833388
target both with `[]` or the child types `number` or `string`.
33843389

3390+
If a value is present both as a key and as a value, neither the key nor the
3391+
value will be reported. Thus one can use this fact to allow both `object`
3392+
and `Object`, for example. Note that in "typescript" mode, this is the default
3393+
behavior.
3394+
33853395
See also the documentation on `settings.jsdoc.preferredTypes` which impacts
33863396
the behavior of `check-types`.
33873397

@@ -3991,6 +4001,18 @@ function quux (foo) {
39914001
}
39924002
// Settings: {"jsdoc":{"preferredTypes":{"Array.<>":"[]","Array<>":"[]"}}}
39934003
// Message: Invalid JSDoc @param "foo" type "Array"; prefer: "[]".
4004+
4005+
/**
4006+
* @typedef {object} foo
4007+
*/
4008+
function a () {}
4009+
4010+
/**
4011+
* @typedef {Object} foo
4012+
*/
4013+
function b () {}
4014+
// Settings: {"jsdoc":{"mode":"typescript","preferredTypes":{"object":"Object"}}}
4015+
// Message: Invalid JSDoc @typedef "foo" type "object"; prefer: "Object".
39944016
````
39954017

39964018
The following patterns are not considered problems:
@@ -4224,6 +4246,37 @@ function quux () {}
42244246
/** @typedef {object<string, string>} foo */
42254247
// Settings: {"jsdoc":{"preferredTypes":{"object<>":"Object<>"}}}
42264248
// Options: [{"exemptTagContexts":[{"tag":"typedef","types":["object<string, string>"]}]}]
4249+
4250+
/**
4251+
* @typedef {object} foo
4252+
*/
4253+
4254+
/**
4255+
* @typedef {Object} foo
4256+
*/
4257+
// Settings: {"jsdoc":{"preferredTypes":{"object":"Object","Object":"object"}}}
4258+
4259+
/**
4260+
* @typedef {object} foo
4261+
*/
4262+
function a () {}
4263+
4264+
/**
4265+
* @typedef {Object} foo
4266+
*/
4267+
function b () {}
4268+
// Settings: {"jsdoc":{"preferredTypes":{"object":"Object","Object":"object"}}}
4269+
4270+
/**
4271+
* @typedef {object} foo
4272+
*/
4273+
function a () {}
4274+
4275+
/**
4276+
* @typedef {Object} foo
4277+
*/
4278+
function b () {}
4279+
// Settings: {"jsdoc":{"mode":"typescript"}}
42274280
````
42284281

42294282

src/rules/checkTypes.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ export default iterateJsdoc(({
115115
});
116116
}
117117
}
118-
const directNameMatch = preferredTypes?.[nodeName] !== undefined;
118+
const directNameMatch = preferredTypes?.[nodeName] !== undefined &&
119+
!Object.values(preferredTypes).includes(nodeName);
119120
const unifiedSyntaxParentMatch = parentType && directNameMatch && unifyParentAndChildTypeChecks;
120121
isGenericMatch = isGenericMatch || unifiedSyntaxParentMatch;
121122

@@ -171,6 +172,9 @@ export default iterateJsdoc(({
171172
}
172173
} else if (!noDefaults && type === 'NAME') {
173174
for (const strictNativeType of strictNativeTypes) {
175+
if (strictNativeType === 'object' && mode === 'typescript') {
176+
continue;
177+
}
174178
if (strictNativeType.toLowerCase() === nodeName.toLowerCase() &&
175179
strictNativeType !== nodeName &&
176180

test/rules/assertions/checkTypes.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1924,6 +1924,44 @@ export default {
19241924
},
19251925
},
19261926
},
1927+
{
1928+
code: `
1929+
/**
1930+
* @typedef {object} foo
1931+
*/
1932+
function a () {}
1933+
1934+
/**
1935+
* @typedef {Object} foo
1936+
*/
1937+
function b () {}
1938+
`,
1939+
errors: [
1940+
{
1941+
line: 3,
1942+
message: 'Invalid JSDoc @typedef "foo" type "object"; prefer: "Object".',
1943+
},
1944+
],
1945+
output: `
1946+
/**
1947+
* @typedef {Object} foo
1948+
*/
1949+
function a () {}
1950+
1951+
/**
1952+
* @typedef {Object} foo
1953+
*/
1954+
function b () {}
1955+
`,
1956+
settings: {
1957+
jsdoc: {
1958+
mode: 'typescript',
1959+
preferredTypes: {
1960+
object: 'Object',
1961+
},
1962+
},
1963+
},
1964+
},
19271965
],
19281966
valid: [
19291967
{
@@ -2387,5 +2425,63 @@ export default {
23872425
},
23882426
},
23892427
},
2428+
{
2429+
code: `
2430+
/**
2431+
* @typedef {object} foo
2432+
*/
2433+
2434+
/**
2435+
* @typedef {Object} foo
2436+
*/
2437+
`,
2438+
settings: {
2439+
jsdoc: {
2440+
preferredTypes: {
2441+
object: 'Object',
2442+
Object: 'object',
2443+
},
2444+
},
2445+
},
2446+
},
2447+
{
2448+
code: `
2449+
/**
2450+
* @typedef {object} foo
2451+
*/
2452+
function a () {}
2453+
2454+
/**
2455+
* @typedef {Object} foo
2456+
*/
2457+
function b () {}
2458+
`,
2459+
settings: {
2460+
jsdoc: {
2461+
preferredTypes: {
2462+
object: 'Object',
2463+
Object: 'object',
2464+
},
2465+
},
2466+
},
2467+
},
2468+
{
2469+
code: `
2470+
/**
2471+
* @typedef {object} foo
2472+
*/
2473+
function a () {}
2474+
2475+
/**
2476+
* @typedef {Object} foo
2477+
*/
2478+
function b () {}
2479+
`,
2480+
settings: {
2481+
jsdoc: {
2482+
mode: 'typescript',
2483+
},
2484+
},
2485+
},
23902486
],
23912487
};

0 commit comments

Comments
 (0)