Skip to content

Commit

Permalink
Merge pull request sass#768 from xzyfer/fix/error-on-duplicate-map-keys
Browse files Browse the repository at this point in the history
Error when instantiating a map with duplicate keys
  • Loading branch information
xzyfer committed Dec 26, 2014
2 parents 715fa7a + a814a8b commit d8c08c6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
9 changes: 8 additions & 1 deletion ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,21 +204,26 @@ namespace Sass {
vector<Expression*> list_;
protected:
size_t hash_;
Expression* duplicate_key_;
void reset_hash() { hash_ = 0; }
void reset_duplicate_key() { duplicate_key_ = 0; }
virtual void adjust_after_pushing(std::pair<Expression*, Expression*> p) { }
public:
Hashed(size_t s = 0) : elements_(unordered_map<Expression*, Expression*>(s)), list_(vector<Expression*>())
{ elements_.reserve(s); list_.reserve(s); }
{ elements_.reserve(s); list_.reserve(s); reset_duplicate_key(); }
virtual ~Hashed();
size_t length() const { return list_.size(); }
bool empty() const { return list_.empty(); }
bool has(Expression* k) const { return elements_.count(k) == 1; }
Expression* at(Expression* k) const { return elements_.at(k); }
bool has_duplicate_key() const { return duplicate_key_ != 0; }
Expression* get_duplicate_key() const { return duplicate_key_; }
Hashed& operator<<(pair<Expression*, Expression*> p)
{
reset_hash();

if (!has(p.first)) list_.push_back(p.first);
else if (!duplicate_key_) duplicate_key_ = p.first;

elements_[p.first] = p.second;

Expand All @@ -236,6 +241,8 @@ namespace Sass {
for (auto key : h->keys()) {
*this << make_pair(key, h->at(key));
}

reset_duplicate_key();
return *this;
}
const unordered_map<Expression*, Expression*>& pairs() const { return elements_; }
Expand Down
4 changes: 4 additions & 0 deletions parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,7 @@ namespace Sass {

Expression* Parser::parse_map()
{
To_String to_string;
Expression* key = parse_list();

// it's not a map so return the lexed value as a list value
Expand Down Expand Up @@ -875,6 +876,9 @@ namespace Sass {
(*map) << make_pair(key, value);
}

if (map->has_duplicate_key())
{ error("Duplicate key \"" + map->get_duplicate_key()->perform(&to_string) + "\" in map " + map->perform(&to_string) + "."); }

return map;
}

Expand Down

0 comments on commit d8c08c6

Please sign in to comment.