Skip to content

Commit

Permalink
Add deprecation messages for color arithmetic
Browse files Browse the repository at this point in the history
  • Loading branch information
xzyfer committed Apr 26, 2018
1 parent 1e52b74 commit d18fd47
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/operators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ namespace Sass {
bool lte(Expression_Obj lhs, Expression_Obj rhs) { return cmp(lhs, rhs, Sass_OP::LTE) || eq(lhs, rhs); }
bool gte(Expression_Obj lhs, Expression_Obj rhs) { return !cmp(lhs, rhs, Sass_OP::GTE) || eq(lhs, rhs); }

/* colour math deprecation warning */
void op_color_deprecation(enum Sass_OP op, std::string lsh, std::string rhs, const ParserState& pstate)
{
std::string op_str(
op == Sass_OP::ADD ? "plus" :
op == Sass_OP::DIV ? "div" :
op == Sass_OP::SUB ? "minus" :
op == Sass_OP::MUL ? "times" : ""
);

std::string msg("The operation `" + lsh + " " + op_str + " " + rhs + "` is deprecated and will be an error in future versions.");
std::string tail("Consider using Sass's color functions instead.\nhttp://sass-lang.com/documentation/Sass/Script/Functions.html#other_color_functions");

deprecated(msg, tail, false, pstate);
}

/* static function, throws OperationError, has no traces but optional pstate for returned value */
Value_Ptr op_strings(Sass::Operand operand, Value& lhs, Value& rhs, struct Sass_Inspect_Options opt, const ParserState& pstate, bool delayed)
{
Expand Down Expand Up @@ -107,12 +123,16 @@ namespace Sass {
/* static function, throws OperationError, has no traces but optional pstate for returned value */
Value_Ptr op_colors(enum Sass_OP op, const Color& lhs, const Color& rhs, struct Sass_Inspect_Options opt, const ParserState& pstate, bool delayed)
{

if (lhs.a() != rhs.a()) {
throw Exception::AlphaChannelsNotEqual(&lhs, &rhs, op);
}
if (op == Sass_OP::DIV && (!rhs.r() || !rhs.g() || !rhs.b())) {
if ((op == Sass_OP::DIV || op == Sass_OP::MOD) && (!rhs.r() || !rhs.g() || !rhs.b())) {
throw Exception::ZeroDivisionError(lhs, rhs);
}

op_color_deprecation(op, lhs.to_string(), rhs.to_string(), pstate);

return SASS_MEMORY_NEW(Color,
pstate,
ops[op](lhs.r(), rhs.r()),
Expand Down Expand Up @@ -195,9 +215,11 @@ namespace Sass {
Value_Ptr op_number_color(enum Sass_OP op, const Number& lhs, const Color& rhs, struct Sass_Inspect_Options opt, const ParserState& pstate, bool delayed)
{
double lval = lhs.value();

switch (op) {
case Sass_OP::ADD:
case Sass_OP::MUL: {
op_color_deprecation(op, lhs.to_string(), rhs.to_string(opt), pstate);
return SASS_MEMORY_NEW(Color,
pstate,
ops[op](lval, rhs.r()),
Expand All @@ -208,6 +230,7 @@ namespace Sass {
case Sass_OP::SUB:
case Sass_OP::DIV: {
std::string color(rhs.to_string(opt));
op_color_deprecation(op, lhs.to_string(), color, pstate);
return SASS_MEMORY_NEW(String_Quoted,
pstate,
lhs.to_string(opt)
Expand All @@ -223,10 +246,14 @@ namespace Sass {
Value_Ptr op_color_number(enum Sass_OP op, const Color& lhs, const Number& rhs, struct Sass_Inspect_Options opt, const ParserState& pstate, bool delayed)
{
double rval = rhs.value();
if (op == Sass_OP::DIV && rval == 0) {

if ((op == Sass_OP::DIV || op == Sass_OP::DIV) && rval == 0) {
// comparison of Fixnum with Float failed?
throw Exception::ZeroDivisionError(lhs, rhs);
}

op_color_deprecation(op, lhs.to_string(), rhs.to_string(), pstate);

return SASS_MEMORY_NEW(Color,
pstate,
ops[op](lhs.r(), rval),
Expand Down

0 comments on commit d18fd47

Please sign in to comment.