Skip to content

Commit bffa6bf

Browse files
committed
docs(no-restricted-syntax): demo erring without returns on contexts with non-void return-type type annotations; closes #517
1 parent 4042eab commit bffa6bf

File tree

2 files changed

+244
-0
lines changed

2 files changed

+244
-0
lines changed

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9692,6 +9692,36 @@ const MyComponent = ({ children }) => {
96929692
*/
96939693
// "jsdoc/no-restricted-syntax": ["error"|"warn", {"contexts":[{"comment":"JsdocBlock:has(JsdocTag[tag=type][parsedType.type!=JsdocTypeStringValue][parsedType.type!=JsdocTypeNumber][parsedType.type!=JsdocTypeName])","context":"any","message":"@type should be limited to numeric or string literals and names"},{"comment":"JsdocBlock:has(JsdocTag[tag=type][parsedType.type=JsdocTypeName]:not(*[parsedType.value=/^(true|false|null|undefined|boolean|number|string)$/]))","context":"any","message":"@type names should only be recognized primitive types or literals"}]}]
96949694
// Message: @type names should only be recognized primitive types or literals
9695+
9696+
/**
9697+
*
9698+
*/
9699+
function test(): string { }
9700+
// "jsdoc/no-restricted-syntax": ["error"|"warn", {"contexts":[{"comment":"JsdocBlock:not(*:has(JsdocTag[tag=/returns/]))","context":"FunctionDeclaration[returnType.typeAnnotation.type!=/TSVoidKeyword|TSUndefinedKeyword/]","message":"Functions with non-void return types must have a @returns tag"}]}]
9701+
// Message: Functions with non-void return types must have a @returns tag
9702+
9703+
/**
9704+
*
9705+
*/
9706+
let test = (): string => { };
9707+
// "jsdoc/no-restricted-syntax": ["error"|"warn", {"contexts":[{"comment":"JsdocBlock:not(*:has(JsdocTag[tag=/returns/]))","context":"ArrowFunctionExpression[returnType.typeAnnotation.type!=/TSVoidKeyword|TSUndefinedKeyword/]","message":"Functions with non-void return types must have a @returns tag"}]}]
9708+
// Message: Functions with non-void return types must have a @returns tag
9709+
9710+
/**
9711+
* @returns
9712+
*/
9713+
let test: () => string;
9714+
// "jsdoc/no-restricted-syntax": ["error"|"warn", {"contexts":[{"comment":"JsdocBlock:not(*:has(JsdocTag[tag=/returns/]:has(JsdocDescriptionLine)))","context":"VariableDeclaration:has(*[typeAnnotation.typeAnnotation.type=/TSFunctionType/][typeAnnotation.typeAnnotation.returnType.typeAnnotation.type!=/TSVoidKeyword|TSUndefinedKeyword/])","message":"FunctionType's with non-void return types must have a @returns tag with a description"}]}]
9715+
// Message: FunctionType's with non-void return types must have a @returns tag with a description
9716+
9717+
/**
9718+
*
9719+
*/
9720+
class Test {
9721+
abstract Test(): string;
9722+
}
9723+
// "jsdoc/no-restricted-syntax": ["error"|"warn", {"contexts":[{"comment":"JsdocBlock:not(*:has(JsdocTag[tag=/returns/]))","context":"TSEmptyBodyFunctionExpression[returnType.typeAnnotation.type!=/TSVoidKeyword|TSUndefinedKeyword/]","message":"methods with non-void return types must have a @returns tag"}]}]
9724+
// Message: methods with non-void return types must have a @returns tag
96959725
````
96969726

96979727
The following patterns are not considered problems:
@@ -9772,6 +9802,32 @@ function foo(): string;
97729802
* @type {boolean}
97739803
*/
97749804
// "jsdoc/no-restricted-syntax": ["error"|"warn", {"contexts":[{"comment":"JsdocBlock:has(JsdocTag[tag=type][parsedType.type!=JsdocTypeStringValue][parsedType.type!=JsdocTypeNumber][parsedType.type!=JsdocTypeName])","context":"any","message":"@type should be limited to numeric or string literals and names"},{"comment":"JsdocBlock:has(JsdocTag[tag=type][parsedType.type=JsdocTypeName]:not(*[parsedType.value=/^(true|false|null|undefined|boolean|number|string)$/]))","context":"any","message":"@type names should only be recognized primitive types or literals"}]}]
9805+
9806+
/**
9807+
*
9808+
*/
9809+
function test(): void { }
9810+
// "jsdoc/no-restricted-syntax": ["error"|"warn", {"contexts":[{"comment":"JsdocBlock:not(*:has(JsdocTag[tag=/returns/]))","context":"FunctionDeclaration[returnType.typeAnnotation.type!=/TSVoidKeyword|TSUndefinedKeyword/]","message":"Functions with return types must have a @returns tag"}]}]
9811+
9812+
/**
9813+
*
9814+
*/
9815+
let test = (): undefined => { };
9816+
// "jsdoc/no-restricted-syntax": ["error"|"warn", {"contexts":[{"comment":"JsdocBlock:not(*:has(JsdocTag[tag=/returns/]))","context":"ArrowFunctionExpression[returnType.typeAnnotation.type!=/TSVoidKeyword|TSUndefinedKeyword/]","message":"Functions with non-void return types must have a @returns tag"}]}]
9817+
9818+
/**
9819+
* @returns A description
9820+
*/
9821+
let test: () => string;
9822+
// "jsdoc/no-restricted-syntax": ["error"|"warn", {"contexts":[{"comment":"JsdocBlock:not(*:has(JsdocTag[tag=/returns/]:has(JsdocDescriptionLine)))","context":"VariableDeclaration:has(*[typeAnnotation.typeAnnotation.type=/TSFunctionType/])","message":"FunctionType's with non-void return types must have a @returns tag"}]}]
9823+
9824+
/**
9825+
*
9826+
*/
9827+
class Test {
9828+
abstract Test(): void;
9829+
}
9830+
// "jsdoc/no-restricted-syntax": ["error"|"warn", {"contexts":[{"comment":"JsdocBlock:not(*:has(JsdocTag[tag=/returns/]))","context":"TSEmptyBodyFunctionExpression[returnType.typeAnnotation.type!=/TSVoidKeyword|TSUndefinedKeyword/]","message":"methods with non-void return types must have a @returns tag"}]}]
97759831
````
97769832

97779833

test/rules/assertions/noRestrictedSyntax.js

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,112 @@ export default {
523523
},
524524
],
525525
},
526+
{
527+
code: `
528+
/**
529+
*
530+
*/
531+
function test(): string { }
532+
`,
533+
errors: [
534+
{
535+
line: 2,
536+
message: 'Functions with non-void return types must have a @returns tag',
537+
},
538+
],
539+
options: [
540+
{
541+
contexts: [
542+
{
543+
comment: 'JsdocBlock:not(*:has(JsdocTag[tag=/returns/]))',
544+
context: 'FunctionDeclaration[returnType.typeAnnotation.type!=/TSVoidKeyword|TSUndefinedKeyword/]',
545+
message: 'Functions with non-void return types must have a @returns tag',
546+
},
547+
],
548+
},
549+
],
550+
parser: require.resolve('@typescript-eslint/parser'),
551+
},
552+
{
553+
code: `
554+
/**
555+
*
556+
*/
557+
let test = (): string => { };
558+
`,
559+
errors: [
560+
{
561+
line: 2,
562+
message: 'Functions with non-void return types must have a @returns tag',
563+
},
564+
],
565+
options: [
566+
{
567+
contexts: [
568+
{
569+
comment: 'JsdocBlock:not(*:has(JsdocTag[tag=/returns/]))',
570+
context: 'ArrowFunctionExpression[returnType.typeAnnotation.type!=/TSVoidKeyword|TSUndefinedKeyword/]',
571+
message: 'Functions with non-void return types must have a @returns tag',
572+
},
573+
],
574+
},
575+
],
576+
parser: require.resolve('@typescript-eslint/parser'),
577+
},
578+
{
579+
code: `
580+
/**
581+
* @returns
582+
*/
583+
let test: () => string;
584+
`,
585+
errors: [
586+
{
587+
line: 2,
588+
message: 'FunctionType\'s with non-void return types must have a @returns tag with a description',
589+
},
590+
],
591+
options: [
592+
{
593+
contexts: [
594+
{
595+
comment: 'JsdocBlock:not(*:has(JsdocTag[tag=/returns/]:has(JsdocDescriptionLine)))',
596+
context: 'VariableDeclaration:has(*[typeAnnotation.typeAnnotation.type=/TSFunctionType/][typeAnnotation.typeAnnotation.returnType.typeAnnotation.type!=/TSVoidKeyword|TSUndefinedKeyword/])',
597+
message: 'FunctionType\'s with non-void return types must have a @returns tag with a description',
598+
},
599+
],
600+
},
601+
],
602+
parser: require.resolve('@typescript-eslint/parser'),
603+
},
604+
{
605+
code: `
606+
/**
607+
*
608+
*/
609+
class Test {
610+
abstract Test(): string;
611+
}
612+
`,
613+
errors: [
614+
{
615+
line: 2,
616+
message: 'methods with non-void return types must have a @returns tag',
617+
},
618+
],
619+
options: [
620+
{
621+
contexts: [
622+
{
623+
comment: 'JsdocBlock:not(*:has(JsdocTag[tag=/returns/]))',
624+
context: 'TSEmptyBodyFunctionExpression[returnType.typeAnnotation.type!=/TSVoidKeyword|TSUndefinedKeyword/]',
625+
message: 'methods with non-void return types must have a @returns tag',
626+
},
627+
],
628+
},
629+
],
630+
parser: require.resolve('@typescript-eslint/parser'),
631+
},
526632
],
527633
valid: [
528634
{
@@ -779,5 +885,87 @@ export default {
779885
},
780886
],
781887
},
888+
{
889+
code: `
890+
/**
891+
*
892+
*/
893+
function test(): void { }
894+
`,
895+
options: [
896+
{
897+
contexts: [
898+
{
899+
comment: 'JsdocBlock:not(*:has(JsdocTag[tag=/returns/]))',
900+
context: 'FunctionDeclaration[returnType.typeAnnotation.type!=/TSVoidKeyword|TSUndefinedKeyword/]',
901+
message: 'Functions with return types must have a @returns tag',
902+
},
903+
],
904+
},
905+
],
906+
parser: require.resolve('@typescript-eslint/parser'),
907+
},
908+
{
909+
code: `
910+
/**
911+
*
912+
*/
913+
let test = (): undefined => { };
914+
`,
915+
options: [
916+
{
917+
contexts: [
918+
{
919+
comment: 'JsdocBlock:not(*:has(JsdocTag[tag=/returns/]))',
920+
context: 'ArrowFunctionExpression[returnType.typeAnnotation.type!=/TSVoidKeyword|TSUndefinedKeyword/]',
921+
message: 'Functions with non-void return types must have a @returns tag',
922+
},
923+
],
924+
},
925+
],
926+
parser: require.resolve('@typescript-eslint/parser'),
927+
},
928+
{
929+
code: `
930+
/**
931+
* @returns A description
932+
*/
933+
let test: () => string;
934+
`,
935+
options: [
936+
{
937+
contexts: [
938+
{
939+
comment: 'JsdocBlock:not(*:has(JsdocTag[tag=/returns/]:has(JsdocDescriptionLine)))',
940+
context: 'VariableDeclaration:has(*[typeAnnotation.typeAnnotation.type=/TSFunctionType/])',
941+
message: 'FunctionType\'s with non-void return types must have a @returns tag',
942+
},
943+
],
944+
},
945+
],
946+
parser: require.resolve('@typescript-eslint/parser'),
947+
},
948+
{
949+
code: `
950+
/**
951+
*
952+
*/
953+
class Test {
954+
abstract Test(): void;
955+
}
956+
`,
957+
options: [
958+
{
959+
contexts: [
960+
{
961+
comment: 'JsdocBlock:not(*:has(JsdocTag[tag=/returns/]))',
962+
context: 'TSEmptyBodyFunctionExpression[returnType.typeAnnotation.type!=/TSVoidKeyword|TSUndefinedKeyword/]',
963+
message: 'methods with non-void return types must have a @returns tag',
964+
},
965+
],
966+
},
967+
],
968+
parser: require.resolve('@typescript-eslint/parser'),
969+
},
782970
],
783971
};

0 commit comments

Comments
 (0)