<commit_before>#include <set>
#include "drafter.h"
#include "nan.h"
#include "refractToV8.h"
#include "refract/Element.h"
#include "refract/VisitorUtils.h" // TypeQuery and GetValue<T>
#include "refract/FilterVisitor.h" // Filtering just annotations
#include "refract/Iterate.h"
#include "Serialize.h" // Included because of serialize key for parseResult

using namespace v8;
using namespace refract;

struct v8Wrapper {

    bool sourcemap;
    Local<Value> v8_value;

    v8Wrapper(): sourcemap(false){};

    v8Wrapper(bool sourcemap): sourcemap(sourcemap){};

    void operator()(const NullElement& e);
    void operator()(const StringElement& e);
    void operator()(const NumberElement& e);
    void operator()(const BooleanElement& e);
    void operator()(const MemberElement& e);
    void operator()(const ArrayElement& e);
    void operator()(const EnumElement& e);
    void operator()(const ExtendElement& e);
    void operator()(const OptionElement& e);
    void operator()(const SelectElement& e);
    void operator()(const ObjectElement& e);
    void operator()(const IElement& e) {};
    void operator()(const RefElement& e);
    void operator()(const HolderElement& e);

    template<typename T>
    void operator()(const T& e) {
        static_assert(sizeof(T) == 0, "Unknown Element in v8Wrapper");
    }
};

const std::set<std::string> basic_elements = {
    "null",
    "string",
    "number",
    "boolean",
    "array",
    "object",
    "member"
};

Local<Value> v8_string(const std::string& value)
{
    return Nan::New<String>(value.c_str()).ToLocalChecked();
}


Local<Value> ElementToObject(const IElement* e, bool sourcemap)
{
    v8Wrapper f(sourcemap);
    Visitor v(f);
    e->content(v);
    return f.v8_value;
}


Local<Object> v8ElementCollection(const IElement::MemberElementCollection& collection, bool sourcemap)
{
    typedef IElement::MemberElementCollection::const_iterator iterator;

    Local<Object> result = Nan::New<Object>();

    for (iterator it = collection.begin(); it != collection.end(); ++it) {

        Local<Value> key;
        Local<Value> value;

        StringElement* str = TypeQueryVisitor::as<StringElement>((*it)->value.first);

        if (!sourcemap) {
            if (str && str->value == "sourceMap"){
                continue;
            }
        }

        if (str) {
            key = v8_string(str->value);
        }
        else if ((*it)->value.first) {
            key = ElementToObject((*it)->value.first, sourcemap);
        }

        if ((*it)->value.second) {
            value = ElementToObject((*it)->value.second, sourcemap);
        }

        result->Set(key, value);
    }

    return result;
}


Local<Object> v8Element(const IElement& e, bool sourcemap)
{
    Local<Object> res = Nan::New<Object>();
    res->Set(v8_string("element"), v8_string(e.element()));
    if (e.meta.size() > 0) {
        res->Set(v8_string("meta"), v8ElementCollection(e.meta, sourcemap));
    }

    if (e.element() == "annotation") {
        sourcemap = true;
    }

    if (e.attributes.size() > 0) {
        Local<Object> attrs = v8ElementCollection(e.attributes, sourcemap);
        MaybeLocal<Array> maybeProps = Nan::GetOwnPropertyNames(attrs);
        if (!maybeProps.IsEmpty()) {
            Local<Array> props = maybeProps.ToLocalChecked();
            if (props->Length() > 0) {
                res->Set(v8_string("attributes"), attrs);
            }
        }
    }
    return res;
}

template <typename T>
Local<Object> v8ValueList(const T& e, bool sourcemap)
{
    Local<Object> obj = v8Element(e, sourcemap);

    if (!e.empty()) {
        size_t i = 0;
        Local<Array> array = Nan::New<Array>();
        typedef typename T::ValueType::const_iterator iterator;

        for (iterator it = e.value.begin(); it != e.value.end(); ++i, ++it) {
            array->Set(i, ElementToObject(*it, sourcemap));
        }
        obj->Set(v8_string("content"), array);
    }

    return obj;
}


Local<Value> v8RefElement(const RefElement& e, bool sourcemap)
{
    typedef RefElement::ValueType::const_iterator iterator;
    Local<Object> obj = v8Element(e, sourcemap);

    obj->Set(v8_string("content"), v8_string(e.value));

    return obj;
}

