sass 3.2.19 → 3.4.25

Sign up to get free protection for your applications and to get access to all the features.
Files changed (246) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +3 -1
  3. data/CODE_OF_CONDUCT.md +10 -0
  4. data/CONTRIBUTING.md +148 -0
  5. data/MIT-LICENSE +1 -1
  6. data/README.md +87 -61
  7. data/Rakefile +119 -15
  8. data/VERSION +1 -1
  9. data/VERSION_DATE +1 -1
  10. data/VERSION_NAME +1 -1
  11. data/bin/sass +1 -1
  12. data/bin/scss +1 -1
  13. data/extra/sass-spec-ref.sh +32 -0
  14. data/extra/update_watch.rb +1 -1
  15. data/lib/sass/cache_stores/base.rb +2 -2
  16. data/lib/sass/cache_stores/chain.rb +2 -1
  17. data/lib/sass/cache_stores/filesystem.rb +8 -12
  18. data/lib/sass/cache_stores/memory.rb +5 -6
  19. data/lib/sass/cache_stores/null.rb +2 -2
  20. data/lib/sass/callbacks.rb +3 -2
  21. data/lib/sass/css.rb +22 -23
  22. data/lib/sass/deprecation.rb +55 -0
  23. data/lib/sass/engine.rb +487 -191
  24. data/lib/sass/environment.rb +172 -58
  25. data/lib/sass/error.rb +21 -24
  26. data/lib/sass/exec/base.rb +199 -0
  27. data/lib/sass/exec/sass_convert.rb +283 -0
  28. data/lib/sass/exec/sass_scss.rb +440 -0
  29. data/lib/sass/exec.rb +5 -703
  30. data/lib/sass/features.rb +47 -0
  31. data/lib/sass/importers/base.rb +50 -7
  32. data/lib/sass/importers/deprecated_path.rb +51 -0
  33. data/lib/sass/importers/filesystem.rb +54 -21
  34. data/lib/sass/importers.rb +1 -0
  35. data/lib/sass/logger/base.rb +9 -5
  36. data/lib/sass/logger/delayed.rb +50 -0
  37. data/lib/sass/logger/log_level.rb +3 -7
  38. data/lib/sass/logger.rb +9 -7
  39. data/lib/sass/media.rb +20 -23
  40. data/lib/sass/plugin/compiler.rb +321 -145
  41. data/lib/sass/plugin/configuration.rb +45 -34
  42. data/lib/sass/plugin/merb.rb +3 -3
  43. data/lib/sass/plugin/rack.rb +3 -3
  44. data/lib/sass/plugin/rails.rb +1 -1
  45. data/lib/sass/plugin/staleness_checker.rb +6 -6
  46. data/lib/sass/plugin.rb +9 -8
  47. data/lib/sass/repl.rb +3 -3
  48. data/lib/sass/script/css_lexer.rb +8 -4
  49. data/lib/sass/script/css_parser.rb +4 -2
  50. data/lib/sass/script/css_variable_warning.rb +52 -0
  51. data/lib/sass/script/functions.rb +1583 -433
  52. data/lib/sass/script/lexer.rb +198 -79
  53. data/lib/sass/script/parser.rb +463 -133
  54. data/lib/sass/script/tree/funcall.rb +313 -0
  55. data/lib/sass/script/tree/interpolation.rb +223 -0
  56. data/lib/sass/script/tree/list_literal.rb +104 -0
  57. data/lib/sass/script/tree/literal.rb +49 -0
  58. data/lib/sass/script/tree/map_literal.rb +64 -0
  59. data/lib/sass/script/{node.rb → tree/node.rb} +42 -14
  60. data/lib/sass/script/tree/operation.rb +156 -0
  61. data/lib/sass/script/tree/selector.rb +26 -0
  62. data/lib/sass/script/tree/string_interpolation.rb +125 -0
  63. data/lib/sass/script/{unary_operation.rb → tree/unary_operation.rb} +6 -6
  64. data/lib/sass/script/tree/variable.rb +57 -0
  65. data/lib/sass/script/tree.rb +16 -0
  66. data/lib/sass/script/{arg_list.rb → value/arg_list.rb} +9 -25
  67. data/lib/sass/script/value/base.rb +241 -0
  68. data/lib/sass/script/value/bool.rb +35 -0
  69. data/lib/sass/script/value/color.rb +698 -0
  70. data/lib/sass/script/value/helpers.rb +272 -0
  71. data/lib/sass/script/value/list.rb +113 -0
  72. data/lib/sass/script/value/map.rb +70 -0
  73. data/lib/sass/script/{null.rb → value/null.rb} +14 -7
  74. data/lib/sass/script/{number.rb → value/number.rb} +196 -86
  75. data/lib/sass/script/value/string.rb +138 -0
  76. data/lib/sass/script/value.rb +11 -0
  77. data/lib/sass/script.rb +38 -11
  78. data/lib/sass/scss/css_parser.rb +25 -5
  79. data/lib/sass/scss/parser.rb +532 -458
  80. data/lib/sass/scss/rx.rb +21 -14
  81. data/lib/sass/scss/static_parser.rb +328 -9
  82. data/lib/sass/scss.rb +0 -2
  83. data/lib/sass/selector/abstract_sequence.rb +36 -19
  84. data/lib/sass/selector/comma_sequence.rb +125 -26
  85. data/lib/sass/selector/pseudo.rb +266 -0
  86. data/lib/sass/selector/sequence.rb +200 -71
  87. data/lib/sass/selector/simple.rb +30 -32
  88. data/lib/sass/selector/simple_sequence.rb +193 -64
  89. data/lib/sass/selector.rb +65 -194
  90. data/lib/sass/shared.rb +2 -2
  91. data/lib/sass/source/map.rb +213 -0
  92. data/lib/sass/source/position.rb +39 -0
  93. data/lib/sass/source/range.rb +41 -0
  94. data/lib/sass/stack.rb +120 -0
  95. data/lib/sass/supports.rb +19 -23
  96. data/lib/sass/tree/at_root_node.rb +83 -0
  97. data/lib/sass/tree/charset_node.rb +1 -1
  98. data/lib/sass/tree/comment_node.rb +4 -4
  99. data/lib/sass/tree/css_import_node.rb +19 -11
  100. data/lib/sass/tree/debug_node.rb +2 -2
  101. data/lib/sass/tree/directive_node.rb +21 -4
  102. data/lib/sass/tree/each_node.rb +8 -8
  103. data/lib/sass/tree/error_node.rb +18 -0
  104. data/lib/sass/tree/extend_node.rb +14 -7
  105. data/lib/sass/tree/for_node.rb +4 -4
  106. data/lib/sass/tree/function_node.rb +14 -4
  107. data/lib/sass/tree/if_node.rb +1 -1
  108. data/lib/sass/tree/import_node.rb +10 -10
  109. data/lib/sass/tree/keyframe_rule_node.rb +15 -0
  110. data/lib/sass/tree/media_node.rb +4 -14
  111. data/lib/sass/tree/mixin_def_node.rb +4 -4
  112. data/lib/sass/tree/mixin_node.rb +21 -8
  113. data/lib/sass/tree/node.rb +59 -15
  114. data/lib/sass/tree/prop_node.rb +42 -24
  115. data/lib/sass/tree/return_node.rb +3 -2
  116. data/lib/sass/tree/root_node.rb +19 -3
  117. data/lib/sass/tree/rule_node.rb +49 -26
  118. data/lib/sass/tree/supports_node.rb +0 -13
  119. data/lib/sass/tree/trace_node.rb +2 -1
  120. data/lib/sass/tree/variable_node.rb +9 -3
  121. data/lib/sass/tree/visitors/base.rb +5 -8
  122. data/lib/sass/tree/visitors/check_nesting.rb +62 -36
  123. data/lib/sass/tree/visitors/convert.rb +111 -76
  124. data/lib/sass/tree/visitors/cssize.rb +206 -74
  125. data/lib/sass/tree/visitors/deep_copy.rb +11 -6
  126. data/lib/sass/tree/visitors/extend.rb +19 -17
  127. data/lib/sass/tree/visitors/perform.rb +308 -190
  128. data/lib/sass/tree/visitors/set_options.rb +21 -7
  129. data/lib/sass/tree/visitors/to_css.rb +273 -92
  130. data/lib/sass/tree/warn_node.rb +2 -2
  131. data/lib/sass/tree/while_node.rb +2 -2
  132. data/lib/sass/util/cross_platform_random.rb +19 -0
  133. data/lib/sass/util/normalized_map.rb +129 -0
  134. data/lib/sass/util/ordered_hash.rb +192 -0
  135. data/lib/sass/util/subset_map.rb +5 -5
  136. data/lib/sass/util/test.rb +0 -1
  137. data/lib/sass/util.rb +620 -193
  138. data/lib/sass/version.rb +22 -24
  139. data/lib/sass.rb +27 -13
  140. data/test/sass/cache_test.rb +62 -20
  141. data/test/sass/callbacks_test.rb +1 -1
  142. data/test/sass/compiler_test.rb +236 -0
  143. data/test/sass/conversion_test.rb +472 -44
  144. data/test/sass/css2sass_test.rb +73 -5
  145. data/test/sass/css_variable_test.rb +132 -0
  146. data/test/sass/encoding_test.rb +219 -0
  147. data/test/sass/engine_test.rb +618 -415
  148. data/test/sass/exec_test.rb +12 -2
  149. data/test/sass/extend_test.rb +419 -168
  150. data/test/sass/functions_test.rb +931 -93
  151. data/test/sass/importer_test.rb +250 -21
  152. data/test/sass/logger_test.rb +1 -1
  153. data/test/sass/more_results/more_import.css +1 -1
  154. data/test/sass/more_templates/more1.sass +10 -10
  155. data/test/sass/more_templates/more_import.sass +2 -2
  156. data/test/sass/plugin_test.rb +26 -34
  157. data/test/sass/results/compact.css +1 -1
  158. data/test/sass/results/complex.css +4 -4
  159. data/test/sass/results/expanded.css +1 -1
  160. data/test/sass/results/import.css +1 -1
  161. data/test/sass/results/import_charset_ibm866.css +2 -2
  162. data/test/sass/results/mixins.css +17 -17
  163. data/test/sass/results/nested.css +1 -1
  164. data/test/sass/results/parent_ref.css +2 -2
  165. data/test/sass/results/script.css +5 -5
  166. data/test/sass/results/scss_import.css +1 -1
  167. data/test/sass/script_conversion_test.rb +97 -39
  168. data/test/sass/script_test.rb +911 -102
  169. data/test/sass/scss/css_test.rb +215 -34
  170. data/test/sass/scss/rx_test.rb +8 -4
  171. data/test/sass/scss/scss_test.rb +2424 -325
  172. data/test/sass/source_map_test.rb +1055 -0
  173. data/test/sass/superselector_test.rb +210 -0
  174. data/test/sass/templates/_partial.sass +1 -1
  175. data/test/sass/templates/basic.sass +10 -10
  176. data/test/sass/templates/bork1.sass +1 -1
  177. data/test/sass/templates/bork5.sass +1 -1
  178. data/test/sass/templates/compact.sass +10 -10
  179. data/test/sass/templates/complex.sass +187 -187
  180. data/test/sass/templates/compressed.sass +10 -10
  181. data/test/sass/templates/expanded.sass +10 -10
  182. data/test/sass/templates/import.sass +2 -2
  183. data/test/sass/templates/importee.sass +3 -3
  184. data/test/sass/templates/mixins.sass +22 -22
  185. data/test/sass/templates/multiline.sass +4 -4
  186. data/test/sass/templates/nested.sass +13 -13
  187. data/test/sass/templates/parent_ref.sass +12 -12
  188. data/test/sass/templates/script.sass +70 -70
  189. data/test/sass/templates/scss_import.scss +2 -1
  190. data/test/sass/templates/subdir/nested_subdir/_nested_partial.sass +1 -1
  191. data/test/sass/templates/subdir/nested_subdir/nested_subdir.sass +2 -2
  192. data/test/sass/templates/subdir/subdir.sass +3 -3
  193. data/test/sass/templates/units.sass +10 -10
  194. data/test/sass/test_helper.rb +1 -1
  195. data/test/sass/util/multibyte_string_scanner_test.rb +11 -3
  196. data/test/sass/util/normalized_map_test.rb +51 -0
  197. data/test/sass/util/subset_map_test.rb +2 -2
  198. data/test/sass/util_test.rb +99 -43
  199. data/test/sass/value_helpers_test.rb +179 -0
  200. data/test/sass-spec.yml +3 -0
  201. data/test/test_helper.rb +42 -12
  202. data/vendor/listen/CHANGELOG.md +1 -228
  203. data/vendor/listen/Gemfile +5 -15
  204. data/vendor/listen/README.md +111 -77
  205. data/vendor/listen/Rakefile +0 -42
  206. data/vendor/listen/lib/listen/adapter.rb +195 -82
  207. data/vendor/listen/lib/listen/adapters/bsd.rb +27 -64
  208. data/vendor/listen/lib/listen/adapters/darwin.rb +21 -58
  209. data/vendor/listen/lib/listen/adapters/linux.rb +23 -55
  210. data/vendor/listen/lib/listen/adapters/polling.rb +25 -34
  211. data/vendor/listen/lib/listen/adapters/windows.rb +50 -46
  212. data/vendor/listen/lib/listen/directory_record.rb +96 -61
  213. data/vendor/listen/lib/listen/listener.rb +135 -37
  214. data/vendor/listen/lib/listen/turnstile.rb +9 -5
  215. data/vendor/listen/lib/listen/version.rb +1 -1
  216. data/vendor/listen/lib/listen.rb +33 -19
  217. data/vendor/listen/listen.gemspec +6 -0
  218. data/vendor/listen/spec/listen/adapter_spec.rb +43 -77
  219. data/vendor/listen/spec/listen/adapters/polling_spec.rb +8 -8
  220. data/vendor/listen/spec/listen/directory_record_spec.rb +81 -56
  221. data/vendor/listen/spec/listen/listener_spec.rb +128 -39
  222. data/vendor/listen/spec/listen_spec.rb +15 -21
  223. data/vendor/listen/spec/spec_helper.rb +4 -0
  224. data/vendor/listen/spec/support/adapter_helper.rb +52 -15
  225. data/vendor/listen/spec/support/directory_record_helper.rb +7 -5
  226. data/vendor/listen/spec/support/listeners_helper.rb +30 -7
  227. metadata +161 -111
  228. data/CONTRIBUTING +0 -3
  229. data/lib/sass/script/bool.rb +0 -18
  230. data/lib/sass/script/color.rb +0 -606
  231. data/lib/sass/script/funcall.rb +0 -245
  232. data/lib/sass/script/interpolation.rb +0 -79
  233. data/lib/sass/script/list.rb +0 -85
  234. data/lib/sass/script/literal.rb +0 -221
  235. data/lib/sass/script/operation.rb +0 -110
  236. data/lib/sass/script/string.rb +0 -51
  237. data/lib/sass/script/string_interpolation.rb +0 -103
  238. data/lib/sass/script/variable.rb +0 -58
  239. data/lib/sass/scss/script_lexer.rb +0 -15
  240. data/lib/sass/scss/script_parser.rb +0 -25
  241. data/test/Gemfile +0 -3
  242. data/test/Gemfile.lock +0 -10
  243. data/vendor/listen/lib/listen/dependency_manager.rb +0 -126
  244. data/vendor/listen/lib/listen/multi_listener.rb +0 -143
  245. data/vendor/listen/spec/listen/dependency_manager_spec.rb +0 -107
  246. data/vendor/listen/spec/listen/multi_listener_spec.rb +0 -174
