From 1184705bd916fbaa02a4cf1c8dad1bca097e6f68 Mon Sep 17 00:00:00 2001 From: Marcio Cruz de Almeida <67694075+marciocadev@users.noreply.github.com> Date: Thu, 24 Aug 2023 18:55:12 -0300 Subject: [PATCH] fix(compiler): error when more than one space is placed between `if` and `let` (#3960) @MarkMcCulloh, your review on PR #3888 made me realize that if I put more than one space between `if let`, it generates errors. I've updated the `if let` to work even with extra spaces. Closes #3959 ## Checklist - [x] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted) - [x] Description explains motivation and solution - [x] Tests added (always) - [ ] Docs updated (only required for features) - [ ] Added `pr/e2e-full` label if this feature requires end-to-end testing *By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*. --- examples/tests/valid/optionals.w | 6 ++++++ libs/tree-sitter-wing/grammar.js | 3 ++- .../test_corpus/valid/optionals.w_compile_tf-aws.md | 8 ++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/examples/tests/valid/optionals.w b/examples/tests/valid/optionals.w index 0ca09eb0423..deacf2a1216 100644 --- a/examples/tests/valid/optionals.w +++ b/examples/tests/valid/optionals.w @@ -66,6 +66,12 @@ if let var z = a { assert(z == 2); } +// extra space between if and let +let b: num? = 1; +if let z = b { + assert(z == 1); +} + // Nested if lets if let parsedName = tryParseName("Good Name") { assert(parsedName.first == "Good"); diff --git a/libs/tree-sitter-wing/grammar.js b/libs/tree-sitter-wing/grammar.js index c9d8ae40ad5..094b038aa9a 100644 --- a/libs/tree-sitter-wing/grammar.js +++ b/libs/tree-sitter-wing/grammar.js @@ -274,7 +274,8 @@ module.exports = grammar({ if_let_statement: ($) => seq( - "if let", + "if", + "let", optional(field("reassignable", $.reassignable)), field("name", $.identifier), "=", diff --git a/tools/hangar/__snapshots__/test_corpus/valid/optionals.w_compile_tf-aws.md b/tools/hangar/__snapshots__/test_corpus/valid/optionals.w_compile_tf-aws.md index 4fb5ec1e07c..05ceea75c62 100644 --- a/tools/hangar/__snapshots__/test_corpus/valid/optionals.w_compile_tf-aws.md +++ b/tools/hangar/__snapshots__/test_corpus/valid/optionals.w_compile_tf-aws.md @@ -476,6 +476,14 @@ class $Root extends $stdlib.std.Resource { {((cond) => {if (!cond) throw new Error("assertion failed: z == 2")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(z,2)))}; } } + const b = 1; + { + const $IF_LET_VALUE = b; + if ($IF_LET_VALUE != undefined) { + const z = $IF_LET_VALUE; + {((cond) => {if (!cond) throw new Error("assertion failed: z == 1")})((((a,b) => { try { return require('assert').deepStrictEqual(a,b) === undefined; } catch { return false; } })(z,1)))}; + } + } { const $IF_LET_VALUE = (tryParseName("Good Name")); if ($IF_LET_VALUE != undefined) {