/
variable_scope.rb
140 lines (127 loc) · 3.87 KB
/
variable_scope.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# Public: Test the manifest tokens for any variables that are referenced in
# the manifest. If the variables are not fully qualified or one of the
# variables automatically created in the scope, check that they have been
# defined in the local scope and record a warning for each variable that has
# not.
#
# https://docs.puppet.com/guides/style_guide.html#namespacing-variables
PuppetLint.new_check(:variable_scope) do
DEFAULT_SCOPE_VARS = Set[
'name',
'title',
'module_name',
'environment',
'clientcert',
'clientversion',
'servername',
'serverip',
'serverversion',
'caller_module_name',
'alias',
'audit',
'before',
'loglevel',
'noop',
'notify',
'require',
'schedule',
'stage',
'subscribe',
'tag',
'facts',
'trusted',
'server_facts',
]
POST_VAR_TOKENS = Set[:COMMA, :EQUALS, :RPAREN]
def check
variables_in_scope = DEFAULT_SCOPE_VARS.clone
(class_indexes + defined_type_indexes).each do |idx|
referenced_variables = Set[]
object_tokens = idx[:tokens]
unless idx[:param_tokens].nil?
idx[:param_tokens].each do |token|
next unless token.type == :VARIABLE
next unless POST_VAR_TOKENS.include?(token.next_code_token.type)
variables_in_scope << token.value
end
end
future_parser_scopes = {}
in_pipe = false
block_params_stack = []
object_tokens.each do |token|
case token.type
when :EQUALS
if token.prev_code_token.type == :VARIABLE
variables_in_scope << token.prev_code_token.value
elsif token.prev_code_token.type == :RBRACK
temp_token = token
brack_depth = 0
while temp_token = temp_token.prev_code_token
case temp_token.type
when :VARIABLE
variables_in_scope << temp_token.value
when :RBRACK
brack_depth += 1
when :LBRACK
brack_depth -= 1
break if brack_depth.zero?
when :COMMA # rubocop:disable Lint/EmptyWhen
# ignore
else # unexpected
break
end
end
end
when :VARIABLE
if in_pipe
block_params_stack[-1] << token.value
else
referenced_variables << token
end
when :PIPE
in_pipe = !in_pipe
if in_pipe
block_params_stack << []
else
start_idx = tokens.find_index(token)
end_token = nil
brace_depth = 0
tokens[start_idx..-1].each do |sub_token|
case sub_token.type
when :LBRACE
brace_depth += 1
when :RBRACE
brace_depth -= 1
if brace_depth.zero?
end_token = sub_token
break
end
end
end
params = block_params_stack.pop
(token.line..end_token.line).each do |line|
future_parser_scopes[line] ||= []
future_parser_scopes[line].concat(params)
end
end
end
end
msg = 'top-scope variable being used without an explicit namespace'
referenced_variables.each do |token|
unless future_parser_scopes[token.line].nil?
next if future_parser_scopes[token.line].include?(token.value.gsub(%r{\[.+\]\Z}, ''))
end
next if token.value.include?('::')
next if token.value =~ %r{^(facts|trusted)\[.+\]}
next if variables_in_scope.include?(token.value.gsub(%r{\[.+\]\Z}, ''))
next if token.value =~ %r{\A\d+\Z}
notify(
:warning,
:message => msg,
:line => token.line,
:column => token.column
)
end
end
end
end