Skip to content

Commit

Permalink
Cleanup, gemify
Browse files Browse the repository at this point in the history
  • Loading branch information
zverok committed Aug 26, 2017
1 parent 1791c23 commit 83fc86e
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 48 deletions.
42 changes: 42 additions & 0 deletions .rubocop.yml
@@ -0,0 +1,42 @@
inherit_from: .rubocop_todo.yml
require: rubocop-rspec

AllCops:
Include:
- 'lib/**/*'
- 'spec/**/*'
Exclude:
- 'vendor/**/*'
- 'examples/**/*'
- 'tmp/**/*'
- Gemfile
- Rakefile
- '*.gemspec'
DisplayCopNames: true
TargetRubyVersion: '2.3'

Style/PercentLiteralDelimiters:
PreferredDelimiters:
default: '{}'

Style/SignalException:
EnforcedStyle: semantic

Style/RegexpLiteral:
Enabled: false

Style/FormatStringToken:
Enabled: false

Style/AndOr:
EnforcedStyle: conditionals

Layout/SpaceInsideHashLiteralBraces:
EnforcedStyle: no_space

Metrics/LineLength:
Enabled: false

# TODO
Style/Documentation:
Enabled: false
1 change: 1 addition & 0 deletions .rubocop_todo.yml
@@ -0,0 +1 @@

8 changes: 1 addition & 7 deletions Gemfile
@@ -1,9 +1,3 @@
source 'https://rubygems.org'

gem 'yard'

group :test do
gem 'rspec'
gem 'rspec-its'
gem 'fakefs'
end
gemspec
40 changes: 40 additions & 0 deletions junk_yard.gemspec
@@ -0,0 +1,40 @@
Gem::Specification.new do |s|
s.name = 'junk_yard'
s.version = '0.0.1pre'
s.authors = ['Victor Shepelev']
s.email = 'zverok.offline@gmail.com'
s.homepage = 'https://github.com/zverok/junk_yard'

s.summary = 'Get rid of junk in your YARD docs'
s.description = <<-EOF
JunkYard is structured logger/error validator plugin for YARD documentation gem.
EOF
s.licenses = ['MIT']

s.required_ruby_version = '>= 2.1.0'

s.files = `git ls-files`.split($RS).reject do |file|
file =~ /^(?:
spec\/.*
|Gemfile
|Rakefile
|\.rspec
|\.gitignore
|\.rubocop.yml
|\.travis.yml
)$/x
end
s.require_paths = ["lib"]

s.add_dependency 'yard'

s.add_development_dependency 'rubocop', '>= 0.30'
s.add_development_dependency 'rspec', '>= 3'
s.add_development_dependency 'rubocop-rspec'
s.add_development_dependency 'rspec-its', '~> 1'
s.add_development_dependency 'fakefs'
s.add_development_dependency 'simplecov', '~> 0.9'
s.add_development_dependency 'rake'
s.add_development_dependency 'rubygems-tasks'
s.add_development_dependency 'yard'
end
36 changes: 19 additions & 17 deletions lib/junk_yard.rb
@@ -1,21 +1,23 @@
require_relative 'junk_yard/logger'

#class YARD::CLI::Yardoc
#alias parse_options_no_junk parse_options

#def parse_options(opts, *arg)
#opts.separator ""
#opts.separator "JunkYard plugin options"
# frozen_string_literal: true

#opts.on('--junk-logger-format [FORMAT]', 'JunkYard::Logger format string') do
#p "HERE we go!"
#end

#opts.separator ""
#opts.separator "Generic options"
require_relative 'junk_yard/logger'

#parse_options_no_junk(opts, *arg)
#end
#end
# class YARD::CLI::Yardoc
# alias parse_options_no_junk parse_options
#
# def parse_options(opts, *arg)
# opts.separator ""
# opts.separator "JunkYard plugin options"
#
# opts.on('--junk-logger-format [FORMAT]', 'JunkYard::Logger format string') do
# p "HERE we go!"
# end
#
# opts.separator ""
# opts.separator "Generic options"
#
# parse_options_no_junk(opts, *arg)
# end
# end

YARD::Logger.prepend JunkYard::Logger::Mixin
12 changes: 5 additions & 7 deletions lib/junk_yard/logger.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'singleton'
require 'pp'

Expand All @@ -12,11 +14,9 @@ def messages
end

def register(msg)
#puts msg
#p caller[1..3]
message = Message.registry
.map { |t| t.try_parse(msg, file: @current_parsed_file) }
.compact.first || Message.new(message: msg, file: @current_parsed_file)
.map { |t| t.try_parse(msg, file: @current_parsed_file) }
.compact.first || Message.new(message: msg, file: @current_parsed_file)
messages << message
puts message
end
Expand All @@ -28,9 +28,7 @@ def start_file(name)
module Mixin
def debug(msg)
# TODO: fragile regexp; cleanup it after everything is parsed.
if msg =~ /Parsing (\w\S+)$/
JunkYard::Logger.instance.start_file($1)
end
JunkYard::Logger.instance.start_file(Regexp.last_match(1)) if msg =~ /Parsing (\w\S+)$/
super
end

Expand Down
21 changes: 12 additions & 9 deletions lib/junk_yard/logger/message.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module JunkYard
class Logger
class Message
Expand All @@ -23,7 +25,7 @@ def ==(other)
other.is_a?(self.class) && to_h == other.to_h
end

DEFAULT_FORMAT = '%{file}:%{line}: [%{type}] %{message}'.freeze
DEFAULT_FORMAT = '%{file}:%{line}: [%{type}] %{message}'

def to_s(format = DEFAULT_FORMAT)
format % to_h
Expand All @@ -45,32 +47,33 @@ def pattern(regexp)
Message.registry << self
end

def search_up(pattern)
def search_up(pattern) # rubocop:disable Style/TrivialAccessors
@search_up = pattern
end

def try_parse(line, **context)
@pattern or fail StandardError, "Pattern is not defined for #{self}"
match = @pattern.match(line) or return nil
data = context.reject { |_, v| v.nil? }.merge(match.names.map(&:to_sym).zip(match.captures).to_h.reject { |_, v| v.nil? })
data = context.reject { |_, v| v.nil? }
.merge(match.names.map(&:to_sym).zip(match.captures).to_h.reject { |_, v| v.nil? })
data = guard_line(data)
new(**data)
end

private

def guard_line(data)
def guard_line(data) # rubocop:disable Metrics/AbcSize
data[:file] && data[:line] && @search_up or return data
data = data.merge(line: data[:line].to_i)
lines = File.readlines(data[:file]) rescue (return data)
lines = File.readlines(data[:file]) rescue (return data) # rubocop:disable Style/RescueModifier
pattern = Regexp.new(@search_up % data)
_, num = lines.map
.with_index { |ln, i| [ln, i + 1] }
.first(data[:line]).reverse
.detect { |ln, i| pattern.match(ln) }
.with_index { |ln, i| [ln, i + 1] }
.first(data[:line]).reverse
.detect { |ln, _| pattern.match(ln) }
num or return data

return data.merge(line: num)
data.merge(line: num)
end
end
end
Expand Down
19 changes: 19 additions & 0 deletions spec/.rubocop.yml
@@ -0,0 +1,19 @@
inherit_from: ../.rubocop.yml

Style/BlockDelimiters:
Enabled: false

Metrics/BlockLength:
Enabled: false

Layout/MultilineBlockLayout:
Enabled: false

Layout/AlignParameters:
EnforcedStyle: with_fixed_indentation

RSpec/DescribeClass:
Enabled: false

RSpec/BeforeAfterAll:
Enabled: false
3 changes: 2 additions & 1 deletion spec/junk_yard/integration/inject_in_parsing_spec.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'yard'
require 'junk_yard/logger'

Expand Down Expand Up @@ -74,7 +76,6 @@ def foo
directive: '@!hello',
line: 2


it_behaves_like 'file parser', 'invalid directive format',
%{
# @!macro
Expand Down
@@ -1,48 +1,57 @@
# frozen_string_literal: true

require 'yard'
require 'junk_yard/logger'

RSpec.describe JunkYard::Logger::Message do
include FakeFS::SpecHelpers

describe '.try_parse' do
subject { klass.try_parse(input) }

let(:klass) {
Class.new(described_class) {
pattern %r{^(?<message>Unknown tag @(?<tag>\S+))( in file `(?<file>[^`]+)` near line (?<line>\d+))?$}
}
}
before { allow(klass).to receive(:name).and_return('UnknownTag') }

subject { klass.try_parse(input) }
before { allow(klass).to receive(:name).and_return('UnknownTag') }

context 'when matches' do
let(:input) { 'Unknown tag @wrong in file `input/lot_of_errors.rb` near line 15' }

its(:to_h) {
is_expected.to eq(name: 'UnknownTag', message: 'Unknown tag @wrong', tag: 'wrong', file: 'input/lot_of_errors.rb', line: 15)
is_expected.to eq(type: 'UnknownTag', message: 'Unknown tag @wrong', tag: 'wrong', file: 'input/lot_of_errors.rb', line: 15)
}
end

context 'when partial match' do
let(:input) { 'Unknown tag @wrong' }

its(:to_h) {
is_expected.to eq(name: 'UnknownTag', message: 'Unknown tag @wrong', tag: 'wrong', file: nil, line: nil)
is_expected.to eq(type: 'UnknownTag', message: 'Unknown tag @wrong', tag: 'wrong', file: nil, line: nil)
}
end

context 'with parsing context' do
let(:input) { 'Unknown tag @wrong' }
subject { klass.try_parse(input, file: 'input/lot_of_errors.rb') }

let(:input) { 'Unknown tag @wrong' }

its(:to_h) {
is_expected.to eq(name: 'UnknownTag', message: 'Unknown tag @wrong', tag: 'wrong', file: 'input/lot_of_errors.rb', line: nil)
is_expected.to eq(type: 'UnknownTag', message: 'Unknown tag @wrong', tag: 'wrong', file: 'input/lot_of_errors.rb', line: nil)
}
end

context 'when not matches' do
let(:input) { '@param tag has unknown parameter name: arg3' }

it { is_expected.to be_nil }
end

context 'with search_up' do
let(:input) { 'Unknown tag @wrong in file `lot_of_errors.rb` near line 5' }

before {
klass.search_up '@%{tag}(\W|$)'
File.write 'lot_of_errors.rb', %{
Expand All @@ -61,10 +70,12 @@ def foo

describe '#to_s' do
context 'by default' do
subject { klass.new(message: 'Unknown tag @wrong', tag: 'wrong', file: 'lot_of_errors.rb', line: 2) }

let(:klass) {
Class.new(described_class)
}
subject { klass.new(message: 'Unknown tag @wrong', tag: 'wrong', file: 'lot_of_errors.rb', line: 2) }

before { allow(klass).to receive(:name).and_return('UnknownTag') }

its(:to_s) { is_expected.to eq 'lot_of_errors.rb:2: [UnknownTag] Unknown tag @wrong' }
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'rspec/its'
require 'fakefs/spec_helpers'
require 'pp'
Expand Down

0 comments on commit 83fc86e

Please sign in to comment.