Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use SASS to parse a list of all CSS selectors in an .scss file?
    text
    copied!<p>I'd like to programmatically parse an .scss file to generate a flat list of selectors that are used in that file, mostly as basis for some static code analysis.</p> <p>In SASS terms, I'm looking for a way to get a list of all <code>Sass::Tree::RuleNode</code> for a given .scss file. </p> <p>So far I'm using <code>Sass::Engine.for_file</code> to create a Tree. Then, in order to use <code>Sass::Tree::RuleNode.resolved_rules</code> on the individual Nodes I have to use <code>Tree::Visitors::Cssize</code>, according to the documentation. But somewhere in there is a (probably simple) error.</p> <pre><code>require 'sass' sass_engine = Sass::Engine.for_file('files/examples.scss',{ :style =&gt; :compact, :load_paths =&gt; ['files'], :syntax =&gt; :scss, }) tree = sass_engine.to_tree Sass::Tree::Visitors::Cssize.visit(tree) tree.each do |node| if node.is_a? Sass::Tree::RuleNode puts node.resolved_rules end end </code></pre> <p>This is the error and stacktrace I'm getting:</p> <pre><code>ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/cssize.rb:186:in `visit_prop': undefined method `empty?' for nil:NilClass (NoMethodError) ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/base.rb:37:in `visit' ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/cssize.rb:21:in `visit' ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/base.rb:53:in `block in visit_children' ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/base.rb:53:in `map' ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/base.rb:53:in `visit_children' ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/cssize.rb:30:in `block in visit_children' ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/cssize.rb:52:in `with_parent' ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/cssize.rb:29:in `visit_children' ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/base.rb:37:in `block in visit' ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/cssize.rb:201:in `visit_rule' ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/base.rb:37:in `visit' ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/cssize.rb:21:in `visit' ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/base.rb:53:in `block in visit_children' ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/base.rb:53:in `map' ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/base.rb:53:in `visit_children' ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/cssize.rb:30:in `block in visit_children' ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/cssize.rb:52:in `with_parent' ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/cssize.rb:29:in `visit_children' ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/base.rb:37:in `block in visit' ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/cssize.rb:65:in `visit_root' ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/base.rb:37:in `visit' ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/cssize.rb:21:in `visit' ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/base.rb:24:in `visit' ruby-1.9.3-p362/gems/sass-3.2.10/lib/sass/tree/visitors/cssize.rb:6:in `visit' </code></pre> <p>Thanks a lot in advance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload