Skip to content

Commit

Permalink
Implemented transform-void-to-nothing plugin.
Browse files Browse the repository at this point in the history
Removes the 'void 0' rval for let-/var-assignments.
  • Loading branch information
Shine Wang committed Oct 18, 2016
1 parent a7a890a commit c8da4f4
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/babel-plugin-transform-void-to-nothing/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
src
__tests__
node_modules
*.log
53 changes: 53 additions & 0 deletions packages/babel-plugin-transform-void-to-nothing/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# babel-plugin-transform-void-to-nothing

This changes 'void 0' rval in var-/let-assignments to nothing.

## Example

**In**

```javascript
var a = void 0;
let b = void 0;
const c = void 0;
```

**Out**

```javascript
var a;
let b;
const c = void 0;
```

## Installation

```sh
$ npm install babel-plugin-transform-void-to-nothing
```

## Usage

### Via `.babelrc` (Recommended)

**.babelrc**

```json
{
"plugins": ["babel-plugin-transform-void-to-nothing"]
}
```

### Via CLI

```sh
$ babel --plugins babel-plugin-transform-void-to-nothing script.js
```

### Via Node API

```javascript
require("babel-core").transform("code", {
plugins: ["babel-plugin-transform-void-to-nothing"]
});
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
jest.autoMockOff();

const babel = require("babel-core");
const plugin = require("../src/index");

function transform(code) {
return babel.transform(code, {
plugins: [plugin],
}).code;
}

describe("transform-void-to-nothing-plugin", () => {
it("should transform `void 0` var-assignments to nothing", () => {
const source = "var x = void 0;";
const expected = "var x;";
expect(transform(source)).toBe(expected);
});

it("should transform multiple `void 0` assignments to nothing", () => {
const source = "var a = void 0, b = 3, c = void 0, d;";
const expected = "var a,\n b = 3,\n c,\n d;";
expect(transform(source)).toBe(expected);
});

it("should transform `void 0` let-assignments to nothing", () => {
const source = "let a = void 0;";
const expected = "let a;";
expect(transform(source)).toBe(expected);
});

it("should not transform `void 0` const-assignments", () => {
const source = "const a = void 0;";
const expected = source;
expect(transform(source)).toBe(expected);
});
});
22 changes: 22 additions & 0 deletions packages/babel-plugin-transform-void-to-nothing/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"use strict";

const v0 = require("../../babel-helper-is-void-0/src");

module.exports = function({ types: t }) {
// "var a = void 0;" -> "var a;"
const isVoid0 = v0(t);
return {
name: "transform-void-to-nothing",
visitor: {
VariableDeclaration({node: {kind: kind, declarations: declarations}}) {
if (kind !== "const") {
for (const declaration of declarations) {
if (isVoid0(declaration.init)) {
declaration.init = null;
}
}
}
}
},
};
};

0 comments on commit c8da4f4

Please sign in to comment.