@@ -1,60 +1,20 @@
1
1
  require 'set'
2
2
 
3
3
  module Sass
4
- # The lexical environment for SassScript.
5
- # This keeps track of variable, mixin, and function definitions.
6
- #
7
- # A new environment is created for each level of Sass nesting.
8
- # This allows variables to be lexically scoped.
9
- # The new environment refers to the environment in the upper scope,
10
- # so it has access to variables defined in enclosing scopes,
11
- # but new variables are defined locally.
12
- #
13
- # Environment also keeps track of the {Engine} options
14
- # so that they can be made available to {Sass::Script::Functions}.
15
- class Environment
16
- # The enclosing environment,
17
- # or nil if this is the global environment.
18
- #
19
- # @return [Environment]
20
- attr_reader :parent
21
- attr_reader :options
22
- attr_writer :caller
23
- attr_writer :content
24
-
25
- # @param options [{Symbol => Object}] The options hash. See
26
- # {file:SASS_REFERENCE.md#sass_options the Sass options documentation}.
27
- # @param parent [Environment] See \{#parent}
28
- def initialize(parent = nil, options = nil)
29
- @parent = parent
30
- @options = options || (parent && parent.options) || {}
31
- end
32
-
33
- # The environment of the caller of this environment's mixin or function.
34
- # @return {Environment?}
35
- def caller
36
- @caller || (@parent && @parent.caller)
37
- end
38
-
39
- # The content passed to this environmnet. This is naturally only set
40
- # for mixin body environments with content passed in.
41
- # @return {Environment?}
42
- def content
43
- @content || (@parent && @parent.content)
44
- end
45
-
46
- private
47
-
4
+ # The abstract base class for lexical environments for SassScript.
5
+ class BaseEnvironment
48
6
  class << self
