Skip to content

Commit 2ba7ffd

Browse files
SulimanAbdulrazzaqbrettz9
authored andcommitted
fix(no-missing-syntax, no-restricted-syntax): support string any contexts and contexts without comment
1 parent 57444cb commit 2ba7ffd

6 files changed

Lines changed: 107 additions & 7 deletions

File tree

‎docs/rules/no-missing-syntax.md‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ function quux () {
152152
/**
153153
* @implements {Bar|Foo}
154154
*/
155-
function quux () {
155+
const quux = function () {
156156

157-
}
157+
};
158158
// "jsdoc/no-missing-syntax": ["error"|"warn", {"contexts":["FunctionDeclaration"]}]
159159
// Message: Syntax is required: FunctionDeclaration
160160

@@ -278,5 +278,26 @@ function quux () {
278278

279279
}
280280
// "jsdoc/no-missing-syntax": ["error"|"warn", {"contexts":[{"comment":"JsdocBlock[postDelimiter=\"\"]:has(JsdocTypeUnion > JsdocTypeName[value=\"Bar\"]:nth-child(1))","context":"any"},{"comment":"JsdocBlock[postDelimiter=\"\"]:has(JsdocTypeUnion > JsdocTypeName[value=\"Foo\"]:nth-child(2))","context":"FunctionDeclaration"}]}]
281+
282+
/**
283+
* @implements {Bar|Foo}
284+
*/
285+
function quux () {
286+
287+
}
288+
// "jsdoc/no-missing-syntax": ["error"|"warn", {"contexts":["FunctionDeclaration"]}]
289+
290+
/**
291+
* @implements {Bar|Foo}
292+
*/
293+
function quux () {
294+
295+
}
296+
// "jsdoc/no-missing-syntax": ["error"|"warn", {"contexts":[{"context":"FunctionDeclaration"}]}]
297+
298+
/**
299+
* @callback Foo
300+
*/
301+
// "jsdoc/no-missing-syntax": ["error"|"warn", {"contexts":["any"]}]
281302
````
282303

‎docs/rules/no-restricted-syntax.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,12 @@ function quux () {
272272
}
273273
// "jsdoc/no-restricted-syntax": ["error"|"warn", {"contexts":[{"comment":"JsdocBlock:has(JsdocTag:has(JsdocInlineTag[format=\"plain\"]))","context":"FunctionDeclaration"}]}]
274274
// Message: Syntax is restricted: FunctionDeclaration with JsdocBlock:has(JsdocTag:has(JsdocInlineTag[format="plain"]))
275+
276+
/**
277+
* @callback Foo
278+
*/
279+
// "jsdoc/no-restricted-syntax": ["error"|"warn", {"contexts":["FunctionDeclaration","any"]}]
280+
// Message: Syntax is restricted: any
275281
````
276282

277283

‎src/iterateJsdoc.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1891,7 +1891,7 @@ const getUtils = (
18911891
utils.findContext = (contexts, comment) => {
18921892
const foundContext = contexts.find((cntxt) => {
18931893
return typeof cntxt === 'string' ?
1894-
esquery.matches(
1894+
cntxt === 'any' || esquery.matches(
18951895
/** @type {Node} */ (node),
18961896
esquery.parse(cntxt),
18971897
undefined,
@@ -2667,7 +2667,9 @@ export default function iterateJsdoc (iterator, ruleConfig) {
26672667
return iterateAllJsdocs(
26682668
iterator,
26692669
ruleConfig,
2670-
hasObjectAny ? /** @type {ContextObject[]} */ (contexts) : null,
2670+
hasObjectAny || ruleConfig.matchContext ?
2671+
/** @type {ContextObject[]} */ (contexts) :
2672+
null,
26712673
ruleConfig.matchContext,
26722674
).create(context);
26732675
}

‎src/rules/noMissingSyntax.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export default iterateJsdoc(({
6565

6666
setDefaults(state);
6767

68-
incrementSelector(state, contextStr, String(comment));
68+
incrementSelector(state, contextStr, comment ?? '');
6969
}, {
7070
contextSelected: true,
7171
exit ({

‎test/rules/assertions/noMissingSyntax.js‎

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,9 @@ export default /** @type {import('../index.js').TestCases} */ ({
189189
/**
190190
* @implements {Bar|Foo}
191191
*/
192-
function quux () {
192+
const quux = function () {
193193
194-
}
194+
};
195195
`,
196196
errors: [
197197
{
@@ -512,5 +512,55 @@ export default /** @type {import('../index.js').TestCases} */ ({
512512
},
513513
],
514514
},
515+
{
516+
code: `
517+
/**
518+
* @implements {Bar|Foo}
519+
*/
520+
function quux () {
521+
522+
}
523+
`,
524+
options: [
525+
{
526+
contexts: [
527+
'FunctionDeclaration',
528+
],
529+
},
530+
],
531+
},
532+
{
533+
code: `
534+
/**
535+
* @implements {Bar|Foo}
536+
*/
537+
function quux () {
538+
539+
}
540+
`,
541+
options: [
542+
{
543+
contexts: [
544+
{
545+
context: 'FunctionDeclaration',
546+
},
547+
],
548+
},
549+
],
550+
},
551+
{
552+
code: `
553+
/**
554+
* @callback Foo
555+
*/
556+
`,
557+
options: [
558+
{
559+
contexts: [
560+
'any',
561+
],
562+
},
563+
],
564+
},
515565
],
516566
});

‎test/rules/assertions/noRestrictedSyntax.js‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,27 @@ export default /** @type {import('../index.js').TestCases} */ ({
750750
},
751751
],
752752
},
753+
{
754+
code: `
755+
/**
756+
* @callback Foo
757+
*/
758+
`,
759+
errors: [
760+
{
761+
line: 2,
762+
message: 'Syntax is restricted: any',
763+
},
764+
],
765+
options: [
766+
{
767+
contexts: [
768+
'FunctionDeclaration',
769+
'any',
770+
],
771+
},
772+
],
773+
},
753774
],
754775
valid: [
755776
{

0 commit comments

Comments
 (0)