These are chat archives for iwarapter/sonar-puppet

9th
Dec 2015
Peter Souter
@petems
Dec 09 2015 00:15
Hi hi! :smile:
iwarapter
@iwarapter
Dec 09 2015 00:15
hello
Peter Souter
@petems
Dec 09 2015 00:15
Thanks for the repo, it's really awesome!
Been using it a lot when customers ask where to start when clean up Puppet code :)
But yeah, I can't figure out how to add an exception for defined($foo)
I've got a failing test to start with
But my Java-Fu is weak
iwarapter
@iwarapter
Dec 09 2015 00:16
haha nice, i wrote it because my company is undoubtably one of them :P
a failing test is always the right place to start ;)
ok cool
have a branch setup with the test case added
iwarapter
@iwarapter
Dec 09 2015 00:21
ok
so
the way the code works
is once its parsed its then looking for certain types of tokens (single quotes strings in this case)
it doesnt really have any context of how you are using it
but we can see if we can add it
Peter Souter
@petems
Dec 09 2015 00:23
Hmm ok. Is there a way to add that context into the scope of the check?
iwarapter
@iwarapter
Dec 09 2015 00:25
we could try looking at the parents of the tokens
what i mean by that is this
given this code: defined('$foo')
its basically read as a FUNCTION_STMT node with an EXPRESSION node with a SINGLE_QUOTED_STRING_LITERAL
Peter Souter
@petems
Dec 09 2015 00:28
Yeah, I saw the FUNCTION_STMT variable, I couldn't figure out how to get it into the context
iwarapter
@iwarapter
Dec 09 2015 00:28
each Token has a parent
its probably the grandparent
since the parent is either an expression of a func_values
so we would need something like node.getParent().getParent() would get it
however
there are some pitfalls here :/
the short answer is im getting close to finishing the parser re-write so i can support 3.8 + 4.x grammars
and these types of changes should become much easier
we could do it with the current parser
but we need to put alot of validation around when you go looking in the AstTree
as it gives no guarantee of what you get
iwarapter
@iwarapter
Dec 09 2015 00:33
what you can get
Peter Souter
@petems
Dec 09 2015 00:37
What can I get? :satisfied:
BTW is there anything I can do to help with the future/4.0 syntax re-write? Cos that's awesommeeeee
iwarapter
@iwarapter
Dec 09 2015 00:45
public void visitNode(AstNode node) {
    if (CheckStringUtils.containsVariable(node.getTokenValue()) && !"defined".equals(getFunctionName(node))) {
      addIssue(node, this, "Use double quotes instead of single quotes for the string to be interpolated.");
    }
  }

  private static String getFunctionName(AstNode node) {
    AstNode parent = node.getParent();
    if (parent != null) {
      AstNode grandparent = parent.getFirstAncestor(PuppetGrammar.FUNCTION_STMT);
      if (grandparent != null) {
        return grandparent.getTokenValue();
      }
    }
    return null;
  }
something like that works
but it feels a little dirty
need to check a few variations of how functions can be defined
Peter Souter
@petems
Dec 09 2015 00:47
Cool!
Put in as temp thing, then remove when the re-write happens?
iwarapter
@iwarapter
Dec 09 2015 00:48
needs a little more testing, to make sure it doesnt break when it sees the range of different ways (from a grammar point of view) to write a function statement
Peter Souter
@petems
Dec 09 2015 00:49
Ok :thumbsup:
iwarapter
@iwarapter
Dec 09 2015 00:49
but in regards to 4.x depends on how well you know the puppet grammar
Peter Souter
@petems
Dec 09 2015 00:49
hah, right now, I'm trying to learn more :P
It's late in the UK, so I'm gonna sign-off, but thanks for the help, I'll see if I can hack on this further tomorrow if possible :smiley:
ttfn! :wave: