<commit_before>/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
**
** This file is part of the Qt Build Suite.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms and
** conditions see http://www.qt.io/terms-conditions. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "astimportshandler.h"

#include "asttools.h"
#include "builtindeclarations.h"
#include "filecontext.h"
#include "itemreadervisitorstate.h"
#include "jsextensions/jsextensions.h"

#include <logging/logger.h>
#include <logging/translator.h>
#include <parser/qmljsast_p.h>
#include <tools/error.h>
#include <tools/fileinfo.h>
#include <tools/qttools.h>
#include <tools/version.h>

#include <QDirIterator>

namespace qbs {
namespace Internal {

ASTImportsHandler::ASTImportsHandler(ItemReaderVisitorState &visitorState, Logger &logger,
                                     const FileContextPtr &file)
    : m_visitorState(visitorState)
    , m_logger(logger)
    , m_file(file)
    , m_directory(FileInfo::path(m_file->filePath()))
{
}

void ASTImportsHandler::handleImports(const QbsQmlJS::AST::UiImportList *uiImportList)
{
    foreach (const QString &searchPath, m_file->searchPaths())
        collectPrototypes(searchPath + QLatin1String("/imports"), QString());

    // files in the same directory are available as prototypes
    collectPrototypes(m_directory, QString());

    for (const auto *it = uiImportList; it; it = it->next)
        handleImport(it->import);

    for (auto it = m_jsImports.constBegin(); it != m_jsImports.constEnd(); ++it)
        m_file->addJsImport(it.value());
}

void ASTImportsHandler::handleImport(const QbsQmlJS::AST::UiImport *import)
{
    QStringList importUri;
    bool isBase = false;
    if (import->importUri) {
        importUri = toStringList(import->importUri);
        isBase = (importUri.size() == 1 && importUri.first() == QLatin1String("qbs"))
                || (importUri.size() == 2 && importUri.first() == QLatin1String("qbs")
                    && importUri.last() == QLatin1String("base"));
        if (isBase) {
            checkImportVersion(import->versionToken);
        } else if (import->versionToken.length) {
            m_logger.printWarning(ErrorInfo(Tr::tr("Superfluous version specification."),
                    toCodeLocation(m_file->filePath(), import->versionToken)));
        }
    }

    QString as;
    if (isBase) {
        if (Q_UNLIKELY(!import->importId.isNull())) {
            throw ErrorInfo(Tr::tr("Import of qbs.base must have no 'as <Name>'"),
                        toCodeLocation(m_file->filePath(), import->importIdToken));
        }
    } else {
        if (importUri.count() == 2 && importUri.first() == QLatin1String("qbs")) {
            const QString extensionName = importUri.last();
            if (JsExtensions::hasExtension(extensionName)) {
                if (Q_UNLIKELY(!import->importId.isNull())) {
                    throw ErrorInfo(Tr::tr("Import of built-in extension '%1' "
                                           "must not have 'as' specifier.").arg(extensionName));
                }
                if (Q_UNLIKELY(m_file->jsExtensions().contains(extensionName))) {
                    m_logger.printWarning(Tr::tr("Built-in extension '%1' already "
                                                 "imported.").arg(extensionName));
                } else {
                    m_file->addJsExtension(extensionName);
                }
                return;
            }
        }

        if (import->importId.isNull()) {
            if (!import->fileName.isNull()) {
                throw ErrorInfo(Tr::tr("File imports require 'as <Name>'"),
                                toCodeLocation(m_file->filePath(), import->importToken));
            }
            if (importUri.isEmpty()) {
                throw ErrorInfo(Tr::tr("Invalid import URI."),
                                toCodeLocation(m_file->filePath(), import->importToken));
            }
            as = importUri.last();
        } else {
            as = import->importId.toString();
        }

        if (Q_UNLIKELY(m_importAsNames.contains(as))) {
            throw ErrorInfo(Tr::tr("Cannot import into the same name more than once."),
                        toCodeLocation(m_file->filePath(), import->importIdToken));
        }
        if (Q_UNLIKELY(JsExtensions::hasExtension(as)))
            throw ErrorInfo(Tr::tr("Cannot reuse the name of built-in extension '%1'.").arg(as));
        m_importAsNames.insert(as);
    }

    if (!import->fileName.isNull()) {
        QString filePath = FileInfo::resolvePath(m_directory, import->fileName.toString());

        QFileInfo fi(filePath);
        if (Q_UNLIKELY(!fi.exists()))
            throw ErrorInfo(Tr::tr("Cannot find imported file %0.").arg(filePath),
                        CodeLocation(m_file->filePath(), import->fileNameToken.startLine,
                                     import->fileNameToken.startColumn));
        filePath = fi.canonicalFilePath();<commit_msg>Use native separators in error message.<commit_after>/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
**
** This file is part of the Qt Build Suite.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms and
** conditions see http://www.qt.io/terms-conditions. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "astimportshandler.h"

#include "asttools.h"
#include "builtindeclarations.h"
#include "filecontext.h"
#include "itemreadervisitorstate.h"
#include "jsextensions/jsextensions.h"

#include <logging/logger.h>
#include <logging/translator.h>
#include <parser/qmljsast_p.h>
#include <tools/error.h>
#include <tools/fileinfo.h>
#include <tools/qttools.h>
#include <tools/version.h>

#include <QDirIterator>

namespace qbs {
namespace Internal {

ASTImportsHandler::ASTImportsHandler(ItemReaderVisitorState &visitorState, Logger &logger,
                                     const FileContextPtr &file)
    : m_visitorState(visitorState)
    , m_logger(logger)
    , m_file(file)
    , m_directory(FileInfo::path(m_file->filePath()))
{
}

void ASTImportsHandler::handleImports(const QbsQmlJS::AST::UiImportList *uiImportList)
{
    foreach (const QString &searchPath, m_file->searchPaths())
        collectPrototypes(searchPath + QLatin1String("/imports"), QString());

    // files in the same directory are available as prototypes
    collectPrototypes(m_directory, QString());

    for (const auto *it = uiImportList; it; it = it->next)
        handleImport(it->import);

    for (auto it = m_jsImports.constBegin(); it != m_jsImports.constEnd(); ++it)
        m_file->addJsImport(it.value());
}

void ASTImportsHandler::handleImport(const QbsQmlJS::AST::UiImport *import)
{
    QStringList importUri;
    bool isBase = false;
    if (import->importUri) {
        importUri = toStringList(import->importUri);
        isBase = (importUri.size() == 1 && importUri.first() == QLatin1String("qbs"))
                || (importUri.size() == 2 && importUri.first() == QLatin1String("qbs")
                    && importUri.last() == QLatin1String("base"));
        if (isBase) {
            checkImportVersion(import->versionToken);
        } else if (import->versionToken.length) {
            m_logger.printWarning(ErrorInfo(Tr::tr("Superfluous version specification."),
                    toCodeLocation(m_file->filePath(), import->versionToken)));
        }
    }

    QString as;
    if (isBase) {
        if (Q_UNLIKELY(!import->importId.isNull())) {
            throw ErrorInfo(Tr::tr("Import of qbs.base must have no 'as <Name>'"),
                        toCodeLocation(m_file->filePath(), import->importIdToken));
        }
    } else {
        if (importUri.count() == 2 && importUri.first() == QLatin1String("qbs")) {
            const QString extensionName = importUri.last();
            if (JsExtensions::hasExtension(extensionName)) {
                if (Q_UNLIKELY(!import->importId.isNull())) {
                    throw ErrorInfo(Tr::tr("Import of built-in extension '%1' "
                                           "must not have 'as' specifier.").arg(extensionName));
                }
                if (Q_UNLIKELY(m_file->jsExtensions().contains(extensionName))) {
                    m_logger.printWarning(Tr::tr("Built-in extension '%1' already "
                                                 "imported.").arg(extensionName));
                } else {
                    m_file->addJsExtension(extensionName);
                }
                return;
            }
        }

        if (import->importId.isNull()) {
            if (!import->fileName.isNull()) {
                throw ErrorInfo(Tr::tr("File imports require 'as <Name>'"),
                                toCodeLocation(m_file->filePath(), import->importToken));
            }
            if (importUri.isEmpty()) {
                throw ErrorInfo(Tr::tr("Invalid import URI."),
                                toCodeLocation(m_file->filePath(), import->importToken));
            }
            as = importUri.last();
        } else {
            as = import->importId.toString();
        }

        if (Q_UNLIKELY(m_importAsNames.contains(as))) {
            throw ErrorInfo(Tr::tr("Cannot import into the same name more than once."),
                        toCodeLocation(m_file->filePath(), import->importIdToken));
        }
        if (Q_UNLIKELY(JsExtensions::hasExtension(as)))
            throw ErrorInfo(Tr::tr("Cannot reuse the name of built-in extension '%1'.").arg(as));
        m_importAsNames.insert(as);
    }

    if (!import->fileName.isNull()) {
        QString filePath = FileInfo::resolvePath(m_directory, import->fileName.toString());

        QFileInfo fi(filePath);
        if (Q_UNLIKELY(!fi.exists()))
            throw ErrorInfo(Tr::tr("Cannot find imported file %0.")
                            .arg(QDir::toNativeSeparators(filePath)),
                            CodeLocation(m_file->filePath(), import->fileNameToken.startLine,
                                         import->fileNameToken.startColumn));
        filePath = fi.canonicalFilePath();
{'fraction_non_alphanumeric': 0.1460264638301244, 'fraction_numerical': 0.004120117264876, 'mean_word_length': 2.7277023036030714, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 85, 'https://': 2, 'lorem ipsum': 0, 'www.': 10, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1553536', 'n_tokens_mistral': 3504, 'n_tokens_neox': 3367, 'n_words': 849}