Module: MilkTea::Linter::LinterSourceHelpers

Included in:
MilkTea::Linter
Defined in:
lib/milk_tea/tooling/linter/source_helpers.rb

Instance Method Summary collapse

Instance Method Details

#condition_span(expr, line:, keyword_pattern:) ⇒ Object



279
280
281
282
283
284
285
286
# File 'lib/milk_tea/tooling/linter/source_helpers.rb', line 279

def condition_span(expr, line:, keyword_pattern:)
  source_span = source_condition_span(line, keyword_pattern:)
  if source_span
    [line, source_span.first, source_span.last]
  else
    [expression_line(expr) || line, expression_column(expr), expression_length(expr)]
  end
end

#condition_symbol_name(expr) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/milk_tea/tooling/linter/source_helpers.rb', line 288

def condition_symbol_name(expr)
  case expr
  when AST::Identifier
    expr.name
  when AST::BooleanLiteral
    expr.value ? "true" : "false"
  when AST::BinaryOp
    condition_symbol_name(expr.left) || condition_symbol_name(expr.right)
  when AST::UnaryOp
    condition_symbol_name(expr.operand)
  when AST::Call
    condition_symbol_name(expr.callee)
  when AST::IndexAccess
    condition_symbol_name(expr.receiver)
  when AST::MemberAccess
    condition_symbol_name(expr.receiver)
  when AST::RangeExpr
    condition_symbol_name(expr.start_expr) || condition_symbol_name(expr.end_expr)
  else
    nil
  end
end

#declaration_column(declaration) ⇒ Object



84
85
86
# File 'lib/milk_tea/tooling/linter/source_helpers.rb', line 84

def declaration_column(declaration)
  declaration.column
end

#each_event_declaration(source_file) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/milk_tea/tooling/linter/source_helpers.rb', line 49

def each_event_declaration(source_file)
  source_file.declarations.each do |declaration|
    case declaration
    when AST::EventDecl
      yield declaration, nil
    when AST::StructDecl
      declaration.events.each { |event_decl| yield event_decl, declaration.name }
      each_nested_struct_event_declaration(declaration, owner_name: declaration.name) { |ev, owner| yield ev, owner }
    end
  end
end

#each_nested_struct_event_declaration(struct_decl, owner_name:) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/milk_tea/tooling/linter/source_helpers.rb', line 61

def each_nested_struct_event_declaration(struct_decl, owner_name:)
  struct_decl.nested_types.each do |nested|
    next unless nested.is_a?(AST::StructDecl)

    nested_owner = "#{owner_name}.#{nested.name}"
    nested.events.each { |event_decl| yield event_decl, nested_owner }
    each_nested_struct_event_declaration(nested, owner_name: nested_owner) { |ev, owner| yield ev, owner }
  end
end

#each_statement_expression(statement, &block) ⇒ Object



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/milk_tea/tooling/linter/source_helpers.rb', line 310

def each_statement_expression(statement, &block)
  case statement
  when AST::LocalDecl
    walk_expression_tree(statement.value, &block)
  when AST::Assignment
    walk_expression_tree(statement.target, &block)
    walk_expression_tree(statement.value, &block)
  when AST::IfBranch
    walk_expression_tree(statement.condition, &block)
  when AST::IfStmt
    statement.branches.each { |branch| walk_expression_tree(branch.condition, &block) }
  when AST::WhileStmt
    walk_expression_tree(statement.condition, &block)
  when AST::ForStmt
    statement.iterables.each { |iterable| walk_expression_tree(iterable, &block) }
  when AST::MatchStmt
    walk_expression_tree(statement.expression, &block)
  when AST::ReturnStmt
    walk_expression_tree(statement.value, &block)
  when AST::DeferStmt
    walk_expression_tree(statement.expression, &block)
  when AST::ExpressionStmt
    walk_expression_tree(statement.expression, &block)
  when AST::StaticAssert
    walk_expression_tree(statement.condition, &block)
  when AST::WhenStmt
    walk_expression_tree(statement.discriminant, &block)
    statement.branches.each { |branch| branch.body.each { |s| each_statement_expression(s, &block) } }
    statement.else_body&.each { |s| each_statement_expression(s, &block) }
  when AST::UnsafeStmt
    statement.body.each { |s| each_statement_expression(s, &block) }
  when AST::ErrorBlockStmt
    statement.body.each { |s| each_statement_expression(s, &block) }
  end
end

#emit_event_capacity_warnings(source_file) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/milk_tea/tooling/linter/source_helpers.rb', line 41

def emit_event_capacity_warnings(source_file)
  each_event_declaration(source_file) do |event_decl, owner_name|
    next unless event_decl.capacity >= EVENT_STACK_SNAPSHOT_WARNING_THRESHOLD

    warn_large_event_capacity(event_decl, owner_name:)
  end
end

#emit_line_too_long_warningsObject



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
# File 'lib/milk_tea/tooling/linter/source_helpers.rb', line 6

def emit_line_too_long_warnings
  return unless @max_line_length.positive?
  return if @source.empty?

  @source_lines.each_with_index do |line, index|
    next if line.empty?
    next if external_or_foreign_function_header_line?(line)
    next unless line.length > @max_line_length
    next unless Formatter.wrappable_long_line_candidate_text?(line)

    fix = Formatter.build_long_line_wrap_fix(
      @source,
      index,
      max_line_length: @max_line_length,
      path: @path,
      tokens: @tokens,
      tokens_by_line: @tokens_by_line,
      validate: false,
    )
    message = "line exceeds max length of #{@max_line_length} columns (#{line.length})"
    message << "; wrap the expression" if fix
    @warnings << Warning.new(
      path: @path,
      line: index + 1,
      column: @max_line_length + 1,
      length: line.length - @max_line_length,
      code: "line-too-long",
      message:,
      severity: :warning,
    )
  end
end

#expression_column(expr) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/milk_tea/tooling/linter/source_helpers.rb', line 158

def expression_column(expr)
  return nil unless expr

  if expr.column
    return expr.column
  end

  case expr
  when AST::BinaryOp
    expression_column(expr.left) || expression_column(expr.right)
  when AST::UnaryOp
    expression_column(expr.operand)
  when AST::Specialization
    expression_column(expr.callee)
  when AST::Call
    expression_column(expr.callee)
  when AST::IndexAccess
    expression_column(expr.receiver)
  when AST::MemberAccess
    expression_column(expr.receiver)
  when AST::RangeExpr
    expression_column(expr.start_expr) || expression_column(expr.end_expr)
  when AST::ExpressionList
    expr.elements.filter_map { |element| expression_column(element) }.first
  when AST::IfExpr
    expression_column(expr.condition) || expression_column(expr.then_expression) || expression_column(expr.else_expression)
  when AST::AwaitExpr
    expression_column(expr.expression)
  when AST::UnsafeExpr
    expression_column(expr.expression)
  else
    nil
  end
end

#expression_length(expr) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/milk_tea/tooling/linter/source_helpers.rb', line 193

def expression_length(expr)
  return nil unless expr

  case expr
  when AST::Identifier
    expr.name.length
  when AST::BooleanLiteral
    expr.value ? 4 : 5
  when AST::NullLiteral
    4
  when AST::IntegerLiteral, AST::FloatLiteral, AST::StringLiteral
    expr.lexeme.length
  when AST::BinaryOp
    left_column = expression_column(expr.left)
    right_column = expression_column(expr.right)
    right_length = expression_length(expr.right)
    if left_column && right_column && right_length
      (right_column + right_length) - left_column
    end
  when AST::UnaryOp
    expression_length(expr.operand)
  when AST::Specialization
    expression_length(expr.callee)
  when AST::Call
    expression_length(expr.callee)
  when AST::AwaitExpr
    expression_length(expr.expression)
  when AST::UnsafeExpr
    expression_length(expr.expression)
  else
    1
  end
