temperlang / temper

3 stars 0 forks source link

JS should escape keywords for identifiers as needed rather than dying #114

Closed tjpalmer closed 7 months ago

tjpalmer commented 7 months ago

JS often can cope with semi-keywords as identifiers, but minimally we could auto-rename to things like with_ rather than crashing.

PS C:\Users\work2\Documents\projects\temper> temper repl
$ let with = 5;
interactive#0: void
$ translate(0, "js")
Exception in thread "main" java.lang.IllegalArgumentException: -repl/i0000//chunk.temper+4-8: `with` is a JS reserved word
        at lang.temper.be.js.Js$Identifier.<init>(Js.kt:778)
        at lang.temper.be.js.JsTranslator.translateIdForLibrary(JsTranslator.kt:728)
        at lang.temper.be.js.JsTranslator.translateId(JsTranslator.kt:720)
        at lang.temper.be.js.JsTranslator.translateId$default(JsTranslator.kt:718)
        at lang.temper.be.js.JsTranslator.translateIdStrict(JsTranslator.kt:1369)
        at lang.temper.be.js.JsTranslator.translateTopLevel(JsTranslator.kt:1218)
        at lang.temper.be.js.JsTranslator.access$translateTopLevel(JsTranslator.kt:79)
        at lang.temper.be.js.JsTranslator$translate$1$2$1.invoke(JsTranslator.kt:139)
        at lang.temper.be.js.JsTranslator$translate$1$2$1.invoke(JsTranslator.kt:138)
        at lang.temper.be.js.JsTranslator.withDependencyMode(JsTranslator.kt:110)
        at lang.temper.be.js.JsTranslator.access$withDependencyMode(JsTranslator.kt:79)
        at lang.temper.be.js.JsTranslator$translate$1.invoke(JsTranslator.kt:138)
        at lang.temper.be.js.JsTranslator$translate$1.invoke(JsTranslator.kt:124)
        at lang.temper.be.js.JsNames.forOrigin(JsNames.kt:31)
        at lang.temper.be.js.JsTranslator.translate(JsTranslator.kt:124)
        at lang.temper.be.js.JsBackend.translate(JsBackend.kt:228)
jasvir commented 7 months ago

Just some background - in which phase different languages handle escaping varies and escaping reserved words in JS was PITA for the Caja project. We specifically found that different browsers treated escaped reserved words differently (egs include "default". Also ES5 unreserved some keywords from ES3 but that not all versions of browsers did (eg namespace). @tjpalmer's suggestion to add a suffix character will always work (depending on the suffix character) but merely escaping (for example the first character) will not.