nicklockwood / SwiftFormat

A command-line tool and Xcode Extension for formatting Swift code
MIT License
7.9k stars 639 forks source link

SwiftFormat crashes when concatenating String literals #1134

Closed observableobject closed 2 years ago

observableobject commented 2 years ago

Hello,

I'm concatenating some string literals that contain javascript for an injection and SwiftFormat is failing on the definitions:

private lazy var inject = #"let url = document.getElementById("privacyPolicy"); url.setAttribute("href", "# + #"""# + privacyPolicyURL + #"");"#
Exit status of command '../Pods/SwiftFormat/CommandLineTool/swiftformat ../' was 70 instead of 0.
Running SwiftFormat...
Reading config file at **path**/.swiftformat
error: Unexpected token private lazy var inject = #"let url = document.getElementById("privacyPolicy"); url.setAttribute("href", "# + # at 25:5 in path/to/file/file.swift.

I don't normally use String literals so it's possible I'm doing something incorrectly (although it builds and works fine), so I figured to submit an issue here to let you know.

nicklockwood commented 2 years ago

@vfrascello sorry for the delayed response. This is a bug in SwiftFormat's parser, triggered by the #"""# token. I'll get this fixed asap, but in the meantime I think you can just rewrite it as:

private lazy var inject = #"let url = document.getElementById("privacyPolicy"); url.setAttribute("href", ""# + privacyPolicyURL + #"");"#
nicklockwood commented 2 years ago

@vfrascello fixed in 0.49.4