gleam-lang / gleam

⭐️ A friendly language for building type-safe, scalable systems!
https://gleam.run
Apache License 2.0
17.25k stars 715 forks source link

Variables in constants not being escaped on JS #3004

Closed PgBiel closed 4 months ago

PgBiel commented 4 months ago

The code below

import gleam/io

pub const class = 5
pub const oh_dear = class

pub fn main() {
  io.println("Hello from mongus!")
}

compiles to

import * as $io from "../gleam_stdlib/gleam/io.mjs";

export function main() {
  return $io.println("Hello from mongus!");
}

export const class$ = 5;

export const oh_dear = class;

which errors at runtime with

export const oh_dear = class;
                            ^

SyntaxError: Unexpected token ';'
    at ModuleLoader.moduleStrategy (node:internal/modules/esm/translators:167:18)
    at callTranslator (node:internal/modules/esm/loader:285:14)
    at ModuleLoader.moduleProvider (node:internal/modules/esm/loader:291:30)

Node.js v20.11.1

due to a missing call to maybe_escape_identifier_doc at

https://github.com/gleam-lang/gleam/blob/99c50dde27f6146a8dbbd6963a74a7dd4fa70ee3/compiler-core/src/javascript/expression.rs#L1289

PgBiel commented 4 months ago

Turns out the line immediately below is also missing an escape, leading to mismatching import and export.

https://github.com/gleam-lang/gleam/blob/99c50dde27f6146a8dbbd6963a74a7dd4fa70ee3/compiler-core/src/javascript/expression.rs#L1290

PgBiel commented 4 months ago

Here as well (this one was hard to find):

https://github.com/gleam-lang/gleam/blob/99c50dde27f6146a8dbbd6963a74a7dd4fa70ee3/compiler-core/src/javascript/expression.rs#L1233