forked from sds/haml-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
6 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
|
||
|