Fork of daniellemaywood.uk/gleam — Wasm codegen work
2

Configure Feed

Select the types of activity you want to include in your feed.

JS: fix missing semicolons after some expressions in statement position

+43 -63
+9 -29
compiler-core/src/javascript/expression.rs
··· 296 296 } 297 297 298 298 pub fn expression(&mut self, expression: &'a TypedExpr) -> Document<'a> { 299 - let document = match expression { 299 + let mut document = match expression { 300 300 TypedExpr::String { value, .. } => string(value), 301 301 302 302 TypedExpr::Int { value, .. } => int(value), ··· 415 415 panic!("invalid expressions should not reach code generation") 416 416 } 417 417 }; 418 + if let Position::Statement = self.scope_position 419 + && expression_requires_semicolon(expression) 420 + { 421 + document = document.append(";"); 422 + } 418 423 if expression.handles_own_return() { 419 424 docvec![ 420 425 self.source_map_tracker(expression.location().start), ··· 803 808 } else { 804 809 let finally_doc = self.expression(finally); 805 810 documents.push(self.add_statement_level(finally_doc)); 806 - 807 - // Add a semicolon if needed, to ensure the pipeline is properly 808 - // delimited 809 - match &self.scope_position { 810 - Position::Statement if expression_requires_semicolon(finally) => { 811 - documents.push(";".to_doc()) 812 - } 813 - Position::Statement 814 - | Position::Expression(_) 815 - | Position::Tail 816 - | Position::Assign(_) => {} 817 - } 818 811 } 819 812 820 813 documents.to_doc().force_break() ··· 897 890 self.function_position = function_position; 898 891 self.scope_position = scope_position; 899 892 900 - if requires_semicolon(statement) { 901 - documents.push(";".to_doc()); 902 - } 903 893 documents.push(line()); 904 894 } else { 905 895 documents.push(self.statement(statement)); ··· 2822 2812 t.is_int() || t.is_float() || t.is_bool() || t.is_nil() || t.is_string() 2823 2813 } 2824 2814 2825 - fn requires_semicolon(statement: &TypedStatement) -> bool { 2826 - match statement { 2827 - Statement::Expression(expression) => expression_requires_semicolon(expression), 2828 - 2829 - Statement::Assignment(_) => false, 2830 - Statement::Use(_) => false, 2831 - Statement::Assert(_) => false, 2832 - } 2833 - } 2834 - 2835 2815 fn expression_requires_semicolon(expression: &TypedExpr) -> bool { 2836 2816 match expression { 2837 2817 TypedExpr::Int { .. } ··· 2850 2830 | TypedExpr::NegateBool { .. } 2851 2831 | TypedExpr::RecordAccess { .. } 2852 2832 | TypedExpr::PositionalAccess { .. } 2853 - | TypedExpr::ModuleSelect { .. } 2854 - | TypedExpr::Block { .. } => true, 2833 + | TypedExpr::ModuleSelect { .. } => true, 2855 2834 2856 2835 TypedExpr::Todo { .. } 2857 2836 | TypedExpr::Case { .. } 2858 2837 | TypedExpr::Panic { .. } 2859 2838 | TypedExpr::Pipeline { .. } 2860 2839 | TypedExpr::RecordUpdate { .. } 2861 - | TypedExpr::Invalid { .. } => false, 2840 + | TypedExpr::Invalid { .. } 2841 + | TypedExpr::Block { .. } => false, 2862 2842 } 2863 2843 } 2864 2844
+2 -2
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__bit_arrays__case_discard_sized.snap
··· 19 19 ----- COMPILED JAVASCRIPT 20 20 export function go(x) { 21 21 if (x.bitSize >= 16 && x.bitSize === 24) { 22 - 1 22 + 1; 23 23 } else { 24 - 2 24 + 2; 25 25 } 26 26 if (x.bitSize >= 16 && x.bitSize === 24) { 27 27 return 1;
+2 -2
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__bit_arrays__case_match_binary_size.snap
··· 23 23 export function go(x) { 24 24 if (x.bitSize >= 8 && x.bitSize === 24) { 25 25 let a = bitArraySlice(x, 8, 24); 26 - a 26 + a; 27 27 } else { 28 - x 28 + x; 29 29 } 30 30 if (x.bitSize >= 8 && x.bitSize === 24) { 31 31 let b = bitArraySlice(x, 8, 24);
+4 -4
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__blocks__blocks_whose_values_are_unused_do_not_generate_assignments.snap
··· 36 36 export function main() { 37 37 { 38 38 let x = 10; 39 - echo(x, undefined, "src/module.gleam", 5) 40 - }; 39 + echo(x, undefined, "src/module.gleam", 5); 40 + } 41 41 { 42 42 let a = 1; 43 43 let b = 2; 44 - a + b 45 - }; 44 + a + b; 45 + } 46 46 return undefined; 47 47 } 48 48
+2 -2
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__blocks__nested_multiexpr_non_ending_blocks.snap
··· 24 24 1; 25 25 { 26 26 2; 27 - 3 28 - }; 27 + 3; 28 + } 29 29 _block = 4; 30 30 } 31 31 let x = _block;
+1 -1
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__case__directly_matching_case_subject.snap
··· 24 24 let x = "ABC"; 25 25 let $ = true; 26 26 let x$1 = 79; 27 - 0 27 + 0; 28 28 return x; 29 29 }
+4 -4
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__case__semicolon_exists_with_case.snap
··· 19 19 let $1 = $[0]; 20 20 if ($1 === 2) { 21 21 let b = $[1]; 22 - b 22 + b; 23 23 } else if ($1 === 1) { 24 24 let $2 = $[1]; 25 25 if ($2 === 3) { 26 - 2 26 + 2; 27 27 } else { 28 28 let a = $1; 29 - a 29 + a; 30 30 } 31 31 } else { 32 32 let a = $1; 33 - a 33 + a; 34 34 } 35 35 return (2 + 4) * 2; 36 36 }
+1 -1
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__case__semicolon_exists_with_directly_matching_case.snap
··· 15 15 export function go() { 16 16 let $ = [1, 2]; 17 17 let a = $[0]; 18 - a 18 + a; 19 19 return (2 + 4) * 2; 20 20 }
+1 -1
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__case__single_clause_variables.snap
··· 18 18 let text = "first defined"; 19 19 let $ = "defined again"; 20 20 let text$1 = $; 21 - undefined 21 + undefined; 22 22 let text$2 = "a third time"; 23 23 return text$2; 24 24 }
+2 -2
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__inlining__inline_shadowed_variable.snap
··· 23 23 let b = 20; 24 24 { 25 25 let _inline_a_0 = 7; 26 - (a + b) + _inline_a_0 27 - }; 26 + (a + b) + _inline_a_0; 27 + } 28 28 return a; 29 29 }
+4 -4
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__inlining__inline_shadowed_variable_nested.snap
··· 25 25 let _inline_a_0 = 7; 26 26 { 27 27 let _inline_a_1 = 10; 28 - ((a + b) + _inline_a_0) - _inline_a_1 29 - }; 30 - _inline_a_0 31 - }; 28 + ((a + b) + _inline_a_0) - _inline_a_1; 29 + } 30 + _inline_a_0; 31 + } 32 32 return a; 33 33 }
+1 -1
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__inlining__inline_variable_shadowed_in_case_pattern.snap
··· 26 26 let $1 = 8; 27 27 let _inline_a_0 = $; 28 28 let _inline_b_1 = $1; 29 - (_inline_a_0 + _inline_b_1) + (a + b) 29 + (_inline_a_0 + _inline_b_1) + (a + b); 30 30 return a + b; 31 31 }
+2 -2
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__inlining__inline_variable_shadowing_parameter.snap
··· 18 18 export function sum(a, b) { 19 19 { 20 20 let _inline_a_0 = 7; 21 - (a + b) + _inline_a_0 22 - }; 21 + (a + b) + _inline_a_0; 22 + } 23 23 return a; 24 24 }
+8 -8
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__strings__string_prefix_utf16.snap
··· 33 33 let $ = "Θ wibble wobble"; 34 34 if ($.charCodeAt(0) === 920) { 35 35 let rest = $.slice(1); 36 - rest 36 + rest; 37 37 } else { 38 - "" 38 + ""; 39 39 } 40 40 let $1 = "🫥 is neutral dotted"; 41 41 if ($1.startsWith("🫥")) { 42 42 let rest = $1.slice(2); 43 - rest 43 + rest; 44 44 } else { 45 - "" 45 + ""; 46 46 } 47 47 let $2 = "🇺🇸 is a cluster"; 48 48 if ($2.startsWith("🇺🇸")) { 49 49 let rest = $2.slice(4); 50 - rest 50 + rest; 51 51 } else { 52 - "" 52 + ""; 53 53 } 54 54 let $3 = "\" is a an escaped quote"; 55 55 if ($3.charCodeAt(0) === 34) { 56 56 let rest = $3.slice(1); 57 - rest 57 + rest; 58 58 } else { 59 - "" 59 + ""; 60 60 } 61 61 let $4 = "\\ is a an escaped backslash"; 62 62 if ($4.charCodeAt(0) === 92) {