Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -413,3 +413,6 @@
updates if the constructor definition has a positional field defined after
labelled fields.
([Hari Mohan](https://gituhub.com/seafoamteal))

- Fixed invalid JavaScript code generation when ints or floats had prefixed `0`s
before and after an `_` (e.g. `000_001`).
9 changes: 2 additions & 7 deletions compiler-core/src/javascript/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2464,16 +2464,11 @@ pub fn eco_string_int<'a>(value: EcoString) -> Document<'a> {
value
};

let value = value.trim_start_matches('0');
let value = value.trim_start_matches(['0', '_']);
if value.is_empty() {
out.push('0');
}

// If the number starts with a `0` then an underscore, the `0` will be stripped,
// leaving the number to look something like `_1_2_3`, which is not valid syntax.
// Therefore, we strip the `_` to avoid this case.
let value = value.trim_start_matches('_');

out.push_str(value);

out.to_doc()
Expand All @@ -2489,7 +2484,7 @@ pub fn float(value: &str) -> Document<'_> {
};
let value = value.trim_start_matches(['+', '-'].as_ref());

let value = value.trim_start_matches('0');
let value = value.trim_start_matches(['0', '_']);
if value.starts_with(['.', 'e', 'E']) {
out.push('0');
}
Expand Down
68 changes: 68 additions & 0 deletions compiler-core/src/javascript/tests/numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,18 @@ pub fn main() {
);
}

// https://github.com/gleam-lang/gleam/issues/5459
#[test]
fn many_preceeding_zeros_int() {
assert_js!(
r#"
pub fn main() {
0000_000_00_9_179
}
"#
);
}

// https://github.com/gleam-lang/gleam/issues/2412
#[test]
fn preceeding_zeros_float() {
Expand All @@ -268,6 +280,18 @@ pub fn main() {
);
}

// https://github.com/gleam-lang/gleam/issues/5459
#[test]
fn many_preceeding_zeros_float() {
assert_js!(
r#"
pub fn main() {
0000_000_00_9_179.1
}
"#
);
}

// https://github.com/gleam-lang/gleam/issues/2412
#[test]
fn preceeding_zeros_int_const() {
Expand All @@ -278,6 +302,16 @@ pub const x = 09_179
);
}

// https://github.com/gleam-lang/gleam/issues/5459
#[test]
fn many_preceeding_zeros_int_const() {
assert_js!(
r#"
pub const x = 0000_000_00_9_179
"#
);
}

// https://github.com/gleam-lang/gleam/issues/2412
#[test]
fn preceeding_zeros_float_const() {
Expand All @@ -288,6 +322,16 @@ pub const x = 09_179.1
);
}

// https://github.com/gleam-lang/gleam/issues/5459
#[test]
fn many_preceeding_zeros_float_const() {
assert_js!(
r#"
pub const x = 0000_000_00_9_179.1
"#
);
}

// https://github.com/gleam-lang/gleam/issues/2412
#[test]
fn preceeding_zeros_int_pattern() {
Expand All @@ -300,6 +344,18 @@ pub fn main(x) {
);
}

// https://github.com/gleam-lang/gleam/issues/5459
#[test]
fn many_preceeding_zeros_int_pattern() {
assert_js!(
r#"
pub fn main(x) {
let assert 0000_000_00_9_179 = x
}
"#
);
}

// https://github.com/gleam-lang/gleam/issues/2412
#[test]
fn preceeding_zeros_float_pattern() {
Expand All @@ -312,6 +368,18 @@ pub fn main(x) {
);
}

// https://github.com/gleam-lang/gleam/issues/5459
#[test]
fn many_preceeding_zeros_float_pattern() {
assert_js!(
r#"
pub fn main(x) {
let assert 0000_000_00_9_179.1 = x
}
"#
);
}

// https://github.com/gleam-lang/gleam/issues/4459
#[test]
fn underscore_after_hexadecimal_prefix() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: compiler-core/src/javascript/tests/numbers.rs
expression: "\npub fn main() {\n 0000_000_00_9_179.1\n}\n"
---
----- SOURCE CODE

pub fn main() {
0000_000_00_9_179.1
}


----- COMPILED JAVASCRIPT
export function main() {
return 9179.1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: compiler-core/src/javascript/tests/numbers.rs
expression: "\npub const x = 0000_000_00_9_179.1\n"
---
----- SOURCE CODE

pub const x = 0000_000_00_9_179.1


----- COMPILED JAVASCRIPT
export const x = 9_179.1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
source: compiler-core/src/javascript/tests/numbers.rs
expression: "\npub fn main(x) {\n let assert 0000_000_00_9_179.1 = x\n}\n"
---
----- SOURCE CODE

pub fn main(x) {
let assert 0000_000_00_9_179.1 = x
}


----- COMPILED JAVASCRIPT
import { makeError } from "../gleam.mjs";

const FILEPATH = "src/module.gleam";

export function main(x) {
if (!(x === 9179.1)) {
throw makeError(
"let_assert",
FILEPATH,
"my/mod",
3,
"main",
"Pattern match failed, no pattern matched the value.",
{ value: x, start: 20, end: 54, pattern_start: 31, pattern_end: 50 }
)
}
return x;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: compiler-core/src/javascript/tests/numbers.rs
expression: "\npub fn main() {\n 0000_000_00_9_179\n}\n"
---
----- SOURCE CODE

pub fn main() {
0000_000_00_9_179
}


----- COMPILED JAVASCRIPT
export function main() {
return 9_179;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: compiler-core/src/javascript/tests/numbers.rs
expression: "\npub const x = 0000_000_00_9_179\n"
---
----- SOURCE CODE

pub const x = 0000_000_00_9_179


----- COMPILED JAVASCRIPT
export const x = 9_179;
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
source: compiler-core/src/javascript/tests/numbers.rs
expression: "\npub fn main(x) {\n let assert 0000_000_00_9_179 = x\n}\n"
---
----- SOURCE CODE

pub fn main(x) {
let assert 0000_000_00_9_179 = x
}


----- COMPILED JAVASCRIPT
import { makeError } from "../gleam.mjs";

const FILEPATH = "src/module.gleam";

export function main(x) {
if (!(x === 9179)) {
throw makeError(
"let_assert",
FILEPATH,
"my/mod",
3,
"main",
"Pattern match failed, no pattern matched the value.",
{ value: x, start: 20, end: 52, pattern_start: 31, pattern_end: 48 }
)
}
return x;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ pub fn main() {

----- COMPILED JAVASCRIPT
export function main() {
return 0b0_1_0_1;
return 0b1_0_1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ pub fn main() {

----- COMPILED JAVASCRIPT
export function main() {
return 0x0_1_2_3;
return 0x1_2_3;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ pub fn main() {

----- COMPILED JAVASCRIPT
export function main() {
return 0o0_1_2_3;
return 0o1_2_3;
}
Loading