Skip to content

Commit 42fd03b

Browse files
committed
docs(no-restricted-syntax): example to limit types on @type; fixes #939
1 parent d6e2355 commit 42fd03b

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9680,6 +9680,18 @@ const MyComponent = ({ children }) => {
96809680
*/
96819681
// "jsdoc/no-restricted-syntax": ["error"|"warn", {"contexts":[{"comment":"JsdocBlock:not(*:has(JsdocTag[tag=see]))","context":"any","message":"@see required on each block"}]}]
96829682
// Message: @see required on each block
9683+
9684+
/**
9685+
* @type {{a: string}}
9686+
*/
9687+
// "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"}]}]
9688+
// Message: @type should be limited to numeric or string literals and names
9689+
9690+
/**
9691+
* @type {abc}
9692+
*/
9693+
// "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"}]}]
9694+
// Message: @type names should only be recognized primitive types or literals
96839695
````
96849696

96859697
The following patterns are not considered problems:
@@ -9750,6 +9762,16 @@ function foo(): string;
97509762
* @param sth Param text followed by newline
97519763
*/
97529764
// "jsdoc/no-restricted-syntax": ["error"|"warn", {"contexts":[{"comment":"JsdocBlock[descriptionStartLine=0][hasPreterminalTagDescription=1]","context":"any","message":"Requiring descriptive text on 0th line but no preterminal description"}]}]
9765+
9766+
/**
9767+
* @type {123}
9768+
*/
9769+
// "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"}]}]
9770+
9771+
/**
9772+
* @type {boolean}
9773+
*/
9774+
// "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"}]}]
97539775
````
97549776

97559777

test/rules/assertions/noRestrictedSyntax.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,64 @@ export default {
465465
},
466466
],
467467
},
468+
{
469+
code: `
470+
/**
471+
* @type {{a: string}}
472+
*/
473+
`,
474+
errors: [
475+
{
476+
line: 2,
477+
message: '@type should be limited to numeric or string literals and names',
478+
},
479+
],
480+
options: [
481+
{
482+
contexts: [
483+
{
484+
comment: 'JsdocBlock:has(JsdocTag[tag=type][parsedType.type!=JsdocTypeStringValue][parsedType.type!=JsdocTypeNumber][parsedType.type!=JsdocTypeName])',
485+
context: 'any',
486+
message: '@type should be limited to numeric or string literals and names',
487+
},
488+
{
489+
comment: 'JsdocBlock:has(JsdocTag[tag=type][parsedType.type=JsdocTypeName]:not(*[parsedType.value=/^(true|false|null|undefined|boolean|number|string)$/]))',
490+
context: 'any',
491+
message: '@type names should only be recognized primitive types or literals',
492+
},
493+
],
494+
},
495+
],
496+
},
497+
{
498+
code: `
499+
/**
500+
* @type {abc}
501+
*/
502+
`,
503+
errors: [
504+
{
505+
line: 2,
506+
message: '@type names should only be recognized primitive types or literals',
507+
},
508+
],
509+
options: [
510+
{
511+
contexts: [
512+
{
513+
comment: 'JsdocBlock:has(JsdocTag[tag=type][parsedType.type!=JsdocTypeStringValue][parsedType.type!=JsdocTypeNumber][parsedType.type!=JsdocTypeName])',
514+
context: 'any',
515+
message: '@type should be limited to numeric or string literals and names',
516+
},
517+
{
518+
comment: 'JsdocBlock:has(JsdocTag[tag=type][parsedType.type=JsdocTypeName]:not(*[parsedType.value=/^(true|false|null|undefined|boolean|number|string)$/]))',
519+
context: 'any',
520+
message: '@type names should only be recognized primitive types or literals',
521+
},
522+
],
523+
},
524+
],
525+
},
468526
],
469527
valid: [
470528
{
@@ -675,5 +733,51 @@ export default {
675733
},
676734
],
677735
},
736+
{
737+
code: `
738+
/**
739+
* @type {123}
740+
*/
741+
`,
742+
options: [
743+
{
744+
contexts: [
745+
{
746+
comment: 'JsdocBlock:has(JsdocTag[tag=type][parsedType.type!=JsdocTypeStringValue][parsedType.type!=JsdocTypeNumber][parsedType.type!=JsdocTypeName])',
747+
context: 'any',
748+
message: '@type should be limited to numeric or string literals and names',
749+
},
750+
{
751+
comment: 'JsdocBlock:has(JsdocTag[tag=type][parsedType.type=JsdocTypeName]:not(*[parsedType.value=/^(true|false|null|undefined|boolean|number|string)$/]))',
752+
context: 'any',
753+
message: '@type names should only be recognized primitive types or literals',
754+
},
755+
],
756+
},
757+
],
758+
},
759+
{
760+
code: `
761+
/**
762+
* @type {boolean}
763+
*/
764+
`,
765+
options: [
766+
{
767+
contexts: [
768+
{
769+
comment: 'JsdocBlock:has(JsdocTag[tag=type][parsedType.type!=JsdocTypeStringValue][parsedType.type!=JsdocTypeNumber][parsedType.type!=JsdocTypeName])',
770+
context: 'any',
771+
message: '@type should be limited to numeric or string literals and names',
772+
},
773+
{
774+
comment: 'JsdocBlock:has(JsdocTag[tag=type][parsedType.type=JsdocTypeName]:not(*[parsedType.value=/^(true|false|null|undefined|boolean|number|string)$/]))',
775+
context: 'any',
776+
message: '@type names should only be recognized primitive types or literals',
777+
},
778+
],
779+
},
780+
],
781+
},
678782
],
679783
};

0 commit comments

Comments
 (0)