Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lexer wrongly detects FUNCTION_NAME instead of NAME #925

Open
jovandeginste opened this issue Jun 21, 2020 · 2 comments
Open

Lexer wrongly detects FUNCTION_NAME instead of NAME #925

jovandeginste opened this issue Jun 21, 2020 · 2 comments

Comments

@jovandeginste
Copy link
Contributor

jovandeginste commented Jun 21, 2020

When I provide the lexer with this code, it will detect myclass as type FUNCTION_NAME, due to the missing space between myclass and the LPAREN:

class myclass(
  $param1,
) {
}

I expect this to be type NAME, same as when I would provide this:

class myclass (
  $param1,
) {
}
@usev6
Copy link
Contributor

usev6 commented Feb 21, 2021

Something like the following would probably help:

diff --git a/lib/puppet-lint/lexer.rb b/lib/puppet-lint/lexer.rb
index e9d1696..7cf3fbe 100644
--- a/lib/puppet-lint/lexer.rb
+++ b/lib/puppet-lint/lexer.rb
@@ -211,6 +211,8 @@ class PuppetLint
           value = chunk[regex, 1]
           next if value.nil?
 
+          next if type == :FUNCTION_NAME && tokens.size > 1 && ['class', 'define'].include?(tokens[-2].value)
+
           i += value.size
           tokens << if type == :NAME && KEYWORDS.include?(value)
                       new_token(value.upcase.to_sym, value)

But IMHO even if the added check for 'does this token follow a class or define keyword?' is cleaned up (by moving it to a separate function and making it less ad hoc) it would still stick out as a strange special case from the tokenizing procedure.

OOC, do you have a case at hand where this misparsing leads to a specific error?

@jovandeginste
Copy link
Contributor Author

No errors per se, but i was writing a linter that adds or removes whitespace at certain locations, eg. in front of a parenthesis. Consensus seems to be that a space should not be put between a function call and its opening parenthesis, but should be put after a class name (as in the example).

Example for the function call: $x = sum($y) (no space)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants