Skip to content

Commit

Permalink
Add RubyParser helper class
Browse files Browse the repository at this point in the history
Add helper class that encapsulates the `parser` and `astrolabe` gems so
that all linters need to do is call `parse_ruby` to get an AST.

Change-Id: I3ae6d7899766ff791ca8107593e115aae20ae97e
Reviewed-on: http://gerrit.causes.com/42570
Tested-by: jenkins <[email protected]>
Reviewed-by: Shane da Silva <[email protected]>
  • Loading branch information
sds committed Sep 14, 2014
1 parent 226d9ac commit d76773e
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions haml-lint.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ Gem::Specification.new do |s|
s.add_dependency 'sysexits', '~> 1.1'

s.add_development_dependency 'rspec', '~> 3.0'
s.add_development_dependency 'rspec-its', '~> 1.0'
s.add_development_dependency 'rubocop', '0.26.0' # Pin for Travis builds
end
1 change: 1 addition & 0 deletions lib/haml_lint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require 'haml_lint/haml_visitor'
require 'haml_lint/lint'
require 'haml_lint/linter_registry'
require 'haml_lint/ruby_parser'
require 'haml_lint/linter'
require 'haml_lint/logger'
require 'haml_lint/reporter'
Expand Down
9 changes: 9 additions & 0 deletions lib/haml_lint/linter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Linter
def initialize(config)
@config = config
@lints = []
@ruby_parser = nil
end

def run(parser)
Expand All @@ -29,6 +30,14 @@ def add_lint(node, message = nil)
@lints << Lint.new(self, parser.filename, node.line, message || self.message)
end

# Parse Ruby code into an abstract syntax tree.
#
# @return [AST::Node]
def parse_ruby(source)
@ruby_parser ||= HamlLint::RubyParser.new
@ruby_parser.parse(source)
end

# Remove the surrounding double quotes from a string, ignoring any
# leading/trailing whitespace.
#
Expand Down
29 changes: 29 additions & 0 deletions lib/haml_lint/ruby_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'astrolabe/builder'
require 'parser/current'

module HamlLint
# Parser for the Ruby language.
#
# This provides a convenient wrapper around the `parser` gem and the
# `astrolabe` integration to go with it. It is intended to be used for linter
# checks that require deep inspection of Ruby code.
class RubyParser
# Creates a reusable parser.
def initialize
@builder = ::Astrolabe::Builder.new
@parser = ::Parser::CurrentRuby.new(@builder)
end

# Parse the given Ruby source into an abstract syntax tree.
#
# @param source [String] Ruby source code
# @return [Array] syntax tree in the form returned by Parser gem
def parse(source)
buffer = ::Parser::Source::Buffer.new('(string)')
buffer.source = source

@parser.reset
@parser.parse(buffer)
end
end
end
22 changes: 22 additions & 0 deletions spec/haml_lint/ruby_parser_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'spec_helper'

describe HamlLint::RubyParser do
describe '#parse' do
subject { super().parse(source) }

context 'when given an empty string' do
let(:source) { '' }

it { should be_nil }
end

context 'when given a valid Ruby program' do
let(:source) { "puts 'Hello World'" }

it { should respond_to :children }
it { should respond_to :type }
it { should respond_to :parent }
its(:type) { should == :send }
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'haml_lint'
require 'rspec/its'

Dir[File.dirname(__FILE__) + '/support/**/*.rb'].each { |f| require f }

Expand Down

0 comments on commit d76773e

Please sign in to comment.