diff options
author | Eike Ziller <eike.ziller@qt.io> | 2019-05-31 15:29:05 +0200 |
---|---|---|
committer | Eike Ziller <eike.ziller@qt.io> | 2019-06-03 06:46:52 +0000 |
commit | 23c222f61b260b56c10775197d6100f130d558b4 (patch) | |
tree | 513c9d0f65d9f7f9ec4884c64db7f92f858beaa2 | |
parent | d73c27125952bcce920a2abd55c7c3758e850ac0 (diff) |
Fix C++ Library wizard when compiled with Qt < 5.12
Only Qt 5.12 got true ECMA Script capabilities, so arrow function
expressions are not supported before that.
To be able to use "regular" inline functions, we allow usage of } by escaping with backslash.
For variables that do not start with "JS:" we already supported backslash for escaping.
Fixes: QTCREATORBUG-22336
Change-Id: I9fc638e64d2757a21fffc16355635e2fcff87a36
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
-rw-r--r-- | share/qtcreator/templates/wizards/projects/cpplibrary/lib.cpp | 2 | ||||
-rw-r--r-- | share/qtcreator/templates/wizards/projects/cpplibrary/lib.h | 2 | ||||
-rw-r--r-- | src/libs/utils/stringutils.cpp | 2 | ||||
-rw-r--r-- | src/plugins/coreplugin/jsexpander.cpp | 4 | ||||
-rw-r--r-- | tests/auto/utils/stringutils/tst_stringutils.cpp | 12 |
5 files changed, 17 insertions, 5 deletions
diff --git a/share/qtcreator/templates/wizards/projects/cpplibrary/lib.cpp b/share/qtcreator/templates/wizards/projects/cpplibrary/lib.cpp index dcdacae6d3..6eb6c86faa 100644 --- a/share/qtcreator/templates/wizards/projects/cpplibrary/lib.cpp +++ b/share/qtcreator/templates/wizards/projects/cpplibrary/lib.cpp @@ -11,7 +11,7 @@ : %{BaseClassName}(parent) { } -%{JS: '%{PluginMethods}'.split('|').map(s => '\n' + s.replace(/([a-zA-Z0-9]+\()/, '%{CN}::$1') + '\n\u007B\n static_assert(false, "You need to implement this function");\n\u007D').join('\n')}\ +%{JS: '%{PluginMethods}'.split('|').map(function(s) { return '\\n' + s.replace(/([a-zA-Z0-9]+\\()/, '%{CN}::$1') + '\\n\{\\n static_assert(false, "You need to implement this function");\\n\}'; \}).join('\\n')}\ @endif %{JS: Cpp.closeNamespaces('%{Class}')}\ diff --git a/share/qtcreator/templates/wizards/projects/cpplibrary/lib.h b/share/qtcreator/templates/wizards/projects/cpplibrary/lib.h index 4a5d1247a4..ae2c90a313 100644 --- a/share/qtcreator/templates/wizards/projects/cpplibrary/lib.h +++ b/share/qtcreator/templates/wizards/projects/cpplibrary/lib.h @@ -36,7 +36,7 @@ public: explicit %{CN}(QObject *parent = nullptr); private: -%{JS: '%{PluginMethods}'.split('|').map(s => ' ' + s + ' override;').join('\n')} +%{JS: '%{PluginMethods}'.split('|').map(function(s) { return ' ' + s + ' override;'; \}).join('\\n')} }; @endif %{JS: Cpp.closeNamespaces('%{Class}')}\ diff --git a/src/libs/utils/stringutils.cpp b/src/libs/utils/stringutils.cpp index b39af367d5..161cf44e74 100644 --- a/src/libs/utils/stringutils.cpp +++ b/src/libs/utils/stringutils.cpp @@ -143,7 +143,7 @@ bool AbstractMacroExpander::expandNestedMacros(const QString &str, int *pos, QSt varName.reserve(strLen - i); for (; i < strLen; prev = c) { c = str.at(i++); - if (c == '\\' && i < strLen && validateVarName(varName)) { + if (c == '\\' && i < strLen) { c = str.at(i++); // For the replacement, do not skip the escape sequence when followed by a digit. // This is needed for enabling convenient capture group replacement, diff --git a/src/plugins/coreplugin/jsexpander.cpp b/src/plugins/coreplugin/jsexpander.cpp index 5aa9279c7c..4ee72752e5 100644 --- a/src/plugins/coreplugin/jsexpander.cpp +++ b/src/plugins/coreplugin/jsexpander.cpp @@ -111,7 +111,9 @@ void JsExpander::registerForExpander(Utils::MacroExpander *macroExpander) "JS", QCoreApplication::translate("Core::JsExpander", "Evaluate simple JavaScript statements.<br>" - "The statements may not contain '{' nor '}' characters."), + "Literal '}' characters must be escaped as \"\\}\", " + "'\\' characters must be escaped as \"\\\\\", " + "and \"%{\" must be escaped as \"%\\{\"."), [this](QString in) -> QString { QString errorMessage; QString result = evaluate(in, &errorMessage); diff --git a/tests/auto/utils/stringutils/tst_stringutils.cpp b/tests/auto/utils/stringutils/tst_stringutils.cpp index 8e51c89300..98e04185ab 100644 --- a/tests/auto/utils/stringutils/tst_stringutils.cpp +++ b/tests/auto/utils/stringutils/tst_stringutils.cpp @@ -68,6 +68,14 @@ public: *ret = "bar"; return true; } + if (name == "JS:with } inside") { + *ret = "yay"; + return true; + } + if (name == "JS:literal%{") { + *ret = "hurray"; + return true; + } return false; } }; @@ -158,7 +166,9 @@ void tst_StringUtils::testMacroExpander_data() {"%{hihi//./c}", "ccc"}, {"%{hihi/(.)(.)r/\\2\\1c}", "abc"}, // no escape for capture groups {"%{hihi/b/c/d}", "c/dar"}, - {"%{hihi/a/e{\\}e}", "be{}er"}, // escape closing brace + {"%{hihi/a/e{\\}e}", "be{}er"}, // escape closing brace + {"%{JS:with \\} inside}", "yay"}, // escape closing brace also in JS: + {"%{JS:literal%\\{}", "hurray"}, {"%{slash/o\\/b/ol's c}", "fool's car"}, {"%{sl\\/sh/(.)(a)(.)/\\2\\1\\3as}", "salsash"}, // escape in variable name {"%{JS:foo/b/c}", "%{JS:foo/b/c}"}, // No replacement for JS (all considered varName) |