I could not get this custom rule working. Either I am doing the regex wrong, or swiftlint does regex differently. My regex works here
I want to force a newline after an extension declaration.
Valid:
extension A {
func temp() {
}
}
Invalid:
extension B {
func temp() {
}
}
custom_rules:
extension_declaration_missing_newline:
regex: 'extension .+\n.'
name: "Extension Declaration Missing Newline"
message: "Enter a newline after opening brackets {"
severity: error''
swxftlint version
0.21.0
PS. Love SwiftLint :) I would have gone insane with inconsistent code if it weren't for SwiftLint. So many thanks and props to you all!
@omerfarukyilmaz There's some quotes in invalid places (after extension_declaration_missing_newline and error.
I was able to get a violation with this configuration:
custom_rules:
extension_declaration_missing_newline:
regex: 'extension .+\n.'
name: "Extension Declaration Missing Newline"
message: "Enter a newline after opening brackets {"
severity: error
Yeah sorry those misplaced quotes are proof of my poor attempt to format correctly in markdown.
Yes, I was able to get a a violation as well but correcting the swift file, as in adding a second newline which should make it valid and_not_ cause a violation still causes a violation.
What I mean is, when I made the rule with multiple \n's (\n\n\n), the violation is still triggered.
I hope I was clear enough.
I've change it to:
custom_rules:
extension_declaration_missing_newline:
regex: 'extension .+\n\n'
name: "Extension Declaration Missing Newline"
message: "Enter a newline after opening brackets {"
severity: error
And it didn't trigger 馃
This is the file:
extension B {
func temp() {
}
}
Hey, sorry for the late reply.
Apologies, never mind that double \n. I got mixed up.
Coming back to the original problem, as you mentioned using this regex
regex: 'extension .+\n.'
on this:
extension B {
func temp() {
}
}
It should violate like you said.
extension B {
func temp() {
}
}
and this example should find 0 but it still finds 1.
Thank you for your help!
I think it's just a matter of improving your regex. For example extension .+?{\n[^\S\n]*\w.
Yes, that regex works amazing. And works great on rubular.com and regex101.com but SwiftLint returns:
Invalid configuration for custom rule 'extension_declaration_missing_newline'.
user@User-MBP Thu Aug 03 04:51:09P
~/Desktop $ cat Test.swift
extension B {
func temp() {
}
}
user@User-MBP Thu Aug 03 04:51:21P
~/Desktop $ cat .swiftlint.yml
custom_rules:
extension_declaration_missing_newline:
regex: 'extension .+?{\n[^\S\n]*\w'
name: "Extension Declaration Missing Newline"
message: "Enter a newline after opening brackets {"
severity: error
user@User-MBP Thu Aug 03 04:51:24P
~/Desktop $ swiftlint lint --path Test.swift
Loading configuration from '.swiftlint.yml'
Invalid configuration for custom rule 'extension_declaration_missing_newline'.
Linting Swift files at path Test.swift
Linting 'Test.swift' (1/1)
Done linting! Found 0 violations, 0 serious in 1 file.
user@User-MBP Thu Aug 03 04:51:27P
~/Desktop $
That is why I had created this issue. A valid regex on regex sites do not work in SwiftLint.
Actually, you have to scape {. So extension .+?\{\n[^\S\n]*\w works for me.
Thank you very much. I had assumed we would not need to escape as we are using single apostrophe but, it seems I was wrong.
Again, thank you a lot for your time! I really appreciate it.
No worries! Feel free to open another issue if you have more questions 馃挴
@marcelofabri Sorry to disturb you again but I can not figure out why the hell is this trigging an error:
extension B {
func temp() {
let hi = "Hello"
}
}
When the first function has at least one line, it triggers the custom rule I am trying to implement 馃槙
I would love to tackle this problem and solve it on my own instead of bothering you but I can not get the regex on SwiftLint to run as it does on sites or sublime...
Probably because we use the .dotMatchesLineSeparators option. So you could simulate it here: http://rubular.com/r/oi9ubccA3e.
There's no way to change this behavior currently, so I guess you'd have to change your regex a bit.
Thanks a lot, again! I'll find a solution from here.
Most helpful comment
Actually, you have to scape
{. Soextension .+?\{\n[^\S\n]*\wworks for me.