Skip to content

Commit 0c0e0aa

Browse files
Alexey Andreevyole
Alexey Andreev
authored andcommitted
JS: add some docs to declarations related to interop
1 parent 322379e commit 0c0e0aa

File tree

2 files changed

+163
-0
lines changed

2 files changed

+163
-0
lines changed

js/js.libraries/src/core/annotations.kt

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,138 @@ internal annotation class library(public val name: String = "")
4040
@Target(CLASS)
4141
internal annotation class marker
4242

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+
*/
4376
@Retention(AnnotationRetention.BINARY)
4477
@Target(CLASS, FUNCTION, PROPERTY, CONSTRUCTOR, PROPERTY_GETTER, PROPERTY_SETTER)
4578
annotation class JsName(val name: String)
4679

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+
*/
47109
@Retention(AnnotationRetention.BINARY)
48110
@Target(CLASS, PROPERTY, FUNCTION, FILE)
49111
annotation class JsModule(val import: String)
50112

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+
*/
51141
@Retention(AnnotationRetention.BINARY)
52142
@Target(CLASS, PROPERTY, FUNCTION, FILE)
53143
annotation class JsNonModule
54144

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+
*/
55175
@Retention(AnnotationRetention.BINARY)
56176
@Target(AnnotationTarget.FILE)
57177
annotation class JsQualifier(val value: String)

js/js.libraries/src/core/core.kt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,32 @@ package kotlin.js
33
@Deprecated(message = "Use `definedExternally` instead", level = DeprecationLevel.ERROR, replaceWith = ReplaceWith("definedExternally"))
44
public external val noImpl: Nothing
55

6+
/**
7+
* The property that can be used as a placeholder for statements and values that are defined in JavaScript.
8+
*
9+
* This property can be used in two cases:
10+
*
11+
* * To represent body of an external function. In most cases Kotlin does not require to provide bodies of external
12+
* functions and properties, but if for some reason you want to (for example, due to limitation of your coding style guides),
13+
* you should use `definedExternally`.
14+
* * To represent value of default argument.
15+
*
16+
* There's two forms of using `definedExternally`:
17+
*
18+
* 1. `= definedExternally` (for functions, properties and parameters).
19+
* 2. `{ definedExternally }` (for functions and property getters/setters).
20+
*
21+
* This property can't be used from normal code.
22+
*
23+
* Examples:
24+
*
25+
* ``` kotlin
26+
* external fun foo(): String = definedExternally
27+
* external fun bar(x: Int) { definedExternally }
28+
* external fun baz(z: Any = definedExternally): Array<Any>
29+
* external val prop: Float = definedExternally
30+
* ```
31+
*/
632
public external val definedExternally: Nothing
733

834
/**
@@ -24,6 +50,23 @@ public external fun parseInt(s: String, radix: Int = definedExternally): Int
2450
@Deprecated("Use toDouble() instead.", ReplaceWith("s.toDouble()"), level = DeprecationLevel.ERROR)
2551
public external fun parseFloat(s: String, radix: Int = definedExternally): Double
2652

53+
/**
54+
* Puts the given piece of a JavaScript code right into the calling function.
55+
* The compiler replaces call to `js(...)` code with the string constant provided as a parameter.
56+
*
57+
* Example:
58+
*
59+
* ``` kotlin
60+
* fun logToConsole(message: String): Unit {
61+
* js("console.log(message)")
62+
* }
63+
* ```
64+
*
65+
* @param code the piece of JavaScript code to put to the generated code.
66+
* Must be a compile-time constant, otherwise compiler produces error message.
67+
* You can safely refer to local variables of calling function (but not to local variables of outer functions),
68+
* including parameters. You can't refer to functions, properties and classes by their short names.
69+
*/
2770
public external fun js(code: String): dynamic
2871

2972
/**

0 commit comments

Comments
 (0)