end

#expression_line(expr) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/milk_tea/tooling/linter/source_helpers.rb', line 121

def expression_line(expr)
  return nil unless expr

  return expr.line if expr.line

  case expr
  when AST::BinaryOp
    expression_line(expr.left) || expression_line(expr.right)
  when AST::UnaryOp
    expression_line(expr.operand)
  when AST::Specialization
    expression_line(expr.callee)
  when AST::Call
    expression_line(expr.callee) || expr.arguments.filter_map { |argument| expression_line(argument.value) }.first
  when AST::IndexAccess
    expression_line(expr.receiver) || expression_line(expr.index)
  when AST::MemberAccess
    expression_line(expr.receiver)
  when AST::RangeExpr
    expression_line(expr.start_expr) || expression_line(expr.end_expr)
  when AST::ExpressionList
    expr.elements.filter_map { |element| expression_line(element) }.first
  when AST::IfExpr
    expression_line(expr.condition) || expression_line(expr.then_expression) || expression_line(expr.else_expression)
  when AST::AwaitExpr
    expression_line(expr.expression)
  when AST::UnsafeExpr
    expression_line(expr.expression)
  when AST::FormatString
    expr.parts.filter_map do |part|
      expression_line(part.expression) if part.is_a?(AST::FormatExprPart)
    end.first
  else
    nil
  end
end

#external_or_foreign_function_header_line?(line) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/milk_tea/tooling/linter/source_helpers.rb', line 38

def external_or_foreign_function_header_line?(line)
  line.strip.match?(/\A(?:[A-Za-z_]\w*\s+)*(?:external|foreign)\s+function\b/)
end

#param_column(param) ⇒ Object



80
81
82
# File 'lib/milk_tea/tooling/linter/source_helpers.rb', line 80

def param_column(param)
  param.column
end

#param_line(param, fallback: nil) ⇒ Object



75
76
77
78
# File 'lib/milk_tea/tooling/linter/source_helpers.rb', line 75

def param_line(param, fallback: nil)
  line = param.line
  line || fallback
end

#profile_phase(name) ⇒ Object



70
71
72
73
74
# File 'lib/milk_tea/tooling/linter/source_helpers.rb', line 70

def profile_phase(name)
  return yield unless @profile

  @profile.measure(name) { yield }
end

#source_code_line(line) ⇒ Object



93
94
95
96
97
98
# File 'lib/milk_tea/tooling/linter/source_helpers.rb', line 93

def source_code_line(line)
  text = source_line_text(line)
  return nil unless text

  text.rstrip
end

#source_condition_span(line, keyword_pattern:) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/milk_tea/tooling/linter/source_helpers.rb', line 109

