const resolveQuestionRef = function()

in packages/lu/src/parser/lu/luMerger.js [239:298]


const resolveQuestionRef = function(srcId, ref, refTree) {
    let utterances = [];
    let patterns = [];
    let srcFile = refTree[srcId][ref.type].srcFile;
    let newId = path.resolve(path.dirname(srcFile ? srcFile : ''), ref.parsedLink.fileName);
    let tgtId = (refTree[ref.parsedLink.fileName] && refTree[ref.parsedLink.fileName].qna !== undefined) ? ref.parsedLink.fileName : undefined;
    tgtId = (tgtId === undefined && refTree[newId] !== undefined && refTree[newId].qna !== undefined) ? newId : tgtId;
    let tgtObj = refTree[ref.parsedLink.fileName] || refTree[newId] || undefined;
    if (!tgtObj && !ref.parsedLink.fileName.endsWith('*')) {
        let error = BuildDiagnostic({
            message: `Unable to parse ${ref.text} in file: ${srcFile}. Cannot find reference.`
        });
        throw (new exception(retCode.errorCode.INVALID_INPUT, error.toString()));
    }
    // Resolve additional references if any in tgt obj
    if (tgtObj && ((tgtObj.luis && tgtObj.luis.refs.length !== 0) || (tgtObj.qna && tgtObj.qna.refs.length !== 0)))
        resolveRefs(refTree, tgtId);
    let parseLCasePath = ref.parsedLink.path.toLowerCase();
    let qnaObj = tgtObj && tgtObj.qna && tgtObj.qna.obj ? tgtObj.qna.obj : undefined;
    let qnaAlt = tgtObj && tgtObj.qna && tgtObj.qna.alt ? tgtObj.qna.alt : undefined;
    let parsedQnABlobs = qnaObj !== undefined ? [qnaObj] : [];
    let parsedQnAAlterations = qnaAlt !== undefined ? [qnaAlt] : [];
    if (ref.parsedLink.fileName.endsWith('*')) {
        // this notation is only valid with file path. So try as file path.
        let tPath = ref.parsedLink.fileName.replace(/\*/g, '');
        for (let prop in refTree) {
            if (prop.startsWith(path.resolve(path.dirname(srcFile), tPath))) {
                parsedQnABlobs.push(refTree[prop].qna.obj);
                parsedQnAAlterations.push(refTree[prop].qna.alt)
            }
        }
    }
    if (parseLCasePath.startsWith('*answers*')) {
        parsedQnABlobs.forEach(blob => blob.qnaList.forEach(item => utterances.push(item.answer)));
    } else if (ref.parsedLink.path.length > 1 && parseLCasePath.startsWith('?') && parseLCasePath.endsWith('?')) {
        let itemsFound = undefined;
        let testQuestion = ref.parsedLink.path.replace(/\?/g, '').replace(/-/g, ' ').trim();
        // find the specific question
        parsedQnABlobs.forEach(blob => {
            if (itemsFound) return;
            itemsFound = blob.qnaList.find(item => item.questions.includes(testQuestion));
        })
        if (itemsFound) {
            itemsFound.questions.forEach(question => utterances.push(question));
        }
    } else if (parseLCasePath.startsWith('*alterations*')) {
        parsedQnAAlterations.forEach(blob => blob.wordAlterations.forEach(item => item.alterations.forEach(alter => utterances.push(alter))));
    } else if (parseLCasePath.startsWith('$') && parseLCasePath.endsWith('?')) {
        // specific alteration to find 
        let alterToFind = ref.parsedLink.path.replace(/[$\?]/g, '').trim();
        parsedQnAAlterations.forEach(blob => blob.wordAlterations.forEach(item => {
            if (item.alterations.includes(alterToFind)) {
                item.alterations.forEach(alter => utterances.push(alter));
            }
        }));
    } else {
        parsedQnABlobs.forEach(blob => blob.qnaList.forEach(item => item.questions.forEach(question => utterances.push(question))));
    }
    return {utterances, patterns}
}