squirrellyjs / squirrelly

Semi-embedded JS template engine that supports helpers, filters, partials, and template inheritance. 4KB minzipped, written in TypeScript ⛺
https://squirrelly.js.org
MIT License
571 stars 81 forks source link

An error can occur if no data is passed to renderFile() as no check is done to see if data is defined #200

Closed futurelucas4502 closed 3 years ago

futurelucas4502 commented 3 years ago

Describe the bug A clear and concise description of what the bug is.

When using renderFile if the data parameter is not passed it can cause an error as there is no check to see if it is undefined.

Changing this section of code to the following ensures no error will occur:

    if (data != undefined){
        if (data.settings) {
            // Pull a few things from known locations
            if (data.settings.views) {
                Config.views = data.settings.views;
            }
            if (data.settings['view cache']) {
                Config.cache = true;
            }
            // Undocumented after Express 2, but still usable, esp. for
            // items that are unsafe to be passed along with data, like `root`
            var viewOpts = data.settings['view options'];
            if (viewOpts) {
                copyProps(Config, viewOpts);
            }
        }
    } 

Environment: Node

nebrelbug commented 3 years ago

Good catch! I never thought about that possibility.

In order to reduce code size, we could even change

function renderFile(filename, data, cb) {
    var Config = getConfig(data || {});

to

function renderFile(filename, data, cb) {
    data = data || {}
    var Config = getConfig(data);

Do you want to submit a PR with the change?

futurelucas4502 commented 3 years ago

Good idea I didn't even think about that and sure I'll do a pull request in just a sec

futurelucas4502 commented 3 years ago

What version of yarn is being used classic or yarn 2?

nebrelbug commented 3 years ago

Yarn classic