@@ -3,7 +3,8 @@ import createQueries from '@nodejs/doc-kit/src/utils/queries/index.mjs';
3
3
import { lintRule } from 'unified-lint-rule' ;
4
4
import { visit } from 'unist-util-visit' ;
5
5
6
- const SPACED_SEPERATOR_RE = / \s \| | \| \s / g;
6
+ const MATCH_RE = / \s \| | \| \s / g;
7
+ const REPLACE_RE = / \s * \| \s * / g;
7
8
8
9
/**
9
10
* Ensures that all type references are valid
@@ -14,17 +15,25 @@ const invalidTypeReference = (tree, vfile) => {
14
15
const types = node . value . match ( createQueries . QUERIES . normalizeTypes ) ;
15
16
16
17
types . forEach ( type => {
17
- if ( type [ 0 ] !== '{' || type . at ( - 1 ) !== '}' ) {
18
+ // Ensure wrapped in {}
19
+ if ( type [ 0 ] !== '{' || type [ type . length - 1 ] !== '}' ) {
18
20
vfile . message (
19
21
`Type reference must be wrapped in "{}"; saw "${ type } "` ,
20
22
node
21
23
) ;
24
+
25
+ node . value = node . value . replace ( type , `{${ type . slice ( 1 , - 1 ) } }` ) ;
22
26
}
23
27
24
- if ( SPACED_SEPERATOR_RE . test ( type ) ) {
28
+ // Fix spaces around |
29
+ if ( MATCH_RE . test ( type ) ) {
25
30
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
27
33
) ;
34
+
35
+ const normalized = type . replace ( REPLACE_RE , '|' ) ;
36
+ node . value = node . value . replace ( type , normalized ) ;
28
37
}
29
38
30
39
if ( transformTypeToReferenceLink ( type ) === type ) {
0 commit comments