Commit 5750efc8 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Detect variable and parameter types in SCSS files.

parent 0de62d51
Loading
Loading
Loading
Loading
+45 −2
Original line number Diff line number Diff line
@@ -49,7 +49,7 @@ module JsDuck
            :tagname => :css_var,
            :name => "$" + node.name,
            :default => node.expr.to_s,
            :type => "*",
            :type => detect_type(node.expr),
          }
        elsif node.class == Sass::Tree::MixinDefNode
          return {
@@ -68,11 +68,54 @@ module JsDuck
          {
            :name => "$" + arg[0].name,
            :default => arg[1] ? arg[1].to_s : nil,
            :type => "*",
            :type => arg[1] ? detect_type(arg[1]) : nil,
          }
        end
      end

      def detect_type(node)
        if LITERAL_TYPES[node.class]
          LITERAL_TYPES[node.class]
        elsif node.class == Sass::Script::Funcall && COLOR_FUNCTIONS[node.name]
          "color"
        else
          nil
        end
      end

      LITERAL_TYPES = {
        Sass::Script::Number => "number",
        Sass::Script::String => "string",
        Sass::Script::Color => "color",
        Sass::Script::Bool => "boolean",
        Sass::Script::List => "list",
      }

      COLOR_FUNCTIONS = {
        # CSS3 builtins
        "rgb" => true,
        "rgba" => true,
        "hsl" => true,
        "hsla" => true,
        # SASS builtins
        "mix" => true,
        "adjust-hue" => true,
        "lighten" => true,
        "darken" => true,
        "saturate" => true,
        "desaturate" => true,
        "grayscale" => true,
        "complement" => true,
        "invert" => true,
        "opacify" => true,
        "fade-in" => true,
        "transparentize" => true,
        "fade-out" => true,
        "adjust-color" => true,
        "scale-color" => true,
        "change-color" => true,
      }

    end

  end
+65 −0
Original line number Diff line number Diff line
@@ -76,6 +76,10 @@ describe JsDuck::Css::SassParser do
    it "detects default value" do
      var[:code][:default].should == "10em"
    end

    it "detects type" do
      var[:code][:type].should == "number"
    end
  end

  describe "parsing SCSS mixin" do
@@ -119,6 +123,10 @@ describe JsDuck::Css::SassParser do
    it "detects default value for second param" do
      var[:code][:params][1][:default].should == "2px"
    end

    it "detects type for second param" do
      var[:code][:params][1][:type].should == "number"
    end
  end

  describe "parsing other SCSS code" do
@@ -140,4 +148,61 @@ describe JsDuck::Css::SassParser do
    end
  end

  describe "detecting a type" do
    def detect(expr)
      parse("/** */ $var: #{expr};")[0][:code][:type]
    end

    it "plain number --> number" do
      detect("3.14").should == "number"
    end
    it "percentage --> number" do
      detect("10%").should == "number"
    end
    it "measurement --> number" do
      detect("15px").should == "number"
    end

    it "unquoted string --> string" do
      detect("bold").should == "string"
    end
    it "quoted string --> string" do
      detect('"blah blah"').should == "string"
    end

    it "color name --> color" do
      detect("orange").should == "color"
    end
    it "color code --> color" do
      detect("#ff00cc").should == "color"
    end
    it "rgba() --> color" do
      detect("rgba(255, 0, 0, 0.5)").should == "color"
    end
    it "hsl() --> color" do
      detect("hsl(0, 100%, 50%)").should == "color"
    end
    it "fade-in() --> color" do
      detect("fade-in(#cc00cc, 0.2)").should == "color"
    end

    it "true --> boolean" do
      detect("true").should == "boolean"
    end
    it "false --> boolean" do
      detect("false").should == "boolean"
    end

    it "comma-separated list --> list" do
      detect("'Arial', Verdana, sans-serif").should == "list"
    end
    it "space-separated list --> list" do
      detect("2px 4px 2px 4px").should == "list"
    end

    it "null --> nil" do
      detect("null").should == nil
    end
  end

end