Skip to content

fix: for "typescript" mode, require object shorthand or index signatures #1012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions .README/rules/check-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ object.

However, `Object.create(null)` objects are not `instanceof Object`, however, so
in the case of such a plain object we lower-case to indicate possible support
for these objects. Also, nowadays, TypeScript also discourages use of `Object`
for these objects. Also, nowadays, TypeScript also [discourages](https://www.typescriptlang.org/docs/handbook/declaration-files/do-s-and-don-ts.html#:~:text=%E2%9D%8C%20Don't%20ever%20use,used%20appropriately%20in%20JavaScript%20code.)
use of `Object`
as a lone type. However, one additional complexity is that TypeScript allows and
actually [currently requires](https://github.com/microsoft/TypeScript/issues/20555)
`Object` (with the initial upper-case) if used in the syntax
Expand All @@ -112,8 +113,14 @@ adhere to that which [JSDoc documents](https://jsdoc.app/tags-type.html).

So, for optimal compatibility with TypeScript (especially since TypeScript
tools can be used on plain JavaScript with JSDoc), we are now requiring this
TypeScript approach by default (if you set `object` type `preferredTypes` in
TypeScript mode, the defaults will not apply).
TypeScript approach by default in non-"typescript" mode (if you set
`object` type `preferredTypes` in TypeScript mode, the defaults will
not apply).

However, for "typescript" mode, a still better choice exists—using index signatures such as `{[key: string]: string}` or using a more precise
shorthand object syntax (e.g., `{a: string, b: number}`). This is superior
for TypeScript because the likes of `Object<string, number>` is not useable
in native TypeScript syntax, even if it is allowed within JSDoc.

Basically, for primitives, we want to define the type as a primitive, because
that's what we use in 99.9% of cases. For everything else, we use the type
Expand Down
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5442,7 +5442,8 @@ object.

However, `Object.create(null)` objects are not `instanceof Object`, however, so
in the case of such a plain object we lower-case to indicate possible support
for these objects. Also, nowadays, TypeScript also discourages use of `Object`
for these objects. Also, nowadays, TypeScript also [discourages](https://www.typescriptlang.org/docs/handbook/declaration-files/do-s-and-don-ts.html#:~:text=%E2%9D%8C%20Don't%20ever%20use,used%20appropriately%20in%20JavaScript%20code.)
use of `Object`
as a lone type. However, one additional complexity is that TypeScript allows and
actually [currently requires](https://github.com/microsoft/TypeScript/issues/20555)
`Object` (with the initial upper-case) if used in the syntax
Expand All @@ -5451,8 +5452,14 @@ adhere to that which [JSDoc documents](https://jsdoc.app/tags-type.html).

So, for optimal compatibility with TypeScript (especially since TypeScript
tools can be used on plain JavaScript with JSDoc), we are now requiring this
TypeScript approach by default (if you set `object` type `preferredTypes` in
TypeScript mode, the defaults will not apply).
TypeScript approach by default in non-"typescript" mode (if you set
`object` type `preferredTypes` in TypeScript mode, the defaults will
not apply).

However, for "typescript" mode, a still better choice exists—using index signatures such as `{[key: string]: string}` or using a more precise
shorthand object syntax (e.g., `{a: string, b: number}`). This is superior
for TypeScript because the likes of `Object<string, number>` is not useable
in native TypeScript syntax, even if it is allowed within JSDoc.

Basically, for primitives, we want to define the type as a primitive, because
that's what we use in 99.9% of cases. For everything else, we use the type
Expand Down Expand Up @@ -6132,7 +6139,7 @@ function quux (foo) {

}
// Settings: {"jsdoc":{"mode":"typescript"}}
// Message: Invalid JSDoc @param "foo" type "object"; prefer: "Object<>".
// Message: Use object shorthand or index signatures instead of `object`, e.g., `{[key: string]: string}`

/**
*
Expand Down Expand Up @@ -6414,7 +6421,7 @@ function b () {}
function a () {}

/**
* @typedef {Object<string>} foo
* @typedef {{[key: string]: number}} foo
*/
function b () {}
// Settings: {"jsdoc":{"mode":"typescript"}}
Expand Down Expand Up @@ -6443,7 +6450,7 @@ function quux (foo) {
}

/**
* @param {Object<string>} foo
* @param {{[key: string]: number}} foo
*/
function quux (foo) {

Expand Down
39 changes: 33 additions & 6 deletions src/rules/checkTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ const adjustNames = (type, preferred, isGenericMatch, typeNodeName, node, parent
}
};

const getMessage = (upperCase) => {
return 'Use object shorthand or index signatures instead of ' +
'`' + (upperCase ? 'O' : 'o') + 'bject`, e.g., `{[key: string]: string}`';
};

export default iterateJsdoc(({
jsdocNode,
sourceCode,
Expand All @@ -97,13 +102,35 @@ export default iterateJsdoc(({
'Object.<>' in preferredTypesOriginal ||
'object<>' in preferredTypesOriginal);

const preferredTypes = {
...injectObjectPreferredTypes ? {
const info = {
message: getMessage(),
replacement: false,
};

const infoUC = {
message: getMessage(true),
replacement: false,
};

const typeToInject = mode === 'typescript' ?
{
Object: 'object',
'object.<>': info,
'Object.<>': infoUC,
'object<>': info,
'Object<>': infoUC,
} :
{
Object: 'object',
'object.<>': 'Object<>',
'Object.<>': 'Object<>',
'object<>': 'Object<>',
} : {},
};

const preferredTypes = {
...injectObjectPreferredTypes ?
typeToInject :
{},
...preferredTypesOriginal,
};

Expand Down Expand Up @@ -364,7 +391,7 @@ export default iterateJsdoc(({
for (const [
badType,
preferredType = '',
message,
msg,
] of invalidTypes) {
const tagValue = jsdocTag.name ? ` "${jsdocTag.name}"` : '';
if (exemptTagContexts.some(({
Expand All @@ -378,13 +405,13 @@ export default iterateJsdoc(({
}

report(
message ||
msg ||
`Invalid JSDoc @${tagName}${tagValue} type "${badType}"` +
(preferredType ? '; ' : '.') +
(preferredType ? `prefer: ${JSON.stringify(preferredType)}.` : ''),
preferredType ? fix : null,
jsdocTag,
message ? {
msg ? {
tagName,
tagValue,
} : null,
Expand Down
14 changes: 3 additions & 11 deletions test/rules/assertions/checkTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2339,17 +2339,9 @@ export default {
errors: [
{
line: 3,
message: 'Invalid JSDoc @param "foo" type "object"; prefer: "Object<>".',
message: 'Use object shorthand or index signatures instead of `object`, e.g., `{[key: string]: string}`',
},
],
output: `
/**
* @param {Object<string>} foo
*/
function quux (foo) {

}
`,
settings: {
jsdoc: {
mode: 'typescript',
Expand Down Expand Up @@ -2944,7 +2936,7 @@ export default {
function a () {}

/**
* @typedef {Object<string>} foo
* @typedef {{[key: string]: number}} foo
*/
function b () {}
`,
Expand Down Expand Up @@ -3020,7 +3012,7 @@ export default {
{
code: `
/**
* @param {Object<string>} foo
* @param {{[key: string]: number}} foo
*/
function quux (foo) {

Expand Down