Skip to content

Commit b8eeca8

Browse files
committed
feat(type-formatting): add parameterDefaultValueSpacing for type parameter spacing
1 parent 2735b6d commit b8eeca8

File tree

5 files changed

+135
-2
lines changed

5 files changed

+135
-2
lines changed

.README/rules/type-formatting.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Currently offers the following options for formatting types.
1818
|Tags|`param`, `property`, `returns`, `this`, `throws`, `type`, `typedef`, `yields`|
1919
|Recommended|false|
2020
|Settings|`mode`|
21-
|Options|`arrayBrackets`, `enableFixer`, `genericDot`, `objectFieldIndent`, `objectFieldQuote`, `objectFieldSeparator`, `objectFieldSeparatorOptionalLinebreak`, `objectFieldSeparatorTrailingPunctuation`, `separatorForSingleObjectField`, `stringQuotes`, `typeBracketSpacing`, `unionSpacing`|
21+
|Options|`arrayBrackets`, `defaultValueSpacing`, `elementSpacing`, `enableFixer`, `genericDot`, `objectFieldIndent`, `objectFieldQuote`, `objectFieldSeparator`, `objectFieldSeparatorOptionalLinebreak`, `objectFieldSeparatorTrailingPunctuation`, `separatorForSingleObjectField`, `stringQuotes`, `typeBracketSpacing`, `unionSpacing`|
2222

2323
## Failing examples
2424

docs/rules/type-formatting.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ A single options object has the following properties.
2222

2323
Determines how array generics are represented. Set to `angle` for the style `Array<type>` or `square` for the style `type[]`. Defaults to "square".
2424

25+
<a name="user-content-type-formatting-options-defaultvaluespacing"></a>
26+
<a name="type-formatting-options-defaultvaluespacing"></a>
27+
### <code>defaultValueSpacing</code>
28+
29+
The space character (if any) to use between the equal signs of a default value
30+
31+
<a name="user-content-type-formatting-options-elementspacing"></a>
32+
<a name="type-formatting-options-elementspacing"></a>
33+
### <code>elementSpacing</code>
34+
35+
The space character (if any) to use between elements in generics and tuples
36+
2537
<a name="user-content-type-formatting-options-enablefixer"></a>
2638
<a name="type-formatting-options-enablefixer"></a>
2739
### <code>enableFixer</code>
@@ -114,7 +126,7 @@ Determines the spacing to add to unions (`|`). Defaults to a single space (`" "`
114126
|Tags|`param`, `property`, `returns`, `this`, `throws`, `type`, `typedef`, `yields`|
115127
|Recommended|false|
116128
|Settings|`mode`|
117-
|Options|`arrayBrackets`, `enableFixer`, `genericDot`, `objectFieldIndent`, `objectFieldQuote`, `objectFieldSeparator`, `objectFieldSeparatorOptionalLinebreak`, `objectFieldSeparatorTrailingPunctuation`, `separatorForSingleObjectField`, `stringQuotes`, `typeBracketSpacing`, `unionSpacing`|
129+
|Options|`arrayBrackets`, `defaultValueSpacing`, `elementSpacing`, `enableFixer`, `genericDot`, `objectFieldIndent`, `objectFieldQuote`, `objectFieldSeparator`, `objectFieldSeparatorOptionalLinebreak`, `objectFieldSeparatorTrailingPunctuation`, `separatorForSingleObjectField`, `stringQuotes`, `typeBracketSpacing`, `unionSpacing`|
118130

119131
<a name="user-content-type-formatting-failing-examples"></a>
120132
<a name="type-formatting-failing-examples"></a>
@@ -288,6 +300,24 @@ The following patterns are considered problems:
288300
*/
289301
// "jsdoc/type-formatting": ["error"|"warn", {"objectFieldIndent":" ","objectFieldSeparator":"semicolon-and-linebreak","objectFieldSeparatorOptionalLinebreak":true}]
290302
// Message: Inconsistent semicolon-and-linebreak separator usage
303+
304+
/**
305+
* @param {SomeType<T, U>} cfg
306+
*/
307+
// "jsdoc/type-formatting": ["error"|"warn", {"elementSpacing":""}]
308+
// Message: Element spacing should be ""
309+
310+
/**
311+
* @param {[string, number]} cfg
312+
*/
313+
// "jsdoc/type-formatting": ["error"|"warn", {"elementSpacing":""}]
314+
// Message: Element spacing should be ""
315+
316+
/**
317+
* @param {<T, U extends V = string, W = string>(x: T) => U} cfg
318+
*/
319+
// "jsdoc/type-formatting": ["error"|"warn", {"defaultValueSpacing":""}]
320+
// Message: Default value spacing should be ""
291321
````
292322

293323

@@ -367,5 +397,20 @@ The following patterns are not considered problems:
367397
* }} cfg
368398
*/
369399
// "jsdoc/type-formatting": ["error"|"warn", {"objectFieldIndent":" ","objectFieldSeparator":"semicolon-and-linebreak","objectFieldSeparatorOptionalLinebreak":true}]
400+
401+
/**
402+
* @param {SomeType<T,U>} cfg
403+
*/
404+
// "jsdoc/type-formatting": ["error"|"warn", {"elementSpacing":""}]
405+
406+
/**
407+
* @param {[string,number]} cfg
408+
*/
409+
// "jsdoc/type-formatting": ["error"|"warn", {"elementSpacing":""}]
410+
411+
/**
412+
* @param {<T, U extends V=string, W=string>(x: T) => U} cfg
413+
*/
414+
// "jsdoc/type-formatting": ["error"|"warn", {"defaultValueSpacing":""}]
370415
````
371416

