-
Notifications
You must be signed in to change notification settings - Fork 110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes to creation of rule set #154
Changes from all commits
e6a9248
9c1128a
e3b4806
219d43e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -164,14 +164,44 @@ def add_block!(block, options = {}) | |
parse_block_into_rule_sets!(block, options) | ||
end | ||
|
||
# Add a CSS rule by setting the +selectors+, +declarations+ and +media_types+. | ||
# Add a CSS rule by setting the +selectors+, +declarations+ | ||
# and +media_types+. Optional pass +filename+ , +offset+ for source | ||
# reference too. | ||
# | ||
# +media_types+ can be a symbol or an array of symbols. | ||
def add_rule!(selectors, declarations, media_types = :all) | ||
rule_set = RuleSet.new(selectors, declarations) | ||
add_rule_set!(rule_set, media_types) | ||
rescue ArgumentError => e | ||
raise e if @options[:rule_set_exceptions] | ||
# +media_types+ can be a symbol or an array of symbols. default to :all | ||
# optional fields for source location for source location | ||
# +filename+ can be a string or uri pointing to the file or url location. | ||
# +offset+ should be Range object representing the start and end byte locations where the rule was found in the file. | ||
def add_rule!(*args, selectors: nil, block: nil, filename: nil, offset: nil, media_types: :all) # rubocop:disable Metrics/ParameterLists | ||
if args.any? | ||
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. ideally not support args at all without 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. Maybe I'm not understanding what your suggesting. |
||
media_types = nil | ||
if selectors || block || filename || offset || media_types | ||
raise ArgumentError, "don't mix positional and keyword arguments arguments" | ||
end | ||
|
||
warn '[DEPRECATION] `add_rule!` with positional arguments is deprecated. ' \ | ||
'Please use keyword arguments instead.', uplevel: 1 | ||
|
||
case args.length | ||
when 2 | ||
selectors, block = args | ||
when 3 | ||
selectors, block, media_types = args | ||
else | ||
raise ArgumentError | ||
end | ||
end | ||
|
||
begin | ||
rule_set = RuleSet.new( | ||
selectors: selectors, block: block, | ||
offset: offset, filename: filename | ||
) | ||
|
||
add_rule_set!(rule_set, media_types) | ||
rescue ArgumentError => e | ||
raise e if @options[:rule_set_exceptions] | ||
end | ||
end | ||
|
||
# Add a CSS rule by setting the +selectors+, +declarations+, +filename+, +offset+ and +media_types+. | ||
|
@@ -180,8 +210,11 @@ def add_rule!(selectors, declarations, media_types = :all) | |
# +offset+ should be Range object representing the start and end byte locations where the rule was found in the file. | ||
# +media_types+ can be a symbol or an array of symbols. | ||
def add_rule_with_offsets!(selectors, declarations, filename, offset, media_types = :all) | ||
rule_set = OffsetAwareRuleSet.new(filename, offset, selectors, declarations) | ||
add_rule_set!(rule_set, media_types) | ||
warn '[DEPRECATION] `add_rule_with_offsets!` is deprecated. Please use `add_rule!` instead.', uplevel: 1 | ||
add_rule!( | ||
selectors: selectors, block: declarations, media_types: media_types, | ||
filename: filename, offset: offset | ||
) | ||
end | ||
|
||
# Add a CssParser RuleSet object. | ||
|
@@ -360,11 +393,14 @@ def parse_block_into_rule_sets!(block, options = {}) # :nodoc: | |
current_declarations.strip! | ||
|
||
unless current_declarations.empty? | ||
add_rule_options = { | ||
selectors: current_selectors, block: current_declarations, | ||
media_types: current_media_queries | ||
} | ||
if options[:capture_offsets] | ||
add_rule_with_offsets!(current_selectors, current_declarations, options[:filename], (rule_start..offset.last), current_media_queries) | ||
else | ||
add_rule!(current_selectors, current_declarations, current_media_queries) | ||
add_rule_options.merge!(filename: options[:filename], offset: rule_start..offset.last) | ||
end | ||
add_rule!(**add_rule_options) | ||
end | ||
|
||
current_selectors = String.new | ||
|
@@ -430,11 +466,14 @@ def parse_block_into_rule_sets!(block, options = {}) # :nodoc: | |
# check for unclosed braces | ||
return unless in_declarations > 0 | ||
|
||
unless options[:capture_offsets] | ||
return add_rule!(current_selectors, current_declarations, current_media_queries) | ||
add_rule_options = { | ||
selectors: current_selectors, block: current_declarations, | ||
media_types: current_media_queries | ||
} | ||
if options[:capture_offsets] | ||
add_rule_options.merge!(filename: options[:filename], offset: rule_start..offset.last) | ||
end | ||
|
||
add_rule_with_offsets!(current_selectors, current_declarations, options[:filename], (rule_start..offset.last), current_media_queries) | ||
add_rule!(**add_rule_options) | ||
end | ||
|
||
# Load a remote CSS file. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -223,6 +223,12 @@ def normalize_property(property) | |
|
||
extend Forwardable | ||
|
||
# optional field for storing source reference | ||
# File offset range | ||
attr_reader :offset | ||
# the local or remote location | ||
attr_accessor :filename | ||
|
||
# Array of selector strings. | ||
attr_reader :selectors | ||
|
||
|
@@ -237,9 +243,38 @@ def normalize_property(property) | |
alias []= add_declaration! | ||
alias remove_declaration! delete | ||
|
||
def initialize(selectors, block, specificity = nil) | ||
def initialize(*args, selectors: nil, block: nil, offset: nil, filename: nil, specificity: nil) # rubocop:disable Metrics/ParameterLists | ||
if args.any? | ||
if selectors || block || offset || filename || specificity | ||
raise ArgumentError, "don't mix positional and keyword arguments" | ||
end | ||
|
||
warn '[DEPRECATION] positional arguments are deprecated use keyword instead.', uplevel: 1 | ||
|
||
case args.length | ||
when 2 | ||
selectors, block = args | ||
when 3 | ||
selectors, block, specificity = args | ||
when 4 | ||
filename, offset, selectors, block = args | ||
when 5 | ||
filename, offset, selectors, block, specificity = args | ||
else | ||
Comment on lines
+259
to
+263
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. why do we need to add this when we only supported 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. because I removed |
||
raise ArgumentError | ||
end | ||
end | ||
|
||
@selectors = [] | ||
@specificity = specificity | ||
|
||
unless offset.nil? == filename.nil? | ||
raise ArgumentError, 'require both offset and filename or no offset and no filename' | ||
end | ||
|
||
@offset = offset | ||
@filename = filename | ||
|
||
parse_selectors!(selectors) if selectors | ||
parse_declarations!(block) | ||
end | ||
|
@@ -650,18 +685,4 @@ def split_value_preserving_function_whitespace(value) | |
end | ||
end | ||
end | ||
|
||
class OffsetAwareRuleSet < RuleSet | ||
# File offset range | ||
attr_reader :offset | ||
|
||
# the local or remote location | ||
attr_accessor :filename | ||
|
||
def initialize(filename, offset, selectors, block, specificity = nil) | ||
super(selectors, block, specificity) | ||
@offset = offset | ||
@filename = filename | ||
end | ||
end | ||
end |
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.
prefer something like
or a
rubocop:reformat
etc, but might refactor that laterThere 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.
I wouldn't default to -A and it is unsafe.
RuboCop::RakeTask.new created three tasks.
So I could use
rake rubocop:autocorrect
to format my code but. The way I work is that I make some changed to the code without thinking to much about style, then run specs and I ran the specs withrake
(the default task) and in those cases it would stop at rubocop. In anouther project I added these two options so I could runrake -a
. I could also change to runrake test
.So maybe I should just drop this change