Class: Mdlint::Linter::Rules::CodeBlockSyntax

Inherits:
Mdlint::Linter::Rule show all
Defined in:
lib/mdlint/linter/rules/code_block_syntax.rb,
sig/internal.rbs

Constant Summary collapse

SUPPORTED_LANGUAGES =
%w[json ruby].freeze

Instance Attribute Summary

Attributes inherited from Mdlint::Linter::Rule

#violations

Instance Method Summary collapse

Methods inherited from Mdlint::Linter::Rule

#add_violation, inherited, #initialize

Constructor Details

This class inherits a constructor from Mdlint::Linter::Rule

Instance Method Details

#check(tokens, _source) ⇒ Object



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
# File 'lib/mdlint/linter/rules/code_block_syntax.rb', line 19

def check(tokens, _source)
  commands = @options.fetch(:code_block_commands, {})
  return @violations unless @options[:check_code_blocks] || !commands.empty?

  tokens.each do |token|
    next unless token.type == :fence

    language = token.info.to_s.split.first.to_s.downcase
    if language.empty?
      add_violation(message: "Code block should specify a language", line: line_for(token), fixable: false)
      next
    end
    unless SUPPORTED_LANGUAGES.include?(language) || commands.key?(language)
      next
    end

    error = if SUPPORTED_LANGUAGES.include?(language)
              syntax_error(language, token.content)
            else
              command_error(commands.fetch(language), token.content)
            end
    next unless error

    add_violation(
      message: "Invalid #{language} syntax: #{error}",
      line: line_for(token),
      fixable: false
    )
  end
  @violations
end

#command_error(command, content) ⇒ Object

Parameters:

  • command (Object)
  • content (Object)

Returns:

  • (Object)


98
99
100
101
102
103
104
105
# File 'lib/mdlint/linter/rules/code_block_syntax.rb', line 98

def command_error(command, content)
  result = command_result(command, content)
  return nil if result[:status].success?

  result[:stderr].lines.first.to_s.strip.empty? ? "command exited with #{result[:status].exitstatus}" : result[:stderr].lines.first.strip
rescue StandardError => error
  "#{error.class}: #{error.message}"
end

#command_output(command, content) ⇒ Object

Parameters:

  • command (Object)
  • content (Object)

Returns:

  • (Object)


107
108
109
110
111
112
113
114
# File 'lib/mdlint/linter/rules/code_block_syntax.rb', line 107

def command_output(command, content)
  result = command_result(command, content)
  return result[:stdout] if result[:status].success?

  nil
rescue StandardError
  nil
end

#command_result(command, content) ⇒ Object

Parameters:

  • command (Object)
  • content (Object)

Returns:

  • (Object)

Raises:

  • (ArgumentError)


116
117
118
119
120
121
122
123
124
# File 'lib/mdlint/linter/rules/code_block_syntax.rb', line 116

def command_result(command, content)
  argv = Shellwords.split(command)
  raise ArgumentError, "empty code block command" if argv.empty?

  stdout, stderr, status = Timeout.timeout(@options.fetch(:code_block_timeout, 10).to_i) do
    Open3.capture3(*argv, stdin_data: content)
  end
  { stdout: stdout, stderr: stderr, status: status }
end

#fix(tokens, source) ⇒ Object



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
# File 'lib/mdlint/linter/rules/code_block_syntax.rb', line 51

def fix(tokens, source)
  commands = @options.fetch(:code_block_format_commands, {})
  return source if commands.empty?

  lines = source.lines
  tokens.reverse_each do |token|
    next unless token.type == :fence

    language = token.info.to_s.split.first.to_s.downcase
    command = commands[language]
    next unless command

    formatted = command_output(command, token.content)
    next unless formatted

    start_line = token.map&.first
    end_line = token.map&.last
    next unless start_line && end_line

    closing_line = end_line - 1
    closing_line = end_line unless lines[closing_line].to_s.match?(/\A {0,3}(`{3,}|~{3,})\s*\r?\n?\z/)
    replacement = formatted.end_with?("\n") ? formatted : "#{formatted}\n"
    body_length = [closing_line - start_line - 1, 0].max
    lines.slice!(start_line + 1, body_length)
    lines.insert(start_line + 1, *replacement.lines)
  end
  lines.join
end

#line_for(token) ⇒ Integer

Parameters:

  • token (Object)

Returns:

  • (Integer)


82
83
84
# File 'lib/mdlint/linter/rules/code_block_syntax.rb', line 82

def line_for(token)
  (token.map&.first || 0) + 1
end

#syntax_error(language, content) ⇒ Object

Parameters:

  • language (Object)
  • content (Object)

Returns:

  • (Object)


86
87
88
89
90
91
92
93
94
95
96
# File 'lib/mdlint/linter/rules/code_block_syntax.rb', line 86

def syntax_error(language, content)
  case language
  when "json"
    JSON.parse(content)
    nil
  when "ruby"
    Ripper.sexp(content) ? nil : "parser rejected the source"
  end
rescue JSON::ParserError => error
  error.message.lines.first.to_s.strip
end