src/rules.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2947,6 +2947,14 @@ export interface Rules {
29472947
* Determines how array generics are represented. Set to `angle` for the style `Array<type>` or `square` for the style `type[]`. Defaults to "square".
29482948
*/
29492949
arrayBrackets?: "angle" | "square";
2950+
/**
2951+
* The space character (if any) to use between the equal signs of a default value
2952+
*/
2953+
defaultValueSpacing?: string;
2954+
/**
2955+
* The space character (if any) to use between elements in generics and tuples
2956+
*/
2957+
elementSpacing?: string;
29502958
/**
29512959
* Whether to enable the fixer. Defaults to `true`.
29522960
*/

src/rules/typeFormatting.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export default iterateJsdoc(({
1717
}) => {
1818
const {
1919
arrayBrackets = 'square',
20+
defaultValueSpacing = ' ',
2021
elementSpacing = ' ',
2122
enableFixer = true,
2223
genericDot = false,
@@ -318,6 +319,18 @@ export default iterateJsdoc(({
318319
break;
319320
}
320321

322+
case 'JsdocTypeTypeParameter': {
323+
const typeNode = /** @type {import('jsdoc-type-pratt-parser').TypeParameterResult} */ (nde);
324+
if (typeNode.defaultValue && (typeNode.meta?.defaultValueSpacing ?? ' ') !== defaultValueSpacing) {
325+
typeNode.meta = {
326+
defaultValueSpacing,
327+
};
328+
errorMessage = `Default value spacing should be "${defaultValueSpacing}"`;
329+
}
330+
331+
break;
332+
}
333+
321334
case 'JsdocTypeUnion': {
322335
const typeNode = /** @type {import('jsdoc-type-pratt-parser').UnionResult} */ (nde);
323336
/* c8 ignore next -- Guard */
@@ -393,6 +406,10 @@ export default iterateJsdoc(({
393406
],
394407
type: 'string',
395408
},
409+
defaultValueSpacing: {
410+
description: 'The space character (if any) to use between the equal signs of a default value',
411+
type: 'string',
412+
},
396413
elementSpacing: {
397414
description: 'The space character (if any) to use between elements in generics and tuples',
398415
type: 'string',

test/rules/assertions/typeFormatting.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,33 @@ export default {
752752
*/
753753
`,
754754
},
755+
{
756+
code: `
757+
/**
758+
* @param {<T, U extends V = string, W = string>(x: T) => U} cfg
759+
*/
760+
`,
761+
errors: [
762+
{
763+
line: 3,
764+
message: 'Default value spacing should be ""',
765+
},
766+
{
767+
line: 3,
768+
message: 'Default value spacing should be ""',
769+
},
770+
],
771+
options: [
772+
{
773+
defaultValueSpacing: '',
774+
},
775+
],
776+
output: `
777+
/**
778+
* @param {<T, U extends V=string, W=string>(x: T) => U} cfg
779+
*/
780+
`,
781+
},
755782
],
756783
valid: [
757784
{
@@ -933,5 +960,41 @@ export default {
933960
},
934961
],
935962
},
963+
{
964+
code: `
965+
/**
966+
* @param {SomeType<T,U>} cfg
967+
*/
968+
`,
969+
options: [
970+
{
971+
elementSpacing: '',
972+
},
973+
],
974+
},
975+
{
976+
code: `
977+
/**
978+
* @param {[string,number]} cfg
979+
*/
980+
`,
981+
options: [
982+
{
983+
elementSpacing: '',
984+
},
985+
],
986+
},
987+
{
988+
code: `
989+
/**
990+
* @param {<T, U extends V=string, W=string>(x: T) => U} cfg
991+
*/
992+
`,
993+
options: [
994+
{
995+
defaultValueSpacing: '',
996+
},
997+
],
998+
},
936999
],
9371000
};

0 commit comments

Comments
 (0)