49
- private
50
- UNDERSCORE, DASH = '_', '-'
51
-
52
7
  # Note: when updating this,
53
8
  # update sass/yard/inherited_hash.rb as well.
54
- def inherited_hash(name)
55
- class_eval <<RUBY, __FILE__, __LINE__ + 1
9
+ def inherited_hash_accessor(name)
10
+ inherited_hash_reader(name)
11
+ inherited_hash_writer(name)
12
+ end
13
+
14
+ def inherited_hash_reader(name)
15
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
56
16
  def #{name}(name)
57
- _#{name}(name.tr(UNDERSCORE, DASH))
17
+ _#{name}(name.tr('_', '-'))
58
18
  end
59
19
 
60
20
  def _#{name}(name)
@@ -62,8 +22,17 @@ module Sass
62
22
  end
63
23
  protected :_#{name}
64
24
 
25
+ def is_#{name}_global?(name)
26
+ return !@parent if @#{name}s && @#{name}s.has_key?(name)
27
+ @parent && @parent.is_#{name}_global?(name)
28
+ end
29
+ RUBY
30
+ end
31
+
32
+ def inherited_hash_writer(name)
33
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
65
34
  def set_#{name}(name, value)
66
- name = name.tr(UNDERSCORE, DASH)
35
+ name = name.tr('_', '-')
67
36
  @#{name}s[name] = value unless try_set_#{name}(name, value)
68
37
  end
69
38
 
@@ -72,7 +41,7 @@ module Sass
72
41
  if @#{name}s.include?(name)
73
42
  @#{name}s[name] = value
74
43
  true
75
- elsif @parent
44
+ elsif @parent && !@parent.global?
76
45
  @parent.try_set_#{name}(name, value)
77
46
  else
78
47
  false
@@ -82,20 +51,165 @@ module Sass
82
51
 
83
52
  def set_local_#{name}(name, value)
84
53
  @#{name}s ||= {}
85
- @#{name}s[name.tr(UNDERSCORE, DASH)] = value
54
+ @#{name}s[name.tr('_', '-')] = value
55
+ end
56
+
57
+ def set_global_#{name}(name, value)
58
+ global_env.set_#{name}(name, value)
86
59
  end
87
- RUBY
60
+ RUBY
88
61
  end
89
62
  end
90
63
 
64
+ # The options passed to the Sass Engine.
65
+ attr_reader :options
66
+
67
+ attr_writer :caller
68
+ attr_writer :content
69
+ attr_writer :selector
70
+
71
+ # variable
72
+ # Script::Value
73
+ inherited_hash_reader :var
74
+
75
+ # mixin
76
+ # Sass::Callable
77
+ inherited_hash_reader :mixin
78
+
79
+ # function
80
+ # Sass::Callable
81
+ inherited_hash_reader :function
82
+
83
+ # @param options [{Symbol => Object}] The options hash. See
84
+ # {file:SASS_REFERENCE.md#Options the Sass options documentation}.
85
+ # @param parent [Environment] See \{#parent}
86
+ def initialize(parent = nil, options = nil)
87
+ @parent = parent
88
+ @options = options || (parent && parent.options) || {}
89
+ @stack = @parent.nil? ? Sass::Stack.new : nil
90
+ @caller = nil
91
+ @content = nil
92
+ @filename = nil
93
+ @functions = nil
94
+ @mixins = nil
95
+ @selector = nil
96
+ @vars = nil
97
+ end
98
+
99
+ # Returns whether this is the global environment.
100
+ #
101
+ # @return [Boolean]
102
+ def global?
103
+ @parent.nil?
104
+ end
105
+
106
+ # The environment of the caller of this environment's mixin or function.
107
+ # @return {Environment?}
108
+ def caller
109
+ @caller || (@parent && @parent.caller)
110
+ end
111
+
112
+ # The content passed to this environment. This is naturally only set
113
+ # for mixin body environments with content passed in.
114
+ #
115
+ # @return {[Array<Sass::Tree::Node>, Environment]?} The content nodes and
116
+ # the lexical environment of the content block.
117
+ def content
118
+ @content || (@parent && @parent.content)
119
+ end
120
+
121
+ # The selector for the current CSS rule, or nil if there is no
122
+ # current CSS rule.
123
+ #
124
+ # @return [Selector::CommaSequence?] The current selector, with any
125
+ # nesting fully resolved.
126
+ def selector
127
+ @selector || (@caller && @caller.selector) || (@parent && @parent.selector)
128
+ end
129
+
130
+ # The top-level Environment object.
131
+ #
132
+ # @return [Environment]
133
+ def global_env
134
+ @global_env ||= global? ? self : @parent.global_env
135
+ end
136
+
137
+ # The import/mixin stack.
138
+ #
139
+ # @return [Sass::Stack]
140
+ def stack
141
+ @stack || global_env.stack
142
+ end
143
+ end
144
+
145
+ # The lexical environment for SassScript.
146
+ # This keeps track of variable, mixin, and function definitions.
147
+ #
148
+ # A new environment is created for each level of Sass nesting.
149
+ # This allows variables to be lexically scoped.
150
+ # The new environment refers to the environment in the upper scope,
151
+ # so it has access to variables defined in enclosing scopes,
152
+ # but new variables are defined locally.
153
+ #
154
+ # Environment also keeps track of the {Engine} options
155
+ # so that they can be made available to {Sass::Script::Functions}.
156
+ class Environment < BaseEnvironment
157
+ # The enclosing environment,
158
+ # or nil if this is the global environment.
159
+ #
160
+ # @return [Environment]
161
+ attr_reader :parent
162
+
91
163
  # variable
