diff options
| author | Semih Yavuz <semih.yavuz@qt.io> | 2025-11-03 11:13:52 +0100 |
|---|---|---|
| committer | Semih Yavuz <semih.yavuz@qt.io> | 2025-11-04 11:50:03 +0100 |
| commit | 617621657a8f6eb94126016c5648ff6d94e30640 (patch) | |
| tree | eb97d41635d9f771b32443bede3243696d4a0090 | |
| parent | 1b06f33da6e998aaf757acd98629446b74c7af5b (diff) | |
qmlformat: Fix SemicolonRule option ignored in config file
The SemicolonRule setting from .qmlformat.ini was not being applied,
only the command line option took effect.
Fixes: QTBUG-141638
Change-Id: I471fde37c3650e872a893ce46bb5f55e9cc3158e
Initial-Patch-By: Dmitry Makarenko <kryksyh@gmail.com>
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
(cherry picked from commit 282446da4cf67a50013cad3a98e5b4a7aaffa710)
| -rw-r--r-- | src/qmlformat/qqmlformatoptions.cpp | 32 | ||||
| -rw-r--r-- | tests/auto/qml/qmlformat/data/iniFiles/semicolonRule.ini | 2 | ||||
| -rw-r--r-- | tests/auto/qml/qmlformat/tst_qmlformat_cli.cpp | 11 |
3 files changed, 39 insertions, 6 deletions
diff --git a/src/qmlformat/qqmlformatoptions.cpp b/src/qmlformat/qqmlformatoptions.cpp index 21241b6eec..e39e5957ee 100644 --- a/src/qmlformat/qqmlformatoptions.cpp +++ b/src/qmlformat/qqmlformatoptions.cpp @@ -67,6 +67,16 @@ QQmlFormatOptionLineEndings QQmlFormatOptions::parseEndings(const QString &endin #endif } +std::optional<QQmlJS::Dom::LineWriterOptions::SemicolonRule> parseSemicolonRule(const QString &value) { + if (value == "always"_L1) { + return QQmlJS::Dom::LineWriterOptions::SemicolonRule::Always; + } else if (value == "essential"_L1) { + return QQmlJS::Dom::LineWriterOptions::SemicolonRule::Essential; + } else { + return std::nullopt; + } +} + void QQmlFormatOptions::applySettings(const QQmlFormatSettings &settings) { // If the options is already set by commandline, don't override it with the values in the .ini @@ -109,6 +119,18 @@ void QQmlFormatOptions::applySettings(const QQmlFormatSettings &settings) && settings.isSet(QQmlFormatSettings::s_sortImportsSetting)) { setSortImports(settings.value(QQmlFormatSettings::s_sortImportsSetting).toBool()); } + + if (!isMarked(Settings::SemicolonRule) + && settings.isSet(QQmlFormatSettings::s_semiColonRuleSetting)) { + const auto semicolonRule = parseSemicolonRule( + settings.value(QQmlFormatSettings::s_semiColonRuleSetting).toString()); + if (!semicolonRule.has_value()) { + qWarning().noquote() << "Invalid semicolon rule in settings file, using 'always'"; + setSemicolonRule(QQmlJS::Dom::LineWriterOptions::SemicolonRule::Always); + } else { + setSemicolonRule(semicolonRule.value()); + } + } } QQmlFormatOptions QQmlFormatOptions::buildCommandLineOptions(const QStringList &args) @@ -321,14 +343,12 @@ QQmlFormatOptions QQmlFormatOptions::buildCommandLineOptions(const QStringList & if (parser.isSet(semicolonRuleOption)) { options.mark(Settings::SemicolonRule); const auto value = parser.value(semicolonRuleOption); - if (value == "always"_L1) { - options.setSemicolonRule(QQmlJS::Dom::LineWriterOptions::SemicolonRule::Always); - } else if (value == "essential"_L1) { - options.setSemicolonRule(QQmlJS::Dom::LineWriterOptions::SemicolonRule::Essential); - } else { - options.addError("Error: Invalid value passed to --semicolon-rule."_L1); + auto semicolonRule = parseSemicolonRule(value); + if (!semicolonRule.has_value()) { + options.addError("Error: Invalid value passed to --semicolon-rule. Must be 'always' or 'essential'."_L1); return options; } + options.setSemicolonRule(semicolonRule.value()); } options.setFiles(files); options.setArguments(parser.positionalArguments()); diff --git a/tests/auto/qml/qmlformat/data/iniFiles/semicolonRule.ini b/tests/auto/qml/qmlformat/data/iniFiles/semicolonRule.ini new file mode 100644 index 0000000000..874aab0d4b --- /dev/null +++ b/tests/auto/qml/qmlformat/data/iniFiles/semicolonRule.ini @@ -0,0 +1,2 @@ +[General] +SemicolonRule=essential diff --git a/tests/auto/qml/qmlformat/tst_qmlformat_cli.cpp b/tests/auto/qml/qmlformat/tst_qmlformat_cli.cpp index f96f6d0a01..6cacd43d7c 100644 --- a/tests/auto/qml/qmlformat/tst_qmlformat_cli.cpp +++ b/tests/auto/qml/qmlformat/tst_qmlformat_cli.cpp @@ -452,6 +452,15 @@ void TestQmlformatCli::settingsFromFileOrCommandLine_data() << testFile("iniFiles/dummySettingsFile.ini") << QStringList{ m_qmlformatPath, "-F", "dummyFilesPath" } << options; } + { + // In settings file, semicolonRule is set to Essential and cli does not override it. + // Essential should be the final value + QQmlFormatOptions expectedOptions; + expectedOptions.setSemicolonRule(QQmlJS::Dom::LineWriterOptions::SemicolonRule::Essential); + QTest::newRow("semiColonRuleFromIniFile") + << testFile("iniFiles/semicolonRule.ini") + << QStringList{ m_qmlformatPath} << expectedOptions; + } } void TestQmlformatCli::settingsFromFileOrCommandLine() @@ -484,6 +493,7 @@ void TestQmlformatCli::settingsFromFileOrCommandLine() QCOMPARE(overridenOptions.objectsSpacing(), expectedOptions.objectsSpacing()); QCOMPARE(overridenOptions.functionsSpacing(), expectedOptions.functionsSpacing()); QCOMPARE(overridenOptions.sortImports(), expectedOptions.sortImports()); + QCOMPARE(overridenOptions.semicolonRule(), expectedOptions.semicolonRule()); }; verify(); @@ -524,6 +534,7 @@ void TestQmlformatCli::multipleSettingsFiles() QCOMPARE(test1Options.objectsSpacing(), test2Options.objectsSpacing()); QCOMPARE(test1Options.functionsSpacing(), test2Options.functionsSpacing()); QCOMPARE(test1Options.sortImports(), test2Options.sortImports()); + QCOMPARE(test1Options.semicolonRule(), test2Options.semicolonRule()); } QTEST_MAIN(TestQmlformatCli) #include "tst_qmlformat_cli.moc" |
