Skip to content

Commit d53884a

Browse files
committed
fix(type-formatting): do not strip quotes for objectFieldQuote when not an ID; always allow unescaped digits
1 parent 261f497 commit d53884a

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

.README/rules/type-formatting.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ Boolean value of whether to use a dot before the angled brackets of a generic (e
2626

2727
Whether and how object field properties should be quoted (e.g., `{"a": string}`).
2828
Set to `single`, `double`, or `null`. Defaults to `null` (no quotes unless
29-
required due to whitespace within the field).
29+
required due to special characters within the field). Digits will be kept as is,
30+
regardless of setting (they can either represent a digit or a string digit).
3031

3132
### `propertyQuotes`
3233

docs/rules/type-formatting.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ Boolean value of whether to use a dot before the angled brackets of a generic (e
3838

3939
Whether and how object field properties should be quoted (e.g., `{"a": string}`).
4040
Set to `single`, `double`, or `null`. Defaults to `null` (no quotes unless
41-
required due to whitespace within the field).
41+
required due to special characters within the field). Digits will be kept as is,
42+
regardless of setting (they can either represent a digit or a string digit).
4243

4344
<a name="user-content-type-formatting-options-propertyquotes"></a>
4445
<a name="type-formatting-options-propertyquotes"></a>
@@ -319,6 +320,20 @@ The following patterns are not considered problems:
319320
* @param {{"a bc": string}} quotedKeyParam
320321
*/
321322

323+
/**
324+
* @param {{55: string}} quotedKeyParam
325+
*/
326+
327+
/**
328+
* @param {{"a-b-c": string}} quotedKeyParam
329+
*/
330+
// "jsdoc/type-formatting": ["error"|"warn", {"objectFieldQuote":null}]
331+
332+
/**
333+
* @param {{55: string}} quotedKeyParam
334+
*/
335+
// "jsdoc/type-formatting": ["error"|"warn", {"objectFieldQuote":"double"}]
336+
322337
/**
323338
* @param {ab.cd.ef} cfg
324339
*/

src/rules/typeFormatting.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,15 @@ export default iterateJsdoc(({
250250
case 'JsdocTypeObjectField': {
251251
const typeNode = /** @type {import('jsdoc-type-pratt-parser').ObjectFieldResult} */ (nde);
252252
if ((objectFieldQuote ||
253-
(typeof typeNode.key === 'string' && !(/\s/v).test(typeNode.key))) &&
254-
typeNode.meta.quote !== (objectFieldQuote ?? undefined)
253+
(typeof typeNode.key === 'string' &&
254+
(
255+
(/^\p{ID_Start}\p{ID_Continue}*$/v).test(typeNode.key) ||
256+
(/^(\d+(\.\d*)?|\.\d+)([eE][\-+]?\d+)?$/v).test(typeNode.key)
257+
)
258+
)) &&
259+
typeNode.meta.quote !== (objectFieldQuote ?? undefined) &&
260+
(typeof typeNode.key !== 'string' ||
261+
!(/^(\d+(\.\d*)?|\.\d+)([eE][\-+]?\d+)?$/v).test(typeNode.key))
255262
) {
256263
typeNode.meta.quote = objectFieldQuote ?? undefined;
257264
errorMessage = `Inconsistent object field quotes ${objectFieldQuote}`;

test/rules/assertions/typeFormatting.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,37 @@ export default {
714714
*/
715715
`,
716716
},
717+
{
718+
code: `
719+
/**
720+
* @param {{55: string}} quotedKeyParam
721+
*/
722+
`,
723+
},
724+
{
725+
code: `
726+
/**
727+
* @param {{"a-b-c": string}} quotedKeyParam
728+
*/
729+
`,
730+
options: [
731+
{
732+
objectFieldQuote: null,
733+
},
734+
],
735+
},
736+
{
737+
code: `
738+
/**
739+
* @param {{55: string}} quotedKeyParam
740+
*/
741+
`,
742+
options: [
743+
{
744+
objectFieldQuote: 'double',
745+
},
746+
],
747+
},
717748
{
718749
code: `
719750
/**

0 commit comments

Comments
 (0)