@@ -451,6 +451,8 @@ type StructuralField<T extends TreeRefs = RawRefs> =
451
451
| VectorElement < T >
452
452
| TypeSignature < T >
453
453
| SignatureLine < T >
454
+ | FunctionAnnotation < T >
455
+ | AnnotationLine < T >
454
456
455
457
/** Type whose fields are all suitable for storage as `Ast` fields. */
456
458
interface FieldObject < T extends TreeRefs > {
@@ -568,6 +570,10 @@ function mapRefs<T extends TreeRefs, U extends TreeRefs>(
568
570
field : VectorElement < T > ,
569
571
f : MapRef < T , U > ,
570
572
) : VectorElement < U >
573
+ function mapRefs < T extends TreeRefs , U extends TreeRefs > (
574
+ field : AnnotationLine < T > ,
575
+ f : MapRef < T , U > ,
576
+ ) : AnnotationLine < U >
571
577
function mapRefs < T extends TreeRefs , U extends TreeRefs > (
572
578
field : SignatureLine < T > ,
573
579
f : MapRef < T , U > ,
@@ -2035,6 +2041,17 @@ interface ArgumentType<T extends TreeRefs = RawRefs> {
2035
2041
type : T [ 'ast' ]
2036
2042
}
2037
2043
2044
+ interface FunctionAnnotation < T extends TreeRefs = RawRefs > {
2045
+ operator : T [ 'token' ]
2046
+ annotation : T [ 'token' ]
2047
+ argument : T [ 'ast' ] | undefined
2048
+ }
2049
+
2050
+ interface AnnotationLine < T extends TreeRefs = RawRefs > {
2051
+ annotation : FunctionAnnotation < T >
2052
+ newlines : T [ 'token' ] [ ]
2053
+ }
2054
+
2038
2055
interface TypeSignature < T extends TreeRefs = RawRefs > {
2039
2056
name : T [ 'ast' ]
2040
2057
operator : T [ 'token' ]
@@ -2046,13 +2063,14 @@ interface SignatureLine<T extends TreeRefs = RawRefs> {
2046
2063
newlines : T [ 'token' ] [ ]
2047
2064
}
2048
2065
2049
- export interface FunctionFields {
2050
- signatureLine : SignatureLine | undefined
2051
- private_ : NodeChild < SyncTokenId > | undefined
2052
- name : NodeChild < AstId >
2053
- argumentDefinitions : ArgumentDefinition [ ]
2054
- equals : NodeChild < SyncTokenId >
2055
- body : NodeChild < AstId > | undefined
2066
+ export interface FunctionFields < T extends TreeRefs = RawRefs > {
2067
+ annotationLines : AnnotationLine < T > [ ]
2068
+ signatureLine : SignatureLine < T > | undefined
2069
+ private_ : T [ 'token' ] | undefined
2070
+ name : T [ 'ast' ]
2071
+ argumentDefinitions : ArgumentDefinition < T > [ ]
2072
+ equals : T [ 'token' ]
2073
+ body : T [ 'ast' ] | undefined
2056
2074
}
2057
2075
/** TODO: Add docs */
2058
2076
export class Function extends Ast {
@@ -2086,24 +2104,24 @@ export class Function extends Ast {
2086
2104
/** TODO: Add docs */
2087
2105
static concrete (
2088
2106
module : MutableModule ,
2089
- signatureLine : SignatureLine < OwnedRefs > | undefined ,
2090
- private_ : NodeChild < Token > | undefined ,
2091
- name : NodeChild < Owned > ,
2092
- argumentDefinitions : ArgumentDefinition < OwnedRefs > [ ] ,
2093
- equals : NodeChild < Token > ,
2094
- body : NodeChild < Owned > | undefined ,
2107
+ fields : Partial < FunctionFields < OwnedRefs > > & { name : object } & { equals : object } ,
2095
2108
) {
2096
2109
const base = module . baseObject ( 'Function' )
2097
2110
const id_ = base . get ( 'id' )
2098
- const fields = composeFieldData ( base , {
2099
- signatureLine : signatureLine && mapRefs ( signatureLine , ownedToRaw ( module , id_ ) ) ,
2100
- private_,
2101
- name : concreteChild ( module , name , id_ ) ,
2102
- argumentDefinitions : argumentDefinitions . map ( def => mapRefs ( def , ownedToRaw ( module , id_ ) ) ) ,
2103
- equals,
2104
- body : concreteChild ( module , body , id_ ) ,
2111
+ const rawFields = composeFieldData ( base , {
2112
+ annotationLines : ( fields . annotationLines ?? [ ] ) . map ( anno =>
2113
+ mapRefs ( anno , ownedToRaw ( module , id_ ) ) ,
2114
+ ) ,
2115
+ signatureLine : fields . signatureLine && mapRefs ( fields . signatureLine , ownedToRaw ( module , id_ ) ) ,
2116
+ private_ : fields . private_ ,
2117
+ name : concreteChild ( module , fields . name , id_ ) ,
2118
+ argumentDefinitions : ( fields . argumentDefinitions ?? [ ] ) . map ( def =>
2119
+ mapRefs ( def , ownedToRaw ( module , id_ ) ) ,
2120
+ ) ,
2121
+ equals : fields . equals ,
2122
+ body : concreteChild ( module , fields . body , id_ ) ,
2105
2123
} )
2106
- return asOwned ( new MutableFunction ( module , fields ) )
2124
+ return asOwned ( new MutableFunction ( module , rawFields ) )
2107
2125
}
2108
2126
2109
2127
/** TODO: Add docs */
@@ -2116,15 +2134,12 @@ export class Function extends Ast {
2116
2134
// Note that a function name may not be an operator if the function is not in the body of a type definition, but we
2117
2135
// can't easily enforce that because we don't currently make a syntactic distinction between top-level functions and
2118
2136
// type methods.
2119
- return MutableFunction . concrete (
2120
- module ,
2121
- undefined ,
2122
- undefined ,
2123
- unspaced ( Ident . newAllowingOperators ( module , name ) ) ,
2137
+ return MutableFunction . concrete ( module , {
2138
+ name : unspaced ( Ident . newAllowingOperators ( module , name ) ) ,
2124
2139
argumentDefinitions,
2125
- spaced ( makeEquals ( ) ) ,
2126
- autospaced ( body ) ,
2127
- )
2140
+ equals : spaced ( makeEquals ( ) ) ,
2141
+ body : autospaced ( body ) ,
2142
+ } )
2128
2143
}
2129
2144
2130
2145
/** Construct a function with simple (name-only) arguments and a body block. */
@@ -2161,7 +2176,15 @@ export class Function extends Ast {
2161
2176
2162
2177
/** TODO: Add docs */
2163
2178
* concreteChildren ( _verbatim ?: boolean ) : IterableIterator < RawNodeChild > {
2164
- const { signatureLine, private_, name, argumentDefinitions, equals, body } = getAll ( this . fields )
2179
+ const { annotationLines, signatureLine, private_, name, argumentDefinitions, equals, body } =
2180
+ getAll ( this . fields )
2181
+ for ( const anno of annotationLines ) {
2182
+ const { operator, annotation, argument } = anno . annotation
2183
+ yield operator
2184
+ yield annotation
2185
+ if ( argument ) yield argument
2186
+ yield * anno . newlines
2187
+ }
2165
2188
if ( signatureLine ) {
2166
2189
const { signature, newlines } = signatureLine
2167
2190
const { name, operator, type } = signature
0 commit comments