@@ -40,18 +40,138 @@ internal annotation class library(public val name: String = "")
40
40
@Target(CLASS )
41
41
internal annotation class marker
42
42
43
+ /* *
44
+ * Gives a declaration (a function, a property or a class) specific name in JavaScript.
45
+ *
46
+ * This may be useful in the following cases:
47
+ *
48
+ * * There are two functions for which the compiler gives same name in JavaScript, you can
49
+ * mark one with `@JsName(...)` to prevent the compiler from reporting error.
50
+ * * You are writing a JavaScript library in Kotlin. The compiler produces mangled names
51
+ * for functions with parameters, which is unnatural for usual JavaScript developer.
52
+ * You can put `@JsName(...)` on functions you want to be available from JavaScript.
53
+ * * For some reason you want to rename declaration, e.g. there's common term in JavaScript
54
+ * for a concept provided by the declaration, which in uncommon in Kotlin.
55
+ *
56
+ * Example:
57
+ *
58
+ * ``` kotlin
59
+ * class Person(val name: String) {
60
+ * fun hello() {
61
+ * println("Hello $name!")
62
+ * }
63
+ *
64
+ * @JsName("helloWithGreeting")
65
+ * fun hello(greeting: String) {
66
+ * println("$greeting $name!")
67
+ * }
68
+ * }
69
+ * ```
70
+ *
71
+ * @property name the name which compiler uses both for declaration itself and for all references to the declaration.
72
+ * It's required to denote a valid JavaScript identifier.
73
+ *
74
+ * @since 1.1
75
+ */
43
76
@Retention(AnnotationRetention .BINARY )
44
77
@Target(CLASS , FUNCTION , PROPERTY , CONSTRUCTOR , PROPERTY_GETTER , PROPERTY_SETTER )
45
78
annotation class JsName (val name : String )
46
79
80
+ /* *
81
+ * Denotes an `external` declaration that must be imported from native JavaScript library.
82
+ *
83
+ * The compiler produces the code relevant for the target module system, for example, in case of CommonJS,
84
+ * it will import the declaration via the `require(...)` function.
85
+ *
86
+ * The annotation can be used on top-level external declarations (classes, properties, functions) and files.
87
+ * In case of file (which can't be `external`) the following rule applies: all the declarations in
88
+ * the file must be `external`. By applying `@JsModule(...)` on a file you tell the compiler to import a JavaScript object
89
+ * that contain all the declarations from the file.
90
+ *
91
+ * Example:
92
+ *
93
+ * ``` kotlin
94
+ * @JsModule("jquery")
95
+ * external abstract class JQuery() {
96
+ * // some declarations here
97
+ * }
98
+ *
99
+ * @JsModule("jquery")
100
+ * external fun JQuery(element: Element): JQuery
101
+ * ```
102
+ *
103
+ * @property import name of a module to import declaration from.
104
+ * It is not interpreted by the Kotlin compiler, it's passed as is directly to the target module system.
105
+ *
106
+ * @see JsNonModule
107
+ * @since 1.1
108
+ */
47
109
@Retention(AnnotationRetention .BINARY )
48
110
@Target(CLASS , PROPERTY , FUNCTION , FILE )
49
111
annotation class JsModule (val import : String )
50
112
113
+ /* *
114
+ * Denotes an `external` declaration that can be used without module system.
115
+ *
116
+ * By default, an `external` declaration is available regardless your target module system.
117
+ * However, by applying [JsModule] annotation you can make a declaration unavailable to *plain* module system.
118
+ * Some JavaScript libraries are distributed both as a standalone downloadable piece of JavaScript and as a module available
119
+ * as an npm package.
120
+ * To tell the Kotlin compiler to accept both cases, you can augment [JsModule] with the `@JsNonModule` annotation.
121
+ *
122
+ * For example:
123
+ *
124
+ * ``` kotlin
125
+ * @JsModule("jquery")
126
+ * @JsNonModule
127
+ * @JsName("$")
128
+ * external abstract class JQuery() {
129
+ * // some declarations here
130
+ * }
131
+ *
132
+ * @JsModule("jquery")
133
+ * @JsNonModule
134
+ * @JsName("$")
135
+ * external fun JQuery(element: Element): JQuery
136
+ * ```
137
+ *
138
+ * @see JsModule
139
+ * @since 1.1
140
+ */
51
141
@Retention(AnnotationRetention .BINARY )
52
142
@Target(CLASS , PROPERTY , FUNCTION , FILE )
53
143
annotation class JsNonModule
54
144
145
+ /* *
146
+ * Adds prefix to `external` declarations in a source file.
147
+ *
148
+ * JavaScript does not have concept of packages (namespaces). They are usually emulated by nested objects.
149
+ * The compiler turns references to `external` declarations either to plain unprefixed names (in case of *plain* modules)
150
+ * or to plain imports.
151
+ * However, if a JavaScript library provides its declarations in packages, you won't be satisfied with this.
152
+ * You can tell the compiler to generate additional prefix before references to `external` declarations using the `@JsQuafier(...)`
153
+ * annotation.
154
+ *
155
+ * Note that a file marked with the `@JsQulifier(...)` annotation can't contain non-`external` declarations.
156
+ *
157
+ * Example:
158
+ *
159
+ * ```
160
+ * @file:JsQualifier("my.jsPackageName")
161
+ * package some.kotlinPackage
162
+ *
163
+ * external fun foo(x: Int)
164
+ *
165
+ * external fun bar(): String
166
+ * ```
167
+ *
168
+ * @property value the qualifier to add to the declarations in the generated code.
169
+ * It must be a sequence of valid JavaScript identifiers separated by the `.` character.
170
+ * Examples of valid qualifiers are: `foo`, `bar.Baz`, `_.$0.f`.
171
+ *
172
+ * @see JsModule
173
+ * @since 1.1
174
+ */
55
175
@Retention(AnnotationRetention .BINARY )
56
176
@Target(AnnotationTarget .FILE )
57
177
annotation class JsQualifier (val value : String )
0 commit comments