92
- # Script::Literal
93
- inherited_hash :var
164
+ # Script::Value
165
+ inherited_hash_writer :var
166
+
94
167
  # mixin
95
168
  # Sass::Callable
96
- inherited_hash :mixin
169
+ inherited_hash_writer :mixin
170
+
97
171
  # function
98
172
  # Sass::Callable
99
- inherited_hash :function
173
+ inherited_hash_writer :function
174
+ end
175
+
176
+ # A read-only wrapper for a lexical environment for SassScript.
177
+ class ReadOnlyEnvironment < BaseEnvironment
178
+ # The read-only environment of the caller of this environment's mixin or function.
179
+ #
180
+ # @see BaseEnvironment#caller
181
+ # @return {ReadOnlyEnvironment}
182
+ def caller
183
+ return @caller if @caller
184
+ env = super
185
+ @caller ||= env.is_a?(ReadOnlyEnvironment) ? env : ReadOnlyEnvironment.new(env, env.options)
186
+ end
187
+
188
+ # The read-only content passed to this environment.
189
+ #
190
+ # @see BaseEnvironment#content
191
+ # @return {ReadOnlyEnvironment}
192
+ def content
193
+ return @content if @content
194
+ env = super
195
+ @content ||= env.is_a?(ReadOnlyEnvironment) ? env : ReadOnlyEnvironment.new(env, env.options)
196
+ end
197
+ end
198
+
199
+ # An environment that can write to in-scope global variables, but doesn't
200
+ # create new variables in the global scope. Useful for top-level control
201
+ # directives.
202
+ class SemiGlobalEnvironment < Environment
203
+ def try_set_var(name, value)
204
+ @vars ||= {}
205
+ if @vars.include?(name)
206
+ @vars[name] = value
207
+ true
208
+ elsif @parent
209
+ @parent.try_set_var(name, value)
210
+ else
211
+ false
212
+ end
213
+ end
100
214
  end
