From 15a7380117468eaf9813bd9c854cc06bbefe1022 Mon Sep 17 00:00:00 2001 From: Petr Mikhalicin Date: Thu, 23 Jun 2022 20:22:19 +0300 Subject: [PATCH 1/3] Add qml support Resolve: https://github.com/beautify-web/js-beautify/issues/1754 --- js/src/cli.js | 4 +++- js/src/javascript/beautifier.js | 2 +- js/src/javascript/options.js | 1 + python/jsbeautifier/__init__.py | 4 ++++ python/jsbeautifier/javascript/beautifier.py | 1 + python/jsbeautifier/javascript/options.py | 1 + 6 files changed, 11 insertions(+), 2 deletions(-) diff --git a/js/src/cli.js b/js/src/cli.js index 070f423e8..e1900baf3 100755 --- a/js/src/cli.js +++ b/js/src/cli.js @@ -88,6 +88,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"], @@ -371,6 +372,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]'); @@ -689,4 +691,4 @@ function logToStdout(str, config) { if (typeof config.quiet === "undefined" || !config.quiet) { console.log(str); } -} \ No newline at end of file +} diff --git a/js/src/javascript/beautifier.js b/js/src/javascript/beautifier.js index 51cd10d8e..abb2e9d36 100644 --- a/js/src/javascript/beautifier.js +++ b/js/src/javascript/beautifier.js @@ -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'; diff --git a/js/src/javascript/options.js b/js/src/javascript/options.js index 1234f2391..ebf955aca 100644 --- a/js/src/javascript/options.js +++ b/js/src/javascript/options.js @@ -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); diff --git a/python/jsbeautifier/__init__.py b/python/jsbeautifier/__init__.py index 45e2248ab..bd733f10a 100644 --- a/python/jsbeautifier/__init__.py +++ b/python/jsbeautifier/__init__.py @@ -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"): diff --git a/python/jsbeautifier/javascript/beautifier.py b/python/jsbeautifier/javascript/beautifier.py index 80736ab41..efc3b7900 100644 --- a/python/jsbeautifier/javascript/beautifier.py +++ b/python/jsbeautifier/javascript/beautifier.py @@ -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: diff --git a/python/jsbeautifier/javascript/options.py b/python/jsbeautifier/javascript/options.py index 541a6d079..0655645bb 100644 --- a/python/jsbeautifier/javascript/options.py +++ b/python/jsbeautifier/javascript/options.py @@ -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 From 6c5d1f2c8b853e1ac7cbc2d4ce9af4b9698db4c9 Mon Sep 17 00:00:00 2001 From: Petr Mikhalicin Date: Mon, 27 Jun 2022 11:51:02 +0300 Subject: [PATCH 2/3] Add qml tests --- test/data/javascript/tests.js | 63 +++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/test/data/javascript/tests.js b/test/data/javascript/tests.js index 367c3e231..29b97606a 100644 --- a/test/data/javascript/tests.js +++ b/test/data/javascript/tests.js @@ -2578,6 +2578,69 @@ exports.test_data = { input_: 'xml=\n foox;', output: 'xml = < a b = "c" > < d / > < e >\n foo < /e>x ;' }] + }, { + 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: "", From 97364e8ce675a4ec72a36e22ee9a98eb8f96ac0e Mon Sep 17 00:00:00 2001 From: Petr Mikhalicin Date: Mon, 27 Jun 2022 12:03:17 +0300 Subject: [PATCH 3/3] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index f9eca3355..bdf3f4f04 100644 --- a/README.md +++ b/README.md @@ -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,