aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmlcompiler/qqmljsvaluetypefromstringcheck.cpp
blob: 40ca97e57918c10e82971242a3969b65d5b15d53 (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
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "qqmljsvaluetypefromstringcheck_p.h"
#include <private/qqmlstringconverters_p.h>

QT_BEGIN_NAMESPACE

using namespace Qt::StringLiterals;

/*!
\internal
\class ValueTypeFromStringError

Helps to differentiate between three states:
\list
\li a value type was constructed by a string in a deprecated way, e.g. "50x70" for a QSizeF
\li a value type was constructed by an invalid string in a deprecated way, e.g. "50,70" for a QSizeF
\li a value type was constructed by a string in a non-deprecated way.
\endlist
*/

/*!
\internal
Checks if known value types are being constructed in a deprecated way, and constructs the code for
the fix-it.
*/
QQmlJSStructuredTypeError QQmlJSValueTypeFromStringCheck::hasError(const QString &typeName,
                                                                  const QString &value)
{
    if (typeName == u"QPointF" || typeName == u"QPoint") {
        std::array<double, 2> numbers;
        if (!QQmlStringConverters::isValidNumberString<2, u','>(value, &numbers))
            return QQmlJSStructuredTypeError::withInvalidString();

        const auto result = QQmlJSStructuredTypeError::fromSuggestedString(
                u"({ x: %1, y: %2 })"_s.arg(numbers[0]).arg(numbers[1]));
        return result;
    } else if (typeName == u"QSizeF" || typeName == u"QSize") {
        std::array<double, 2> numbers;
        if (!QQmlStringConverters::isValidNumberString<2, u'x'>(value, &numbers))
            return QQmlJSStructuredTypeError::withInvalidString();

        const auto result = QQmlJSStructuredTypeError::fromSuggestedString(
                u"({ width: %1, height: %2 })"_s.arg(numbers[0]).arg(numbers[1]));
        return result;
    } else if (typeName == u"QRectF" || typeName == u"QRect") {
        std::array<double, 4> numbers;
        if (!QQmlStringConverters::isValidNumberString<4, u',', u',', u'x'>(value, &numbers))
            return QQmlJSStructuredTypeError::withInvalidString();

        const auto result = QQmlJSStructuredTypeError::fromSuggestedString(
                u"({ x: %1, y: %2, width: %3, height: %4 })"_s.arg(numbers[0])
                        .arg(numbers[1])
                        .arg(numbers[2])
                        .arg(numbers[3]));
        return result;
    }

    return QQmlJSStructuredTypeError::withValidString();
}

QT_END_NAMESPACE