Rubberduck: Let's track literals

Created on 30 Jan 2017  路  2Comments  路  Source: rubberduck-vba/Rubberduck

We already have an EmptyStringLiteralListener:

````csharp
public class EmptyStringLiteralListener : VBAParserBaseListener
{
private readonly IList _contexts =
new List();

public IEnumerable<VBAParser.LiteralExpressionContext> Contexts { get { return _contexts; } }

public override void ExitLiteralExpression(VBAParser.LiteralExpressionContext context)
{
    var literal = context.STRINGLITERAL();
    if (literal != null && literal.GetText() == "\"\"")
    {
        _contexts.Add(context);
    }
}

}
````

Trivial, right? Right now the only thing interested in literals is the EmptyStringLiteralInspection. If we expanded this listener to capture more literal tokens, we could have an inspection that detects repeated literals in a given scope, or across multiple scopes, and suggests introducing procedure or module level constants.

enhancement feature-inspections

Most helpful comment

Here's a simple procedure and module level example:

'Module1
Public Sub Foo()

  Dim sourceFileName As String
  Dim targetFileName As String

  sourceFileName = "SalesReport" & Format$(Now(), "YYYYMMDD") & ".xlsm"
  targetFileName = "SummaryReport" & Format$(Now(), "YYYYMMDD") & ".xlsm"

  DoSomething sourceFileName, targetFileName

End Sub

Sub Bar()
  Debug.Print Format$(Now(), "YYYYMMDD"), "Bar was called"
End Sub 

Would become:

Private Const ISODateFormat = "YYYYMMDD"

Public Sub Foo()
  Const XLSMExtension = ".xlsm"

  Dim sourceFileName As String
  Dim targetFileName As String

  sourceFileName = "SalesReport" & Format$(Now(), ISODateFormat) & XLSMExtension
  targetFileName = "SummaryReport" & Format$(Now(), ISODateFormat) & XLSMExtension

  DoSomething sourceFileName, targetFileName

End Sub

Sub Bar()
  Debug.Print Format$(Now(), ISODateFormat), "Bar was called"
End Sub 

But the scope of the ISODateFormat constant would need to change if say, Bar() was in Module2.

Ideally the user would be prompted for:

  • Name of the new constant
  • String selections that are to be replaced by the new constant
  • The location of the constant declaration, if it used in multiple modules

All 2 comments

Do you have any specific examples of inspections you'd like done? This is an extremely vague issue.

Here's a simple procedure and module level example:

'Module1
Public Sub Foo()

  Dim sourceFileName As String
  Dim targetFileName As String

  sourceFileName = "SalesReport" & Format$(Now(), "YYYYMMDD") & ".xlsm"
  targetFileName = "SummaryReport" & Format$(Now(), "YYYYMMDD") & ".xlsm"

  DoSomething sourceFileName, targetFileName

End Sub

Sub Bar()
  Debug.Print Format$(Now(), "YYYYMMDD"), "Bar was called"
End Sub 

Would become:

Private Const ISODateFormat = "YYYYMMDD"

Public Sub Foo()
  Const XLSMExtension = ".xlsm"

  Dim sourceFileName As String
  Dim targetFileName As String

  sourceFileName = "SalesReport" & Format$(Now(), ISODateFormat) & XLSMExtension
  targetFileName = "SummaryReport" & Format$(Now(), ISODateFormat) & XLSMExtension

  DoSomething sourceFileName, targetFileName

End Sub

Sub Bar()
  Debug.Print Format$(Now(), ISODateFormat), "Bar was called"
End Sub 

But the scope of the ISODateFormat constant would need to change if say, Bar() was in Module2.

Ideally the user would be prompted for:

  • Name of the new constant
  • String selections that are to be replaced by the new constant
  • The location of the constant declaration, if it used in multiple modules
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Gener4tor picture Gener4tor  路  3Comments

ThunderFrame picture ThunderFrame  路  3Comments

retailcoder picture retailcoder  路  4Comments

retailcoder picture retailcoder  路  3Comments

Hosch250 picture Hosch250  路  3Comments