101
215
  end
data/lib/sass/error.rb CHANGED
@@ -69,14 +69,14 @@ module Sass
69
69
  # The name of the mixin in which the error occurred.
70
70
  # This could be `nil` if the error occurred outside a mixin.
71
71
  #
72
- # @return [Fixnum]
72
+ # @return [String]
73
73
  def sass_mixin
74
74
  sass_backtrace.first[:mixin]
75
75
  end
76
76
 
77
77
  # The line of the Sass template on which the error occurred.
78
78
  #
79
- # @return [Fixnum]
79
+ # @return [Integer]
80
80
  def sass_line
81
81
  sass_backtrace.first[:line]
82
82
  end
@@ -86,7 +86,7 @@ module Sass
86
86
  # @param attrs [{Symbol => Object}] The information in the backtrace entry.
87
87
  # See \{#sass\_backtrace}
88
88
  def add_backtrace(attrs)
89
- sass_backtrace << attrs.reject {|k, v| v.nil?}
89
+ sass_backtrace << attrs.reject {|_k, v| v.nil?}
90
90
  end
91
91
 
92
92
  # Modify the top Sass backtrace entries
@@ -104,12 +104,12 @@ module Sass
104
104
  # @param attrs [{Symbol => Object}] The information to add to the backtrace entry.
105
105
  # See \{#sass\_backtrace}
106
106
  def modify_backtrace(attrs)
107
- attrs = attrs.reject {|k, v| v.nil?}
107
+ attrs = attrs.reject {|_k, v| v.nil?}
108
108
  # Move backwards through the backtrace
109
- (0...sass_backtrace.size).to_a.reverse.each do |i|
109
+ (0...sass_backtrace.size).to_a.reverse_each do |i|
110
110
  entry = sass_backtrace[i]
111
111
  sass_backtrace[i] = attrs.merge(entry)
112
- attrs.reject! {|k, v| entry.include?(k)}
112
+ attrs.reject! {|k, _v| entry.include?(k)}
113
113
  break if attrs.empty?
114
114
  end
115
115
  end
@@ -127,7 +127,7 @@ module Sass
127
127
  return nil if super.nil?
128
128
  return super if sass_backtrace.all? {|h| h.empty?}
129
129
  sass_backtrace.map do |h|
130
- "#{h[:filename] || "(sass)"}:#{h[:line]}" +
130
+ "#{h[:filename] || '(sass)'}:#{h[:line]}" +
131
131
  (h[:mixin] ? ":in `#{h[:mixin]}'" : "")
132
132
  end + super
133
133
  end
@@ -138,36 +138,34 @@ module Sass
138
138
  # @see #sass_backtrace
139
139
  # @return [String]
140
140
  def sass_backtrace_str(default_filename = "an unknown file")
141
- lines = self.message.split("\n")
141
+ lines = message.split("\n")
142
142
  msg = lines[0] + lines[1..-1].
143
- map {|l| "\n" + (" " * "Syntax error: ".size) + l}.join
144
- "Syntax error: #{msg}" +
143
+ map {|l| "\n" + (" " * "Error: ".size) + l}.join
144
+ "Error: #{msg}" +
145
145
  Sass::Util.enum_with_index(sass_backtrace).map do |entry, i|
146
- "\n #{i == 0 ? "on" : "from"} line #{entry[:line]}" +
147
- " of #{entry[:filename] || default_filename}" +
148
- (entry[:mixin] ? ", in `#{entry[:mixin]}'" : "")
149
- end.join
146
+ "\n #{i == 0 ? 'on' : 'from'} line #{entry[:line]}" +
147
+ " of #{entry[:filename] || default_filename}" +
148
+ (entry[:mixin] ? ", in `#{entry[:mixin]}'" : "")
149
+ end.join
150
150
  end
