swiftlang/swift-format

Ignoring a rule on a variable declaration doesn't work

Opened this issue · 4 comments

I tried to ignore a rule on a variable declaration like this:

struct Bar {}
struct Foo {
  // swift-format-ignore: DontRepeatTypeInStaticProperties
  var fooBar: Bar 

However this didn't seem to work and I had to move the ignore to the struct declaration to work.

Synced to Apple’s issue tracker as rdar://131991753

To paraphrase our offline chat here: I suspect it's a bug in the way we visit the nodes. We visit the type/extension and then manually recurse into the member list to emit the violations instead of implementing visit methods directly for the members. That would bypass the swift-format-ignore logic. (The implementation of the rule preceded the implementation of ignore, and we never caught this.)

@allevato It is definitely the case that the issue is with how the nodes are visited. I was looking at converting the DontRepeatTypeInStaticProperties rule to use the visit function on VariableDeclSyntax nodes, which would require knowing the current type name in the visit method. I was going to use the existing visit methods for that purpose, but because of nested types it seems necessary to push/pop the current type name using visit/visitPost pairs. Unfortunately, although everything works fine in the linter.walk(...) flow in LintOrFormatRuleTestCase, the visitPost methods are not called in the pipeline.lint(...) flow. Is this known or expected behavior? Or is there a better way to get the name of the enclosing type from a syntax node?

Update: I changed the code generator to make sure visitPost gets called, but don't use that. I just climb the tree looking for a type name instead.