···308308 // Handled in collect_definitions
309309 Definition::CustomType(CustomType { .. }) => None,
310310311311+ // If a custom type is unused then we don't need to generate code for it
312312+ Definition::ModuleConstant(ModuleConstant { location, .. })
313313+ | Definition::Function(Function { location, .. })
314314+ if self
315315+ .module
316316+ .unused_definition_positions
317317+ .contains(&location.start) =>
318318+ {
319319+ None
320320+ }
321321+311322 Definition::ModuleConstant(ModuleConstant {
312323 publicity,
313324 name,
···413424 .definitions
414425 .iter()
415426 .flat_map(|statement| match statement {
427427+ // If a custom type is unused then we don't need to generate code for it
428428+ Definition::CustomType(CustomType { location, .. })
429429+ if self
430430+ .module
431431+ .unused_definition_positions
432432+ .contains(&location.start) =>
433433+ {
434434+ vec![]
435435+ }
436436+416437 Definition::CustomType(CustomType {
417438 publicity,
418439 constructors,
···474474 assert_js!(
475475 ("package", "other_module", "pub type T { A }"),
476476 r#"import other_module.{A as B}
477477-fn func() {
477477+pub fn func() {
478478 case B {
479479 x if x == B -> True
480480 _ -> False
···491491pub type X {
492492 Ok
493493}
494494-fn func() {
494494+pub fn func() {
495495 case Y {
496496 y if y == Y -> True
497497 _ -> False
···508508pub type X {
509509 Ok
510510}
511511-fn func(x) {
511511+pub fn func(x) {
512512 case gleam.Ok {
513513 _ if [] == [ gleam.Ok ] -> True
514514 _ -> False
···522522#[test]
523523fn constructor_function_in_guard() {
524524 assert_js!(
525525- r#"fn func(x) {
525525+ r#"pub fn func(x) {
526526 case [] {
527527 _ if [] == [ Ok ] -> True
528528 _ -> False
···6060 ("rocket_ship", r#"pub const x = 1"#),
6161 r#"
6262import rocket_ship.{ x as y }
6363-const z = y
6363+pub const z = y
6464"#,
6565 );
6666}
···7171 ("x", r#"pub const v = 1"#),
7272 r#"
7373import x as y
7474-const z = y.v
7474+pub const z = y.v
7575"#,
7676 );
7777}
···11---
22source: compiler-core/src/javascript/tests/assignments.rs
33assertion_line: 37
44-expression: "\n\nfn go(x, wibble) {\n let a = 1\n wibble(a)\n let a = 2\n wibble(a)\n let assert #(a, 3) = x\n let b = a\n wibble(b)\n let c = {\n let a = a\n #(a, b)\n }\n wibble(a)\n // make sure arguments are counted in initial state\n let x = c\n x\n}\n"
44+expression: "\n\npub fn go(x, wibble) {\n let a = 1\n wibble(a)\n let a = 2\n wibble(a)\n let assert #(a, 3) = x\n let b = a\n wibble(b)\n let c = {\n let a = a\n #(a, b)\n }\n wibble(a)\n // make sure arguments are counted in initial state\n let x = c\n x\n}\n"
55snapshot_kind: text
66---
77----- SOURCE CODE
88991010-fn go(x, wibble) {
1010+pub fn go(x, wibble) {
1111 let a = 1
1212 wibble(a)
1313 let a = 2
···31313232const FILEPATH = "src/module.gleam";
33333434-function go(x, wibble) {
3434+export function go(x, wibble) {
3535 let a = 1;
3636 wibble(a);
3737 let a$1 = 2;
···4444 8,
4545 "go",
4646 "Pattern match failed, no pattern matched the value.",
4747- { value: x, start: 71, end: 93, pattern_start: 82, pattern_end: 89 }
4747+ { value: x, start: 75, end: 97, pattern_start: 86, pattern_end: 93 }
4848 )
4949 }
5050 let a$2 = x[0];
···11---
22source: compiler-core/src/javascript/tests/bit_arrays.rs
33-expression: "\nfn go(x) {\n let n = 3\n case x {\n <<a:bytes-size(n)>> -> a\n _ -> x\n }\n}\n"
33+assertion_line: 1090
44+expression: "\npub fn go(x) {\n let n = 3\n case x {\n <<a:bytes-size(n)>> -> a\n _ -> x\n }\n}\n"
55+snapshot_kind: text
46---
57----- SOURCE CODE
6877-fn go(x) {
99+pub fn go(x) {
810 let n = 3
911 case x {
1012 <<a:bytes-size(n)>> -> a
···1618----- COMPILED JAVASCRIPT
1719import { bitArraySlice } from "../gleam.mjs";
18201919-function go(x) {
2121+export function go(x) {
2022 let n = 3;
2123 if (n >= 0 && x.bitSize === n * 8) {
2224 let a = bitArraySlice(x, 0, n * 8);
···11---
22source: compiler-core/src/javascript/tests/bit_arrays.rs
33-expression: "\nfn go(x) {\n let n = 16\n case x {\n <<a:size(n)>> -> a\n _ -> 1\n }\n}\n"
33+assertion_line: 951
44+expression: "\npub fn go(x) {\n let n = 16\n case x {\n <<a:size(n)>> -> a\n _ -> 1\n }\n}\n"
55+snapshot_kind: text
46---
57----- SOURCE CODE
6877-fn go(x) {
99+pub fn go(x) {
810 let n = 16
911 case x {
1012 <<a:size(n)>> -> a
···1618----- COMPILED JAVASCRIPT
1719import { bitArraySliceToInt } from "../gleam.mjs";
18201919-function go(x) {
2121+export function go(x) {
2022 let n = 16;
2123 if (n >= 0 && x.bitSize === n) {
2224 let a = bitArraySliceToInt(x, 0, n, true, false);
···11---
22source: compiler-core/src/javascript/tests/bit_arrays.rs
33-expression: "\nfn go(x) {\n let n = 8\n case x {\n <<a:size(n), 0b010101:size(8)>> -> a\n _ -> 1\n }\n}\n"
33+assertion_line: 1036
44+expression: "\npub fn go(x) {\n let n = 8\n case x {\n <<a:size(n), 0b010101:size(8)>> -> a\n _ -> 1\n }\n}\n"
55+snapshot_kind: text
46---
57----- SOURCE CODE
6877-fn go(x) {
99+pub fn go(x) {
810 let n = 8
911 case x {
1012 <<a:size(n), 0b010101:size(8)>> -> a
···1618----- COMPILED JAVASCRIPT
1719import { bitArraySliceToInt } from "../gleam.mjs";
18201919-function go(x) {
2121+export function go(x) {
2022 let n = 8;
2123 if (
2224 n >= 0 &&
···11---
22source: compiler-core/src/javascript/tests/bit_arrays.rs
33-expression: "\nfn go(x) {\n let n = 16\n let n = 5\n case x {\n <<a:size(n)>> -> a\n _ -> 1\n }\n}\n"
33+assertion_line: 1008
44+expression: "\npub fn go(x) {\n let n = 16\n let n = 5\n case x {\n <<a:size(n)>> -> a\n _ -> 1\n }\n}\n"
55+snapshot_kind: text
46---
57----- SOURCE CODE
6877-fn go(x) {
99+pub fn go(x) {
810 let n = 16
911 let n = 5
1012 case x {
···1719----- COMPILED JAVASCRIPT
1820import { bitArraySliceToInt } from "../gleam.mjs";
19212020-function go(x) {
2222+export function go(x) {
2123 let n = 16;
2224 let n$1 = 5;
2325 if (n$1 >= 0 && x.bitSize === n$1) {
···11---
22source: compiler-core/src/javascript/tests/bit_arrays.rs
33-expression: "\nfn go(x) {\n let n = 16\n let m = 32\n case x {\n <<first:size(8), a:size(n), b:size(m), rest:bits>> -> first + a + b\n _ -> 1\n }\n}\n"
33+assertion_line: 979
44+expression: "\npub fn go(x) {\n let n = 16\n let m = 32\n case x {\n <<first:size(8), a:size(n), b:size(m), rest:bits>> -> first + a + b\n _ -> 1\n }\n}\n"
55+snapshot_kind: text
46---
57----- SOURCE CODE
6877-fn go(x) {
99+pub fn go(x) {
810 let n = 16
911 let m = 32
1012 case x {
···1719----- COMPILED JAVASCRIPT
1820import { bitArraySlice, bitArraySliceToInt } from "../gleam.mjs";
19212020-function go(x) {
2222+export function go(x) {
2123 let n = 16;
2224 let m = 32;
2325 if (
···11---
22source: compiler-core/src/javascript/tests/bit_arrays.rs
33-expression: "\nfn go(x) {\n let n = -10\n case x {\n <<int:size(n)>> -> int\n _ -> 2\n }\n}\n"
33+assertion_line: 1962
44+expression: "\npub fn go(x) {\n let n = -10\n case x {\n <<int:size(n)>> -> int\n _ -> 2\n }\n}\n"
55+snapshot_kind: text
46---
57----- SOURCE CODE
6877-fn go(x) {
99+pub fn go(x) {
810 let n = -10
911 case x {
1012 <<int:size(n)>> -> int
···1618----- COMPILED JAVASCRIPT
1719import { bitArraySliceToInt } from "../gleam.mjs";
18201919-function go(x) {
2121+export function go(x) {
2022 let n = -10;
2123 if (n >= 0 && x.bitSize === n) {
2224 let int = bitArraySliceToInt(x, 0, n, true, false);
···11---
22source: compiler-core/src/javascript/tests/blocks.rs
33-expression: "\nfn b() {\n let x = 1\n {\n let x = 2\n x + 1\n }\n}\n"
33+assertion_line: 236
44+expression: "\npub fn b() {\n let x = 1\n {\n let x = 2\n x + 1\n }\n}\n"
55+snapshot_kind: text
46---
57----- SOURCE CODE
6877-fn b() {
99+pub fn b() {
810 let x = 1
911 {
1012 let x = 2
···141615171618----- COMPILED JAVASCRIPT
1717-function b() {
1919+export function b() {
1820 let x = 1;
1921 {
2022 let x$1 = 2;
···11---
22source: compiler-core/src/javascript/tests/blocks.rs
33-expression: "\nfn b() {\n let x = 1\n {\n let x = x\n }\n}\n"
33+assertion_line: 251
44+expression: "\npub fn b() {\n let x = 1\n {\n let x = x\n }\n}\n"
55+snapshot_kind: text
46---
57----- SOURCE CODE
6877-fn b() {
99+pub fn b() {
810 let x = 1
911 {
1012 let x = x
···131514161517----- COMPILED JAVASCRIPT
1616-function b() {
1818+export function b() {
1719 let x = 1;
1820 return x;
1921}
···11---
22source: compiler-core/src/javascript/tests/bools.rs
33-expression: "\nconst a = True\nconst b = False\nconst c = Nil\n"
33+assertion_line: 18
44+expression: "\npub const a = True\npub const b = False\npub const c = Nil\n"
55+snapshot_kind: text
46---
57----- SOURCE CODE
6877-const a = True
88-const b = False
99-const c = Nil
99+pub const a = True
1010+pub const b = False
1111+pub const c = Nil
101211131214----- COMPILED JAVASCRIPT
1313-const a = true;
1515+export const a = true;
14161515-const b = false;
1717+export const b = false;
16181717-const c = undefined;
1919+export const c = undefined;
···11---
22source: compiler-core/src/javascript/tests/bools.rs
33-expression: "\nfn go(a, b) {\n a == True\n a != True\n a == False\n a != False\n a == a\n a != a\n b == Nil\n b != Nil\n b == b\n}\n"
33+assertion_line: 95
44+expression: "\npub fn go(a, b) {\n a == True\n a != True\n a == False\n a != False\n a == a\n a != a\n b == Nil\n b != Nil\n b == b\n}\n"
55+snapshot_kind: text
46---
57----- SOURCE CODE
6877-fn go(a, b) {
99+pub fn go(a, b) {
810 a == True
911 a != True
1012 a == False
···182019212022----- COMPILED JAVASCRIPT
2121-function go(a, b) {
2323+export function go(a, b) {
2224 a === true;
2325 a !== true;
2426 a === false;
···11---
22source: compiler-core/src/javascript/tests/case.rs
33-expression: "\nfn go(x) {\n case x {\n // We want `a` to be defined before the guard check, and\n // `b` to be defined only if the predicate on a matches!\n [a, b] if a == 1 -> a + b\n _ -> 2\n }\n}\n"
33+assertion_line: 34
44+expression: "\npub fn go(x) {\n case x {\n // We want `a` to be defined before the guard check, and\n // `b` to be defined only if the predicate on a matches!\n [a, b] if a == 1 -> a + b\n _ -> 2\n }\n}\n"
55+snapshot_kind: text
46---
57----- SOURCE CODE
6877-fn go(x) {
99+pub fn go(x) {
810 case x {
911 // We want `a` to be defined before the guard check, and
1012 // `b` to be defined only if the predicate on a matches!
···1719----- COMPILED JAVASCRIPT
1820import { Empty as $Empty } from "../gleam.mjs";
19212020-function go(x) {
2222+export function go(x) {
2123 if (x instanceof $Empty) {
2224 return 2;
2325 } else {
···11---
22source: compiler-core/src/javascript/tests/case_clause_guards.rs
33-expression: "import other_module.{A as B}\nfn func() {\n case B {\n x if x == B -> True\n _ -> False\n }\n}\n"
33+assertion_line: 474
44+expression: "import other_module.{A as B}\npub fn func() {\n case B {\n x if x == B -> True\n _ -> False\n }\n}\n"
55+snapshot_kind: text
46---
57----- SOURCE CODE
68import other_module.{A as B}
77-fn func() {
99+pub fn func() {
810 case B {
911 x if x == B -> True
1012 _ -> False
···1719import { A as B } from "../../package/other_module.mjs";
1820import { isEqual } from "../gleam.mjs";
19212020-function func() {
2222+export function func() {
2123 let $ = new B();
2224 let x = $;
2325 if (isEqual(x, new B())) {
···11---
22source: compiler-core/src/javascript/tests/case_clause_guards.rs
33-expression: "import gleam.{Ok as Y}\npub type X {\n Ok\n}\nfn func() {\n case Y {\n y if y == Y -> True\n _ -> False\n }\n}\n"
33+assertion_line: 489
44+expression: "import gleam.{Ok as Y}\npub type X {\n Ok\n}\npub fn func() {\n case Y {\n y if y == Y -> True\n _ -> False\n }\n}\n"
55+snapshot_kind: text
46---
57----- SOURCE CODE
68import gleam.{Ok as Y}
79pub type X {
810 Ok
911}
1010-fn func() {
1212+pub fn func() {
1113 case Y {
1214 y if y == Y -> True
1315 _ -> False
···21232224export class Ok extends $CustomType {}
23252424-function func() {
2626+export function func() {
2527 let $ = (var0) => { return new Y(var0); };
2628 let y = $;
2729 if (isEqual(y, (var0) => { return new Y(var0); })) {
···11---
22source: compiler-core/src/javascript/tests/custom_types.rs
33-expression: "import other.{Two}\nconst a = Two\n"
33+assertion_line: 114
44+expression: "import other.{Two}\npub const a = Two\n"
55+snapshot_kind: text
46---
57----- SOURCE CODE
68-- other.gleam
···810911-- main.gleam
1012import other.{Two}
1111-const a = Two
1313+pub const a = Two
121413151416----- COMPILED JAVASCRIPT
1517import * as $other from "../other.mjs";
1618import { Two } from "../other.mjs";
17191818-const a = /* @__PURE__ */ new Two();
2020+export const a = /* @__PURE__ */ new Two();
···11---
22source: compiler-core/src/javascript/tests/modules.rs
33-expression: "\nimport rocket_ship.{ x as y }\nconst z = y\n"
33+assertion_line: 59
44+expression: "\nimport rocket_ship.{ x as y }\npub const z = y\n"
55+snapshot_kind: text
46---
57----- SOURCE CODE
68-- rocket_ship.gleam
···911-- main.gleam
10121113import rocket_ship.{ x as y }
1212-const z = y
1414+pub const z = y
131514161517----- COMPILED JAVASCRIPT
1618import * as $rocket_ship from "../rocket_ship.mjs";
1719import { x as y } from "../rocket_ship.mjs";
18201919-const z = y;
2121+export const z = y;
···11---
22source: compiler-core/src/javascript/tests/modules.rs
33-expression: "\nimport x as y\nconst z = y.v\n"
33+assertion_line: 70
44+expression: "\nimport x as y\npub const z = y.v\n"
55+snapshot_kind: text
46---
57----- SOURCE CODE
68-- x.gleam
···911-- main.gleam
10121113import x as y
1212-const z = y.v
1414+pub const z = y.v
131514161517----- COMPILED JAVASCRIPT
1618import * as $y from "../x.mjs";
17191818-const z = $y.v;
2020+export const z = $y.v;
···11---
22source: compiler-core/src/javascript/tests/numbers.rs
33-expression: "\nfn go(y) {\n let x = 1.0\n x == y\n}\n"
33+assertion_line: 193
44+expression: "\npub fn go(y) {\n let x = 1.0\n x == y\n}\n"
55+snapshot_kind: text
46---
57----- SOURCE CODE
6877-fn go(y) {
99+pub fn go(y) {
810 let x = 1.0
911 x == y
1012}
111312141315----- COMPILED JAVASCRIPT
1414-function go(y) {
1616+export function go(y) {
1517 let x = 1.0;
1618 return x === y;
1719}
···11---
22source: compiler-core/src/javascript/tests/numbers.rs
33-expression: "\nfn go(y) {\n let x = 1\n x == y\n}\n"
33+assertion_line: 169
44+expression: "\npub fn go(y) {\n let x = 1\n x == y\n}\n"
55+snapshot_kind: text
46---
57----- SOURCE CODE
6877-fn go(y) {
99+pub fn go(y) {
810 let x = 1
911 x == y
1012}
111312141315----- COMPILED JAVASCRIPT
1414-function go(y) {
1616+export function go(y) {
1517 let x = 1;
1618 return x === y;
1719}
···11---
22source: compiler-core/src/javascript/tests/numbers.rs
33-expression: "\nfn go() {\n let a = 3\n let b = -a\n}\n"
33+assertion_line: 227
44+expression: "\npub fn go() {\n let a = 3\n let b = -a\n}\n"
55+snapshot_kind: text
46---
57----- SOURCE CODE
6877-fn go() {
99+pub fn go() {
810 let a = 3
911 let b = -a
1012}
111312141315----- COMPILED JAVASCRIPT
1414-function go() {
1616+export function go() {
1517 let a = 3;
1618 let b = - a;
1719 return b;
···11---
22source: compiler-core/src/javascript/tests/numbers.rs
33-expression: "\nfn go() {\n let a = 3\n let b = --a\n}\n"
33+assertion_line: 239
44+expression: "\npub fn go() {\n let a = 3\n let b = --a\n}\n"
55+snapshot_kind: text
46---
57----- SOURCE CODE
6877-fn go() {
99+pub fn go() {
810 let a = 3
911 let b = --a
1012}
111312141315----- COMPILED JAVASCRIPT
1414-function go() {
1616+export function go() {
1517 let a = 3;
1618 let b = - - a;
1719 return b;
···11---
22source: compiler-core/src/javascript/tests/todo.rs
33assertion_line: 51
44-expression: "\nfn go(f) {\n let boop = todo as \"I should do this\"\n f(todo as \"Boom\")\n}\n"
44+expression: "\npub fn go(f) {\n let boop = todo as \"I should do this\"\n f(todo as \"Boom\")\n}\n"
55snapshot_kind: text
66---
77----- SOURCE CODE
8899-fn go(f) {
99+pub fn go(f) {
1010 let boop = todo as "I should do this"
1111 f(todo as "Boom")
1212}
···17171818const FILEPATH = "src/module.gleam";
19192020-function go(f) {
2020+export function go(f) {
2121 let _block;
2222 throw makeError("todo", FILEPATH, "my/mod", 3, "go", "I should do this", {})
2323 let boop = _block;
···11---
22source: compiler-core/src/javascript/tests/todo.rs
33assertion_line: 27
44-expression: "\nfn go() {\n todo as \"I should do this\"\n}\n"
44+expression: "\npub fn go() {\n todo as \"I should do this\"\n}\n"
55snapshot_kind: text
66---
77----- SOURCE CODE
8899-fn go() {
99+pub fn go() {
1010 todo as "I should do this"
1111}
1212···16161717const FILEPATH = "src/module.gleam";
18181919-function go() {
1919+export function go() {
2020 throw makeError("todo", FILEPATH, "my/mod", 3, "go", "I should do this", {})
2121}
···11---
22source: compiler-core/src/javascript/tests/todo.rs
33assertion_line: 38
44-expression: "\nfn go() {\n let x = \"I should \" <> \"do this\"\n todo as x\n}\n"
44+expression: "\npub fn go() {\n let x = \"I should \" <> \"do this\"\n todo as x\n}\n"
55snapshot_kind: text
66---
77----- SOURCE CODE
8899-fn go() {
99+pub fn go() {
1010 let x = "I should " <> "do this"
1111 todo as x
1212}
···17171818const FILEPATH = "src/module.gleam";
19192020-function go() {
2020+export function go() {
2121 let x = "I should " + "do this";
2222 throw makeError("todo", FILEPATH, "my/mod", 4, "go", x, {})
2323}
···44fn without_message() {
55 assert_js!(
66 r#"
77-fn go() {
77+pub fn go() {
88 todo
99}
1010"#,
···2626fn with_message() {
2727 assert_js!(
2828 r#"
2929-fn go() {
2929+pub fn go() {
3030 todo as "I should do this"
3131}
3232"#,
···3737fn with_message_expr() {
3838 assert_js!(
3939 r#"
4040-fn go() {
4040+pub fn go() {
4141 let x = "I should " <> "do this"
4242 todo as x
4343}
···5050fn as_expression() {
5151 assert_js!(
5252 r#"
5353-fn go(f) {
5353+pub fn go(f) {
5454 let boop = todo as "I should do this"
5555 f(todo as "Boom")
5656}