1- import { transformTypeToReferenceLink } from '@node-core/doc-kit/src/utils/parser/index .mjs' ;
2- import createQueries from '@node-core/doc-kit/src/utils/queries/index.mjs' ;
1+ import { transformTypeToReferenceLink } from '@node-core/doc-kit/src/generators/metadata/utils/transformers .mjs' ;
2+ import { QUERIES } from '@node-core/doc-kit/src/utils/queries/index.mjs' ;
33import { lintRule } from 'unified-lint-rule' ;
44import { visit } from 'unist-util-visit' ;
55
@@ -11,36 +11,42 @@ const REPLACE_RE = /\s*\| */g;
1111 * @type {import('unified-lint-rule').Rule<, import('../api.mjs').Options> }
1212 */
1313const invalidTypeReference = ( tree , vfile , { typeMap = { } } ) => {
14- visit ( tree , createQueries . UNIST . isTextWithType , node => {
15- const types = node . value . match ( createQueries . QUERIES . normalizeTypes ) ;
16-
17- types . forEach ( type => {
18- // Ensure wrapped in {}
19- if ( type [ 0 ] !== '{' || type [ type . length - 1 ] !== '}' ) {
20- vfile . message (
21- `Type reference must be wrapped in "{}"; saw "${ type } "` ,
22- node
23- ) ;
24-
25- node . value = node . value . replace ( type , `{${ type . slice ( 1 , - 1 ) } }` ) ;
26- }
27-
28- // Fix spaces around |
29- if ( MATCH_RE . test ( type ) ) {
30- vfile . message (
31- `Type reference should be separated by "|", without spaces; saw "${ type } "` ,
32- node
33- ) ;
34-
35- const normalized = type . replace ( REPLACE_RE , '|' ) ;
36- node . value = node . value . replace ( type , normalized ) ;
37- }
38-
39- if ( transformTypeToReferenceLink ( type , typeMap ) === type ) {
40- vfile . message ( `Invalid type reference: ${ type } ` , node ) ;
41- }
42- } ) ;
43- } ) ;
14+ visit (
15+ tree ,
16+ ( { value } ) => QUERIES . normalizeTypes . test ( value ) ,
17+ node => {
18+ const types = node . value . match ( QUERIES . normalizeTypes ) ;
19+
20+ types . forEach ( type => {
21+ // Ensure wrapped in {}
22+ if ( type [ 0 ] !== '{' || type [ type . length - 1 ] !== '}' ) {
23+ vfile . message (
24+ `Type reference must be wrapped in "{}"; saw "${ type } "` ,
25+ node
26+ ) ;
27+
28+ const newType = `{${ type . slice ( 1 , - 1 ) } }` ;
29+ node . value = node . value . replace ( type , newType ) ;
30+ type = newType ;
31+ }
32+
33+ // Fix spaces around |
34+ if ( MATCH_RE . test ( type ) ) {
35+ vfile . message (
36+ `Type reference should be separated by "|", without spaces; saw "${ type } "` ,
37+ node
38+ ) ;
39+
40+ const normalized = type . replace ( REPLACE_RE , '|' ) ;
41+ node . value = node . value . replace ( type , normalized ) ;
42+ }
43+
44+ if ( transformTypeToReferenceLink ( type , typeMap ) === type ) {
45+ vfile . message ( `Invalid type reference: ${ type } ` , node ) ;
46+ }
47+ } ) ;
48+ }
49+ ) ;
4450} ;
4551
4652export default lintRule (
0 commit comments