Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add support for Haml 5
Haml 5 is has been in Beta 2 in their repository for a while and has some breaking API changed when compared with Haml 4. By adding an adapter layer, we can maintain compatibility with both Haml 4 and 5. To test on both Haml 4 and Haml 5, this introduces the Appraisal gem that allows you specify different Gemfiles to test against. There was a slight incompatibility with the Overcommit tool that we use during our build process that required a tweak to the Overcommit configuration. Overcommit's bundler integration works by overriding the `BUNDLER_GEMFILE` environment variable. Appraisal works by specifying this environment variable before running a command. The two were conflicting and Overcommit was always overriding the Gemfile. By disabling this feature, we can test on both Haml 4 and Haml 5 with an easy-to-use command, yet still use the Overcommit gem as intended, since it is present in both of the generated Gemfiles. Closes #152
- Loading branch information
1 parent
5b1cb13
commit 2403072
Showing
16 changed files
with
208 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
Gemfile.lock | ||
/coverage | ||
/gemfiles/*.lock | ||
/pkg |
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,7 @@ | ||
appraise 'haml4' do | ||
gem 'haml', '~> 4' | ||
end | ||
|
||
appraise 'haml5' do | ||
gem 'haml', '~> 5.0.0.beta.2', git: 'https://github.com/haml/haml.git' | ||
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
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,13 @@ | ||
# This file was generated by Appraisal | ||
|
||
source "https://rubygems.org" | ||
|
||
gem "haml", "~> 4" | ||
gem "rspec", "~> 3.0" | ||
gem "rspec-its", "~> 1.0" | ||
gem "appraisal" | ||
gem "overcommit", "0.37.0" | ||
gem "rubocop", "0.47.0" | ||
gem "coveralls", :require => false | ||
|
||
gemspec :path => "../" |
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,13 @@ | ||
# This file was generated by Appraisal | ||
|
||
source "https://rubygems.org" | ||
|
||
gem "haml", "~> 5.0.0.beta.2", :git => "https://github.com/haml/haml.git" | ||
gem "rspec", "~> 3.0" | ||
gem "rspec-its", "~> 1.0" | ||
gem "appraisal" | ||
gem "overcommit", "0.37.0" | ||
gem "rubocop", "0.47.0" | ||
gem "coveralls", :require => false | ||
|
||
gemspec :path => "../" |
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,36 @@ | ||
require 'haml_lint/adapter/haml_4' | ||
require 'haml_lint/adapter/haml_5' | ||
require 'haml_lint/exceptions' | ||
|
||
module HamlLint | ||
# Determines the adapter to use for the current Haml version | ||
class Adapter | ||
# Detects the adapter to use for the current Haml version | ||
# | ||
# @example | ||
# HamlLint::Adapter.detect_class.new('%div') | ||
# | ||
# @api public | ||
# @return [Class] the adapter class | ||
# @raise [HamlLint::Exceptions::UnknownHamlVersion] | ||
def self.detect_class | ||
version = haml_version | ||
case version | ||
when '~> 4.0' then HamlLint::Adapter::Haml4 | ||
when '~> 5.0' then HamlLint::Adapter::Haml5 | ||
else fail HamlLint::Exceptions::UnknownHamlVersion, "Cannot handle Haml version: #{version}" | ||
end | ||
end | ||
|
||
# Determines the approximate version of Haml that is installed | ||
# | ||
# @api private | ||
# @return [String] the approximate Haml version | ||
def self.haml_version | ||
Gem::Version | ||
.new(Haml::VERSION) | ||
.approximate_recommendation | ||
end | ||
private_class_method :haml_version | ||
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,40 @@ | ||
module HamlLint | ||
class Adapter | ||
# Adapts the Haml::Parser from Haml 4 for use in HamlLint | ||
# :reek:UncommunicativeModuleName | ||
class Haml4 < Adapter | ||
extend Forwardable | ||
|
||
# Parses the specified Haml code into an abstract syntax tree | ||
# | ||
# @example | ||
# HamlLint::Adapter::Haml4.new('%div') | ||
# | ||
# @api public | ||
# @param source [String] Haml code to parse | ||
# @param options [Haml::Options] | ||
def initialize(source, options = Haml::Options.new) | ||
@parser = Haml::Parser.new(source, options) | ||
end | ||
|
||
# @!method | ||
# Parses the source code into an abstract syntax tree | ||
# | ||
# @example | ||
# HamlLint::Adapter::Haml4.new('%div') | ||
# | ||
# @api public | ||
# @return [Haml::Parser::ParseNode] | ||
# @raise [Haml::Error] | ||
def_delegator :parser, :parse | ||
|
||
private | ||
|
||
# The Haml parser to adapt for HamlLint | ||
# | ||
# @api private | ||
# @return [Haml::Parser] the Haml 4 parser | ||
attr_reader :parser | ||
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,46 @@ | ||
module HamlLint | ||
class Adapter | ||
# Adapts the Haml::Parser from Haml 5 for use in HamlLint | ||
# :reek:UncommunicativeModuleName | ||
class Haml5 < Adapter | ||
# Parses the specified Haml code into an abstract syntax tree | ||
# | ||
# @example | ||
# HamlLint::Adapter::Haml5.new('%div') | ||
# | ||
# @api public | ||
# @param source [String] Haml code to parse | ||
# @param options [Haml::Options] | ||
def initialize(source, options = Haml::Options.new) | ||
@source = source | ||
@parser = Haml::Parser.new(options) | ||
end | ||
|
||
# Parses the source code into an abstract syntax tree | ||
# | ||
# @example | ||
# HamlLint::Adapter::Haml5.new('%div').parse | ||
# | ||
# @api public | ||
# @return [Haml::Parser::ParseNode] | ||
# @raise [Haml::Error] | ||
def parse | ||
parser.call(source) | ||
end | ||
|
||
private | ||
|
||
# The Haml parser to adapt for HamlLint | ||
# | ||
# @api private | ||
# @return [Haml::Parser] the Haml 4 parser | ||
attr_reader :parser | ||
|
||
# The Haml code to parse | ||
# | ||
# @api private | ||
# @return [String] Haml code to parse | ||
attr_reader :source | ||
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
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,27 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe HamlLint::Adapter do | ||
describe '.detect_class' do | ||
subject { described_class.detect_class } | ||
|
||
context 'on Haml 4' do | ||
before { stub_const('Haml::VERSION', '4.0.7') } | ||
|
||
it { should == HamlLint::Adapter::Haml4 } | ||
end | ||
|
||
context 'on Haml 5' do | ||
before { stub_const('Haml::VERSION', '5.0.0.beta.2') } | ||
|
||
it { should == HamlLint::Adapter::Haml5 } | ||
end | ||
|
||
context 'on unknown version of Haml' do | ||
before { stub_const('Haml::VERSION', '3.0.0') } | ||
|
||
it 'raises an error' do | ||
expect { subject }.to raise_error(HamlLint::Exceptions::UnknownHamlVersion) | ||
end | ||
end | ||
end | ||
end |