Skip to content

Commit de83d30

Browse files
committed
Fix ParserState for SourceMaps
- Fixes parent selector mappings - Fixes media block/query mappings - Fixes variable assignment mappings - Fixes range over binary expressions - Don't include semicolon for statics
1 parent 01f4ffa commit de83d30

File tree

6 files changed

+121
-25
lines changed

6 files changed

+121
-25
lines changed

src/ast.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ namespace Sass {
122122
pstate_.offset += pstate - pstate_ + pstate.offset;
123123
}
124124

125+
void AST_Node::set_pstate_offset(const Offset& offset)
126+
{
127+
pstate_.offset = offset;
128+
}
129+
125130
inline bool is_ns_eq(const std::string& l, const std::string& r)
126131
{
127132
if (l.empty() && r.empty()) return true;
@@ -1056,22 +1061,30 @@ namespace Sass {
10561061
if (Class_Selector* sq = dynamic_cast<Class_Selector*>(rh->last())) {
10571062
Class_Selector* sqs = SASS_MEMORY_NEW(ctx.mem, Class_Selector, *sq);
10581063
sqs->name(sqs->name() + (*h)[0]->name());
1064+
sqs->pstate((*h)[0]->pstate());
10591065
(*rh)[rh->length()-1] = sqs;
1066+
rh->pstate(h->pstate());
10601067
for (i = 1; i < L; ++i) *rh << (*h)[i];
10611068
} else if (Id_Selector* sq = dynamic_cast<Id_Selector*>(rh->last())) {
10621069
Id_Selector* sqs = SASS_MEMORY_NEW(ctx.mem, Id_Selector, *sq);
10631070
sqs->name(sqs->name() + (*h)[0]->name());
1071+
sqs->pstate((*h)[0]->pstate());
10641072
(*rh)[rh->length()-1] = sqs;
1073+
rh->pstate(h->pstate());
10651074
for (i = 1; i < L; ++i) *rh << (*h)[i];
10661075
} else if (Element_Selector* ts = dynamic_cast<Element_Selector*>(rh->last())) {
10671076
Element_Selector* tss = SASS_MEMORY_NEW(ctx.mem, Element_Selector, *ts);
10681077
tss->name(tss->name() + (*h)[0]->name());
1078+
tss->pstate((*h)[0]->pstate());
10691079
(*rh)[rh->length()-1] = tss;
1080+
rh->pstate(h->pstate());
10701081
for (i = 1; i < L; ++i) *rh << (*h)[i];
10711082
} else if (Placeholder_Selector* ps = dynamic_cast<Placeholder_Selector*>(rh->last())) {
10721083
Placeholder_Selector* pss = SASS_MEMORY_NEW(ctx.mem, Placeholder_Selector, *ps);
10731084
pss->name(pss->name() + (*h)[0]->name());
1085+
pss->pstate((*h)[0]->pstate());
10741086
(*rh)[rh->length()-1] = pss;
1087+
rh->pstate(h->pstate());
10751088
for (i = 1; i < L; ++i) *rh << (*h)[i];
10761089
} else {
10771090
*last()->head_ += h;
@@ -1150,8 +1163,19 @@ namespace Sass {
11501163
Sequence_Selector* ss = this->clone(ctx);
11511164
ss->tail(t ? t->clone(ctx) : 0);
11521165
SimpleSequence_Selector* h = head_->clone(ctx);
1166+
// remove parent selector from sequence
11531167
if (h->length()) h->erase(h->begin());
11541168
ss->head(h->length() ? h : 0);
1169+
// adjust for parent selector (1 char)
1170+
if (h->length()) {
1171+
ParserState state((*h)[0]->pstate());
1172+
state.offset.column += 1;
1173+
state.column -= 1;
1174+
(*h)[0]->pstate(state);
1175+
}
1176+
// keep old parser state
1177+
s->pstate(pstate());
1178+
// append new tail
11551179
s->append(ctx, ss);
11561180
*retval << s;
11571181
}
@@ -1171,10 +1195,21 @@ namespace Sass {
11711195
}
11721196
ss->tail(tail ? tail->clone(ctx) : 0);
11731197
SimpleSequence_Selector* h = head_->clone(ctx);
1198+
// remove parent selector from sequence
11741199
if (h->length()) h->erase(h->begin());
11751200
ss->head(h->length() ? h : 0);
11761201
// \/ IMO ruby sass bug \/
11771202
ss->has_line_feed(false);
1203+
// adjust for parent selector (1 char)
1204+
if (h->length()) {
1205+
ParserState state((*h)[0]->pstate());
1206+
state.offset.column += 1;
1207+
state.column -= 1;
1208+
(*h)[0]->pstate(state);
1209+
}
1210+
// keep old parser state
1211+
s->pstate(pstate());
1212+
// append new tail
11781213
s->append(ctx, ss);
11791214
*retval << s;
11801215
}
@@ -1557,6 +1592,13 @@ namespace Sass {
15571592
return result;
15581593
}
15591594

1595+
SimpleSequence_Selector& SimpleSequence_Selector::operator<<(Simple_Selector* element)
1596+
{
1597+
Vectorized<Simple_Selector*>::operator<<(element);
1598+
pstate_.offset += element->pstate().offset;
1599+
return *this;
1600+
}
1601+
15601602
SimpleSequence_Selector* SimpleSequence_Selector::minus(SimpleSequence_Selector* rhs, Context& ctx)
15611603
{
15621604
SimpleSequence_Selector* result = SASS_MEMORY_NEW(ctx.mem, SimpleSequence_Selector, pstate());

src/ast.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ namespace Sass {
101101
// virtual Block* block() { return 0; }
102102
public:
103103
void update_pstate(const ParserState& pstate);
104+
void set_pstate_offset(const Offset& offset);
104105
public:
105106
Offset off() { return pstate(); }
106107
Position pos() { return pstate(); }
@@ -235,7 +236,7 @@ namespace Sass {
235236
T& operator[](size_t i) { return elements_[i]; }
236237
virtual const T& at(size_t i) const { return elements_.at(i); }
237238
const T& operator[](size_t i) const { return elements_[i]; }
238-
Vectorized& operator<<(T element)
239+
virtual Vectorized& operator<<(T element)
239240
{
240241
if (!element) return *this;
241242
reset_hash();
@@ -2292,6 +2293,8 @@ namespace Sass {
22922293
return false;
22932294
};
22942295

2296+
SimpleSequence_Selector& operator<<(Simple_Selector* element);
2297+
22952298
bool is_universal() const
22962299
{
22972300
return length() == 1 && (*this)[0]->is_universal();

src/eval.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ namespace Sass {
502502
// only the last item will be used to eval the binary expression
503503
if (String_Schema* s_l = dynamic_cast<String_Schema*>(b->left())) {
504504
if (!s_l->has_interpolant() && (!s_l->is_right_interpolant())) {
505-
ret_schema = SASS_MEMORY_NEW(ctx.mem, String_Schema, s_l->pstate());
505+
ret_schema = SASS_MEMORY_NEW(ctx.mem, String_Schema, b->pstate());
506506
Binary_Expression* bin_ex = SASS_MEMORY_NEW(ctx.mem, Binary_Expression, b->pstate(),
507507
b->op(), s_l->last(), b->right());
508508
bin_ex->is_delayed(b->left()->is_delayed() || b->right()->is_delayed()); // unverified
@@ -515,7 +515,7 @@ namespace Sass {
515515
}
516516
if (String_Schema* s_r = dynamic_cast<String_Schema*>(b->right())) {
517517
if (!s_r->has_interpolant() && (!s_r->is_left_interpolant() || op_type == Sass_OP::DIV)) {
518-
ret_schema = SASS_MEMORY_NEW(ctx.mem, String_Schema, s_r->pstate());
518+
ret_schema = SASS_MEMORY_NEW(ctx.mem, String_Schema, b->pstate());
519519
Binary_Expression* bin_ex = SASS_MEMORY_NEW(ctx.mem, Binary_Expression, b->pstate(),
520520
b->op(), b->left(), s_r->first());
521521
bin_ex->is_delayed(b->left()->is_delayed() || b->right()->is_delayed()); // verified
@@ -599,15 +599,15 @@ namespace Sass {
599599
std::string value(str->value());
600600
const char* start = value.c_str();
601601
if (Prelexer::sequence < Prelexer::dimension, Prelexer::end_of_file >(start) != 0) {
602-
lhs = SASS_MEMORY_NEW(ctx.mem, Textual, lhs->pstate(), Textual::DIMENSION, str->value());
602+
lhs = SASS_MEMORY_NEW(ctx.mem, Textual, b->pstate(), Textual::DIMENSION, str->value());
603603
lhs = lhs->perform(this);
604604
}
605605
}
606606
if (String_Constant* str = dynamic_cast<String_Constant*>(rhs)) {
607607
std::string value(str->value());
608608
const char* start = value.c_str();
609609
if (Prelexer::sequence < Prelexer::number >(start) != 0) {
610-
rhs = SASS_MEMORY_NEW(ctx.mem, Textual, rhs->pstate(), Textual::DIMENSION, str->value());
610+
rhs = SASS_MEMORY_NEW(ctx.mem, Textual, b->pstate(), Textual::DIMENSION, str->value());
611611
rhs = rhs->perform(this);
612612
}
613613
}
@@ -636,7 +636,7 @@ namespace Sass {
636636
str += b->separator();
637637
if (b->op().ws_after) str += " ";
638638
str += v_r->to_string(ctx.c_options);
639-
String_Constant* val = SASS_MEMORY_NEW(ctx.mem, String_Constant, lhs->pstate(), str);
639+
String_Constant* val = SASS_MEMORY_NEW(ctx.mem, String_Constant, b->pstate(), str);
640640
val->is_interpolant(b->left()->has_interpolant());
641641
return val;
642642
}
@@ -1545,7 +1545,7 @@ namespace Sass {
15451545
(sep != "/" || !rqstr || !rqstr->quote_mark()) */
15461546
) {
15471547
// create a new string that might be quoted on output (but do not unquote what we pass)
1548-
return SASS_MEMORY_NEW(mem, String_Quoted, lhs.pstate(), lstr + rstr, 0, false, true);
1548+
return SASS_MEMORY_NEW(mem, String_Quoted, pstate ? *pstate : lhs.pstate(), lstr + rstr, 0, false, true);
15491549
}
15501550

15511551
if (sep != "" && !delayed) {
@@ -1558,7 +1558,7 @@ namespace Sass {
15581558
if (rqstr && rqstr->quote_mark()) rstr = quote(rstr);
15591559
}
15601560

1561-
return SASS_MEMORY_NEW(mem, String_Constant, lhs.pstate(), lstr + sep + rstr);
1561+
return SASS_MEMORY_NEW(mem, String_Constant, pstate ? *pstate : lhs.pstate(), lstr + sep + rstr);
15621562
}
15631563

15641564
Expression* cval_to_astnode(Memory_Manager& mem, union Sass_Value* v, Context& ctx, Backtrace* backtrace, ParserState pstate)

src/json.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ char *json_encode_string(const char *str)
402402
try {
403403
emit_string(&sb, str);
404404
}
405-
catch (std::exception &e) {
405+
catch (std::exception) {
406406
sb_free(&sb);
407407
throw;
408408
}
@@ -421,7 +421,7 @@ char *json_stringify(const JsonNode *node, const char *space)
421421
else
422422
emit_value(&sb, node);
423423
}
424-
catch (std::exception &e) {
424+
catch (std::exception) {
425425
sb_free(&sb);
426426
throw;
427427
}

0 commit comments

Comments
 (0)