Skip to content
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

Merged
merged 7 commits into from
Nov 22, 2024

Conversation

lens0021
Copy link
Contributor

Ignore trailing commas in imports.

I've used GH-123 as a reference and given ChatGPT's advice.

@lens0021 lens0021 force-pushed the import-with-trailing-comma branch from f8c2267 to 5a95666 Compare November 18, 2024 16:22
@lens0021 lens0021 force-pushed the import-with-trailing-comma branch from 5a95666 to 410390c Compare November 18, 2024 16:28
@Mte90 Mte90 requested review from Ph0enixKM and hdwalters November 18, 2024 16:42
@lens0021 lens0021 marked this pull request as ready for review November 18, 2024 16:43
Copy link
Contributor

@hdwalters hdwalters left a 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/tests/validity/import_with_trailing_comma.ab Outdated Show resolved Hide resolved
@@ -146,20 +146,24 @@ impl SyntaxModule<ParserMetadata> for Import {
token(meta, "{")?;
let mut exports = vec![];
loop {
if token(meta, "}").is_ok() {
Copy link
Contributor

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())?;
...

Copy link
Member

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![] to Vec::new()

why? we use vec![] throughout the codebase. there is no reason to have an inconsistency like this

Copy link
Contributor

@hdwalters hdwalters Nov 19, 2024

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.

b1ek

This comment was marked as outdated.

@lens0021

This comment was marked as outdated.

@lens0021
Copy link
Contributor Author

All test passed now, but I am not sure the code I've written is clean. Feel free to suggest changes.

Copy link
Member

@b1ek b1ek left a 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

Copy link
Member

@Ph0enixKM Ph0enixKM left a 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");
}
}
}
}
Copy link
Member

@Ph0enixKM Ph0enixKM Nov 20, 2024

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:

Suggested change
}
} else {
let message = Message::new_warn_at_token(meta, Some(self.token_import.clone()))
.message("Empty import statement")
meta.add_message(message);
}

Copy link
Contributor Author

@lens0021 lens0021 Nov 21, 2024

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?

Copy link
Member

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

@Mte90 Mte90 requested a review from Ph0enixKM November 21, 2024 09:36
replace_regex,
} from "std/text"
import { sum, abs } from "std/math"
import { } from "std/env"
Copy link
Contributor

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.

Copy link
Member

@Ph0enixKM Ph0enixKM left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @lens0021

@Mte90 Mte90 merged commit bb15220 into amber-lang:master Nov 22, 2024
1 check passed
@lens0021 lens0021 deleted the import-with-trailing-comma branch December 12, 2024 14:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants