puppet-strings 2.5.0 → 2.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (87) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +90 -4
  3. data/COMMITTERS.md +17 -17
  4. data/CONTRIBUTING.md +6 -6
  5. data/README.md +10 -10
  6. data/lib/puppet/application/strings.rb +2 -0
  7. data/lib/puppet/face/strings.rb +4 -1
  8. data/lib/puppet/feature/rgen.rb +2 -0
  9. data/lib/puppet/feature/yard.rb +2 -0
  10. data/lib/puppet-strings/describe.rb +2 -0
  11. data/lib/puppet-strings/json.rb +2 -0
  12. data/lib/puppet-strings/markdown/base.rb +11 -3
  13. data/lib/puppet-strings/markdown/data_type.rb +2 -0
  14. data/lib/puppet-strings/markdown/data_types.rb +3 -1
  15. data/lib/puppet-strings/markdown/defined_type.rb +2 -0
  16. data/lib/puppet-strings/markdown/defined_types.rb +3 -1
  17. data/lib/puppet-strings/markdown/function.rb +9 -7
  18. data/lib/puppet-strings/markdown/functions.rb +3 -1
  19. data/lib/puppet-strings/markdown/puppet_class.rb +2 -0
  20. data/lib/puppet-strings/markdown/puppet_classes.rb +3 -1
  21. data/lib/puppet-strings/markdown/puppet_plan.rb +2 -0
  22. data/lib/puppet-strings/markdown/puppet_plans.rb +3 -1
  23. data/lib/puppet-strings/markdown/puppet_task.rb +2 -0
  24. data/lib/puppet-strings/markdown/puppet_tasks.rb +3 -1
  25. data/lib/puppet-strings/markdown/resource_type.rb +2 -0
  26. data/lib/puppet-strings/markdown/resource_types.rb +3 -1
  27. data/lib/puppet-strings/markdown/table_of_contents.rb +3 -1
  28. data/lib/puppet-strings/markdown/templates/classes_and_defines.erb +7 -3
  29. data/lib/puppet-strings/markdown/templates/data_type.erb +12 -4
  30. data/lib/puppet-strings/markdown/templates/data_type_function.erb +1 -1
  31. data/lib/puppet-strings/markdown/templates/function.erb +1 -1
  32. data/lib/puppet-strings/markdown/templates/puppet_task.erb +1 -1
  33. data/lib/puppet-strings/markdown/templates/resource_type.erb +8 -2
  34. data/lib/puppet-strings/markdown.rb +11 -9
  35. data/lib/puppet-strings/monkey_patches/display_object_command.rb +2 -0
  36. data/lib/puppet-strings/tasks/generate.rb +2 -0
  37. data/lib/puppet-strings/tasks/gh_pages.rb +3 -0
  38. data/lib/puppet-strings/tasks/validate.rb +42 -0
  39. data/lib/puppet-strings/tasks.rb +3 -0
  40. data/lib/puppet-strings/version.rb +3 -1
  41. data/lib/puppet-strings/yard/code_objects/base.rb +2 -0
  42. data/lib/puppet-strings/yard/code_objects/class.rb +4 -2
  43. data/lib/puppet-strings/yard/code_objects/data_type.rb +4 -2
  44. data/lib/puppet-strings/yard/code_objects/data_type_alias.rb +2 -0
  45. data/lib/puppet-strings/yard/code_objects/defined_type.rb +4 -2
  46. data/lib/puppet-strings/yard/code_objects/function.rb +6 -3
  47. data/lib/puppet-strings/yard/code_objects/group.rb +3 -0
  48. data/lib/puppet-strings/yard/code_objects/plan.rb +4 -2
  49. data/lib/puppet-strings/yard/code_objects/provider.rb +6 -0
  50. data/lib/puppet-strings/yard/code_objects/task.rb +2 -0
  51. data/lib/puppet-strings/yard/code_objects/type.rb +6 -1
  52. data/lib/puppet-strings/yard/code_objects.rb +2 -0
  53. data/lib/puppet-strings/yard/handlers/helpers.rb +2 -0
  54. data/lib/puppet-strings/yard/handlers/json/base.rb +2 -0
  55. data/lib/puppet-strings/yard/handlers/json/task_handler.rb +2 -0
  56. data/lib/puppet-strings/yard/handlers/puppet/base.rb +3 -0
  57. data/lib/puppet-strings/yard/handlers/puppet/class_handler.rb +2 -0
  58. data/lib/puppet-strings/yard/handlers/puppet/data_type_alias_handler.rb +2 -0
  59. data/lib/puppet-strings/yard/handlers/puppet/defined_type_handler.rb +2 -0
  60. data/lib/puppet-strings/yard/handlers/puppet/function_handler.rb +3 -1
  61. data/lib/puppet-strings/yard/handlers/puppet/plan_handler.rb +2 -0
  62. data/lib/puppet-strings/yard/handlers/ruby/base.rb +5 -0
  63. data/lib/puppet-strings/yard/handlers/ruby/data_type_handler.rb +33 -17
  64. data/lib/puppet-strings/yard/handlers/ruby/function_handler.rb +9 -7
  65. data/lib/puppet-strings/yard/handlers/ruby/provider_handler.rb +13 -0
  66. data/lib/puppet-strings/yard/handlers/ruby/rsapi_handler.rb +4 -1
  67. data/lib/puppet-strings/yard/handlers/ruby/type_base.rb +14 -6
  68. data/lib/puppet-strings/yard/handlers/ruby/type_extras_handler.rb +15 -5
  69. data/lib/puppet-strings/yard/handlers/ruby/type_handler.rb +12 -1
  70. data/lib/puppet-strings/yard/handlers.rb +2 -0
  71. data/lib/puppet-strings/yard/parsers/json/parser.rb +2 -0
  72. data/lib/puppet-strings/yard/parsers/json/task_statement.rb +2 -0
  73. data/lib/puppet-strings/yard/parsers/puppet/parser.rb +18 -14
  74. data/lib/puppet-strings/yard/parsers/puppet/statement.rb +4 -0
  75. data/lib/puppet-strings/yard/parsers.rb +2 -0
  76. data/lib/puppet-strings/yard/tags/enum_tag.rb +2 -0
  77. data/lib/puppet-strings/yard/tags/factory.rb +2 -0
  78. data/lib/puppet-strings/yard/tags/overload_tag.rb +4 -1
  79. data/lib/puppet-strings/yard/tags/parameter_directive.rb +5 -4
  80. data/lib/puppet-strings/yard/tags/property_directive.rb +5 -4
  81. data/lib/puppet-strings/yard/tags/summary_tag.rb +2 -0
  82. data/lib/puppet-strings/yard/tags.rb +2 -0
  83. data/lib/puppet-strings/yard/templates/default/puppet_function/html/setup.rb +1 -1
  84. data/lib/puppet-strings/yard/util.rb +4 -1
  85. data/lib/puppet-strings/yard.rb +3 -0
  86. data/lib/puppet-strings.rb +2 -0
  87. metadata +9 -8
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'puppet-strings/yard/handlers/helpers'
2
4
  require 'puppet-strings/yard/handlers/ruby/base'
3
5
  require 'puppet-strings/yard/code_objects'
@@ -10,8 +12,10 @@ class PuppetStrings::Yard::Handlers::Ruby::DataTypeHandler < PuppetStrings::Yard
10
12
 
11
13
  process do
12
14
  return unless statement.count > 1
15
+
13
16
  ruby_module_name = statement[0].source
14
17
  return unless ruby_module_name == 'Puppet::DataTypes' || ruby_module_name == 'DataTypes' # rubocop:disable Style/MultipleComparison This reads better
18
+
15
19
  object = get_datatype_yard_object(get_name(statement, 'Puppet::DataTypes.create_type'))
16
20
  # Extract the interface definition
17
21
  type_interface = extract_data_type_interface
@@ -65,8 +69,10 @@ class PuppetStrings::Yard::Handlers::Ruby::DataTypeHandler < PuppetStrings::Yard
65
69
  next false unless node.is_a?(YARD::Parser::Ruby::MethodCallNode) &&
66
70
  node.method_name &&
67
71
  node.method_name.source == 'interface'
72
+
68
73
  parameters = node.parameters(false)
69
74
  next false unless parameters.count >= 1
75
+
70
76
  interface_string = node_as_string(parameters[0])
71
77
  next false unless interface_string
72
78
 
@@ -97,8 +103,10 @@ class PuppetStrings::Yard::Handlers::Ruby::DataTypeHandler < PuppetStrings::Yard
97
103
  # @return [YARD::Parser::Ruby::AstNode, nil]
98
104
  def find_ruby_ast_node(ast_node, recurse = false, &block)
99
105
  raise ArgumentError, 'find_ruby_ast_node requires a block' unless block_given?
106
+
100
107
  is_found = yield ast_node
101
108
  return ast_node if is_found
109
+
102
110
  if ast_node.respond_to?(:children)
103
111
  ast_node.children.each do |child_node|
104
112
  child_found = find_ruby_ast_node(child_node, recurse, &block)
@@ -128,7 +136,7 @@ class PuppetStrings::Yard::Handlers::Ruby::DataTypeHandler < PuppetStrings::Yard
128
136
  # Anything else is ignored
129
137
  class LazyLiteralEvaluator
130
138
  def initialize
131
- @literal_visitor ||= ::Puppet::Pops::Visitor.new(self, "literal", 0, 0)
139
+ @literal_visitor = ::Puppet::Pops::Visitor.new(self, "literal", 0, 0)
132
140
  end
133
141
 
134
142
  def literal(ast)
@@ -136,72 +144,72 @@ class PuppetStrings::Yard::Handlers::Ruby::DataTypeHandler < PuppetStrings::Yard
136
144
  end
137
145
 
138
146
  # ----- The following methods are different/additions from the original Literal_evaluator
139
- def literal_Object(o) # rubocop:disable Naming/UncommunicativeMethodParamName
147
+ def literal_Object(o)
140
148
  # Ignore any other object types
141
149
  end
142
150
 
143
- def literal_AccessExpression(o) # rubocop:disable Naming/UncommunicativeMethodParamName
151
+ def literal_AccessExpression(o)
144
152
  # Extract the raw text of the Access Expression
145
153
  ::Puppet::Pops::Adapters::SourcePosAdapter.adapt(o).extract_text
146
154
  end
147
155
 
148
- def literal_QualifiedReference(o) # rubocop:disable Naming/UncommunicativeMethodParamName
156
+ def literal_QualifiedReference(o)
149
157
  # Extract the raw text of the Qualified Reference
150
158
  ::Puppet::Pops::Adapters::SourcePosAdapter.adapt(o).extract_text
151
159
  end
152
160
 
153
161
  # ----- The following methods are the same as the original Literal_evaluator
154
- def literal_Factory(o) # rubocop:disable Naming/UncommunicativeMethodParamName
162
+ def literal_Factory(o)
155
163
  literal(o.model)
156
164
  end
157
165
 
158
- def literal_Program(o) # rubocop:disable Naming/UncommunicativeMethodParamName
166
+ def literal_Program(o)
159
167
  literal(o.body)
160
168
  end
161
169
 
162
- def literal_LiteralString(o) # rubocop:disable Naming/UncommunicativeMethodParamName
170
+ def literal_LiteralString(o)
163
171
  o.value
164
172
  end
165
173
 
166
- def literal_QualifiedName(o) # rubocop:disable Naming/UncommunicativeMethodParamName
174
+ def literal_QualifiedName(o)
167
175
  o.value
168
176
  end
169
177
 
170
- def literal_LiteralNumber(o) # rubocop:disable Naming/UncommunicativeMethodParamName
178
+ def literal_LiteralNumber(o)
171
179
  o.value
172
180
  end
173
181
 
174
- def literal_UnaryMinusExpression(o) # rubocop:disable Naming/UncommunicativeMethodParamName
182
+ def literal_UnaryMinusExpression(o)
175
183
  -1 * literal(o.expr)
176
184
  end
177
185
 
178
- def literal_LiteralBoolean(o) # rubocop:disable Naming/UncommunicativeMethodParamName
186
+ def literal_LiteralBoolean(o)
179
187
  o.value
180
188
  end
181
189
 
182
- def literal_LiteralUndef(o) # rubocop:disable Naming/UncommunicativeMethodParamName
190
+ def literal_LiteralUndef(o)
183
191
  nil
184
192
  end
185
193
 
186
- def literal_LiteralDefault(o) # rubocop:disable Naming/UncommunicativeMethodParamName
194
+ def literal_LiteralDefault(o)
187
195
  :default
188
196
  end
189
197
 
190
- def literal_LiteralRegularExpression(o) # rubocop:disable Naming/UncommunicativeMethodParamName
198
+ def literal_LiteralRegularExpression(o)
191
199
  o.value
192
200
  end
193
201
 
194
- def literal_ConcatenatedString(o) # rubocop:disable Naming/UncommunicativeMethodParamName
202
+ def literal_ConcatenatedString(o)
195
203
  # use double quoted string value if there is no interpolation
196
204
  throw :not_literal unless o.segments.size == 1 && o.segments[0].is_a?(Model::LiteralString)
197
205
  o.segments[0].value
198
206
  end
199
207
 
200
- def literal_LiteralList(o) # rubocop:disable Naming/UncommunicativeMethodParamName
208
+ def literal_LiteralList(o)
201
209
  o.values.map { |v| literal(v) }
202
210
  end
203
211
 
204
- def literal_LiteralHash(o) # rubocop:disable Naming/UncommunicativeMethodParamName
212
+ def literal_LiteralHash(o)
205
213
  o.entries.reduce({}) do |result, entry|
206
214
  result[literal(entry.key)] = literal(entry.value)
207
215
  result
@@ -275,8 +283,10 @@ class PuppetStrings::Yard::Handlers::Ruby::DataTypeHandler < PuppetStrings::Yard
275
283
  # Find param tags with a type that is different from the actual definition
276
284
  object.tags(:param).reject { |tag| tag.types.nil? }.each do |tag|
277
285
  next if actual_params_hash[tag.name].nil?
286
+
278
287
  actual_data_type = actual_params_hash[tag.name][:types]
279
288
  next if actual_data_type.nil?
289
+
280
290
  log.warn "The @param tag for '#{tag.name}' has a different type definition than the actual attribute near #{object.file}:#{object.line}." if tag.types != actual_data_type
281
291
  end
282
292
 
@@ -290,6 +300,7 @@ class PuppetStrings::Yard::Handlers::Ruby::DataTypeHandler < PuppetStrings::Yard
290
300
  # Set the type in the param tag
291
301
  object.tags(:param).each do |tag|
292
302
  next if actual_params_hash[tag.name].nil?
303
+
293
304
  tag.types = actual_params_hash[tag.name][:types]
294
305
  end
295
306
  end
@@ -311,10 +322,13 @@ class PuppetStrings::Yard::Handlers::Ruby::DataTypeHandler < PuppetStrings::Yard
311
322
  # Functions with the wrong return type
312
323
  object.meths.each do |meth|
313
324
  next unless actual_func_names.include?(meth.name.to_s)
325
+
314
326
  return_tag = meth.docstring.tag(:return)
315
327
  next if return_tag.nil?
328
+
316
329
  actual_return_types = [actual_functions_hash[meth.name.to_s][:return_type]]
317
330
  next if return_tag.types == actual_return_types
331
+
318
332
  log.warn "The @return tag for '#{meth.name}' has a different type definition than the actual function near #{object.file}:#{object.line}. Expected #{actual_return_types}"
319
333
  return_tag.types = actual_return_types
320
334
  end
@@ -329,6 +343,7 @@ class PuppetStrings::Yard::Handlers::Ruby::DataTypeHandler < PuppetStrings::Yard
329
343
  # Add the return type for the methods if missing
330
344
  object.meths.each do |meth|
331
345
  next unless meth.docstring.tag(:return).nil?
346
+
332
347
  meth.docstring.add_tag(YARD::Tags::Tag.new(:return, '', actual_functions_hash[meth.name.to_s][:return_type]))
333
348
  end
334
349
 
@@ -336,6 +351,7 @@ class PuppetStrings::Yard::Handlers::Ruby::DataTypeHandler < PuppetStrings::Yard
336
351
  object.meths.each do |meth|
337
352
  validate_function_method!(object, meth, actual_functions_hash[meth.name.to_s])
338
353
  next unless meth.docstring.tag(:return).nil?
354
+
339
355
  meth.docstring.add_tag(YARD::Tags::Tag.new(:return, '', actual_functions_hash[meth.name.to_s][:return_type]))
340
356
  end
341
357
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'puppet-strings/yard/handlers/helpers'
2
4
  require 'puppet-strings/yard/handlers/ruby/base'
3
5
  require 'puppet-strings/yard/code_objects'
@@ -29,6 +31,7 @@ class PuppetStrings::Yard::Handlers::Ruby::FunctionHandler < PuppetStrings::Yard
29
31
  # newline, YARD ignores the namespace and uses `newfunction` as the source of the
30
32
  # first statement.
31
33
  return unless statement.count > 1
34
+
32
35
  module_name = statement[0].source
33
36
  return unless module_name == 'Puppet::Functions' || module_name == 'Puppet::Parser::Functions' || module_name == 'newfunction'
34
37
 
@@ -219,8 +222,7 @@ class PuppetStrings::Yard::Handlers::Ruby::FunctionHandler < PuppetStrings::Yard
219
222
 
220
223
  # Populate the required parameters
221
224
  params = parameters.unnamed_required_params
222
- if params
223
- params.each do |parameter|
225
+ params&.each do |parameter|
224
226
  add_param_tag(
225
227
  overload_tag,
226
228
  param_tags,
@@ -229,12 +231,10 @@ class PuppetStrings::Yard::Handlers::Ruby::FunctionHandler < PuppetStrings::Yard
229
231
  parameter.line
230
232
  )
231
233
  end
232
- end
233
234
 
234
235
  # Populate the optional parameters
235
236
  params = parameters.unnamed_optional_params
236
- if params
237
- params.each do |parameter|
237
+ params&.each do |parameter|
238
238
  add_param_tag(
239
239
  overload_tag,
240
240
  param_tags,
@@ -246,7 +246,6 @@ class PuppetStrings::Yard::Handlers::Ruby::FunctionHandler < PuppetStrings::Yard
246
246
  true
247
247
  )
248
248
  end
249
- end
250
249
 
251
250
  # Populate the splat parameter
252
251
  param = parameters.splat_param
@@ -301,7 +300,7 @@ class PuppetStrings::Yard::Handlers::Ruby::FunctionHandler < PuppetStrings::Yard
301
300
  name = '&' + name
302
301
  end
303
302
 
304
- type ||= tag && tag.types ? tag.type : 'Any'
303
+ type ||= tag&.types ? tag.type : 'Any'
305
304
  type = optional ? "Optional[#{type}]" : type
306
305
 
307
306
  object.parameters << [name, to_puppet_literal(default)]
@@ -328,6 +327,7 @@ class PuppetStrings::Yard::Handlers::Ruby::FunctionHandler < PuppetStrings::Yard
328
327
  # Validate that tags have matching parameters
329
328
  overload.tags(:param).each do |tag|
330
329
  next if overload.parameters.find { |p| tag.name == p[0] }
330
+
331
331
  log.warn "The @param tag for parameter '#{tag.name}' has no matching parameter at #{file}:#{line}."
332
332
  end
333
333
  end
@@ -338,9 +338,11 @@ class PuppetStrings::Yard::Handlers::Ruby::FunctionHandler < PuppetStrings::Yard
338
338
  parameters[1].each do |kvp|
339
339
  next unless kvp.count == 2
340
340
  next unless node_as_string(kvp[0]) == 'doc'
341
+
341
342
  docstring = node_as_string(kvp[1])
342
343
 
343
344
  log.error "Failed to parse docstring for 3.x Puppet function '#{name}' near #{statement.file}:#{statement.line}." and return nil unless docstring
345
+
344
346
  return PuppetStrings::Yard::Util.scrub_string(docstring)
345
347
  end
346
348
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'puppet-strings/yard/handlers/helpers'
2
4
  require 'puppet-strings/yard/handlers/ruby/base'
3
5
  require 'puppet-strings/yard/code_objects'
@@ -20,6 +22,7 @@ class PuppetStrings::Yard::Handlers::Ruby::ProviderHandler < PuppetStrings::Yard
20
22
  # Extract the type name
21
23
  type_call_parameters = type_call.parameters(false)
22
24
  return unless type_call_parameters.count >= 1
25
+
23
26
  type_name = node_as_string(type_call_parameters.first)
24
27
  raise YARD::Parser::UndocumentableError, "Could not determine the resource type name for the provider defined at #{statement.file}:#{statement.line}." unless type_name
25
28
 
@@ -47,8 +50,10 @@ class PuppetStrings::Yard::Handlers::Ruby::ProviderHandler < PuppetStrings::Yard
47
50
  if child.type == :assign
48
51
  ivar = child.jump(:ivar)
49
52
  next unless ivar != child && ivar.source == '@doc'
53
+
50
54
  docstring = node_as_string(child[1])
51
55
  log.error "Failed to parse docstring for Puppet provider '#{object.name}' (resource type '#{object.type_name}') near #{child.file}:#{child.line}." and return nil unless docstring
56
+
52
57
  register_docstring(object, PuppetStrings::Yard::Util.scrub_string(docstring), nil)
53
58
  return nil
54
59
  elsif child.is_a?(YARD::Parser::Ruby::MethodCallNode)
@@ -60,6 +65,7 @@ class PuppetStrings::Yard::Handlers::Ruby::ProviderHandler < PuppetStrings::Yard
60
65
 
61
66
  docstring = node_as_string(child.parameters[0])
62
67
  log.error "Failed to parse docstring for Puppet provider '#{object.name}' (resource type '#{object.type_name}') near #{child.file}:#{child.line}." and return nil unless docstring
68
+
63
69
  register_docstring(object, PuppetStrings::Yard::Util.scrub_string(docstring), nil)
64
70
  return nil
65
71
  end
@@ -71,6 +77,7 @@ class PuppetStrings::Yard::Handlers::Ruby::ProviderHandler < PuppetStrings::Yard
71
77
  # Traverse the block looking for confines/defaults/commands
72
78
  block = statement.block
73
79
  return unless block && block.count >= 2
80
+
74
81
  block[1].children.each do |node|
75
82
  next unless node.is_a?(YARD::Parser::Ruby::MethodCallNode) && node.method_name
76
83
 
@@ -80,8 +87,10 @@ class PuppetStrings::Yard::Handlers::Ruby::ProviderHandler < PuppetStrings::Yard
80
87
  if method_name == 'confine'
81
88
  # Add a confine to the object
82
89
  next unless parameters.count >= 1
90
+
83
91
  parameters[0].each do |kvp|
84
92
  next unless kvp.count == 2
93
+
85
94
  object.add_confine(node_as_string(kvp[0]) || kvp[0].source, node_as_string(kvp[1]) || kvp[1].source)
86
95
  end
87
96
  elsif method_name == 'has_feature' || method_name == 'has_features'
@@ -92,9 +101,11 @@ class PuppetStrings::Yard::Handlers::Ruby::ProviderHandler < PuppetStrings::Yard
92
101
  elsif method_name == 'defaultfor'
93
102
  # Add a default to the object
94
103
  next unless parameters.count >= 1
104
+
95
105
  # Some defaultfor statements contain multiple constraints.
96
106
  parameters.each do |kvps|
97
107
  next unless kvps.count >= 1
108
+
98
109
  defaultfor = []
99
110
  kvps.each do |kvp|
100
111
  defaultfor << [node_as_string(kvp[0]) || kvp[0].source, node_as_string(kvp[1]) || kvp[1].source]
@@ -104,8 +115,10 @@ class PuppetStrings::Yard::Handlers::Ruby::ProviderHandler < PuppetStrings::Yard
104
115
  elsif method_name == 'commands'
105
116
  # Add the commands to the object
106
117
  next unless parameters.count >= 1
118
+
107
119
  parameters[0].each do |kvp|
108
120
  next unless kvp.count == 2
121
+
109
122
  object.add_command(node_as_string(kvp[0]) || kvp[0].source, node_as_string(kvp[1]) || kvp[1].source)
110
123
  end
111
124
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'puppet-strings/yard/handlers/helpers'
2
4
  require 'puppet-strings/yard/handlers/ruby/base'
3
5
  require 'puppet-strings/yard/code_objects'
@@ -6,7 +8,7 @@ require 'puppet-strings/yard/util'
6
8
  # Implements the handler for Puppet resource types written in Ruby.
7
9
  class PuppetStrings::Yard::Handlers::Ruby::RsapiHandler < PuppetStrings::Yard::Handlers::Ruby::Base
8
10
  # The default docstring when ensurable is used without given a docstring.
9
- DEFAULT_ENSURABLE_DOCSTRING = 'The basic property that the resource should be in.'.freeze
11
+ DEFAULT_ENSURABLE_DOCSTRING = 'The basic property that the resource should be in.'
10
12
 
11
13
  namespace_only
12
14
  handles method_call(:register_type)
@@ -14,6 +16,7 @@ class PuppetStrings::Yard::Handlers::Ruby::RsapiHandler < PuppetStrings::Yard::H
14
16
  process do
15
17
  # Only accept calls to Puppet::ResourceApi
16
18
  return unless statement.count > 1
19
+
17
20
  module_name = statement[0].source
18
21
  return unless ['Puppet::ResourceApi'].include? module_name
19
22
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'puppet-strings/yard/handlers/ruby/base'
2
4
 
3
5
  class PuppetStrings::Yard::Handlers::Ruby::TypeBase < PuppetStrings::Yard::Handlers::Ruby::Base
@@ -22,8 +24,10 @@ class PuppetStrings::Yard::Handlers::Ruby::TypeBase < PuppetStrings::Yard::Handl
22
24
  if child.type == :assign
23
25
  ivar = child.jump(:ivar)
24
26
  next unless ivar != child && ivar.source == '@doc'
27
+
25
28
  docstring = node_as_string(child[1])
26
29
  log.error "Failed to parse docstring for #{kind} near #{child.file}:#{child.line}." and return nil unless docstring
30
+
27
31
  return PuppetStrings::Yard::Util.scrub_string(docstring)
28
32
  elsif child.is_a?(YARD::Parser::Ruby::MethodCallNode)
29
33
  # Look for a call to a dispatch method with a block
@@ -33,6 +37,7 @@ class PuppetStrings::Yard::Handlers::Ruby::TypeBase < PuppetStrings::Yard::Handl
33
37
 
34
38
  docstring = node_as_string(child.parameters[0])
35
39
  log.error "Failed to parse docstring for #{kind} near #{child.file}:#{child.line}." and return nil unless docstring
40
+
36
41
  return PuppetStrings::Yard::Util.scrub_string(docstring)
37
42
  end
38
43
  end
@@ -69,6 +74,7 @@ class PuppetStrings::Yard::Handlers::Ruby::TypeBase < PuppetStrings::Yard::Handl
69
74
 
70
75
  if method_name == 'newvalue'
71
76
  next unless parameters.count >= 1
77
+
72
78
  object.add(node_as_string(parameters[0]) || parameters[0].source)
73
79
  elsif method_name == 'newvalues'
74
80
  parameters.each do |p|
@@ -76,9 +82,11 @@ class PuppetStrings::Yard::Handlers::Ruby::TypeBase < PuppetStrings::Yard::Handl
76
82
  end
77
83
  elsif method_name == 'aliasvalue'
78
84
  next unless parameters.count >= 2
85
+
79
86
  object.alias(node_as_string(parameters[0]) || parameters[0].source, node_as_string(parameters[1]) || parameters[1].source)
80
87
  elsif method_name == 'defaultto'
81
88
  next unless parameters.count >= 1
89
+
82
90
  object.default = node_as_string(parameters[0]) || parameters[0].source
83
91
  elsif method_name == 'isnamevar'
84
92
  object.isnamevar = true
@@ -103,6 +111,7 @@ class PuppetStrings::Yard::Handlers::Ruby::TypeBase < PuppetStrings::Yard::Handl
103
111
  parameters[1].each do |kvp|
104
112
  next unless kvp.count == 2
105
113
  next unless node_as_string(kvp[0]) == 'parent'
114
+
106
115
  if kvp[1].source == 'Puppet::Parameter::Boolean'
107
116
  object.add('true') unless object.values.include? 'true' # rubocop:disable Performance/InefficientHashSearch Not supported on Ruby 2.1
108
117
  object.add('false') unless object.values.include? 'false' # rubocop:disable Performance/InefficientHashSearch Not supported on Ruby 2.1
@@ -117,19 +126,18 @@ class PuppetStrings::Yard::Handlers::Ruby::TypeBase < PuppetStrings::Yard::Handl
117
126
 
118
127
  def set_default_namevar(object)
119
128
  return unless object.properties || object.parameters
129
+
120
130
  default = nil
121
- if object.properties
122
- object.properties.each do |property|
131
+ object.properties&.each do |property|
123
132
  return nil if property.isnamevar
133
+
124
134
  default = property if property.name == 'name'
125
135
  end
126
- end
127
- if object.parameters
128
- object.parameters.each do |parameter|
136
+ object.parameters&.each do |parameter|
129
137
  return nil if parameter.isnamevar
138
+
130
139
  default ||= parameter if parameter.name == 'name'
131
140
  end
132
- end
133
141
  default.isnamevar = true if default
134
142
  end
135
143
  end
@@ -1,16 +1,19 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'puppet-strings/yard/handlers/helpers'
2
4
  require 'puppet-strings/yard/handlers/ruby/type_base'
3
5
  require 'puppet-strings/yard/code_objects'
4
6
  require 'puppet-strings/yard/util'
5
7
 
6
- # Implements the handler for Puppet resource type newparam/newproperty calls written in Ruby.
8
+ # Implements the handler for Puppet resource type newparam/newproperty/ensurable calls written in Ruby.
7
9
  class PuppetStrings::Yard::Handlers::Ruby::TypeExtrasHandler < PuppetStrings::Yard::Handlers::Ruby::TypeBase
8
10
  # The default docstring when ensurable is used without given a docstring.
9
- DEFAULT_ENSURABLE_DOCSTRING = 'The basic property that the resource should be in.'.freeze
11
+ DEFAULT_ENSURABLE_DOCSTRING = 'The basic property that the resource should be in.'
10
12
 
11
13
  namespace_only
12
14
  handles method_call(:newparam)
13
15
  handles method_call(:newproperty)
16
+ handles method_call(:ensurable)
14
17
 
15
18
  process do
16
19
 
@@ -31,13 +34,20 @@ class PuppetStrings::Yard::Handlers::Ruby::TypeExtrasHandler < PuppetStrings::Ya
31
34
  # propertyname: "content"
32
35
 
33
36
  return unless (statement.count > 1) && (statement[0].children.count > 2)
37
+
34
38
  module_name = statement[0].children[0].source
35
39
  method1_name = statement[0].children.drop(1).find{ |c| c.type == :ident }.source
36
- return unless (module_name == 'Puppet::Type' || module_name == 'Type') && method1_name == 'type'
40
+ return unless ['Type', 'Puppet::Type'].include?(module_name) && method1_name == 'type'
37
41
 
42
+ # ensurable is syntatic sugar for newproperty
38
43
  typename = get_name(statement[0], 'Puppet::Type.type')
39
- method2_name = caller_method
40
- propertyname = get_name(statement, "Puppet::Type.type().#{method2_name}")
44
+ if caller_method == 'ensurable'
45
+ method2_name = 'newproperty'
46
+ propertyname = 'ensure'
47
+ else
48
+ method2_name = caller_method
49
+ propertyname = get_name(statement, "Puppet::Type.type().#{method2_name}")
50
+ end
41
51
 
42
52
  typeobject = get_type_yard_object(typename)
43
53
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'puppet-strings/yard/handlers/helpers'
2
4
  require 'puppet-strings/yard/handlers/ruby/type_base'
3
5
  require 'puppet-strings/yard/code_objects'
@@ -6,7 +8,7 @@ require 'puppet-strings/yard/util'
6
8
  # Implements the handler for Puppet resource types written in Ruby.
7
9
  class PuppetStrings::Yard::Handlers::Ruby::TypeHandler < PuppetStrings::Yard::Handlers::Ruby::TypeBase
8
10
  # The default docstring when ensurable is used without given a docstring.
9
- DEFAULT_ENSURABLE_DOCSTRING = 'The basic property that the resource should be in.'.freeze
11
+ DEFAULT_ENSURABLE_DOCSTRING = 'The basic property that the resource should be in.'
10
12
 
11
13
  namespace_only
12
14
  handles method_call(:newtype)
@@ -14,6 +16,7 @@ class PuppetStrings::Yard::Handlers::Ruby::TypeHandler < PuppetStrings::Yard::Ha
14
16
  process do
15
17
  # Only accept calls to Puppet::Type
16
18
  return unless statement.count > 1
19
+
17
20
  module_name = statement[0].source
18
21
  return unless module_name == 'Puppet::Type' || module_name == 'Type'
19
22
 
@@ -40,6 +43,7 @@ class PuppetStrings::Yard::Handlers::Ruby::TypeHandler < PuppetStrings::Yard::Ha
40
43
  # Traverse the block looking for properties/parameters/features
41
44
  block = statement.block
42
45
  return unless block && block.count >= 2
46
+
43
47
  block[1].children.each do |node|
44
48
  next unless node.is_a?(YARD::Parser::Ruby::MethodCallNode) &&
45
49
  node.method_name
@@ -50,24 +54,31 @@ class PuppetStrings::Yard::Handlers::Ruby::TypeHandler < PuppetStrings::Yard::Ha
50
54
  if method_name == 'newproperty'
51
55
  # Add a property to the object
52
56
  next unless parameters.count >= 1
57
+
53
58
  name = node_as_string(parameters[0])
54
59
  next unless name
60
+
55
61
  object.add_property(create_property(name, node))
56
62
  elsif method_name == 'newparam'
57
63
  # Add a parameter to the object
58
64
  next unless parameters.count >= 1
65
+
59
66
  name = node_as_string(parameters[0])
60
67
  next unless name
68
+
61
69
  object.add_parameter(create_parameter(name, node))
62
70
  elsif method_name == 'newcheck'
63
71
  # Add a check to the object
64
72
  next unless parameters.count >= 1
73
+
65
74
  name = node_as_string(parameters[0])
66
75
  next unless name
76
+
67
77
  object.add_check(create_check(name, node))
68
78
  elsif method_name == 'feature'
69
79
  # Add a feature to the object
70
80
  next unless parameters.count >= 2
81
+
71
82
  name = node_as_string(parameters[0])
72
83
  next unless name
73
84
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # The module for custom YARD handlers.
2
4
  module PuppetStrings::Yard::Handlers
3
5
  # The module for custom Ruby YARD handlers.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'puppet-strings/yard/parsers/json/task_statement'
2
4
 
3
5
  class PuppetStrings::Yard::Parsers::JSON::Parser < YARD::Parser::Base
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module PuppetStrings::Yard::Parsers::JSON
2
4
  # Represents the Puppet Task statement.
3
5
  class TaskStatement
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'puppet'
2
4
  require 'puppet/pops'
3
5
  require 'puppet-strings/yard/parsers/puppet/statement'
@@ -20,14 +22,16 @@ class PuppetStrings::Yard::Parsers::Puppet::Parser < YARD::Parser::Base
20
22
  # @return [void]
21
23
  def parse
22
24
  begin
23
- Puppet[:tasks] = true if Puppet.settings.include?(:tasks)
24
- if Puppet::Util::Package.versioncmp(Puppet.version, "5.0.0") < 0 && @file.to_s.match(/^plans\//)
25
- log.warn "Skipping #{@file}: Puppet Plans require Puppet 5 or greater."
26
- return
25
+ if @file.to_s.match(/^plans|\/plans\//)
26
+ if Puppet::Util::Package.versioncmp(Puppet.version, "5.0.0") < 0
27
+ log.warn "Skipping #{@file}: Puppet Plans require Puppet 5 or greater."
28
+ return
29
+ end
30
+ Puppet[:tasks] = true if Puppet.settings.include?(:tasks)
27
31
  end
28
32
  @statements ||= (@visitor.visit(::Puppet::Pops::Parser::Parser.new.parse_string(source)) || []).compact
29
- rescue ::Puppet::ParseError => ex
30
- log.error "Failed to parse #{@file}: #{ex.message}"
33
+ rescue ::Puppet::ParseError => e
34
+ log.error "Failed to parse #{@file}: #{e.message}"
31
35
  @statements = []
32
36
  end
33
37
  @statements.freeze
@@ -42,47 +46,47 @@ class PuppetStrings::Yard::Parsers::Puppet::Parser < YARD::Parser::Base
42
46
 
43
47
  private
44
48
 
45
- def transform_Program(o) # rubocop:disable Naming/UncommunicativeMethodParamName
49
+ def transform_Program(o)
46
50
  # Cache the lines of the source text; we'll use this to locate comments
47
51
  @lines = o.source_text.lines.to_a
48
52
  o.definitions.map { |d| @visitor.visit(d) }
49
53
  end
50
54
 
51
- def transform_Factory(o) # rubocop:disable Naming/UncommunicativeMethodParamName
55
+ def transform_Factory(o)
52
56
  @visitor.visit(o.current)
53
57
  end
54
58
 
55
- def transform_HostClassDefinition(o) # rubocop:disable Naming/UncommunicativeMethodParamName
59
+ def transform_HostClassDefinition(o)
56
60
  statement = PuppetStrings::Yard::Parsers::Puppet::ClassStatement.new(o, @file)
57
61
  statement.extract_docstring(@lines)
58
62
  statement
59
63
  end
60
64
 
61
- def transform_ResourceTypeDefinition(o) # rubocop:disable Naming/UncommunicativeMethodParamName
65
+ def transform_ResourceTypeDefinition(o)
62
66
  statement = PuppetStrings::Yard::Parsers::Puppet::DefinedTypeStatement.new(o, @file)
63
67
  statement.extract_docstring(@lines)
64
68
  statement
65
69
  end
66
70
 
67
- def transform_FunctionDefinition(o) # rubocop:disable Naming/UncommunicativeMethodParamName
71
+ def transform_FunctionDefinition(o)
68
72
  statement = PuppetStrings::Yard::Parsers::Puppet::FunctionStatement.new(o, @file)
69
73
  statement.extract_docstring(@lines)
70
74
  statement
71
75
  end
72
76
 
73
- def transform_PlanDefinition(o) # rubocop:disable Naming/UncommunicativeMethodParamName
77
+ def transform_PlanDefinition(o)
74
78
  statement = PuppetStrings::Yard::Parsers::Puppet::PlanStatement.new(o, @file)
75
79
  statement.extract_docstring(@lines)
76
80
  statement
77
81
  end
78
82
 
79
- def transform_TypeAlias(o) # rubocop:disable Naming/UncommunicativeMethodParamName
83
+ def transform_TypeAlias(o)
80
84
  statement = PuppetStrings::Yard::Parsers::Puppet::DataTypeAliasStatement.new(o, @file)
81
85
  statement.extract_docstring(@lines)
82
86
  statement
83
87
  end
84
88
 
85
- def transform_Object(o) # rubocop:disable Naming/UncommunicativeMethodParamName
89
+ def transform_Object(o)
86
90
  # Ignore anything else (will be compacted out of the resulting array)
87
91
  end
88
92
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'puppet'
2
4
  require 'puppet/pops'
3
5
 
@@ -32,11 +34,13 @@ module PuppetStrings::Yard::Parsers::Puppet
32
34
  comment = []
33
35
  (0..@line-2).reverse_each do |index|
34
36
  break unless index <= lines.count
37
+
35
38
  line = lines[index].strip
36
39
  count = line.size
37
40
  line.gsub!(COMMENT_REGEX, '')
38
41
  # Break out if nothing was removed (wasn't a comment line)
39
42
  break unless line.size < count
43
+
40
44
  comment << line
41
45
  end
42
46
  @comments_range = (@line - comment.size - 1..@line - 1)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # The module for custom YARD parsers.
2
4
  module PuppetStrings::Yard::Parsers
3
5
  # The module for custom YARD parsers for JSON.