|
| 1 | +/*! ***************************************************************************** |
| 2 | +Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); you may not use |
| 4 | +this file except in compliance with the License. You may obtain a copy of the |
| 5 | +License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +
|
| 7 | +THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 8 | +KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED |
| 9 | +WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, |
| 10 | +MERCHANTABLITY OR NON-INFRINGEMENT. |
| 11 | +
|
| 12 | +See the Apache Version 2.0 License for specific language governing permissions |
| 13 | +and limitations under the License. |
| 14 | +***************************************************************************** */ |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +/// <reference no-default-lib="true"/> |
| 19 | + |
| 20 | + |
| 21 | +/** |
| 22 | + * The decorator context types provided to class member decorators. |
| 23 | + */ |
| 24 | +type ClassMemberDecoratorContext = |
| 25 | + | ClassMethodDecoratorContext |
| 26 | + | ClassGetterDecoratorContext |
| 27 | + | ClassSetterDecoratorContext |
| 28 | + | ClassFieldDecoratorContext |
| 29 | + | ClassAccessorDecoratorContext |
| 30 | + ; |
| 31 | + |
| 32 | +/** |
| 33 | + * The decorator context types provided to any decorator. |
| 34 | + */ |
| 35 | +type DecoratorContext = |
| 36 | + | ClassDecoratorContext |
| 37 | + | ClassMemberDecoratorContext |
| 38 | + ; |
| 39 | + |
| 40 | +/** |
| 41 | + * Context provided to a class decorator. |
| 42 | + * @template Class The type of the decorated class associated with this context. |
| 43 | + */ |
| 44 | +interface ClassDecoratorContext< |
| 45 | + Class extends abstract new (...args: any) => any = abstract new (...args: any) => any, |
| 46 | +> { |
| 47 | + /** The kind of element that was decorated. */ |
| 48 | + readonly kind: "class"; |
| 49 | + |
| 50 | + /** The name of the decorated class. */ |
| 51 | + readonly name: string | undefined; |
| 52 | + |
| 53 | + /** |
| 54 | + * Adds a callback to be invoked after the class definition has been finalized. |
| 55 | + * |
| 56 | + * @example |
| 57 | + * ```ts |
| 58 | + * function customElement(name: string): ClassDecoratorFunction { |
| 59 | + * return (target, context) => { |
| 60 | + * context.addInitializer(function () { |
| 61 | + * customElements.define(name, this); |
| 62 | + * }); |
| 63 | + * } |
| 64 | + * } |
| 65 | + * |
| 66 | + * @customElement("my-element") |
| 67 | + * class MyElement {} |
| 68 | + * ``` |
| 69 | + */ |
| 70 | + addInitializer(initializer: (this: Class) => void): void; |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * Context provided to a class method decorator. |
| 75 | + * @template This The type on which the class element will be defined. For a static class element, this will be |
| 76 | + * the type of the constructor. For a non-static class element, this will be the type of the instance. |
| 77 | + * @template Value The type of the decorated class method. |
| 78 | + */ |
| 79 | +interface ClassMethodDecoratorContext< |
| 80 | + This = unknown, |
| 81 | + Value extends (this: This, ...args: any) => any = (this: This, ...args: any) => any, |
| 82 | +> { |
| 83 | + /** The kind of class member that was decorated. */ |
| 84 | + readonly kind: "method"; |
| 85 | + |
| 86 | + /** The name of the decorated class member. */ |
| 87 | + readonly name: string | symbol; |
| 88 | + |
| 89 | + /** A value indicating whether the class member is a static (`true`) or instance (`false`) member. */ |
| 90 | + readonly static: boolean; |
| 91 | + |
| 92 | + /** A value indicating whether the class member has a private name. */ |
| 93 | + readonly private: boolean; |
| 94 | + |
| 95 | + // NOTE: Disabled, pending the outcome of https://github.com/tc39/proposal-decorators/issues/494 |
| 96 | + // /** An object that can be used to access the current value of the class member at runtime. */ |
| 97 | + // readonly access: { |
| 98 | + // /** |
| 99 | + // * Gets the current value of the method from the provided receiver. |
| 100 | + // * |
| 101 | + // * @example |
| 102 | + // * let fn = context.access.get.call(instance); |
| 103 | + // */ |
| 104 | + // get(this: This): Value; |
| 105 | + // }; |
| 106 | + |
| 107 | + /** |
| 108 | + * Adds a callback to be invoked either before static initializers are run (when |
| 109 | + * decorating a `static` member), or before instance initializers are run (when |
| 110 | + * decorating a non-`static` member). |
| 111 | + * |
| 112 | + * @example |
| 113 | + * ```ts |
| 114 | + * const bound: ClassMethodDecoratorFunction = (value, context) { |
| 115 | + * if (context.private) throw new TypeError("Not supported on private methods."); |
| 116 | + * context.addInitializer(function () { |
| 117 | + * this[context.name] = this[context.name].bind(this); |
| 118 | + * }); |
| 119 | + * } |
| 120 | + * |
| 121 | + * class C { |
| 122 | + * message = "Hello"; |
| 123 | + * |
| 124 | + * @bound |
| 125 | + * m() { |
| 126 | + * console.log(this.message); |
| 127 | + * } |
| 128 | + * } |
| 129 | + * ``` |
| 130 | + */ |
| 131 | + addInitializer(initializer: (this: This) => void): void; |
| 132 | +} |
| 133 | + |
| 134 | +/** |
| 135 | + * Context provided to a class getter decorator. |
| 136 | + * @template This The type on which the class element will be defined. For a static class element, this will be |
| 137 | + * the type of the constructor. For a non-static class element, this will be the type of the instance. |
| 138 | + * @template Value The property type of the decorated class getter. |
| 139 | + */ |
| 140 | +interface ClassGetterDecoratorContext< |
| 141 | + This = unknown, |
| 142 | + Value = unknown, |
| 143 | +> { |
| 144 | + /** The kind of class member that was decorated. */ |
| 145 | + readonly kind: "getter"; |
| 146 | + |
| 147 | + /** The name of the decorated class member. */ |
| 148 | + readonly name: string | symbol; |
| 149 | + |
| 150 | + /** A value indicating whether the class member is a static (`true`) or instance (`false`) member. */ |
| 151 | + readonly static: boolean; |
| 152 | + |
| 153 | + /** A value indicating whether the class member has a private name. */ |
| 154 | + readonly private: boolean; |
| 155 | + |
| 156 | + // NOTE: Disabled, pending the outcome of https://github.com/tc39/proposal-decorators/issues/494 |
| 157 | + // /** An object that can be used to access the current value of the class member at runtime. */ |
| 158 | + // readonly access: { |
| 159 | + // /** |
| 160 | + // * Invokes the getter on the provided receiver. |
| 161 | + // * |
| 162 | + // * @example |
| 163 | + // * let value = context.access.get.call(instance); |
| 164 | + // */ |
| 165 | + // get(this: This): Value; |
| 166 | + // }; |
| 167 | + |
| 168 | + /** |
| 169 | + * Adds a callback to be invoked either before static initializers are run (when |
| 170 | + * decorating a `static` member), or before instance initializers are run (when |
| 171 | + * decorating a non-`static` member). |
| 172 | + */ |
| 173 | + addInitializer(initializer: (this: This) => void): void; |
| 174 | +} |
| 175 | + |
| 176 | +/** |
| 177 | + * Context provided to a class setter decorator. |
| 178 | + * @template This The type on which the class element will be defined. For a static class element, this will be |
| 179 | + * the type of the constructor. For a non-static class element, this will be the type of the instance. |
| 180 | + * @template Value The type of the decorated class setter. |
| 181 | + */ |
| 182 | +interface ClassSetterDecoratorContext< |
| 183 | + This = unknown, |
| 184 | + Value = unknown, |
| 185 | +> { |
| 186 | + /** The kind of class member that was decorated. */ |
| 187 | + readonly kind: "setter"; |
| 188 | + |
| 189 | + /** The name of the decorated class member. */ |
| 190 | + readonly name: string | symbol; |
| 191 | + |
| 192 | + /** A value indicating whether the class member is a static (`true`) or instance (`false`) member. */ |
| 193 | + readonly static: boolean; |
| 194 | + |
| 195 | + /** A value indicating whether the class member has a private name. */ |
| 196 | + readonly private: boolean; |
| 197 | + |
| 198 | + // NOTE: Disabled, pending the outcome of https://github.com/tc39/proposal-decorators/issues/494 |
| 199 | + /** An object that can be used to access the current value of the class member at runtime. */ |
| 200 | + // readonly access: { |
| 201 | + // /** |
| 202 | + // * Invokes the setter on the provided receiver. |
| 203 | + // * |
| 204 | + // * @example |
| 205 | + // * context.access.set.call(instance, value); |
| 206 | + // */ |
| 207 | + // set(this: This, value: Value): void; |
| 208 | + // }; |
| 209 | + |
| 210 | + /** |
| 211 | + * Adds a callback to be invoked either before static initializers are run (when |
| 212 | + * decorating a `static` member), or before instance initializers are run (when |
| 213 | + * decorating a non-`static` member). |
| 214 | + */ |
| 215 | + addInitializer(initializer: (this: This) => void): void; |
| 216 | +} |
| 217 | + |
| 218 | +/** |
| 219 | + * Context provided to a class `accessor` field decorator. |
| 220 | + * @template This The type on which the class element will be defined. For a static class element, this will be |
| 221 | + * the type of the constructor. For a non-static class element, this will be the type of the instance. |
| 222 | + * @template Value The type of decorated class field. |
| 223 | + */ |
| 224 | +interface ClassAccessorDecoratorContext< |
| 225 | + This = unknown, |
| 226 | + Value = unknown, |
| 227 | +> { |
| 228 | + /** The kind of class member that was decorated. */ |
| 229 | + readonly kind: "accessor"; |
| 230 | + |
| 231 | + /** The name of the decorated class member. */ |
| 232 | + readonly name: string | symbol; |
| 233 | + |
| 234 | + /** A value indicating whether the class member is a static (`true`) or instance (`false`) member. */ |
| 235 | + readonly static: boolean; |
| 236 | + |
| 237 | + /** A value indicating whether the class member has a private name. */ |
| 238 | + readonly private: boolean; |
| 239 | + |
| 240 | + // NOTE: Disabled, pending the outcome of https://github.com/tc39/proposal-decorators/issues/494 |
| 241 | + // /** An object that can be used to access the current value of the class member at runtime. */ |
| 242 | + // readonly access: { |
| 243 | + // /** |
| 244 | + // * Invokes the getter on the provided receiver. |
| 245 | + // * |
| 246 | + // * @example |
| 247 | + // * let value = context.access.get.call(instance); |
| 248 | + // */ |
| 249 | + // get(this: This): Value; |
| 250 | + |
| 251 | + // /** |
| 252 | + // * Invokes the setter on the provided receiver. |
| 253 | + // * |
| 254 | + // * @example |
| 255 | + // * context.access.set.call(instance, value); |
| 256 | + // */ |
| 257 | + // set(this: This, value: Value): void; |
| 258 | + // }; |
| 259 | + |
| 260 | + /** |
| 261 | + * Adds a callback to be invoked either before static initializers are run (when |
| 262 | + * decorating a `static` member), or before instance initializers are run (when |
| 263 | + * decorating a non-`static` member). |
| 264 | + */ |
| 265 | + addInitializer(initializer: (this: This) => void): void; |
| 266 | +} |
| 267 | + |
| 268 | +/** |
| 269 | + * Describes the target provided to class `accessor` field decorators. |
| 270 | + * @template This The `this` type to which the target applies. |
| 271 | + * @template Value The property type for the class `accessor` field. |
| 272 | + */ |
| 273 | +interface ClassAccessorDecoratorTarget<This, Value> { |
| 274 | + /** |
| 275 | + * Invokes the getter that was defined prior to decorator application. |
| 276 | + * |
| 277 | + * @example |
| 278 | + * let value = target.get.call(instance); |
| 279 | + */ |
| 280 | + get(this: This): Value; |
| 281 | + |
| 282 | + /** |
| 283 | + * Invokes the setter that was defined prior to decorator application. |
| 284 | + * |
| 285 | + * @example |
| 286 | + * target.set.call(instance, value); |
| 287 | + */ |
| 288 | + set(this: This, value: Value): void; |
| 289 | +} |
| 290 | + |
| 291 | +/** |
| 292 | + * Describes the allowed return value from a class `accessor` field decorator. |
| 293 | + * @template This The `this` type to which the target applies. |
| 294 | + * @template Value The property type for the class `accessor` field. |
| 295 | + */ |
| 296 | +interface ClassAccessorDecoratorResult<This, Value> { |
| 297 | + /** |
| 298 | + * An optional replacement getter function. If not provided, the existing getter function is used instead. |
| 299 | + */ |
| 300 | + get?(this: This): Value; |
| 301 | + |
| 302 | + /** |
| 303 | + * An optional replacement setter function. If not provided, the existing setter function is used instead. |
| 304 | + */ |
| 305 | + set?(this: This, value: Value): void; |
| 306 | + |
| 307 | + /** |
| 308 | + * An optional initializer mutator that is invoked when the underlying field initializer is evaluated. |
| 309 | + * @param value The incoming initializer value. |
| 310 | + * @returns The replacement initializer value. |
| 311 | + */ |
| 312 | + init?(this: This, value: Value): Value; |
| 313 | +} |
| 314 | + |
| 315 | +/** |
| 316 | + * Context provided to a class field decorator. |
| 317 | + * @template This The type on which the class element will be defined. For a static class element, this will be |
| 318 | + * the type of the constructor. For a non-static class element, this will be the type of the instance. |
| 319 | + * @template Value The type of the decorated class field. |
| 320 | + */ |
| 321 | +interface ClassFieldDecoratorContext< |
| 322 | + This = unknown, |
| 323 | + Value = unknown, |
| 324 | +> { |
| 325 | + /** The kind of class member that was decorated. */ |
| 326 | + readonly kind: "field"; |
| 327 | + |
| 328 | + /** The name of the decorated class member. */ |
| 329 | + readonly name: string | symbol; |
| 330 | + |
| 331 | + /** A value indicating whether the class member is a static (`true`) or instance (`false`) member. */ |
| 332 | + readonly static: boolean; |
| 333 | + |
| 334 | + /** A value indicating whether the class member has a private name. */ |
| 335 | + readonly private: boolean; |
| 336 | + |
| 337 | + // NOTE: Disabled, pending the outcome of https://github.com/tc39/proposal-decorators/issues/494 |
| 338 | + // /** An object that can be used to access the current value of the class member at runtime. */ |
| 339 | + // readonly access: { |
| 340 | + // /** |
| 341 | + // * Gets the value of the field on the provided receiver. |
| 342 | + // */ |
| 343 | + // get(this: This): Value; |
| 344 | + |
| 345 | + // /** |
| 346 | + // * Sets the value of the field on the provided receiver. |
| 347 | + // */ |
| 348 | + // set(this: This, value: Value): void; |
| 349 | + // }; |
| 350 | + |
| 351 | + /** |
| 352 | + * Adds a callback to be invoked either before static initializers are run (when |
| 353 | + * decorating a `static` member), or before instance initializers are run (when |
| 354 | + * decorating a non-`static` member). |
| 355 | + */ |
| 356 | + addInitializer(initializer: (this: This) => void): void; |
| 357 | +} |
0 commit comments