rubberduck-vba / Rubberduck

Every programmer needs a rubberduck. COM add-in for the VBA & VB6 IDE (VBE).
https://rubberduckvba.com
GNU General Public License v3.0
1.92k stars 301 forks source link

QuickFix 'Add Missing Attribute' causes an illegal attribute value given non-literal annotation argument #4946

Open ChrisBrackett opened 5 years ago

ChrisBrackett commented 5 years ago

Rubberduck version information

Version 2.4.1.4688
OS: Microsoft Windows NT 10.0.17763.0, x64
Host Product: Microsoft Office 2016 x64
Host Version: 16.0.4810.1000
Host Executable: EXCEL.EXE

Description The last time I applied the "Add Missing Attribute" hotfix for a member attribute annotation I went from this:

'@Description("Resets the cache of all pivot tables in the target  workbook and removes filters for items no longer in the source data." & vbnewline & vbnewline & "NEW LINE HERE")
Public Sub ResetAllPivotCaches(ByVal targetBook As Excel.Workbook)

...to this:

'@Description("Resets the cache of all pivot tables in the target  workbook and removes filters for items no longer in the source data." & vbnewline & vbnewline & "NEW LINE HERE")
Public Sub ResetAllPivotCaches(ByVal targetBook As Excel.Workbook)
Attribute ResetAllPivotCaches.VB_Description = "Resets the cache of all pivot tables in the target  workbook and removes filters for items no longer in the source data." & vbnewline & vbnewline & "NEW LINE HERE"

The code couldn't compile with that attribute as a visible line of code (syntax error), however RD did not show an error even after I initiated a couple of redundant reparses.

To Reproduce Steps to reproduce the behavior:

  1. Download this file (Module1.bas.txt) and remove the .txt extension (so that it's called Module1.bas):
  2. Import it into a new/empty VBA (I used an Excel workbook).
  3. Have Rubberduck parse the file and then apply the hotfix to "Add Missing Attribute".
  4. See how the attribute is added as visible code that cannot be compiled.

I was able to reproduce the error after exporting the module and re-importing it into a fresh Excel workbook.

Logfile RubberduckLog.txt

ChrisBrackett commented 5 years ago

I see what went wrong. I wanted to see if the member.Description attribute could handle a line break so I concatenated one into the text, basically '@Description("Some text " & vbNewLine & vbNewLine & " more text."). Looks like that doesn't work. Could we add an inspection result like "Invalid Attribute Annotation"?.

For now, the obvious workaround is to be less creative with attribute annotations.

retailcoder commented 5 years ago

Ah! well spotted, hadn't noticed! You could do @Description("Some text \nMore Text") - the \n newline isn't currently handled, but could very well be in the future.

ChrisBrackett commented 5 years ago

Are module/member description attributes a VBE thing just a Rubberduck feature?

retailcoder commented 5 years ago

@CHR-IS-B we make them more visible and easier to manipulate, but the VBE provides a (rather convoluted) way to tweak them (but ), via the Object Browser - right-click a module or member, select "Properties"; note that Rubberduck can't re-import document modules, so that's essentially (currently) the only way to provide a description attribute for a Worksheet module and its members.

retailcoder commented 5 years ago

The VBE doesn't provide any way (in VBA at least) to manipulate member attributes other than VB_Description though.

ChrisBrackett commented 5 years ago

Got it. That means RD could do anything with member attributes! So I'm thinking '@Description("This procedure makes toast!" & InsertAnimatedGif("Dancing_Dwarf")). On second thought...maybe not.

PeterMTaylor commented 5 years ago

I’ll add a vote that once our plug in architecture is in place of our code base InsertedAnimatedGif feature or any other plug in becomes accessible to annotations.