Skip to content

Commit

Permalink
Trailing comma in imports (#602)
Browse files Browse the repository at this point in the history
Co-authored-by: Phoenix Himself <[email protected]>
Co-authored-by: b1ek <[email protected]>
  • Loading branch information
3 people authored Nov 22, 2024
1 parent 19738f5 commit bb15220
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
38 changes: 26 additions & 12 deletions src/modules/imports/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,21 +146,35 @@ impl SyntaxModule<ParserMetadata> for Import {
Err(_) => {
token(meta, "{")?;
let mut exports = vec![];
loop {
let tok = meta.get_current_token();
let name = variable(meta, variable_name_extensions())?;
let alias = match token(meta, "as") {
Ok(_) => Some(variable(meta, variable_name_extensions())?),
Err(_) => None
};
exports.push((name, alias, tok));
match token(meta, ",") {
Ok(_) => {},
Err(_) => break
if token(meta, "}").is_err() {
loop {
let tok = meta.get_current_token();
let name = variable(meta, variable_name_extensions())?;
let alias = match token(meta, "as") {
Ok(_) => Some(variable(meta, variable_name_extensions())?),
Err(_) => None
};
exports.push((name, alias, tok));
if token(meta, "}").is_ok() {
break;
}
match token(meta, ",") {
Ok(_) => {
if token(meta, "}").is_ok() {
break
}
}
Err(_) => {
return error!(meta, meta.get_current_token(), "Expected ',' or '}' after import");
}
}
}
} else {
let message = Message::new_warn_at_token(meta, self.token_import.clone())
.message("Empty import statement");
meta.add_message(message);
}
self.export_defs = exports;
token(meta, "}")?;
}
}
token(meta, "from")?;
Expand Down
12 changes: 12 additions & 0 deletions src/tests/validity/import_with_trailing_comma.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {
replace,
replace_regex,
} from "std/text"
import { sum, abs } from "std/math"

// Output
// c
// 3

echo replace_regex(replace("aa", "a", "b"), "b+", "c", true)
echo sum([abs(-2), 1])

0 comments on commit bb15220

Please sign in to comment.