Skip to content

Allow comments in .swift-format #1014

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

Merged

Conversation

antigluten
Copy link
Contributor

Enable allowsJSON5 on the JSONDecoder. With this change, the decoder can now parse JSON5-formatted data.

Resolves #1011.

@allevato
Copy link
Member

allevato commented May 7, 2025

Unfortunately it looks like allowsJSON5 wasn't added to Foundation on Linux until the 6.0 toolchain (so I'm assuming it came with the swift-foundation changeover). I suspect this would be true for other non-Apple platforms as well, like Android. (On macOS, it's available since 12.0, which swift-format's deployment target is already above.)

We'll need to guard this property (and the test) appropriately. For conciseness, we could use canImport(Darwin) as an indicator that we're on an Apple platform and canImport(FoundationEssentials) to make sure we have a compatible Foundation on other platforms, so something like #if canImport(Darwin) || canImport(FoundationEssentials) ought to be fine, I think.

@ahoppen Do you think that's ok or would you prefer a more explicit check involving something like the compiler version on non-Apple OSes?

@ahoppen
Copy link
Member

ahoppen commented May 8, 2025

I would prefer to check for a compiler version on non-Darwin OSs because that’s something that’s easier to remove in the future. It’s easy to tell that #if compiler(>=6) will always evaluate to true if we no longer support building swift-format using a Swift 5 compiler. But for #if canImport(FoundationEssentials) is a bit harder to determine if that’s always true because you need to remember when FoundationEssentials was introduced.

@antigluten
Copy link
Contributor Author

Hi @allevato @ahoppen, added checking for compiler version.

XCTAssertEqual(config.reflowMultilineStringLiterals, expectedBehavior)
}
}

func testConfigurationWithComments() throws {
#if !canImport(Darwin) && compiler(<6)
Copy link
Member

Choose a reason for hiding this comment

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

You'll need to wrap the rest of the body in an #else here, too, otherwise it will still try to compile the code below in the compiler(<6) case (since it's just unguarded code right now).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

✅ Fixed

Copy link
Member

@allevato allevato left a comment

Choose a reason for hiding this comment

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

Thank you!

@allevato allevato merged commit e70ab31 into swiftlang:main May 9, 2025
21 checks passed
@antigluten antigluten deleted the anti/allow-comments-in-configuration branch May 24, 2025 09:40
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.

Allow comments in .swift-format files
3 participants