From 6d51f997df14f2b22c265c5b2fda679ece9edef3 Mon Sep 17 00:00:00 2001 From: Fawzi Mohamed Date: Wed, 3 Feb 2021 09:06:09 +0100 Subject: Uniformly support shebang The "qml" tool was the only way of loading QML files that would respect a shebang line. This is problematic as this way you cannot load such files programatically using QQmlComponent, limiting their re-use. Common tools like Qt Creator, but also qmllint, qmlformat, qmlcachegen, etc would not recognize files with shebangs. By moving she-bang support directly in the lexer all tools implicitly support it. Note that we could just as easily support '#' as extra comment character along with //, but here we narrowly add support for in the first line only, as node does (this means that javascript files using she-bang accepted by node, are now accepted also by qml). The only tool needing some adjustments is qmlformat, that has to emit the she-bang again as she-bang and as first line. Add tests for qmlformat, and sprinkle some she-bangs in the other tests just to be sure it doesn't affect anything. Change-Id: I1f6d881c7438bdb23163b5dbe829d59a35d11132 Reviewed-by: Fabian Kosmale Reviewed-by: Shawn Rutledge --- src/qml/parser/qqmljslexer.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src/qml') diff --git a/src/qml/parser/qqmljslexer.cpp b/src/qml/parser/qqmljslexer.cpp index 0c4b2ef32f..0e355c54e6 100644 --- a/src/qml/parser/qqmljslexer.cpp +++ b/src/qml/parser/qqmljslexer.cpp @@ -768,6 +768,20 @@ again: else return scanNumber(ch); + case '#': + if (_currentLineNumber == 1 && _currentColumnNumber == 2) { + // shebang support + while (_codePtr <= _endPtr && !isLineTerminator()) { + scanChar(); + } + if (_engine) { + _engine->addComment(tokenOffset(), _codePtr - _tokenStartPtr - 1, + tokenStartLine(), tokenStartColumn()); + } + goto again; + } + Q_FALLTHROUGH(); + default: { uint c = ch.unicode(); bool identifierWithEscapeChars = false; -- cgit v1.2.3