aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmlcompiler/qqmljslintervisitor.cpp
diff options
context:
space:
mode:
authorSami Shalayel <sami.shalayel@qt.io>2025-04-29 09:38:41 +0200
committerOlivier De Cannière <olivier.decanniere@qt.io>2025-05-16 16:59:43 +0200
commit5e024a74531632a1938797fb5730978bd4dce576 (patch)
tree7670a41eb5d75441348ff4282813acfee6cd6a16 /src/qmlcompiler/qqmljslintervisitor.cpp
parent69a6ea8334605cb22eaf95f75a01c4fa425f91a9 (diff)
LinterImportVisitor: move linting code from QQmlJSImportVisitor
Move the stringliteral linting code into the new LinterImportVisitor class. Task-number: QTBUG-129307 Change-Id: I5f358f91c41e99383582be9fc1628814563848ed Reviewed-by: Olivier De Cannière <olivier.decanniere@qt.io>
Diffstat (limited to 'src/qmlcompiler/qqmljslintervisitor.cpp')
-rw-r--r--src/qmlcompiler/qqmljslintervisitor.cpp45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/qmlcompiler/qqmljslintervisitor.cpp b/src/qmlcompiler/qqmljslintervisitor.cpp
index 6dc08a7b70..ebfa696f26 100644
--- a/src/qmlcompiler/qqmljslintervisitor.cpp
+++ b/src/qmlcompiler/qqmljslintervisitor.cpp
@@ -5,6 +5,9 @@
QT_BEGIN_NAMESPACE
+using namespace Qt::StringLiterals;
+using namespace QQmlJS::AST;
+
namespace QQmlJS {
/*!
\internal
@@ -14,6 +17,48 @@ namespace QQmlJS {
are purely syntactic checks, or style-checks warnings that don't make sense during compilation.
*/
+bool LinterVisitor::visit(StringLiteral *sl)
+{
+ QQmlJSImportVisitor::visit(sl);
+ const QString s = m_logger->code().mid(sl->literalToken.begin(), sl->literalToken.length);
+
+ if (s.contains(QLatin1Char('\r')) || s.contains(QLatin1Char('\n')) || s.contains(QChar(0x2028u))
+ || s.contains(QChar(0x2029u))) {
+ QString templateString;
+
+ bool escaped = false;
+ const QChar stringQuote = s[0];
+ for (qsizetype i = 1; i < s.size() - 1; i++) {
+ const QChar c = s[i];
+
+ if (c == u'\\') {
+ escaped = !escaped;
+ } else if (escaped) {
+ // If we encounter an escaped quote, unescape it since we use backticks here
+ if (c == stringQuote)
+ templateString.chop(1);
+
+ escaped = false;
+ } else {
+ if (c == u'`')
+ templateString += u'\\';
+ if (c == u'$' && i + 1 < s.size() - 1 && s[i + 1] == u'{')
+ templateString += u'\\';
+ }
+
+ templateString += c;
+ }
+
+ QQmlJSFixSuggestion suggestion = { "Use a template literal instead."_L1, sl->literalToken,
+ u"`" % templateString % u"`" };
+ suggestion.setAutoApplicable();
+ m_logger->log(QStringLiteral("String contains unescaped line terminator which is "
+ "deprecated."),
+ qmlMultilineStrings, sl->literalToken, true, true, suggestion);
+ }
+ return true;
+}
+
} // namespace QQmlJS
QT_END_NAMESPACE