TotalTechGeek / json-logic-engine

Construct complex rules with JSON & process them.
MIT License
43 stars 9 forks source link

Direct `eval` gives warnings when being bundled with esbuild #16

Closed lionel-rowe closed 10 months ago

lionel-rowe commented 10 months ago

This library's use of direct eval gives warnings when bundled with esbuild:

▲ [WARNING] Using direct eval with a bundler is not recommended and may cause problems [direct-eval]

    vendor/json-logic-engine.js:672:21:
      672 │   return declareSync(eval(final)(state, values, methods, gen, notTraversed, Override, asyncIterators), !buildStat...
          ╵                      ~~~~

  You can read more about direct eval and bundling here: https://esbuild.github.io/link/direct-eval

Explanation:

Although the expression eval(x) looks like a normal function call, it actually takes on special behavior in JavaScript. Using eval in this way means that the evaluated code stored in x can reference any variable in any containing scope by name.

Example of the different behavior of direct vs indirect eval:

function direct() {
    const x = 123
    return eval('x')
}

direct() // 123

function indirect() {
    const x = 123
    return globalThis.eval('x')
}

indirect() // Uncaught ReferenceError: x is not defined
TotalTechGeek commented 10 months ago

Good call!

It's a little funny, because Bun used the globalThis.eval behavior, some of the the scope stuff had already been coded for. I didn't think to look around to change the function call.

I'm going to do some checking to make sure this works across different browsers / environments, but thank you for the MR.

TotalTechGeek commented 10 months ago

Released in v1.2.9, thank you for the contribution!