Skip to content

Commit 5603da9

Browse files
committed
test: add require-jsdoc test
1 parent 9ccb27f commit 5603da9

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

docs/rules/require-jsdoc.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2018,6 +2018,29 @@ export function arrayMap<Target, Source extends AnyArrayType>(data: Source, call
20182018
}
20192019
// "jsdoc/require-jsdoc": ["error"|"warn", {"skipInterveningOverloadedDeclarations":true}]
20202020

2021+
/**
2022+
* Array map function with overload for NonEmptyArray
2023+
* @example
2024+
* const data = [{value: 'value'}] as const;
2025+
* const result1: NonEmptyReadonlyArray<'value'> = arrayMap(data, (value) => value.value); // pick type from data
2026+
* const result2: NonEmptyReadonlyArray<'value'> = arrayMap<'value', typeof data>(data, (value) => value.value); // enforce output type
2027+
* @template Target - The type of the array to map to
2028+
* @template Source - The type of the array to map from
2029+
* @param {Source} data - The array to map
2030+
* @param {MapCallback<Target, Source>} callback - Callback function to map data from the array
2031+
* @returns {AnyArrayType<Target>} Mapped array
2032+
* @since v0.2.0
2033+
*/
2034+
export function arrayMap<Target, Source extends NonEmptyArray<unknown> | NonEmptyReadonlyArray<unknown>>(
2035+
data: Source,
2036+
callback: MapCallback<Target, Source>,
2037+
): NonEmptyArray<Target>;
2038+
export function arrayMap<Target, Source extends Array<unknown>>(data: Source, callback: MapCallback<Target, Source>): Array<Target>;
2039+
export function arrayMap<Target, Source extends AnyArrayType>(data: Source, callback: MapCallback<Target, Source>): AnyArrayType<Target> {
2040+
return data.map(callback);
2041+
}
2042+
// "jsdoc/require-jsdoc": ["error"|"warn", {"contexts":["TSDeclareFunction"],"exemptOverloadedImplementations":false,"skipInterveningOverloadedDeclarations":true}]
2043+
20212044
export interface A {
20222045
a: string;
20232046
/**

test/rules/assertions/requireJsdoc.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6624,6 +6624,43 @@ function quux (foo) {
66246624
},
66256625
],
66266626
},
6627+
{
6628+
code: `
6629+
/**
6630+
* Array map function with overload for NonEmptyArray
6631+
* @example
6632+
* const data = [{value: 'value'}] as const;
6633+
* const result1: NonEmptyReadonlyArray<'value'> = arrayMap(data, (value) => value.value); // pick type from data
6634+
* const result2: NonEmptyReadonlyArray<'value'> = arrayMap<'value', typeof data>(data, (value) => value.value); // enforce output type
6635+
* @template Target - The type of the array to map to
6636+
* @template Source - The type of the array to map from
6637+
* @param {Source} data - The array to map
6638+
* @param {MapCallback<Target, Source>} callback - Callback function to map data from the array
6639+
* @returns {AnyArrayType<Target>} Mapped array
6640+
* @since v0.2.0
6641+
*/
6642+
export function arrayMap<Target, Source extends NonEmptyArray<unknown> | NonEmptyReadonlyArray<unknown>>(
6643+
data: Source,
6644+
callback: MapCallback<Target, Source>,
6645+
): NonEmptyArray<Target>;
6646+
export function arrayMap<Target, Source extends Array<unknown>>(data: Source, callback: MapCallback<Target, Source>): Array<Target>;
6647+
export function arrayMap<Target, Source extends AnyArrayType>(data: Source, callback: MapCallback<Target, Source>): AnyArrayType<Target> {
6648+
return data.map(callback);
6649+
}
6650+
`,
6651+
languageOptions: {
6652+
parser: typescriptEslintParser,
6653+
},
6654+
options: [
6655+
{
6656+
contexts: [
6657+
'TSDeclareFunction',
6658+
],
6659+
exemptOverloadedImplementations: false,
6660+
skipInterveningOverloadedDeclarations: true,
6661+
},
6662+
],
6663+
},
66276664
{
66286665
code: `
66296666
export interface A {

0 commit comments

Comments
 (0)