-
Notifications
You must be signed in to change notification settings - Fork 92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Trailing comma in imports #602
Conversation
f8c2267
to
5a95666
Compare
5a95666
to
410390c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved with some minor suggestions.
src/modules/imports/import.rs
Outdated
@@ -146,20 +146,24 @@ impl SyntaxModule<ParserMetadata> for Import { | |||
token(meta, "{")?; | |||
let mut exports = vec![]; | |||
loop { | |||
if token(meta, "}").is_ok() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This token check is only required on the first iteration through the loop, to catch an empty import list. I believe that on all subsequent iterations, any trailing brace is caught by the other check below.
It won't make any noticeable difference to the performance, but it may help subsequent developers understand the logic better, if you move the token check out of the loop. You might also change vec![]
to Vec::new()
:
token(meta, "{")?;
let mut exports = Vec::new();
if token(meta, "}").is_err() {
loop {
let tok = meta.get_current_token();
let name = variable(meta, variable_name_extensions())?;
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might also change
vec![]
toVec::new()
why? we use vec![]
throughout the codebase. there is no reason to have an inconsistency like this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, fair enough. In isolated code, I would prefer a straight Vec::new()
call because simpler is better, and you really only need the vec![]
macro to initialise an array from a sequence of items. However, I'm all for internal consistency in a project; happy for it to remain as-is.
This comment was marked as outdated.
This comment was marked as outdated.
All test passed now, but I am not sure the code I've written is clean. Feel free to suggest changes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
your code is fine
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well done! I came up with an idea to add a warning when nothing is actually imported
Err(_) => { | ||
return error!(meta, meta.get_current_token(), "Expected ',' or '}' after import"); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A warning for developers to quickly spot an empty import statement:
import { } from "std/text"
With a warning message like:
} | |
} else { | |
let message = Message::new_warn_at_token(meta, Some(self.token_import.clone())) | |
.message("Empty import statement") | |
meta.add_message(message); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Committed with some modification: i) Adding semicolon, and ii) Removing Some()
. (E0308: expected struct heraclitus_compiler::compiling::Token
, found enum Option<heraclitus_compiler::compiling::Token>
)
And I have a question. Is there a way to make a unit test for warning?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uhhh not for now but that should be implemented at some point. I'll make an issue about it
Co-authored-by: Phoenix Himself <[email protected]>
replace_regex, | ||
} from "std/text" | ||
import { sum, abs } from "std/math" | ||
import { } from "std/env" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't this empty import
now generate a warning? If so, please remove.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, @lens0021
Ignore trailing commas in imports.
I've used GH-123 as a reference and given ChatGPT's advice.