Skip to content

Add qml support #2067

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -184,6 +184,7 @@ Beautifier Options:
-x, --unescape-strings Decode printable characters encoded in xNN notation
-w, --wrap-line-length Wrap lines that exceed N characters [0]
-X, --e4x Pass E4X xml literals through untouched
--qml Add support of qml format
--good-stuff Warm the cockles of Crockford's heart
-C, --comma-first Put commas at the beginning of new line instead of end
-O, --operator-position Set operator position (before-newline|after-newline|preserve-newline) [before-newline]
@@ -217,6 +218,7 @@ Which correspond to the underscored option keys for both library interfaces
"unescape_strings": false,
"wrap_line_length": 0,
"e4x": false,
"qml": false,
"comma_first": false,
"operator_position": "before-newline",
"indent_empty_lines": false,
4 changes: 3 additions & 1 deletion js/src/cli.js
Original file line number Diff line number Diff line change
@@ -101,6 +101,7 @@ var path = require('path'),
"wrap_attributes": ["auto", "force", "force-aligned", "force-expand-multiline", "aligned-multiple", "preserve", "preserve-aligned"],
"wrap_attributes_indent_size": Number,
"e4x": Boolean,
"qml": Boolean,
"end_with_newline": Boolean,
"comma_first": Boolean,
"operator_position": ["before-newline", "after-newline", "preserve-newline"],
@@ -384,6 +385,7 @@ function usage(err) {
msg.push(' -x, --unescape-strings Decode printable characters encoded in xNN notation');
msg.push(' -w, --wrap-line-length Wrap lines that exceed N characters [0]');
msg.push(' -X, --e4x Pass E4X xml literals through untouched');
msg.push(' --qml Add Qml support');
msg.push(' --good-stuff Warm the cockles of Crockford\'s heart');
msg.push(' -C, --comma-first Put commas at the beginning of new line instead of end');
msg.push(' -O, --operator-position Set operator position (before-newline|after-newline|preserve-newline) [before-newline]');
@@ -702,4 +704,4 @@ function logToStdout(str, config) {
if (typeof config.quiet === "undefined" || !config.quiet) {
console.log(str);
}
}
}
2 changes: 1 addition & 1 deletion js/src/javascript/beautifier.js
Original file line number Diff line number Diff line change
@@ -974,7 +974,7 @@ Beautifier.prototype.handle_word = function(current_token) {
}

if (reserved_array(current_token, line_starters) && this._flags.last_token.text !== ')') {
if (this._flags.inline_frame || this._flags.last_token.text === 'else' || this._flags.last_token.text === 'export') {
if (this._flags.inline_frame || this._flags.last_token.text === 'else' || this._flags.last_token.text === 'export' || (this._options.qml && this._flags.last_token.text === 'property')) {
prefix = 'SPACE';
} else {
prefix = 'NEWLINE';
1 change: 1 addition & 0 deletions js/src/javascript/options.js
Original file line number Diff line number Diff line change
@@ -74,6 +74,7 @@ function Options(options) {
this.space_before_conditional = this._get_boolean('space_before_conditional', true);
this.unescape_strings = this._get_boolean('unescape_strings');
this.e4x = this._get_boolean('e4x');
this.qml = this._get_boolean('qml');
this.comma_first = this._get_boolean('comma_first');
this.operator_position = this._get_selection('operator_position', validPositionValues);

4 changes: 4 additions & 0 deletions python/jsbeautifier/__init__.py
Original file line number Diff line number Diff line change
@@ -124,6 +124,7 @@ def usage(stream=sys.stdout):
-f, --keep-function-indentation Do not re-indent function bodies defined in var lines.
-x, --unescape-strings Decode printable chars encoded in \\xNN notation.
-X, --e4x Pass E4X xml literals through untouched
--qml Add QML support
-C, --comma-first Put commas at the beginning of new line instead of end.
-m,
--max-preserve-newlines=NUMBER Number of line-breaks to be preserved in one chunk (default 10)
@@ -186,6 +187,7 @@ def main():
"operator-position=",
"outfile=",
"quiet",
"qml",
"replace",
"space-after-anon-function",
"space-after-named-function",
@@ -254,6 +256,8 @@ def main():
js_options.unescape_strings = True
elif opt in ("--e4x", "-X"):
js_options.e4x = True
elif opt in ("--qml",):
js_options.qml = True
elif opt in ("--end-with-newline", "-n"):
js_options.end_with_newline = True
elif opt in ("--comma-first", "-C"):
1 change: 1 addition & 0 deletions python/jsbeautifier/javascript/beautifier.py
Original file line number Diff line number Diff line change
@@ -1093,6 +1093,7 @@ def handle_word(self, current_token):
self._flags.inline_frame
or self._flags.last_token.text == "else "
or self._flags.last_token.text == "export"
or (self._options.qml and self._flags.last_token.text == "property")
):
prefix = "SPACE"
else:
1 change: 1 addition & 0 deletions python/jsbeautifier/javascript/options.py
Original file line number Diff line number Diff line change
@@ -86,6 +86,7 @@ def __init__(self, options=None):
)
self.unescape_strings = self._get_boolean("unescape_strings")
self.e4x = self._get_boolean("e4x")
self.qml = self._get_boolean("qml")
self.comma_first = self._get_boolean("comma_first")
self.operator_position = self._get_selection(
"operator_position", OPERATOR_POSITION
63 changes: 63 additions & 0 deletions test/data/javascript/tests.js
Original file line number Diff line number Diff line change
@@ -2578,6 +2578,69 @@ exports.test_data = {
input_: 'xml=<a b="c"><d/><e>\n foo</e>x</a>;',
output: 'xml = < a b = "c" > < d / > < e >\n foo < /e>x</a > ;'
}]
}, {
name: "qml true",
description: "",
options: [
{ name: 'qml', value: true }
],
tests: [
{ unchanged: 'property var x = {};' },
{ unchanged: 'readonly property var x = {};' },
{
unchanged: [
'// MessageLabel.qml',
'import QtQuick',
'',
'Rectangle {',
' height: 50',
' property string message: "debug message"',
' property var msgType: ["debug", "warning", "critical"]',
' color: "black"',
'',
' Column {',
' anchors.fill: parent',
' padding: 5.0',
' spacing: 2',
' Text {',
' text: msgType.toString().toUpperCase() + ":"',
' font.bold: msgType == "critical"',
' font.family: "Terminal Regular"',
' color: msgType === "warning" || msgType === "critical" ? "red" : "yellow"',
' ColorAnimation on color {',
' running: msgType == "critical"',
' from: "red"',
' to: "black"',
' duration: 1000',
' loops: msgType == "critical" ? Animation.Infinite : 1',
' }',
' }',
' Text {',
' text: message',
' color: msgType === "warning" || msgType === "critical" ? "red" : "yellow"',
' font.family: "Terminal Regular"',
' }',
' }',
'}'
]
}
]
}, {
name: "qml false",
description: "",
options: [
{ name: 'qml', value: false }
],
tests: [
{
input: 'property var x = {};',
output: 'property\nvar x = {};'
},
{
input: 'readonly property var x = {};',
output: 'readonly property\nvar x = {};'
}
]
}, {
name: "Multiple braces",
description: "",