The `with` package provides a utility to transform JavaScript code, effectively simulating the behavior of the deprecated `with` statement at compile time. This allows developers to safely use `with`-like scoping in strict mode environments and ensures compatibility with minification tools, which typically struggle with native `with`. Currently at version 7.0.2, the package maintains an active release cadence with minor bug fixes and performance improvements following major version updates. Key differentiators include its strict mode compatibility, predictable variable assignment within the generated scope (unlike native `with` which can leak assignments), and the explicit declaration of all non-excluded variables, enabling `if (foo === undefined)` checks. It also offers detailed parsing errors for easier debugging.
npm install withVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic usage of `addWith` to generate strict-mode-safe `with` statements, including the `exclude` option.
Upgrade Node.js to version 10 or higher, or pin the `with` package to a version less than 7.0.0 (e.g., `"with": "^6.0.0"`).
Be aware that `with` generated by this package creates local variables for assignments. If you intend to modify `obj` properties, you must explicitly reference `obj.foo = 'bar'` in your source code passed to `addWith`.
Understand the scoping rules: all non-excluded identifiers will be treated as locally declared within the synthetic `with` block. Use the `exclude` array for true globals or known external variables to optimize performance.
When catching errors from `addWith`, inspect `error.babylonError` and `error.component` for detailed information on syntax issues and their location within your input code.
Use the `with` package's `addWith` utility to generate strict-mode-safe code, as native `with` is deprecated and problematic. For example: `addWith('data', 'console.log(myVar)');` instead of `with (data) { console.log(myVar); }`Review the `src` and `obj` strings for syntax errors. The error object returned by `addWith` will contain `error.babylonError` with detailed location information (line, column) and `error.component` ('src' or 'obj') to pinpoint the issue.No dependency data recorded yet.