diff options
| author | Semih Yavuz <semih.yavuz@qt.io> | 2025-03-24 12:52:33 +0100 |
|---|---|---|
| committer | Semih Yavuz <semih.yavuz@qt.io> | 2025-05-13 19:07:10 +0300 |
| commit | 5a2a6a8d7732548c02bb52ad4960775f61419003 (patch) | |
| tree | 9fa852e01c54a6608603b3f3c24eb659e75db8c7 /src/qmlformat/qqmlformatoptions.cpp | |
| parent | f4dddb5b6e47a08ffe9d000aad586b781b4fe82f (diff) | |
qmlformat: customizable semicolon
Add semicolon option to qmlformat. While --Always always appends
semicolons to the JS statements, --essential removes the semicolons
unless it is not safe to rely on ASI once semicolons are removed.
Change the way EmptyStatements are handled. Prior to this
commit, semicolons following if, for, foreach, while statements
without bodies were added to the new line with some indentation.
Make the semicolon following no-body items stick to the right
paranthesis. If there is a chain of empty statements, write out a
single one.
[ChangeLog][qmlformat] New option semicolon-rule is added and
EmptyStatement formatting behavior has changed.
* Added --semicolon-rule option with modes:
- --semicolon-rule=always: Always appends semicolons to JS statements.
- --semicolon-rule=essential: Removes semicolons unless ASI makes it unsafe.
* Changed handling of EmptyStatements:
- Semicolons after control structures without a body (e.g., if, for, while)
now appear directly after the closing parenthesis, instead of on a new line.
- Consecutive empty statements are collapsed into a single semicolon.
Fixes: QTBUG-107152
Change-Id: Ic95047a1f0077937d4c1f01328d77a3e6a4f22d6
Reviewed-by: Olivier De Cannière <olivier.decanniere@qt.io>
Diffstat (limited to 'src/qmlformat/qqmlformatoptions.cpp')
| -rw-r--r-- | src/qmlformat/qqmlformatoptions.cpp | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/qmlformat/qqmlformatoptions.cpp b/src/qmlformat/qqmlformatoptions.cpp index e76fffcb0d..d3b1686061 100644 --- a/src/qmlformat/qqmlformatoptions.cpp +++ b/src/qmlformat/qqmlformatoptions.cpp @@ -180,6 +180,13 @@ QQmlFormatOptions QQmlFormatOptions::buildCommandLineOptions(const QStringList & QStringLiteral("Sort imports alphabetically " "(Warning: this might change semantics if a given " "name identifies types in multiple modules!)."))); + QCommandLineOption semicolonRuleOption( + QStringList() << "semicolon-rule"_L1, + QStringLiteral("Specify the semicolon rule to use (always, essential).\n" + "always: always adds semicolon [default].\n" + "essential: adds only when ASI wouldn't be relied on."), + "rule"_L1, "always"_L1); + parser.addOption(semicolonRuleOption); parser.addPositionalArgument("filenames"_L1, "files to be processed by qmlformat"_L1); @@ -277,6 +284,19 @@ QQmlFormatOptions QQmlFormatOptions::buildCommandLineOptions(const QStringList & options.mark(Settings::NewlineType); options.setNewline(QQmlFormatOptions::parseEndings(parser.value("newline"_L1))); } + + 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); + return options; + } + } options.setFiles(files); options.setArguments(parser.positionalArguments()); |