void v8Wrapper::operator()(const NullElement& e)
{
    Local<Object> obj = v8Element(e, sourcemap);
    obj->Set(v8_string("content"), Nan::Null());<commit_msg>fix: Remove unused typedef<commit_after>#include <set>
#include "drafter.h"
#include "nan.h"
#include "refractToV8.h"
#include "refract/Element.h"
#include "refract/VisitorUtils.h" // TypeQuery and GetValue<T>
#include "refract/FilterVisitor.h" // Filtering just annotations
#include "refract/Iterate.h"
#include "Serialize.h" // Included because of serialize key for parseResult

using namespace v8;
using namespace refract;

struct v8Wrapper {

    bool sourcemap;
    Local<Value> v8_value;

    v8Wrapper(): sourcemap(false){};

    v8Wrapper(bool sourcemap): sourcemap(sourcemap){};

    void operator()(const NullElement& e);
    void operator()(const StringElement& e);
    void operator()(const NumberElement& e);
    void operator()(const BooleanElement& e);
    void operator()(const MemberElement& e);
    void operator()(const ArrayElement& e);
    void operator()(const EnumElement& e);
    void operator()(const ExtendElement& e);
    void operator()(const OptionElement& e);
    void operator()(const SelectElement& e);
    void operator()(const ObjectElement& e);
    void operator()(const IElement& e) {};
    void operator()(const RefElement& e);
    void operator()(const HolderElement& e);

    template<typename T>
    void operator()(const T& e) {
        static_assert(sizeof(T) == 0, "Unknown Element in v8Wrapper");
    }
};

const std::set<std::string> basic_elements = {
    "null",
    "string",
    "number",
    "boolean",
    "array",
    "object",
    "member"
};

Local<Value> v8_string(const std::string& value)
{
    return Nan::New<String>(value.c_str()).ToLocalChecked();
}


Local<Value> ElementToObject(const IElement* e, bool sourcemap)
{
    v8Wrapper f(sourcemap);
    Visitor v(f);
    e->content(v);
    return f.v8_value;
}


Local<Object> v8ElementCollection(const IElement::MemberElementCollection& collection, bool sourcemap)
{
    typedef IElement::MemberElementCollection::const_iterator iterator;

    Local<Object> result = Nan::New<Object>();

    for (iterator it = collection.begin(); it != collection.end(); ++it) {

        Local<Value> key;
        Local<Value> value;

        StringElement* str = TypeQueryVisitor::as<StringElement>((*it)->value.first);

        if (!sourcemap) {
            if (str && str->value == "sourceMap"){
                continue;
            }
        }

        if (str) {
            key = v8_string(str->value);
        }
        else if ((*it)->value.first) {
            key = ElementToObject((*it)->value.first, sourcemap);
        }

        if ((*it)->value.second) {
            value = ElementToObject((*it)->value.second, sourcemap);
        }

        result->Set(key, value);
    }

    return result;
}


Local<Object> v8Element(const IElement& e, bool sourcemap)
{
    Local<Object> res = Nan::New<Object>();
    res->Set(v8_string("element"), v8_string(e.element()));
    if (e.meta.size() > 0) {
        res->Set(v8_string("meta"), v8ElementCollection(e.meta, sourcemap));
    }

    if (e.element() == "annotation") {
        sourcemap = true;
    }

    if (e.attributes.size() > 0) {
        Local<Object> attrs = v8ElementCollection(e.attributes, sourcemap);
        MaybeLocal<Array> maybeProps = Nan::GetOwnPropertyNames(attrs);
        if (!maybeProps.IsEmpty()) {
            Local<Array> props = maybeProps.ToLocalChecked();
            if (props->Length() > 0) {
                res->Set(v8_string("attributes"), attrs);
            }
        }
    }
    return res;
}

template <typename T>
Local<Object> v8ValueList(const T& e, bool sourcemap)
{
    Local<Object> obj = v8Element(e, sourcemap);

    if (!e.empty()) {
        size_t i = 0;
        Local<Array> array = Nan::New<Array>();
        typedef typename T::ValueType::const_iterator iterator;

        for (iterator it = e.value.begin(); it != e.value.end(); ++i, ++it) {
            array->Set(i, ElementToObject(*it, sourcemap));
        }
        obj->Set(v8_string("content"), array);
    }

    return obj;
}


Local<Value> v8RefElement(const RefElement& e, bool sourcemap)
{
    Local<Object> obj = v8Element(e, sourcemap);

    obj->Set(v8_string("content"), v8_string(e.value));

    return obj;
}

void v8Wrapper::operator()(const NullElement& e)
{
    Local<Object> obj = v8Element(e, sourcemap);
    obj->Set(v8_string("content"), Nan::Null());
{'fraction_non_alphanumeric': 0.1534045240555747, 'fraction_numerical': 0.00780801469743943, 'mean_word_length': 3.1694590713259934, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 99, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1412850', 'n_tokens_mistral': 2795, 'n_tokens_neox': 2712, 'n_words': 615}