# babel-plugin-proper-tail-calls

> A Babel plugin that optimizes tail recursion

Latest version **1.0.2** (published 2019-12-03) · ISC license · 0 weekly downloads

## Install

```sh
npm install babel-plugin-proper-tail-calls
pnpm add babel-plugin-proper-tail-calls
yarn add babel-plugin-proper-tail-calls
bun add babel-plugin-proper-tail-calls
```

## 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 | 1.0.2 |
| Published | 2019-12-03 |
| First published | 2019-12-03 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 12.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Alec Davidson |
| Maintainers | adavidson |
| Keywords | functional, tail call, tail calls, babel, plugin, scala, haskell, clojure, recursion |

## Links

- npm: https://www.npmjs.com/package/babel-plugin-proper-tail-calls
- npm.io page: https://npm.io/package/babel-plugin-proper-tail-calls

## Dependencies (2)

- [@babel/core](https://npm.io/package/@babel/core.md) ^7.7.4
- [@babel/template](https://npm.io/package/@babel/template.md) ^7.7.4

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.0.2 (latest) — 2019-12-03
- 1.0.1 — 2019-12-03
- 1.0.0 — 2019-12-03

## README

# Proper Tail Calls in JavaScript

Proper tail calls are recursive function calls that do not need to allocate extra stack space proportional to recursion depth. They are a part of the ECMAScript 6 standard but are currently [only supported in Safari](https://kangax.github.io/compat-table/es6/). This plugin implements proper tail calls through a technique called function [trampolining](https://raganwald.com/2013/03/28/trampolines-in-javascript.html). Using the proper-tail-calls plugin, a program could make an unbounded number of consecutive tail calls without unboundedly growing the stack.

## Example

```JavaScript
function factorial(num, accumulated = 1) {
    if (num <= 1) {
        return accumulated;
    } else {
        return factorial(num - 1, num * accumulated); // proper tail position
    }
}

factorial(10)
  //=> 3628800
factorial(32687)
  //=> RangeError: Maximum call stack size exceeded

const { code } = babel.transform(factorial.toString(), {
  plugins: ["proper-tail-calls"]
})

factorial = Function(`${code} return factorial`)()
factorial(32687)
  //=> Infinity
```

## How It Works

Recursive calls that are in a [proper tail position](https://webkit.org/blog/6240/ecmascript-6-proper-tail-calls-in-webkit/) will be *trampolined*. Instead of recursing directly, the recursive call is deferred and a wrapper function is returned.

The factorial example above transpiles to:

```JavaScript
var factorial = _trampoline(function factorial(num, accumulated = 1) {
    if (num <= 1) {
        return accumulated;
    } else {
        return () => {
            return factorial(num - 1, num * accumulated);
        }
    }
})

function _trampoline(fn) {
  return function trampolined(...args) {
    let result = fn(...args);
    while (typeof result === 'function') {
      result = result();
    }

    return result;
  };
}
```

## Installation

```sh
$ npm install --save-dev babel-plugin-proper-tail-calls
```

## Usage

Add the following line to your .babelrc file:

```json
{
  "plugins": ["proper-tail-calls"]
}
```

```sh
babel --plugins proper-tail-calls script.js
```

### Via Node API

```javascript
require("@babel/core").transform("code", {
  plugins: ["proper-tail-calls"]
});
```

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