const handleClosedList = function()

in parsers/LU/JS/packages/lu/src/parser/lufile/parseFileContents.js [1576:1642]


const handleClosedList = function (parsedContent, entityName, listLines, entityRoles, range) {
    // check if this list entity is already labelled in an utterance and or added as a simple entity. if so, throw an error.
    try {
        let rolesImport = VerifyAndUpdateSimpleEntityCollection(parsedContent, entityName, 'List');
        rolesImport.forEach(role => {
            if (!entityRoles.includes(role)) {
                entityRoles.push(role)
            }
        });
    } catch (err) {
        throw (err);
    }
    // Find closed list by this name if it exists
    let closedListExists = parsedContent.LUISJsonStructure.closedLists.find(item => item.name == entityName);
    let addCL = false;
    if (closedListExists === undefined) {
        closedListExists = new helperClass.closedLists(entityName);
        addCL = true;
    }
    let addNV = false;    
    let nvExists;
    listLines.forEach(line => {
        line = line.substr(1).trim();
        if (line.toLowerCase().endsWith(':')) {
            // close if we are in the middle of a sublist.
            if (addNV) {
                closedListExists.subLists.push(nvExists);
                addNV = false;
                nvExists = undefined;
            }
            // find the matching sublist and if none exists, create one. 
            let normalizedValue = line.replace(/:$/g, '').trim();
            nvExists = closedListExists.subLists.find(item => item.canonicalForm == normalizedValue);
            if (nvExists === undefined) {
                nvExists = new helperClass.subList(normalizedValue);
                addNV = true;
            }
        } else {
            line.split(/[,;]/g).forEach(item => {
                item = item.trim();
                if (!nvExists || !nvExists.list) {
                    let errorMsg = `Closed list ${entityName} has synonyms list "${line}" without a normalized value.`;
                    let error = BuildDiagnostic({
                        message: errorMsg,
                        range: range
                    })

                    throw (new exception(retCode.errorCode.SYNONYMS_NOT_A_LIST, error.toString(), [error]));
                }
                nvExists.list.push(item);
            })
        }
    });
    // de-dupe the list
    if (nvExists && nvExists.list)nvExists.list = [...new Set(nvExists.list)];
    if (addNV) {
        closedListExists.subLists.push(nvExists);
    }
    // merge roles
    entityRoles.forEach(item => {
        if(!closedListExists.roles.includes(item)) closedListExists.roles.push(item);
    });

    if (addCL) {
        parsedContent.LUISJsonStructure.closedLists.push(closedListExists);
    }
}