def source_condition_span(line, keyword_pattern:)
  text = source_code_line(line)
  return nil unless text

  match = text.match(/\A\s*(?:#{keyword_pattern})\s+(.*?)\s*:/)
  return nil unless match

  condition = match[1].rstrip
  return nil if condition.empty?

  [match.begin(1) + 1, condition.length]
end

#source_line_text(line) ⇒ Object



87
88
89
90
91
# File 'lib/milk_tea/tooling/linter/source_helpers.rb', line 87

def source_line_text(line)
  return nil unless line && line >= 1 && line <= @source_lines.length

  @source_lines[line - 1]
end

#source_statement_span(line) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/milk_tea/tooling/linter/source_helpers.rb', line 100

def source_statement_span(line)
  text = source_code_line(line)
  return nil unless text

  start_index = text.index(/\S/)
  return nil unless start_index

  [start_index + 1, text.length - start_index]
end

#statement_column(statement) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/milk_tea/tooling/linter/source_helpers.rb', line 227

def statement_column(statement)
  return nil unless statement

  if statement.column
    return statement.column
  end

  case statement
  when AST::IfStmt
    statement.branches.first&.column || statement.else_column
  when AST::WhileStmt
    expression_column(statement.condition)
  when AST::MatchStmt
    expression_column(statement.expression)
  when AST::ReturnStmt
    expression_column(statement.value)
  when AST::DeferStmt
    expression_column(statement.expression)
  when AST::ExpressionStmt
    expression_column(statement.expression) || source_statement_span(statement.line)&.first
  else
    nil
  end
end

#statement_length(statement) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/milk_tea/tooling/linter/source_helpers.rb', line 252

def statement_length(statement)
  return nil unless statement

  if statement.respond_to?(:length) && statement.length
    return statement.length
  end

  case statement
  when AST::LocalDecl, AST::ForStmt
    statement.name.length
  when AST::IfStmt
    statement.branches.first&.length || (statement.else_column ? 4 : nil)
  when AST::WhileStmt
    expression_length(statement.condition)
  when AST::MatchStmt
    expression_length(statement.expression)
  when AST::ReturnStmt
    expression_length(statement.value)
  when AST::DeferStmt
    expression_length(statement.expression)
  when AST::ExpressionStmt
    expression_length(statement.expression) || source_statement_span(statement.line)&.last
  else
    nil
  end
end

#walk_expression_tree(expression) {|expression| ... } ⇒ Object

Yields:

  • (expression)


346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/milk_tea/tooling/linter/source_helpers.rb', line 346

def walk_expression_tree(expression, &block)
  return unless expression

  yield expression
  case expression
  when AST::MemberAccess
    walk_expression_tree(expression.receiver, &block)
  when AST::IndexAccess
    walk_expression_tree(expression.receiver, &block)
    walk_expression_tree(expression.index, &block)
  when AST::Specialization
    walk_expression_tree(expression.callee, &block)
  when AST::Call
    walk_expression_tree(expression.callee, &block)
    expression.arguments.each { |argument| walk_expression_tree(argument.value, &block) }
  when AST::UnaryOp
    walk_expression_tree(expression.operand, &block)
  when AST::BinaryOp
    walk_expression_tree(expression.left, &block)
    walk_expression_tree(expression.right, &block)
  when AST::RangeExpr
    walk_expression_tree(expression.start_expr, &block)
    walk_expression_tree(expression.end_expr, &block)
  when AST::ExpressionList
    expression.elements.each { |element| walk_expression_tree(element, &block) }
  when AST::IfExpr
    walk_expression_tree(expression.condition, &block)
    walk_expression_tree(expression.then_expression, &block)
    walk_expression_tree(expression.else_expression, &block)
  when AST::AwaitExpr
    walk_expression_tree(expression.expression, &block)
  when AST::UnsafeExpr
    walk_expression_tree(expression.expression, &block)
  when AST::FormatString
    expression.parts.each do |part|
      walk_expression_tree(part.expression, &block) if part.is_a?(AST::FormatExprPart)
    end
  end
end

#walk_statement_lists(stmts) {|stmts| ... } ⇒ Object

Yields:

  • (stmts)


385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/milk_tea/tooling/linter/source_helpers.rb', line 385

def walk_statement_lists(stmts, &block)
  return if stmts.nil? || stmts.empty?

  yield stmts
  stmts.each do |statement|
    case statement
    when AST::IfStmt
      statement.branches.each { |branch| walk_statement_lists(branch.body, &block) }
      walk_statement_lists(statement.else_body, &block) if statement.else_body
    when AST::MatchStmt
      statement.arms.each { |arm| walk_statement_lists(arm.body, &block) }
    when AST::UnsafeStmt, AST::ForStmt, AST::WhileStmt
      walk_statement_lists(statement.body, &block)
    when AST::DeferStmt
      walk_statement_lists(statement.body, &block) if statement.body
    end
  end
end