Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented transform-remove-undefined plugin. #197

Merged
merged 13 commits into from
Nov 8, 2016
4 changes: 4 additions & 0 deletions packages/babel-plugin-transform-remove-undefined/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
src
__tests__
node_modules
*.log
59 changes: 59 additions & 0 deletions packages/babel-plugin-transform-remove-undefined/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# babel-plugin-transform-remove-undefined

For variable assignments, this removes rvals that evaluate to `undefined`
(`var`s in functions only).
For functions, this removes return arguments that evaluate to `undefined`.

## Example

**In**

```javascript
let a = void 0;
function foo() {
var b = undefined;
return undefined;
}
```

**Out**

```javascript
let a;
function foo() {
var b;
return;
}
```

## Installation

```sh
$ npm install babel-plugin-transform-remove-undefined
```

## Usage

### Via `.babelrc` (Recommended)

**.babelrc**

```json
{
"plugins": ["babel-plugin-transform-remove-undefined"]
}
```

### Via CLI

```sh
$ babel --plugins babel-plugin-transform-remove-undefined script.js
```

### Via Node API

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

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

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

describe("transform-remove-undefined-plugin", () => {
it("should remove multiple undefined assignments", () => {
const source = "let a = undefined, b = 3, c = undefined, d;";
const expected = "let a,\n b = 3,\n c,\n d;";
expect(transform(source)).toBe(expected);
});

it("should remove let-assignments to undefined", () => {
const source = "let a = undefined;";
const expected = "let a;";
expect(transform(source)).toBe(expected);
});

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

it("should remove undefined return value", () => {
const source = unpad(`
function foo() {
const a = undefined;
return undefined;
}`);
const expected = unpad(`
function foo() {
const a = undefined;
return;
}`);
expect(transform(source)).toBe(expected);
});

it("should remove var declarations in functions", () => {
const source = unpad(`
function foo() {
var a = undefined;
}`);
const expected = unpad(`
function foo() {
var a;
}`);
expect(transform(source)).toBe(expected);
});

it("should remove let-assignments in inner blocks", () => {
const source = unpad(`
let a = 1;
{
let a = undefined;
}`);
const expected = unpad(`
let a = 1;
{
let a;
}`);
expect(transform(source)).toBe(expected);
});

it("should not remove const-assignments to undefined", () => {
const source = "const a = undefined;";
expect(transform(source)).toBe(source);
});

it("should remove var-assignments in loops", () => {
const source = unpad(`
for (var a = undefined;;) {
var b = undefined;
}`);
const expected = unpad(`
for (var a;;) {
var b;
}`);
expect(transform(source)).toBe(expected);
});

it("should not remove var-assignments in loops 2", () => {
const source = unpad(`
for (var a;;) {
var b = undefined;
console.log(b);
b = 3;
}`);
expect(transform(source)).toBe(source);
});

it("should not remove var-assignments if referenced before", () => {
const source = unpad(`
function foo() {
a = 3;
var a = undefined;
}`);
expect(transform(source)).toBe(source);
});

it("should not remove nested var-assignments if referenced before", () => {
const source = unpad(`
function foo() {
aa = 3;
var { a: aa, b: bb } = undefined;
}`);
expect(transform(source)).toBe(source);
});

it("should not remove if call to constant violation function", () => {
const source = unpad(`
function foo() {
bar();
var x = undefined;
console.log(x);
function bar() {
x = 3;
}
}`);
expect(transform(source)).toBe(source);
});

it("should remove if not referenced in any way before", () => {
const source = unpad(`
function foo() {
var x = undefined;
bar();
console.log(x);
function bar() {
x = 3;
}
}`);
const expected = unpad(`
function foo() {
var x;
bar();
console.log(x);
function bar() {
x = 3;
}
}`);
expect(transform(source)).toBe(expected);
});

it("should remove ...", () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be more descriptive? or just make this instead of the previous test?

const source = unpad(`
foo();
function foo() {
var x = undefined;
bar();
console.log(x);
function bar() {
x = 3;
}
}`);
const expected = unpad(`
foo();
function foo() {
var x;
bar();
console.log(x);
function bar() {
x = 3;
}
}`);
expect(transform(source)).toBe(expected);
});

it("should not remove nor break on mutually recursive function", () => {
const source = unpad(`
function foo() {
a();
var c = undefined;
function a() {
b();
}
function b() {
a();
c = 3;
}
}`);
expect(transform(source)).toBe(source);
});

it("should not remove if rval has side effects", () => {
const source = unpad(`
function foo() {
var a = void b();
return void bar();
}`);
expect(transform(source)).toBe(source);
});
});
16 changes: 16 additions & 0 deletions packages/babel-plugin-transform-remove-undefined/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "babel-plugin-transform-remove-undefined",
"version": "0.0.1",
"description": "This removes rvals that are equivalent to undefined wherever possible",
"homepage": "https://github.com/babel/babili#readme",
"repository": "https://github.com/babel/babili/tree/master/packages/babel-plugin-transform-remove-undefined",
"bugs": "https://github.com/babel/babili/issues",
"author": "shinew",
"license": "MIT",
"main": "lib/index.js",
"keywords": [
"babel-plugin"
],
"dependencies": {},
"devDependencies": {}
}
Loading