def METHOD_NAME(inputProgram, fileName):
    """Transform the input program by adding just before each function
    a declaration for its undeclared variables
    """
    # parse Javascript code into abstract syntax tree:
    # NB: esprima: https://media.readthedocs.org/pdf/esprima/4.0/esprima.pdf
    fileName = Path(str(fileName))
    try:
        ast = esprima.parseScript(inputProgram, {'range': True, 'tolerant': True})
    except esprima.error_handler.Error as err:
        if fileName:
            logging.error(f"Error parsing JS in {fileName.name}:\n{err}")
        else:
            logging.error(f"Error parsing JS: {err}")
        logging.flush()
        return inputProgram  # So JS can be written to file
    # find undeclared vars in functions and declare them before the function
    outputProgram = inputProgram
    offset = 0
    allUndeclaredVariables = []
    for expression in ast.body:
        if expression.type == 'FunctionDeclaration':
            # find all undeclared variables:
            undeclaredVariables = findUndeclaredVariables(expression.body.body,
                                                          allUndeclaredVariables)
            # add declarations (var) just before the function:
            funSpacing = ['', '\n'][len(undeclaredVariables) > 0]  # for consistent function spacing
            declaration = funSpacing + '\n'.join(['var ' + variable + ';' for variable in
                                     undeclaredVariables]) + '\n'
            startIndex = expression.range[0] + offset
            outputProgram = outputProgram[
                            :startIndex] + declaration + outputProgram[
                                                         startIndex:]
            offset += len(declaration)
    return outputProgram