Skip to content

Commit ef5d38a

Browse files
committed
JS modules: prohibit to use the annotation on var; minor fixes in text.
1 parent ac8d5a6 commit ef5d38a

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

Diff for: proposals/js/consume-js-modules.md

+25-17
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* **Author**: Zalim Bashorov
55
* **Contributors**: Andrey Breslav, Alexey Andreev
66
* **Status**: Submitted
7-
* **Prototype**:
87

98
## Goal
109

@@ -14,12 +13,12 @@ and use this information to generate dependencies.
1413
## Intro
1514

1615
In general case JS module can be:
17-
- object with a bunch of declarations (in terms of kotlin it can be package or object)
16+
- object with a bunch of declarations (in terms of Kotlin it can be package or object)
1817
- class
1918
- function
2019
- variable
2120

22-
Additionally we should keep in mind that module import string should not be valid identifier.
21+
Additionally, we should keep in mind that module import string should not be a valid identifier.
2322

2423

2524
## Proposed solution
@@ -36,7 +35,7 @@ Propose to add:
3635
AnnotationTarget.FILE)
3736
annotation class JsModule(
3837
val import: String,
39-
vararg val kind: JsModuleKind = arrayOf(JsModuleKind.AMD, JsModuleKind.COMMON_JS, JsModuleKind.UMD)
38+
vararg val kind: JsModuleKind = ... // arrayOf(JsModuleKind.AMD, JsModuleKind.COMMON_JS, JsModuleKind.UMD)
4039
)
4140

4241
enum class JsModuleKind {
@@ -53,14 +52,21 @@ It should have **binary retention** to be available from binary data.
5352

5453
Annotation **allowed on classes, properties and functions** according to what can be exported from JS modules.
5554

55+
_**Frontend:** prohibit to use the annotation on `var`_
56+
<br/>
57+
_It doesn't make sense on module systems except CommonJS (node.js)
58+
and to use it with CommonJS we should call `require` on every call sites to get the current value._
59+
<br/>
60+
_Since it very rare case propose to prohibit it now._
61+
5662
In JS world objects often used as namespace/package so will be nice to allow to map such objects to Kotlin packages.
5763

5864
Why just not using objects in all cases?
5965
1. Some IDE features works better with package level declarations (e.g. auto import)
6066
2. Accessing to package must not have side effects, so in some cases we can generate better code (alternative solution is add marker annotation)
6167

62-
So to achieve that files allowed as target of the annotation.
63-
But it's not enough, additionally we should provide the way to specify custom qualifier for native declarations in the file.
68+
So to achieve that files allowed as the target of the annotation.
69+
But it's not enough, additionally, we should provide the way to specify custom qualifier for native declarations in the file.
6470

6571
It can be achieved by adding yet another parameter to the annotation or add a new annotation, like:
6672

@@ -72,9 +78,10 @@ annotation class JsPackage(val path: String)
7278
[TODO] think up a better name.
7379

7480
The new annotation can be reused in case when we want to have package with long path in Kotlin,
75-
but don't want to have long path in generated JS, e.g. for public API.
76-
Of course, this problem can be fixed by adding another file "facade" with short qualifier.
81+
but don't want to have a long path in generated JS, e.g. for public API.
82+
Of course, this problem can be fixed by adding another file "facade" with a short qualifier.
7783

84+
// it can't part of JsModule becouse will be reused for native declarations inside nested namespaces/packages
7885

7986
Parameters:
8087
- `import` -- string which will be used to import related module.
@@ -131,7 +138,7 @@ package MyExternalModule
131138
@native fun foo() {}
132139
```
133140

134-
**Export by assignment form toplevel**
141+
**Export by assignment from toplevel**
135142

136143
In TypeScript:
137144
```typescript
@@ -245,13 +252,14 @@ When **module kind is SIMPLE**
245252
```
246253

247254
### Frontend
248-
- Report error when try to use native declaration which has `JsModule` annotations, but no one specify rule for current module kind.
255+
- Report error when try to use native declaration which has `JsModule` annotations, but no one specifies a rule for current module kind.
249256
- Prohibit to have many annotations which explicitly provide the same module kind.
250257
- Prohibit to apply `JsModule` annotation to non-native declarations, except files.<br/>
251-
It can be relaxed later e.g. to reuse this annotation to allow translate a file to separate JS module, it can be useful to interop with some frameworks (see [KT-12093](https://youtrack.jetbrains.com/issue/KT-12093))
258+
It can be relaxed later e.g. to reuse this annotation to allow translate a file to separate JS module,
259+
it can be useful to interop with some frameworks (see [KT-12093](https://youtrack.jetbrains.com/issue/KT-12093))
252260

253261
### IDE
254-
- Add inspection for case when some declarations with the same fq-name have different JsModule annotations?
262+
- Add inspection for the case when some declarations with the same fq-
255263
Consider next cases:
256264
- function overloads
257265
- package and functions
@@ -260,15 +268,15 @@ It can be relaxed later e.g. to reuse this annotation to allow translate a file
260268
1. Can we introduce default value for `import` parameter of `JsModule` and use the name of declaration as import string when argument not provided?<br/>
261269
If so, how it should work when the annotation used on file?
262270

263-
2. What should be default value of `kind` parameter of `JsModule`?
271+
2. What should be used as default value of `kind` parameter of `JsModule`?
264272
1. all kinds
265273
2. all kinds except SIMPLE
266274

267275
In TypeScript (external) modules can be used only when compiler ran with module kind (in our terms it's all except SIMPLE).
268-
So, should second be default to generate simpler code from TS declarations?
276+
So, should the second be default to generate simpler code from TS declarations?
269277

270-
3. Actually right now we needs to know only is `kind === SIMPLE` or not, so should we simplify API?
278+
3. Actually, right now we needs to know only is `kind === SIMPLE` or not, so should we simplify API?
271279

272-
4. Unfortunately we can't use constants for `kind` parameter to make API better. Can we fix it somehow?
280+
4. Unfortunately, we can't use constants for `kind` parameter to make API better. Can we fix it somehow?
273281

274-
5. Will be nice to have the way to say that all declarations in this file is native. But how it fit with idea to replace `@native` with `external`?
282+
5. Will be nice to have the way to say that all declarations in this file are native. But how it fit with the idea to replace `@native` with `external`?

0 commit comments

Comments
 (0)