css_modules 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 352a7700216a0f529079aff18c7035208211015e
4
- data.tar.gz: 84a4377828e9abae5e54cc2cab1efbf71d695054
3
+ metadata.gz: ac4a3d14787aa438918ff5b6686f7dc162a2f7fd
4
+ data.tar.gz: 18c62d5cb744d9efb95ba0c9ce60b9e6e1c5937a
5
5
  SHA512:
6
- metadata.gz: 0f97e68229bab8c72efa3d9b35606c5b47679c82c2b2ae5ea7533abd624b74ec1353e37612d2f4dcc391518800b66fe9addc99a161dc581d43337baa03a693ab
7
- data.tar.gz: 94b85889226a878d04137af172ffc6b8f7d3361328ca856323b9828ebe0389ab68a045d7a7ade68551925e067033ea3e7b0e1bb331bb0dc3daf06b96562076e4
6
+ metadata.gz: 4ea78e745fc2ff7435ea8bfcc0a83fa5c978a233f133c2c7535d2844bc848eef4b935506d52409e757fc034f361edda430214f57fd58260213dc935c636fdaa1
7
+ data.tar.gz: 11efd91084b1872e522972223d1afcc5c26501270345e1ea517c55ef8187db4447b2da5887b9dd290019056d6496adf151dda9a473e51f692d259d7ffc02a839
@@ -5,7 +5,7 @@ module CSSModules
5
5
  module Rewrite
6
6
  # Module Scopes
7
7
  # :module(login) { .button {color: red;} }
8
- RE_MODULE = /^\:module\((?<module_name>.*?)\)\s+(?<declarations>.*)/
8
+ RE_MODULE = /^\:module\((?<module_name>.*?)\)/
9
9
 
10
10
  module_function
11
11
  # Take css module code as input, and rewrite it as
@@ -36,7 +36,6 @@ module CSSModules
36
36
  # No need to `yield` here since we run _after_ Sass -- it's already flattened.
37
37
  # @return [void]
38
38
  def visit_rule(node)
39
- node.rule = node.rule.map { |r| transform_selector(r) }
40
39
  node.parsed_rules = rebuild_parsed_rules(node.parsed_rules)
41
40
  end
42
41
 
@@ -44,46 +43,72 @@ module CSSModules
44
43
 
45
44
  def rebuild_parsed_rules(parsed_rules)
46
45
  new_members = parsed_rules.members.map do |member_seq|
47
- # `member_seq` is actually a deeply-nested array-like thing
48
- # but since we're running _after_ Sass itself,
49
- # we know all the tricky stuff is already taken care of.
50
- # So now we can flatten it and re-parse it, no problem.
51
- rule_s = member_seq.to_s
52
- transformed_rule_s = transform_selector(rule_s)
53
- selector_to_sass(transformed_rule_s)
46
+ leading_rule = first_selector(member_seq).to_s
47
+ matches = Rewrite::RE_MODULE.match(leading_rule)
48
+ if matches
49
+ module_name = matches[:module_name]
50
+ deeply_transform(module_name, member_seq)
51
+ else
52
+ member_seq
53
+ end
54
54
  end
55
55
  Sass::Selector::CommaSequence.new(new_members)
56
56
  end
57
57
 
58
- def selector_to_sass(selector)
59
- # This strips off leading whitespace, should I be putting it back on?
60
- Sass::SCSS::StaticParser.new(selector.strip, nil, 1).parse_selector
58
+ # Get the first non-sequence member of `seq`
59
+ def first_selector(seq)
60
+ case seq
61
+ when Sass::Selector::AbstractSequence
62
+ first_selector(seq.members.first)
63
+ else
64
+ seq
65
+ end
66
+ end
67
+
68
+ # We know this is a modulized rule
69
+ # now we should transform its ID and classes to modulized
70
+ def deeply_transform(module_name, seq)
71
+ case seq
72
+ when Sass::Selector::AbstractSequence
73
+ new_members = seq.members.map { |m| deeply_transform(module_name, m) }
74
+ new_members.compact! # maybe a module selector returned nil
75
+ clone_sequence(seq, new_members)
76
+ when Sass::Selector::Id, Sass::Selector::Class
77
+ modulized_name = Rewrite.modulize_selector(module_name, seq.name)
78
+ seq.class.new(modulized_name)
79
+ when Sass::Selector::Pseudo
80
+ if seq.to_s =~ /:module/
81
+ nil
82
+ else
83
+ seq
84
+ end
85
+ else
86
+ seq
87
+ end
61
88
  end
62
89
 
63
- # This parses the selector for `:module` definitions or does nothing.
64
- def transform_selector(selector)
65
- matches = Rewrite::RE_MODULE.match(selector)
66
- if matches.nil?
67
- selector
90
+ # Make a new kind of `seq`, containing `new_members`
91
+ def clone_sequence(seq, new_members)
92
+ case seq
93
+ when Sass::Selector::Sequence
94
+ seq.class.new(new_members)
95
+ when Sass::Selector::CommaSequence
96
+ seq.class.new(new_members)
97
+ when Sass::Selector::SimpleSequence
98
+ seq.class.new(new_members, seq.subject?, seq.source_range)
68
99
  else
69
- module_name = matches[:module_name]
70
- declaration_parts = matches[:declarations].split(" ")
71
- declaration_parts
72
- .map { |declaration_or_operator| rebuild_selector(module_name, declaration_or_operator) }
73
- .join(" ")
100
+ raise("Unknown sequence to clone: #{seq.class}")
74
101
  end
75
102
  end
76
103
 
77
- # If `selector` is a class or ID, scope it to this module.
78
- # If it is a bare element, leave it unscoped.
79
- def rebuild_selector(module_name, selector)
80
- case selector[0]
81
- when "#"
82
- "##{Rewrite.modulize_selector(module_name, selector[1..-1])}"
83
- when "."
84
- ".#{Rewrite.modulize_selector(module_name, selector[1..-1])}"
104
+ # Debug print-out of a sequence:
105
+ def deep_print(seq, indent = "")
106
+ case seq
107
+ when Sass::Selector::AbstractSequence
108
+ puts indent + seq.class.name + " => "
109
+ seq.members.map { |m| deep_print(m, indent + " ") }
85
110
  else
86
- selector
111
+ puts indent + seq.class.name + " (#{seq})"
87
112
  end
88
113
  end
89
114
  end
@@ -1,3 +1,3 @@
1
1
  module CSSModules
2
- VERSION = "0.5.0"
2
+ VERSION = "0.6.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: css_modules
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Mosolgo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-14 00:00:00.000000000 Z
11
+ date: 2016-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails