This repository was archived by the owner on Aug 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Add rule for verifying consistent usage of quotes #115
Open
pbadenski
wants to merge
1
commit into
turboladen:master
Choose a base branch
from
pbadenski:verify-consistent-quotes-usage
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -15,3 +15,4 @@ pkg/ | |
| tmp/ | ||
|
|
||
| */.DS_Store | ||
| *.swp | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,33 @@ | ||
| require_relative '../ruler' | ||
|
|
||
| class Tailor | ||
| module Rulers | ||
| class QuotesRuler < Tailor::Ruler | ||
| def initialize(config, options) | ||
| super(config, options) | ||
| add_lexer_observers :tstring_beg | ||
| end | ||
|
|
||
| def tstring_beg_update(lexed_line, lineno, column, token) | ||
| log "<#{self.class}> Line length: #{lexed_line.line_length}" | ||
| measure(lexed_line, lineno, column, token) | ||
| end | ||
|
|
||
| # Checks to see if the usage of quotes is consistent and as expected by +@config+. | ||
| # | ||
| # @param [Fixnum] lexed_line The line to measure. | ||
| # @param [Fixnum] lineno Line the potential problem is on. | ||
| # @param [Fixnum] column Column the potential problem is on | ||
| # @param [Fixnum] token Begin quote string | ||
| def measure(lexed_line, lineno, column, token) | ||
| spec = { "single" => "\"", "double" => "'" } | ||
| if spec.has_key? @config and token == spec[@config] | ||
| msg = "Use #{@config} quotes" | ||
|
|
||
| @problems << Problem.new(problem_type, lineno, column, msg, | ||
| @options[:level]) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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 hidden or 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,107 @@ | ||
| require_relative '../spec_helper' | ||
| require 'tailor/critic' | ||
| require 'tailor/configuration/style' | ||
|
|
||
| describe "Consistent usage of quotes" do | ||
| before do | ||
| Tailor::Logger.stub(:log) | ||
| FakeFS.activate! | ||
| File.open(file_name.to_s, 'w') { |f| f.write contents } | ||
| critic.check_file(file_name.to_s, style.to_hash) | ||
| end | ||
|
|
||
| let(:critic) do | ||
| Tailor::Critic.new | ||
| end | ||
|
|
||
| let(:contents) { | ||
| { | ||
| :single_quotes => %Q{a = 'test'}, | ||
| :double_quotes => %Q{a = "test"}, | ||
| :mixed_quotes => %Q{a = 'test'; b = "test"}, | ||
| :special_quotes => %Q{a = %q(test); b = %Q(test)} | ||
| }[file_name] | ||
| } | ||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This (extra) line should be removed. |
||
|
|
||
| describe "single quotes" do | ||
| let(:style) do | ||
| style = Tailor::Configuration::Style.new | ||
| style.trailing_newlines 0, level: :off | ||
| style.allow_invalid_ruby true, level: :off | ||
| style.quotes "single", level: :error | ||
| style | ||
| end | ||
|
|
||
| context "ok" do | ||
| let(:file_name) { :single_quotes } | ||
|
|
||
| specify { critic.problems[file_name.to_s].size.should == 0 } | ||
| end | ||
| context "double quotes" do | ||
| let(:file_name) { :double_quotes } | ||
|
|
||
| specify { critic.problems[file_name.to_s].size.should == 1 } | ||
| specify { critic.problems[file_name.to_s].first[:type].should == "quotes" } | ||
| specify { critic.problems[file_name.to_s].first[:line].should be 1 } | ||
| specify { critic.problems[file_name.to_s].first[:column].should be 4 } | ||
| specify { critic.problems[file_name.to_s].first[:level].should be :error } | ||
| end | ||
| context "mixed quotes" do | ||
| let(:file_name) { :mixed_quotes } | ||
|
|
||
| specify { critic.problems[file_name.to_s].size.should == 1 } | ||
| specify { critic.problems[file_name.to_s].first[:type].should == "quotes" } | ||
| specify { critic.problems[file_name.to_s].first[:line].should be 1 } | ||
| specify { critic.problems[file_name.to_s].first[:column].should be 16 } | ||
| specify { critic.problems[file_name.to_s].first[:level].should be :error } | ||
| end | ||
| context "special quotes" do | ||
| let(:file_name) { :special_quotes } | ||
|
|
||
| specify { critic.problems[file_name.to_s].size.should == 0 } | ||
| end | ||
| end | ||
|
|
||
| describe "double quotes" do | ||
| let(:style) do | ||
| style = Tailor::Configuration::Style.new | ||
| style.trailing_newlines 0, level: :off | ||
| style.allow_invalid_ruby true, level: :off | ||
| style.quotes "double", level: :error | ||
| style | ||
| end | ||
|
|
||
| context "ok" do | ||
| let(:file_name) { :double_quotes } | ||
|
|
||
| specify { critic.problems[file_name.to_s].size.should == 0 } | ||
| end | ||
|
|
||
| context "single quotes" do | ||
| let(:file_name) { :single_quotes } | ||
|
|
||
| specify { critic.problems[file_name.to_s].size.should == 1 } | ||
| specify { critic.problems[file_name.to_s].first[:type].should == "quotes" } | ||
| specify { critic.problems[file_name.to_s].first[:line].should be 1 } | ||
| specify { critic.problems[file_name.to_s].first[:column].should be 4 } | ||
| specify { critic.problems[file_name.to_s].first[:level].should be :error } | ||
| end | ||
|
|
||
| context "mixed quotes" do | ||
| let(:file_name) { :mixed_quotes } | ||
|
|
||
| specify { critic.problems[file_name.to_s].size.should == 1 } | ||
| specify { critic.problems[file_name.to_s].first[:type].should == "quotes" } | ||
| specify { critic.problems[file_name.to_s].first[:line].should be 1 } | ||
| specify { critic.problems[file_name.to_s].first[:column].should be 4 } | ||
| specify { critic.problems[file_name.to_s].first[:level].should be :error } | ||
| end | ||
|
|
||
| context "special quotes" do | ||
| let(:file_name) { :special_quotes } | ||
|
|
||
| specify { critic.problems[file_name.to_s].size.should == 0 } | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this backwards?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems to read as though that is backwards. @pbadenski could you either clarify here or clarify the variable name(s)?