# with

> Compile time `with` for strict mode JavaScript

Latest version **7.0.2** (published 2020-05-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install with
pnpm add with
yarn add with
bun add with
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 7.0.2 |
| Published | 2020-05-29 |
| First published | 2013-05-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 10.0.0 |
| Dependencies | 4 |
| Unpacked size | 39.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 28 |
| Author | ForbesLindesay |
| Maintainers | forbeslindesay, with-bot |

## Links

- npm: https://www.npmjs.com/package/with
- Repository: https://github.com/pugjs/with
- Homepage: https://github.com/pugjs/with#readme
- Issues: https://github.com/pugjs/with/issues
- npm.io page: https://npm.io/package/with

## Dependencies (4)

- [babel-walk](https://npm.io/package/babel-walk.md) 3.0.0-canary-5
- [@babel/types](https://npm.io/package/@babel/types.md) ^7.9.6
- [assert-never](https://npm.io/package/assert-never.md) ^1.2.1
- [@babel/parser](https://npm.io/package/@babel/parser.md) ^7.9.6

## Recent versions

- 7.0.2 (latest) — 2020-05-29
- 7.0.2-canary-3 (canary) — 2020-05-29
- 7.0.1 — 2020-05-27
- 7.0.1-canary-2 — 2020-05-27
- 7.0.0 — 2020-05-25
- 7.0.0-canary-1 — 2020-05-25
- 6.0.0 — 2018-06-01
- 5.1.1 — 2016-06-04
- 5.1.0 — 2016-06-03
- 5.0.2 — 2016-06-03
- 5.0.1 — 2016-06-03
- 5.0.0 — 2015-04-08
- 4.0.3 — 2015-04-06
- 4.0.2 — 2015-03-29
- 4.0.1 — 2015-02-07
- … 11 more at https://npm.io/package/with/versions

## README

# with

Compile time `with` for strict mode JavaScript

[![Build Status](https://img.shields.io/github/workflow/status/pugjs/with/Publish%20Canary/master?style=for-the-badge)](https://github.com/pugjs/with/actions?query=workflow%3A%22Publish+Canary%22)
[![Rolling Versions](https://img.shields.io/badge/Rolling%20Versions-Enabled-brightgreen?style=for-the-badge)](https://rollingversions.com/pugjs/with)
[![NPM version](https://img.shields.io/npm/v/with?style=for-the-badge)](https://www.npmjs.com/package/with)

## Installation

    $ npm install with

## Usage

```js
var addWith = require('with');

addWith('obj', 'console.log(a)');
// => ';(function (console, a) {
//       console.log(a)
//     }("console" in obj ? obj.console :
//                          typeof console!=="undefined" ? console : undefined,
//       "a" in obj ? obj.a :
//                    typeof a !== "undefined" ? a : undefined));'

addWith('obj', 'console.log(a)', ['console']);
// => ';(function (console, a) {
//       console.log(a)
//     }("a" in obj ? obj.a :
//                    typeof a !== "undefined" ? a : undefined));'
```

## API

### addWith(obj, src[, exclude])

The idea is that this is roughly equivallent to:

```js
with (obj) {
  src;
}
```

There are a few differences though. For starters, assignments to variables will always remain contained within the with block.

e.g.

```js
var foo = 'foo';
with ({}) {
  foo = 'bar';
}
assert(foo === 'bar'); // => This fails for compile time with but passes for native with

var obj = {foo: 'foo'};
with ({}) {
  foo = 'bar';
}
assert(obj.foo === 'bar'); // => This fails for compile time with but passes for native with
```

It also makes everything be declared, so you can always do:

```js
if (foo === undefined)
```

instead of

```js
if (typeof foo === 'undefined')
```

This is not the case if foo is in `exclude`. If a variable is excluded, we ignore it entirely. This is useful if you know a variable will be global as it can lead to efficiency improvements.

It is also safe to use in strict mode (unlike `with`) and it minifies properly (`with` disables virtually all minification).

#### Parsing Errors

with internally uses babylon to parse code passed to `addWith`. If babylon throws an error, probably due to a syntax error, `addWith` returns an error wrapping the babylon error, so you can
retrieve location information. `error.component` is `"src"` if the error is in the body or `"obj"` if it's in the object part of the with expression. `error.babylonError` is
the error thrown from babylon.

## License

MIT

---
_Source: https://npm.io/package/with · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
