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

[Feature] Add support for JSON output #900

Merged
merged 5 commits into from
Apr 16, 2021
Merged

[Feature] Add support for JSON output #900

merged 5 commits into from
Apr 16, 2021

Conversation

danieleformichelli
Copy link
Contributor

Fix for #890

The JSON format is inspired by the format of SwiftLint JSON reporter

@nicklockwood
Copy link
Owner

Looks good. I think I'd prefer to follow SwiftLint's pattern of --reporter json for the CLI argument though, as it will make it easier to add additional formats in future.

@danieleformichelli
Copy link
Contributor Author

danieleformichelli commented Apr 11, 2021

Thanks for the review @nicklockwood, can you have a look now? 🙌

@@ -198,6 +198,8 @@ func printHelp(as type: CLI.OutputType) {
--lenient Suppress errors for unformatted code in --lint mode
--verbose Display detailed formatting output and warnings/errors
--quiet Disables non-critical output messages and warnings
--reporter Defines a custom reporter (only `json` is supported)
--reporter-output Reporter output file (defaults to `output.<reporter>`)
Copy link
Owner

Choose a reason for hiding this comment

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

The --reporter-output parameter seems redundant - we can just re-use the existing --output parameter.

Also, the output shouldn't default to a hard-coded filename if no output file is specified, it should just write the json to stdout.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Isn't --output the output of the formatted file? reusing it here would be a bit confusing 🤔

    --output           Output path for formatted file(s) (defaults to input path)

What about having just a --report parameter, and determine the type from the provided file extension?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Replaced reporter and reporter-output with single report parameter (from which also the type can be inferred in the future.
Please let me know whether it is fine 🙌

Copy link
Owner

Choose a reason for hiding this comment

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

The problem with that is that if you don't specify an --output file then there's no way for it to infer what format should be sent to stdout.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The --report parameters tales in input the report file, not the type, so you can use it as:

swiftformat <whatever option> --report report.json

So when printing the json report the rest of the output and the parameters are not affected.

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nicklockwood did you had time to have a look at this?
Not urgent at all, just wanted to be sure you didn't miss my message 🙌

Copy link
Owner

Choose a reason for hiding this comment

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

Hmm, I was under the impression that reporting was only applicable when running in --lint mode, in which case --output is unused, so having --report take a separate path seemed redundant. But I now realize that you're intending for report to work even when formatting? In which case I agree a separate --report path makes sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For my use case I would use it only for linting, but I think having a separate parameter may be more generic and cover more use cases 🙌

guard reporter == "json" else {
throw FormatError.options("Unsupported reporter: \(reporter).")
}
let outputFile = args["reporter-output"] ?? "output.\(reporter)"
Copy link
Owner

Choose a reason for hiding this comment

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

Instead of inferring output file from reporter, we could infer the reporter from the output file, so if the user specifies an output file with a json extension it could preselect json as the exporter type.

Copy link
Contributor Author

Choose a reason for hiding this comment

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


public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.filePath ?? "", forKey: .file)
Copy link
Owner

Choose a reason for hiding this comment

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

Does it make sense to write a blank string if the filePath is nil rather than just omitting the key?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

case file
case line
case reason
case rule_id
Copy link
Owner

Choose a reason for hiding this comment

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

I'd prefer to us camelCase here, in accordance with Swift naming conventions:

case ruleID = "rule_id"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nicklockwood nicklockwood merged commit b577e4b into nicklockwood:master Apr 16, 2021
@danieleformichelli danieleformichelli deleted the feature/json-reporter branch May 10, 2021 11:38
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.

2 participants