aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/qmljs/qmljsbundle.cpp
blob: 28f88fca4520608163342c7c7abc7825d8f2c751 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "qmljsbundle.h"

#include "jsoncheck.h"

#include <QString>
#include <QFile>
#include <QRegularExpression>
#include <QTextStream>
#include <QHash>


namespace QmlJS {
typedef PersistentTrie::Trie Trie;

QmlBundle::QmlBundle()
{ }

QmlBundle::QmlBundle(const QString &bundleName, const Trie &searchPaths,
                     const Trie &installPaths, const Trie &supportedImports,
                     const Trie &implicitImports)
    : m_name(bundleName), m_searchPaths(searchPaths), m_installPaths(installPaths),
      m_supportedImports(supportedImports), m_implicitImports(implicitImports)
{ }


QString QmlBundle::name() const
{
    return m_name;
}

Trie QmlBundle::installPaths() const
{
    return m_installPaths;
}

Trie QmlBundle::searchPaths() const
{
    return m_searchPaths;
}

Trie QmlBundle::implicitImports() const
{
    return m_implicitImports;
}

Trie QmlBundle::supportedImports() const
{
    return m_supportedImports;
}

void QmlBundle::merge(const QmlBundle &o)
{
    *this = mergeF(o);
}

void QmlBundle::intersect(const QmlBundle &o)
{
    *this = intersectF(o);
}


QmlBundle QmlBundle::mergeF(const QmlBundle &o) const
{
    return QmlBundle(QString::fromLatin1("(%1)||(%2)").arg(name()).arg(o.name()),
                     searchPaths().mergeF(o.searchPaths()),
                     installPaths().mergeF(o.installPaths()),
                     supportedImports().mergeF(o.supportedImports()),
                     implicitImports().mergeF(o.implicitImports()));
}

QmlBundle QmlBundle::intersectF(const QmlBundle &o) const
{
    return QmlBundle(QString::fromLatin1("(%1)&&(%2)").arg(name()).arg(o.name()),
                     searchPaths().mergeF(o.searchPaths()),
                     installPaths().mergeF(o.installPaths()),
                     supportedImports().intersectF(o.supportedImports()),
                     implicitImports().mergeF(o.implicitImports()));
}

bool QmlBundle::isEmpty() const
{
    return m_implicitImports.isEmpty() && m_installPaths.isEmpty()
            && m_searchPaths.isEmpty() && m_supportedImports.isEmpty();
}

void QmlBundle::replaceVars(const QHash<QString, QString> &replacements)
{
    m_implicitImports.replace(replacements);
    m_installPaths.replace(replacements);
    m_searchPaths.replace(replacements);
    m_supportedImports.replace(replacements);
}

QmlBundle QmlBundle::replaceVarsF(const QHash<QString, QString> &replacements) const
{
    QmlBundle res(*this);
    res.replaceVars(replacements);
    return res;
}

bool QmlBundle::writeTo(const QString &path) const
{
    QFile f(path);
    if (!f.open(QIODevice::WriteOnly | QIODevice::Text))
        return false;
    QTextStream stream(&f);
    return writeTo(stream);
}

bool QmlBundle::operator==(const QmlBundle &o) const
{
    return o.implicitImports() == implicitImports()
            && o.installPaths() == installPaths()
            && o.supportedImports() == supportedImports(); // name is not considered
}

bool QmlBundle::operator!=(const QmlBundle &o) const
{
    return !((*this) == o);
}

void QmlBundle::printEscaped(QTextStream &s, const QString &str)
{
    s << QLatin1Char('"');
    QString::const_iterator i = str.constBegin(), iLast = str.constBegin(),
            iEnd = str.constEnd();
    while (i != iEnd) {
        if ((*i) != QLatin1Char('"')) {
            s << str.mid(static_cast<int>(iLast - str.constBegin()), static_cast<int>(i - iLast))
              << QLatin1Char('\\');
            iLast = i;
        }
        ++i;
    }
    s << str.mid(static_cast<int>(iLast - str.constBegin()), static_cast<int>(i - iLast));
}

void QmlBundle::writeTrie(QTextStream &stream, const Trie &t, const QString &indent) {
    stream << QLatin1Char('[');
    bool firstLine = true;
    const QStringList list = t.stringList();
    for (const QString &i : list) {
        if (firstLine)
            firstLine = false;
        else
            stream << QLatin1Char(',');
        stream << QLatin1String("\n") << indent << QLatin1String("    ");
        printEscaped(stream, i);
    }
    stream << QLatin1Char(']');
}

bool QmlBundle::writeTo(QTextStream &stream, const QString &indent) const
{
    QString innerIndent = QString::fromLatin1("    ").append(indent);
    stream << indent << QLatin1String("{\n")
           << indent << QLatin1String("    \"name\": ");
    printEscaped(stream, name());
    stream << QLatin1String(",\n")
           << indent << QLatin1String("    \"searchPaths\": ");
    writeTrie(stream, searchPaths(), innerIndent);
    stream << QLatin1String(",\n")
           << indent << QLatin1String("    \"installPaths\": ");
    writeTrie(stream, installPaths(), innerIndent);
    stream << QLatin1String(",\n")
           << indent << QLatin1String("    \"supportedImports\": ");
    writeTrie(stream, supportedImports(), innerIndent);
    stream << QLatin1String(",\n")
           << QLatin1String("    \"implicitImports\": ");
    writeTrie(stream, implicitImports(), innerIndent);
    stream << QLatin1String("\n")
           << indent << QLatin1Char('}');
    return true;
}

QString QmlBundle::toString(const QString &indent)
{
    QString res;
    QTextStream s(&res);
    writeTo(s, indent);
    return res;
}

QStringList QmlBundle::maybeReadTrie(Trie &trie, JsonObjectValue *config,
                                     const QString &path, const QString &propertyName,
                                     bool required, bool stripVersions)
{
    static const QRegularExpression versionNumberAtEnd("^(.+)( \\d+\\.\\d+)$");
    QStringList res;
    if (!config->hasMember(propertyName)) {
        if (required)
            res << QString::fromLatin1("Missing required property \"%1\" from %2").arg(propertyName,
                                                                                       path);
        return res;
    }

    JsonValue *imp0 = config->member(propertyName);
    JsonArrayValue *imp = ((imp0 != nullptr) ? imp0->toArray() : nullptr);
    if (imp != nullptr) {
        const QList<JsonValue *> elements = imp->elements();
        for (JsonValue *v : elements) {
            JsonStringValue *impStr = ((v != nullptr) ? v->toString() : nullptr);
            if (impStr != nullptr) {
                QString value = impStr->value();
                if (stripVersions) {
                    const QRegularExpressionMatch match = versionNumberAtEnd.match(value);
                    if (match.hasMatch())
                        value = match.captured(1);
                }
                trie.insert(value);
            } else {
                res.append(QString::fromLatin1("Expected all elements of array in property \"%1\" "
                                               "to be strings in QmlBundle at %2.")
                           .arg(propertyName, path));
                break;
            }
        }
    } else {
        res.append(QString::fromLatin1("Expected string array in property \"%1\" in QmlBundle at %2.")
                   .arg(propertyName, path));
    }
    return res;
}

bool QmlBundle::readFrom(QString path, bool stripVersions, QStringList *errors)
{
    JsonMemoryPool pool;

    QFile f(path);
    if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) {
        if (errors)
            (*errors) << QString::fromLatin1("Could not open file at %1 .").arg(path);
        return false;
    }
    JsonObjectValue *config = JsonValue::create(QString::fromUtf8(f.readAll()), &pool)->toObject();
    if (config == nullptr) {
        if (errors)
            (*errors) << QString::fromLatin1("Could not parse json object in file at %1 .").arg(path);
        return false;
    }
    QStringList errs;
    if (config->hasMember(QLatin1String("name"))) {
        JsonValue *n0 = config->member(QLatin1String("name"));
        JsonStringValue *n = ((n0 != nullptr) ? n0->toString() : nullptr);
        if (n != nullptr)
            m_name = n->value();
        else
            errs.append(QString::fromLatin1("Property \"name\" in QmlBundle at %1 is expected "
                                             "to be a string.").arg(path));
    } else {
        errs.append(QString::fromLatin1("Missing required property \"name\" in QmlBundle "
                                         "at %1 .").arg(path));
    }
    errs << maybeReadTrie(m_searchPaths, config, path, QLatin1String("searchPaths"));
    errs << maybeReadTrie(m_installPaths, config, path, QLatin1String("installPaths"));
    errs << maybeReadTrie(m_supportedImports, config, path, QLatin1String("supportedImports"),
                          true, stripVersions);
    errs << maybeReadTrie(m_implicitImports, config, path, QLatin1String("implicitImports"));
    if (errors)
        (*errors) << errs;
    return errs.isEmpty();
}

QmlBundle QmlLanguageBundles::bundleForLanguage(Dialect l) const
{
    if (m_bundles.contains(l))
        return m_bundles.value(l);
    return QmlBundle();
}

void QmlLanguageBundles::mergeBundleForLanguage(Dialect l, const QmlBundle &bundle)
{
    if (bundle.isEmpty())
        return;
    if (m_bundles.contains(l))
        m_bundles[l].merge(bundle);
    else
        m_bundles.insert(l,bundle);
}

const QList<Dialect> QmlLanguageBundles::languages() const
{
    return m_bundles.keys();
}

void QmlLanguageBundles::mergeLanguageBundles(const QmlLanguageBundles &o)
{
    for (Dialect l : o.languages())
        mergeBundleForLanguage(l, o.bundleForLanguage(l));
}

} // end namespace QmlJS