Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Comments

Close side panel

Removed redundant space before struct and class declaration#38

Merged
liamnichols merged 1 commit intoCreateAPI:mainCreateAPI/CreateAPI:mainfrom
imjn:remove-redundant-space-before-internal-structimjn/CreateAPI:remove-redundant-space-before-internal-structCopy head branch name to clipboard
Jun 10, 2022
Merged

Removed redundant space before struct and class declaration#38
liamnichols merged 1 commit intoCreateAPI:mainCreateAPI/CreateAPI:mainfrom
imjn:remove-redundant-space-before-internal-structimjn/CreateAPI:remove-redundant-space-before-internal-structCopy head branch name to clipboard

Conversation

@imjn
Copy link
Member

@imjn imjn commented Jun 10, 2022

closes #32

Background

While we do lhs.joined(separator: " ") and sometimes have "" for the access level, it inserts unnecessary space before struct and class declaration.

What

Remove the redundant space.

How

  • Filtering "" too, not only nil.
  • Fixed tests


func `class`(name: TypeName, contents: [String], protocols: Protocols) -> String {
let type = options.entities.isMakingClassesFinal ? "final class" : "class"
let lhs = [options.access, type, name.rawValue].compactMap { $0 }
Copy link
Member Author

Choose a reason for hiding this comment

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

Here we had removed only nil so any empty string had been passing this filter.

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

I also had an idea that I should add the .filter { !$0.isEmpty } to where we combine them like below ↓

private func declaration(lhs: [String], rhs: [String], contents: [String]) -> String {
    let lhs = lhs.joined(separator: " ").filter { $0.isEmpty } ⬅️⬅️⬅️
    let rhs = rhs.joined(separator: ", ")
    return """
    \(rhs.isEmpty ? lhs : "\(lhs): \(rhs)") {
    \(contents.joined(separator: "\n\n").indented)
    }
    """
}

With this, we can achieve what we want with less code.
However, I decided to add the filter to func class() and func struct() as they already filter nil and I thought filtering nil and empty string should be in the same place and func class() should not care about that.

What do you think about this...?

Copy link
Member

Choose a reason for hiding this comment

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

Good questions.. More generally, I think maybe we could improve some of this further since there is also the var access: String computed property at the top of this file (but we can't use it here since it appends whitespace too).

So for now, I think that what you have done is probably fine 👍

Copy link
Member

@liamnichols liamnichols Jun 10, 2022

Choose a reason for hiding this comment

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

I think ideally, options.access should never be allowed to be an empty string in the first place either. That's something else we might want to look at 😄 If that was the case, the compactMap(_:) would have worked without the need to filter on !isEmpty

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I think so too. We should unify the way we build a String from multiple Strings at some point. Thanks for your comment!

@imjn imjn self-assigned this Jun 10, 2022
@imjn imjn marked this pull request as ready for review June 10, 2022 10:58
@imjn imjn requested a review from liamnichols June 10, 2022 10:58
Copy link
Member

@liamnichols liamnichols left a comment

Choose a reason for hiding this comment

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

Looks good to me! 🚀


func `class`(name: TypeName, contents: [String], protocols: Protocols) -> String {
let type = options.entities.isMakingClassesFinal ? "final class" : "class"
let lhs = [options.access, type, name.rawValue].compactMap { $0 }
Copy link
Member

Choose a reason for hiding this comment

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

Good questions.. More generally, I think maybe we could improve some of this further since there is also the var access: String computed property at the top of this file (but we can't use it here since it appends whitespace too).

So for now, I think that what you have done is probably fine 👍

@liamnichols liamnichols merged commit 876eb68 into CreateAPI:main Jun 10, 2022
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.

Tests/CreateAPITests/Expected/edgecases-change-access-control/Sources/Entities.swift has redundant spaces

2 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.