Anyone knows what kind of checks the compiler perf...
# javascript
r
Anyone knows what kind of checks the compiler performs with "js" function (https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.js/js.html#js)? I'm experimenting and keep getting "JavaScript: syntax error" at compile time.
a
I think it just inlines js code. You can run whatever string you are inlining with any js checked. Maybe just paste in in a separate js file or scratch
r
the compiler definitely performs some checks
i can do
Copy code
js("blahblah")
and it compiles fine
but I can't do
Copy code
js("import")
because I have JavaScript:Syntax error
at compile time
a
Yep. I think it uses some kind of js syntax check during the compilation. I did not know about it, but it is good to have.
r
I can't do
Copy code
js("impor t")
because it gives JavaScript: missing ; before statement
I've tried to find the sources but its hard 🙂
s
It has to be valid JavaScript expression or a list of statements. If you want to look at sources, good place to start would be
parseJsCode
at
kotlin/js/js.translator/src/org/jetbrains/kotlin/js/translate/reference/CallExpressionTranslator.java
g
Do you use DCE? There is a bug in DCE that doesn't allow to use
import()
function: https://youtrack.jetbrains.com/issue/KT-28523
r
@Svyatoslav Kuzmich [JB] So why is
Copy code
js("blahblah")
ok and
Copy code
js("import")
not ok?
@gildor There is no DCE at this point - this is Kotlin compile-time error.
s
I’m not exactly sure, i’ll speculate that
import
is a reserved word and standalone
import;
would be a syntax error since ES5
r
I think this check is somewhere in here: js/js.parser/src/com/google/gwt/dev/js/rhino/Parser.java
but after looking at this package I have no idea what makes the "import" keyword so special 😉
s
com/google/gwt/dev/js/rhino/TokenStream.java:483
KEYWORDS.put("import", IMPORT);
r
probably just the fact it is not processed in the statementHelper method at all 🙂
@Svyatoslav Kuzmich [JB] Other keywords defined in the same place work fine. E.g.
Copy code
js("switch (d) { case 1: break}")
is correct.
s
js("switch")
does not work tho
r
but gives different error message
the statementHelper method in Parser.java is the key
import support is missing there
it's processed by the default: branch and reportError(ts, "msg.syntax"); is the result
maybe it's not too hard to fix 😉
But in the meantime i know it's not necessary at all to play with js("import") 😉
KT-28523 gives better alternative
And i hope the bug itself will be fixed soon
thx @Svyatoslav Kuzmich [JB] for help