# generate-code

> The plugin exports a class that helps generate code and sourcemaps.

Latest version **2.3.2** (published 2017-06-29) · 0 weekly downloads

## Install

```sh
npm install generate-code
pnpm add generate-code
yarn add generate-code
bun add generate-code
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.3.2 |
| Published | 2017-06-29 |
| First published | 2017-06-12 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 5 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | dwaynejf |

## Links

- npm: https://www.npmjs.com/package/generate-code
- npm.io page: https://npm.io/package/generate-code

## Dependencies (5)

- [lodash](https://npm.io/package/lodash.md) ^4.17.4
- [source-map](https://npm.io/package/source-map.md) ^0.5.6
- [sourcemap-codec](https://npm.io/package/sourcemap-codec.md) ^1.3.1
- [merge-source-map](https://npm.io/package/merge-source-map.md) ^1.0.3
- [lines-and-columns](https://npm.io/package/lines-and-columns.md) ^1.1.6

## Recent versions

- 2.3.2 (latest) — 2017-06-29
- 2.3.1 — 2017-06-29
- 2.3.0 — 2017-06-21
- 2.2.2 — 2017-06-14
- 2.2.1 — 2017-06-13
- 2.2.0 — 2017-06-13
- 2.1.0 — 2017-06-13
- 2.0.0 — 2017-06-13
- 1.0.0 — 2017-06-12

## README

# generate-code

The plugin exports a class that helps generate code and sourcemaps.

### Installation

```bash
npm install --save generate-code
```

### Usage

```js
const CodeGenerator = require('generate-code');

const code = new CodeGenerator(options);

code.add('var a, b;\n');
code.addWithMapping(
  'var c;',
  { line: 1, column: 10 },
  'c'
);

const generatedCode = code.toString();

// var a, b;
// var c;

const generatedMap = code.generateMap();

// {
//   version: 3,
//   ...
// }
```

### API

##### CodeGenerator

```
new CodeGenerator(options: Options)
```

* `options.filename` (required): filename for the sourcemap.
* `options.sourceContent` (required): source content for the sourcemap.
* `options.sourceMap` (default: `true`): if it is needed to generate
a sourcemap.
* `options.inputSourceMap` (default: `null`): input sourcemap.

##### CodeGenerator#add

```
add(chunk: string): this
```

Adds a `chunk` of code to the generated code.

Example:

```js
code.add('fun(1, 2);');
```

##### CodeGenerator#addWithMap

```
addWithMap(
  chunk: string,
  map: SourceMap,
  position?: { line: number, column: number } | number
): this
```

Applies the `map` to the existing map (shifting it according to the
current position in the generated code) and then adds the `chunk`
of code.

Third optional parameter is the position of the mappings relatively
to the source. Can be a number or a `{ line, column }` object with
0-indexed line.

Example:

```js
const code = new CodeGenerator({
  filename: 'index.js',
  sourceContent: `var a = 10;
var b = {
  c: 1,
  d: 2
};
var c = [];`
});

code.add(`var a = 10;
var b = `);

const {
  code: generated,
  map
} = addUnderscoreToProps(`{
  c: 1,
  d: 2
}`);

code.addWithMap(
  generated,
  map,
  { line: 1, column: 8 } // where '{' is located
);
code.add(`;
var c = [];`);

console.log(code.toString());

// var a = 10;
// var b = {
//   _c: 1,
//   _d: 2,
// };
```

##### CodeGenerator#addWithMapping

```
addWithMapping(
  chunk: string,
  position?: { line: number, column: number } | number,
  name?: string
): this
```

Applies the mapping to the existing map (shifting it according to the
current position in the generated code) and then adds the `chunk`
of code.

Second optional parameter is the position of the mapping relatively
to the source. Can be a number or a `{ line, column }` object with
0-indexed line.

Third optional argument describes the mapping name.

Example:

```js
const code = new CodeGenerator({
  filename: 'index.js',
  sourceContent: `var a = 10;
var b = {
  c: 1,
  d: 2
};
var c = [];`
});

code.add(`var a = 10;
var b = {
  `);

code.addWithMapping(
  '_c',
  { line: 2, column: 2 }, // where 'c' is located
  'c'
);

code.add(`: 1,
  `);

code.addWithMapping(
  '_d',
  { line: 3, column: 2 }, // where 'd' is located
  'd'
);

code.add(`: 2
};
var c = [];`);

console.log(code.toString());

// var a = 10;
// var b = {
//   _c: 1,
//   _d: 2,
// };
```

##### CodeGenerator#getCurrentIndent()

```
getCurrentIndent(): string
```

Returns current code indent.

Example:

```js
const code = new CodeGenerator({
  filename: 'index.js',
  sourceContent: 'abc'
});

code.add('\na');

code.toString();         // '\na'
code.getCurrentIndent(); // ''

code.add('\n  b');

code.toString();         // '\na\n  b'
code.getCurrentIndent(); // '  '

code.add('\n\tc');

code.toString();         // '\na\n  b\n\tc'
code.getCurrentIndent(); // '\t'
```

##### CodeGenerator#toString()

```
toString(): string
```

Returns the generated code.

##### CodeGenerator#generateMap()

```
generateMap(): SourceMap
```

Returns the generated sourcemap.

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