151
151
 
152
152
  class << self
153
153
  # Returns an error report for an exception in CSS format.
154
154
  #
155
155
  # @param e [Exception]
156
- # @param options [{Symbol => Object}] The options passed to {Sass::Engine#initialize}
156
+ # @param line_offset [Integer] The number of the first line of the Sass template.
157
157
  # @return [String] The error report
158
158
  # @raise [Exception] `e`, if the
159
159
  # {file:SASS_REFERENCE.md#full_exception-option `:full_exception`} option
160
160
  # is set to false.
161
- def exception_to_css(e, options)
162
- raise e unless options[:full_exception]
163
-
164
- header = header_string(e, options)
161
+ def exception_to_css(e, line_offset = 1)
162
+ header = header_string(e, line_offset)
165
163
 
166
164
  <<END
167
165
  /*
168
- #{header.gsub("*/", "*\\/")}
166
+ #{header.gsub('*/', '*\\/')}
169
167
 
170
- Backtrace:\n#{e.backtrace.join("\n").gsub("*/", "*\\/")}
168
+ Backtrace:\n#{e.backtrace.join("\n").gsub('*/', '*\\/')}
171
169
  */
172
170
  body:before {
173
171
  white-space: pre;
@@ -178,15 +176,14 @@ END
178
176
 
179
177
  private
180
178
 
181
- def header_string(e, options)
179
+ def header_string(e, line_offset)
182
180
  unless e.is_a?(Sass::SyntaxError) && e.sass_line && e.sass_template
183
181
  return "#{e.class}: #{e.message}"
184
182
  end
185
183
 
186
- line_offset = options[:line] || 1
187
184
  line_num = e.sass_line + 1 - line_offset
188
185
  min = [line_num - 6, 0].max
189
- section = e.sass_template.rstrip.split("\n")[min ... line_num + 5]
186
+ section = e.sass_template.rstrip.split("\n")[min...line_num + 5]
190
187
  return e.sass_backtrace_str if section.nil? || section.empty?
191
188
 
192
189
  e.sass_backtrace_str + "\n\n" + Sass::Util.enum_with_index(section).
@@ -0,0 +1,199 @@
1
+ require 'optparse'
2
+
3
+ module Sass::Exec
4
+ # The abstract base class for Sass executables.
5
+ class Base
6
+ # @param args [Array<String>] The command-line arguments
7
+ def initialize(args)
8
+ @args = args
9
+ @options = {}
10
+ end
11
+
12
+ # Parses the command-line arguments and runs the executable.
13
+ # Calls `Kernel#exit` at the end, so it never returns.
14
+ #
15
+ # @see #parse
16
+ def parse!
17
+ # rubocop:disable RescueException
18
+ begin
19
+ parse
20
+ rescue Exception => e
21
+ # Exit code 65 indicates invalid data per
22
+ # http://www.freebsd.org/cgi/man.cgi?query=sysexits. Setting it via
23
+ # at_exit is a bit of a hack, but it allows us to rethrow when --trace
24
+ # is active and get both the built-in exception formatting and the
25
+ # correct exit code.
26
+ at_exit {exit Sass::Util.windows? ? 13 : 65} if e.is_a?(Sass::SyntaxError)
27
+
28
+ raise e if @options[:trace] || e.is_a?(SystemExit)
29
+
30
+ if e.is_a?(Sass::SyntaxError)
31
+ $stderr.puts e.sass_backtrace_str("standard input")
32
+ else
33
+ $stderr.print "#{e.class}: " unless e.class == RuntimeError
34
+ $stderr.puts e.message.to_s
35
+ end
36
+ $stderr.puts " Use --trace for backtrace."
37
+
38
+ exit 1
39
+ end
40
+ exit 0
41
+ # rubocop:enable RescueException
42
+ end
43
+
44
+ # Parses the command-line arguments and runs the executable.
45
+ # This does not handle exceptions or exit the program.
46
+ #
47
+ # @see #parse!
48
+ def parse
49
+ @opts = OptionParser.new(&method(:set_opts))
50
+ @opts.parse!(@args)
51
+
52
+ process_result
53
+
54
+ @options
55
+ end
56
+
57
+ # @return [String] A description of the executable
58
+ def to_s
59
+ @opts.to_s
60
+ end
61
+
62
+ protected
63
+
64
+ # Finds the line of the source template
65
+ # on which an exception was raised.
66
+ #
67
+ # @param exception [Exception] The exception
68
+ # @return [String] The line number
69
+ def get_line(exception)
70
+ # SyntaxErrors have weird line reporting
71
+ # when there's trailing whitespace
72
+ if exception.is_a?(::SyntaxError)
73
+ return (exception.message.scan(/:(\d+)/).first || ["??"]).first
74
+ end
75
+ (exception.backtrace[0].scan(/:(\d+)/).first || ["??"]).first
76
+ end
77
+
78
+ # Tells optparse how to parse the arguments
79
+ # available for all executables.
80
+ #
81
+ # This is meant to be overridden by subclasses
82
+ # so they can add their own options.
83
+ #
84
+ # @param opts [OptionParser]
85
+ def set_opts(opts)
86
+ Sass::Util.abstract(this)
87
+ end
88
+
89
+ # Set an option for specifying `Encoding.default_external`.
90
+ #
91
+ # @param opts [OptionParser]
92
+ def encoding_option(opts)
93
+ encoding_desc = if Sass::Util.ruby1_8?
94
+ 'Does not work in Ruby 1.8.'
95
+ else
96
+ 'Specify the default encoding for input files.'
97
+ end
98
+ opts.on('-E', '--default-encoding ENCODING', encoding_desc) do |encoding|
99
+ if Sass::Util.ruby1_8?
100
+ $stderr.puts "Specifying the encoding is not supported in ruby 1.8."
101
+ exit 1
102
+ else
103
+ Encoding.default_external = encoding
104
+ end
105
+ end
106
+ end
107
+
108
+ # Processes the options set by the command-line arguments. In particular,
109
+ # sets `@options[:input]` and `@options[:output]` to appropriate IO streams.
110
+ #
111
+ # This is meant to be overridden by subclasses
112
+ # so they can run their respective programs.
113
+ def process_result
114
+ input, output = @options[:input], @options[:output]
115
+ args = @args.dup
116
+ input ||=
117
+ begin
118
+ filename = args.shift
119
+ @options[:filename] = filename
120
+ open_file(filename) || $stdin
121
+ end
122
+ @options[:output_filename] = args.shift
123
+ output ||= @options[:output_filename] || $stdout
124
+ @options[:input], @options[:output] = input, output
125
+ end
126
+
127
+ COLORS = {:red => 31, :green => 32, :yellow => 33}
128
+
129
+ # Prints a status message about performing the given action,
130
+ # colored using the given color (via terminal escapes) if possible.
131
+ #
132
+ # @param name [#to_s] A short name for the action being performed.
133
+ # Shouldn't be longer than 11 characters.
134
+ # @param color [Symbol] The name of the color to use for this action.
135
+ # Can be `:red`, `:green`, or `:yellow`.
136
+ def puts_action(name, color, arg)
137
+ return if @options[:for_engine][:quiet]
138
+ printf color(color, "%11s %s\n"), name, arg
139
+ STDOUT.flush
140
+ end
141
+
142
+ # Same as `Kernel.puts`, but doesn't print anything if the `--quiet` option is set.
143
+ #
144
+ # @param args [Array] Passed on to `Kernel.puts`
145
+ def puts(*args)
146
+ return if @options[:for_engine][:quiet]
147
+ Kernel.puts(*args)
148
+ end
149
+
150
+ # Wraps the given string in terminal escapes
151
+ # causing it to have the given color.
152
+ # If terminal escapes aren't supported on this platform,
153
+ # just returns the string instead.
154
+ #
155
+ # @param color [Symbol] The name of the color to use.
156
+ # Can be `:red`, `:green`, or `:yellow`.
157
+ # @param str [String] The string to wrap in the given color.
158
+ # @return [String] The wrapped string.
159
+ def color(color, str)
160
+ raise "[BUG] Unrecognized color #{color}" unless COLORS[color]
161
+
162
+ # Almost any real Unix terminal will support color,
163
+ # so we just filter for Windows terms (which don't set TERM)
164
+ # and not-real terminals, which aren't ttys.
165
+ return str if ENV["TERM"].nil? || ENV["TERM"].empty? || !STDOUT.tty?
166
+ "\e[#{COLORS[color]}m#{str}\e[0m"
167
+ end
168
+
169
+ def write_output(text, destination)
170
+ if destination.is_a?(String)
171
+ open_file(destination, 'w') {|file| file.write(text)}
172
+ else
173
+ destination.write(text)
174
+ end
175
+ end
176
+
177
+ private
178
+
179
+ def open_file(filename, flag = 'r')
180
+ return if filename.nil?
181
+ flag = 'wb' if @options[:unix_newlines] && flag == 'w'
182
+ file = File.open(filename, flag)
183
+ return file unless block_given?
184
+ yield file
185
+ file.close
186
+ end
187
+
188
+ def handle_load_error(err)
189
+ dep = err.message[/^no such file to load -- (.*)/, 1]
190
+ raise err if @options[:trace] || dep.nil? || dep.empty?
191
+ $stderr.puts <<MESSAGE
192
+ Required dependency #{dep} not found!
193
+ Run "gem install #{dep}" to get it.
194
+ Use --trace for backtrace.
195
+ MESSAGE
196
+ exit 1
197
+ end
198
+ end
199
+ end