@@ -3,7 +3,8 @@ import createQueries from '@nodejs/doc-kit/src/utils/queries/index.mjs';
33import { lintRule } from 'unified-lint-rule' ;
44import { visit } from 'unist-util-visit' ;
55
6- const SPACED_SEPERATOR_RE = / \s \| | \| \s / g;
6+ const MATCH_RE = / \s \| | \| \s / g;
7+ const REPLACE_RE = / \s * \| \s * / g;
78
89/**
910 * Ensures that all type references are valid
@@ -14,17 +15,25 @@ const invalidTypeReference = (tree, vfile) => {
1415 const types = node . value . match ( createQueries . QUERIES . normalizeTypes ) ;
1516
1617 types . forEach ( type => {
17- if ( type [ 0 ] !== '{' || type . at ( - 1 ) !== '}' ) {
18+ // Ensure wrapped in {}
19+ if ( type [ 0 ] !== '{' || type [ type . length - 1 ] !== '}' ) {
1820 vfile . message (
1921 `Type reference must be wrapped in "{}"; saw "${ type } "` ,
2022 node
2123 ) ;
24+
25+ node . value = node . value . replace ( type , `{${ type . slice ( 1 , - 1 ) } }` ) ;
2226 }
2327
24- if ( SPACED_SEPERATOR_RE . test ( type ) ) {
28+ // Fix spaces around |
29+ if ( MATCH_RE . test ( type ) ) {
2530 vfile . message (
26- `Type reference should be seperated by "|", without spaces; saw "${ type } "`
31+ `Type reference should be separated by "|", without spaces; saw "${ type } "` ,
32+ node
2733 ) ;
34+
35+ const normalized = type . replace ( REPLACE_RE , '|' ) ;
36+ node . value = node . value . replace ( type , normalized ) ;
2837 }
2938
3039 if ( transformTypeToReferenceLink ( type ) === type ) {
0 commit comments