Skip to content

Commit

Permalink
feat!: replace elif with else if (#6976)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chriscbr authored Aug 2, 2024
1 parent 524796c commit 35799ca
Show file tree
Hide file tree
Showing 22 changed files with 212 additions and 198 deletions.
8 changes: 4 additions & 4 deletions docs/api/03-language/04-flow-controls.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,19 @@ for value in 1..=5 {
}
```

### If elif else
### If / else if / else

```ts playground example
let grade = (score: num): str => {
// Parentheses are optional in conditions.
// However, curly braces are required in `if/else` statements.
if 0 < score && score < 55 {
return "F";
} elif 55 <= score && score < 65 {
} else if 55 <= score && score < 65 {
return "C";
} elif 65 <= score && score < 75 {
} else if 65 <= score && score < 75 {
return "B";
} elif 75 <= score && score <= 100 {
} else if 75 <= score && score <= 100 {
return "A";
} else {
return "Invalid grade";
Expand Down
6 changes: 3 additions & 3 deletions docs/api/05-language-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1284,16 +1284,16 @@ includes for and while loops currently.
### 2.5 if
Flow control can be done with `if/elif/else` statements.
The **if** statement is optionally followed by **elif** and **else**.
Flow control can be done with `if/else if/else` statements.
The **if** statement is optionally followed by **else if** and **else**.
> ```TS
> // Wing program:
> let x = 1;
> let y = "sample";
> if x == 2 {
> log("x is 2");
> } elif y != "sample" {
> } else if y != "sample" {
> log("y is not sample");
> } else {
> log("x is 1 and y is sample");
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/tests/sdk_tests/bucket/signed_url.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ test "signedUrl duration option is respected" {

if target == "tf-aws" {
result = output.contains("<Code>AccessDenied</Code><Message>Request has expired</Message>");
} elif target == "tf-gcp" {
} else if target == "tf-gcp" {
result = output.contains("<Code>ExpiredToken</Code><Message>Invalid argument.</Message>");
}

Expand All @@ -95,4 +95,4 @@ test "signedUrl duration option is respected" {
let output = util.shell("curl \"{getSignedUrl}\"");

expect.equal(isExpiredTokenError(output), true);
}
}
2 changes: 1 addition & 1 deletion examples/tests/sdk_tests/function/aws-function.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ new std.Test(inflight () => {
assert(info.get("functionArn").contains(":function:"));
assert(info.get("functionArn").contains("aws-wing-function"));
assert(info.get("functionName").contains("aws-wing-function"));
} elif target == "awscdk" {
} else if target == "awscdk" {
assert(info.get("functionArn").contains("arn:aws:lambda:"));
assert(info.get("functionArn").contains(":function:"));
assert(info.get("functionArn").contains("awswingfunction"));
Expand Down
4 changes: 2 additions & 2 deletions examples/tests/valid/dynamo.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ class DynamoTable {
inflight _attributeTypeToString(type: AttributeType): str {
if type == AttributeType.String {
return "S";
} elif type == AttributeType.Number {
} else if type == AttributeType.Number {
return "N";
} elif type == AttributeType.Binary {
} else if type == AttributeType.Binary {
return "B";
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/tests/valid/dynamo_awscdk.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ class DynamoTable {
inflight _attributeTypeToString(type: AttributeType): str {
if type == AttributeType.String {
return "S";
} elif type == AttributeType.Number {
} else if type == AttributeType.Number {
return "N";
} elif type == AttributeType.Binary {
} else if type == AttributeType.Binary {
return "B";
}
}
Expand Down
6 changes: 3 additions & 3 deletions examples/tests/valid/optionals.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ let json_obj = Json { ghost: "spooky" };
let var something_else = false;
if let y = json_obj.tryAsBool() {
assert(y == true || y == false);
} elif let y = json_obj.tryAsNum() {
} else if let y = json_obj.tryAsNum() {
assert(y + 0 == y);
} elif let y = json_obj.tryAsStr() {
} else if let y = json_obj.tryAsStr() {
assert(y.length >= 0);
} else {
something_else = true;
Expand Down Expand Up @@ -199,7 +199,7 @@ let str2: str? = nil;

if let s1 = str1 {
assert(false); // Should not happen
} elif let s2 = str2 {
} else if let s2 = str2 {
assert(true);
}

Expand Down
18 changes: 9 additions & 9 deletions examples/tests/valid/statements_if.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ if true {
if true && x + 2 == 4 {
if true && x + 3 == 4 {
assert(false);
} elif true && x + 3 == 6 {
} else if true && x + 3 == 6 {
assert(false);
} elif false || x + 3 == 5 {
} else if false || x + 3 == 5 {
assert(true);
} elif !f {
} else if !f {
assert(!!!f);
} else {
assert(false);
Expand All @@ -26,9 +26,9 @@ test "test" {
if true && x + 2 == 4 {
if true && x + 3 == 4 {
assert(false);
} elif true && x + 3 == 6 {
} else if true && x + 3 == 6 {
assert(false);
} elif false || x + 3 == 5 {
} else if false || x + 3 == 5 {
assert(true);
} else {
assert(false);
Expand All @@ -45,9 +45,9 @@ if true {
let c: str? = "c";
if let d = a {
assert(false);
} elif b != nil {
} else if b != nil {
assert(true);
} elif let e = c {
} else if let e = c {
assert(false);
} else {
assert(false);
Expand All @@ -60,9 +60,9 @@ if true {
let c: str? = "c";
if let d = a {
assert(false);
} elif let e = c {
} else if let e = c {
assert(true);
} elif b != nil {
} else if b != nil {
assert(false);
} else {
assert(false);
Expand Down
15 changes: 8 additions & 7 deletions libs/tree-sitter-wing/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,16 +309,17 @@ module.exports = grammar({
field("block", $.block),
repeat(
choice(
field("elif_let_block", $.elif_let_block),
field("elif_block", $.elif_block)
field("else_if_let_block", $.else_if_let_block),
field("else_if_block", $.else_if_block)
)
),
optional(seq("else", field("else_block", $.block)))
),

elif_let_block: ($) =>
else_if_let_block: ($) =>
seq(
"elif",
"else",
"if",
"let",
optional(field("reassignable", $.reassignable)),
field("name", $.identifier),
Expand All @@ -332,12 +333,12 @@ module.exports = grammar({
"if",
field("condition", $.expression),
field("block", $.block),
repeat(field("elif_block", $.elif_block)),
repeat(field("else_if_block", $.else_if_block)),
optional(seq("else", field("else_block", $.block)))
),

elif_block: ($) =>
seq("elif", field("condition", $.expression), field("block", $.block)),
else_if_block: ($) =>
seq("else", "if", field("condition", $.expression), field("block", $.block)),

try_catch_statement: ($) =>
seq(
Expand Down
2 changes: 1 addition & 1 deletion libs/tree-sitter-wing/queries/folds.scm
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
(while_statement)
(if_statement)
(if_let_statement)
(elif_block)
(else_if_block)
(struct_definition)
(enum_definition)
(try_catch_statement)
Expand Down
28 changes: 18 additions & 10 deletions libs/tree-sitter-wing/src/grammar.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 35799ca

Please sign in to comment.