diff --git a/README.md b/README.md index a2f1964df..7dd69b2c6 100644 --- a/README.md +++ b/README.md @@ -8193,6 +8193,13 @@ function quux () { // "jsdoc/no-missing-syntax": ["error"|"warn", {"contexts":[{"comment":"JsdocBlock:has(JsdocTag[name=/opt_/])","context":"any","message":"Require names matching `/^opt_/i`."}]}] // Message: Require names matching `/^opt_/i`. +/** + * @param ab + * @param cd + */ +// "jsdoc/no-missing-syntax": ["error"|"warn", {"contexts":[{"comment":"JsdocBlock:has(JsdocTag[name=/opt_/])","message":"Require names matching `/^opt_/i`."}]}] +// Message: Require names matching `/^opt_/i`. + /** * @param ab * @param cd @@ -8247,6 +8254,13 @@ function baz () { */ function quux () {} // "jsdoc/no-missing-syntax": ["error"|"warn", {"contexts":[{"comment":"JsdocBlock:has(JsdocTag[name=/opt_/])","context":"any","message":"Require names matching `/^opt_/i`."}]}] + +/** + * @param opt_a + * @param opt_b + */ +function quux () {} +// "jsdoc/no-missing-syntax": ["error"|"warn", {"contexts":[{"comment":"JsdocBlock:has(JsdocTag[name=/opt_/])","message":"Require names matching `/^opt_/i`."}]}] ```` @@ -8550,6 +8564,14 @@ function a () {} // "jsdoc/no-restricted-syntax": ["error"|"warn", {"contexts":[{"comment":"JsdocBlock:has(JsdocTag[name=/opt_/])","context":"any","message":"Only allowing names not matching `/^opt_/i`."}]}] // Message: Only allowing names not matching `/^opt_/i`. +/** + * @param opt_a + * @param opt_b + */ +function a () {} +// "jsdoc/no-restricted-syntax": ["error"|"warn", {"contexts":[{"comment":"JsdocBlock:has(JsdocTag[name=/opt_/])","message":"Only allowing names not matching `/^opt_/i`."}]}] +// Message: Only allowing names not matching `/^opt_/i`. + /** * @param opt_a * @param opt_b @@ -8597,6 +8619,12 @@ function a () {} * @param cd */ // "jsdoc/no-restricted-syntax": ["error"|"warn", {"contexts":[{"comment":"JsdocBlock:has(JsdocTag[name=/opt_/])","context":"any","message":"Only allowing names not matching `/^opt_/i`."}]}] + +/** + * @param ab + * @param cd + */ +// "jsdoc/no-restricted-syntax": ["error"|"warn", {"contexts":[{"comment":"JsdocBlock:has(JsdocTag[name=/opt_/])","message":"Only allowing names not matching `/^opt_/i`."}]}] ```` diff --git a/src/iterateJsdoc.js b/src/iterateJsdoc.js index 2c4507c74..a5c6445d3 100644 --- a/src/iterateJsdoc.js +++ b/src/iterateJsdoc.js @@ -933,6 +933,17 @@ export default function iterateJsdoc (iterator, ruleConfig) { let contexts; if (ruleConfig.contextDefaults || ruleConfig.contextSelected) { contexts = jsdocUtils.enforcedContexts(context, ruleConfig.contextDefaults); + + if (contexts) { + contexts = contexts.map((obj) => { + if (typeof obj === 'object' && !obj.context) { + return {...obj, context: 'any'}; + } + + return obj; + }); + } + const hasPlainAny = contexts?.includes('any'); const hasObjectAny = !hasPlainAny && contexts?.find((ctxt) => { return ctxt?.context === 'any'; diff --git a/src/rules/noMissingSyntax.js b/src/rules/noMissingSyntax.js index 5dbde0ad6..f57511517 100644 --- a/src/rules/noMissingSyntax.js +++ b/src/rules/noMissingSyntax.js @@ -49,7 +49,7 @@ export default iterateJsdoc(({ // Report when MISSING contexts.some((cntxt) => { - const contextStr = typeof cntxt === 'object' ? cntxt.context : cntxt; + const contextStr = typeof cntxt === 'object' ? cntxt.context ?? 'any' : cntxt; const comment = cntxt?.comment ?? ''; const contextKey = contextStr === 'any' ? undefined : contextStr; diff --git a/src/rules/noRestrictedSyntax.js b/src/rules/noRestrictedSyntax.js index b093601b7..43447a4bb 100644 --- a/src/rules/noRestrictedSyntax.js +++ b/src/rules/noRestrictedSyntax.js @@ -15,12 +15,12 @@ export default iterateJsdoc(({ const foundContext = contexts.find((cntxt) => { return cntxt === selector || typeof cntxt === 'object' && - (cntxt.context === 'any' || selector === cntxt.context) && + (!cntxt.context || cntxt.context === 'any' || selector === cntxt.context) && comment === cntxt.comment; }); const contextStr = typeof foundContext === 'object' ? - foundContext.context : + foundContext.context ?? 'any' : foundContext; const message = foundContext?.message ?? 'Syntax is restricted: {{context}}.'; diff --git a/test/rules/assertions/noMissingSyntax.js b/test/rules/assertions/noMissingSyntax.js index 6e3a6e6a5..928832ce0 100644 --- a/test/rules/assertions/noMissingSyntax.js +++ b/test/rules/assertions/noMissingSyntax.js @@ -136,6 +136,26 @@ export default { ], }], }, + { + code: ` + /** + * @param ab + * @param cd + */ + `, + errors: [{ + line: 1, + message: 'Require names matching `/^opt_/i`.', + }], + options: [{ + contexts: [ + { + comment: 'JsdocBlock:has(JsdocTag[name=/opt_/])', + message: 'Require names matching `/^opt_/i`.', + }, + ], + }], + }, { code: ` /** @@ -246,5 +266,22 @@ export default { ], }], }, + { + code: ` + /** + * @param opt_a + * @param opt_b + */ + function quux () {} + `, + options: [{ + contexts: [ + { + comment: 'JsdocBlock:has(JsdocTag[name=/opt_/])', + message: 'Require names matching `/^opt_/i`.', + }, + ], + }], + }, ], }; diff --git a/test/rules/assertions/noRestrictedSyntax.js b/test/rules/assertions/noRestrictedSyntax.js index 3d613215e..f11148049 100644 --- a/test/rules/assertions/noRestrictedSyntax.js +++ b/test/rules/assertions/noRestrictedSyntax.js @@ -159,6 +159,27 @@ export default { ], }], }, + { + code: ` + /** + * @param opt_a + * @param opt_b + */ + function a () {} + `, + errors: [{ + line: 2, + message: 'Only allowing names not matching `/^opt_/i`.', + }], + options: [{ + contexts: [ + { + comment: 'JsdocBlock:has(JsdocTag[name=/opt_/])', + message: 'Only allowing names not matching `/^opt_/i`.', + }, + ], + }], + }, { code: ` /** @@ -277,5 +298,21 @@ export default { ], }], }, + { + code: ` + /** + * @param ab + * @param cd + */ + `, + options: [{ + contexts: [ + { + comment: 'JsdocBlock:has(JsdocTag[name=/opt_/])', + message: 'Only allowing names not matching `/^opt_/i`.', + }, + ], + }], + }, ], };