@@ -44,21 +44,6 @@ function replaceTemplatedStringFromProperties(formatString: string, sourceObject
44
44
} ) ;
45
45
}
46
46
47
- function setTemplatedStringProperty (
48
- key : symbol ,
49
- program : Program ,
50
- target : Type ,
51
- text : string ,
52
- sourceObject ?: Type
53
- ) {
54
- // If an object was passed in, use it to format the documentation string
55
- if ( sourceObject ) {
56
- text = replaceTemplatedStringFromProperties ( text , sourceObject ) ;
57
- }
58
-
59
- program . stateMap ( key ) . set ( target , text ) ;
60
- }
61
-
62
47
function createStateSymbol ( name : string ) {
63
48
return Symbol . for ( `TypeSpec.${ name } ` ) ;
64
49
}
@@ -79,14 +64,31 @@ export function $summary(
79
64
text : string ,
80
65
sourceObject : Type
81
66
) {
82
- setTemplatedStringProperty ( summaryKey , context . program , target , text , sourceObject ) ;
67
+ if ( sourceObject ) {
68
+ text = replaceTemplatedStringFromProperties ( text , sourceObject ) ;
69
+ }
70
+
71
+ context . program . stateMap ( summaryKey ) . set ( target , text ) ;
83
72
}
84
73
85
74
export function getSummary ( program : Program , type : Type ) : string | undefined {
86
75
return program . stateMap ( summaryKey ) . get ( type ) ;
87
76
}
88
77
89
78
const docsKey = createStateSymbol ( "docs" ) ;
79
+ export interface DocData {
80
+ /**
81
+ * Doc value.
82
+ */
83
+ value : string ;
84
+
85
+ /**
86
+ * How was the doc set.
87
+ * - `@doc` means the `@doc` decorator was used
88
+ * - `comment` means it was set from a `/** comment * /`
89
+ */
90
+ source : "@doc" | "comment" ;
91
+ }
90
92
/**
91
93
* @doc attaches a documentation string. Works great with multi-line string literals.
92
94
*
@@ -97,13 +99,42 @@ const docsKey = createStateSymbol("docs");
97
99
*/
98
100
export function $doc ( context : DecoratorContext , target : Type , text : string , sourceObject ?: Type ) {
99
101
validateDecoratorUniqueOnNode ( context , target , $doc ) ;
100
- setTemplatedStringProperty ( docsKey , context . program , target , text , sourceObject ) ;
102
+ if ( sourceObject ) {
103
+ text = replaceTemplatedStringFromProperties ( text , sourceObject ) ;
104
+ }
105
+ setDocData ( context . program , target , { value : text , source : "@doc" } ) ;
101
106
}
102
107
103
- export function getDoc ( program : Program , target : Type ) : string | undefined {
108
+ /**
109
+ * @internal to be used to set the `@doc` from doc comment.
110
+ */
111
+ export function $docFromComment ( context : DecoratorContext , target : Type , text : string ) {
112
+ setDocData ( context . program , target , { value : text , source : "comment" } ) ;
113
+ }
114
+
115
+ function setDocData ( program : Program , target : Type , data : DocData ) {
116
+ program . stateMap ( docsKey ) . set ( target , data ) ;
117
+ }
118
+ /**
119
+ * Get the documentation information for the given type. In most cases you probably just want to use {@link getDoc}
120
+ * @param program Program
121
+ * @param target Type
122
+ * @returns Doc data with source information.
123
+ */
124
+ export function getDocData ( program : Program , target : Type ) : DocData | undefined {
104
125
return program . stateMap ( docsKey ) . get ( target ) ;
105
126
}
106
127
128
+ /**
129
+ * Get the documentation string for the given type.
130
+ * @param program Program
131
+ * @param target Type
132
+ * @returns Documentation value
133
+ */
134
+ export function getDoc ( program : Program , target : Type ) : string | undefined {
135
+ return getDocData ( program , target ) ?. value ;
136
+ }
137
+
107
138
export function $inspectType ( program : Program , target : Type , text : string ) {
108
139
// eslint-disable-next-line no-console
109
140
if ( text ) console . log ( text ) ;
0 commit comments