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-remove-undefined-if-possible/.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-remove-undefined-if-possible/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# babel-plugin-remove-undefined-if-possible

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-remove-undefined-if-possible
```

## Usage

### Via `.babelrc` (Recommended)

**.babelrc**

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

### Via CLI

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

### Via Node API

```javascript
require("babel-core").transform("code", {
plugins: ["babel-plugin-remove-undefined-if-possible"]
});
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
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("remove-undefined-if-possible-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 nested var-assignments if not referenced before", () => {
const source = unpad(`
function foo() {
a = 3;
var { a: aa, b: bb } = undefined;
}`);
const expected = unpad(`
function foo() {
a = 3;
var { a: aa, b: bb };
}`);
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 not remove var-assignments in loops", () => {
const source = unpad(`
for (var a = undefined;;) {
var b = undefined;
}`);
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);
});
Copy link
Member

Choose a reason for hiding this comment

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

test cases for

  • void foo()

Copy link
Member

Choose a reason for hiding this comment

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

It doesn't convert

function foo() {
  var x = void 0;
  function bar() {
    var x = void 0;
    function baz() {
      var x = void 0;
    }
  }
}

});
16 changes: 16 additions & 0 deletions packages/babel-plugin-remove-undefined-if-possible/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "babel-plugin-remove-undefined-if-possible",
"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-remove-undefined-if-possible",
"bugs": "https://github.com/babel/babili/issues",
"author": "shinew",
"license": "MIT",
"main": "lib/index.js",
"keywords": [
"babel-plugin"
],
"dependencies": {},
"devDependencies": {}
}
82 changes: 82 additions & 0 deletions packages/babel-plugin-remove-undefined-if-possible/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
"use strict";

function removeRvalIfUndefined(declaratorPath) {
const rval = declaratorPath.get("init")
.evaluate();
Copy link
Member

Choose a reason for hiding this comment

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

can you push evaluate to the same line. It's not really long to be added to a separate line.

if (rval.confident === true && rval.value === undefined) {
Copy link
Member

Choose a reason for hiding this comment

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

This also needs to be pure.

var x = void console.log("hi");
var y = void foo();

shouldn't be evaluated to undefined and be removed.

declaratorPath.node.init = null;
}
}

function areAllBindingsNotSeen(declaratorPath, seenNames, t) {
const id = declaratorPath.node.id;
const names = t.getBindingIdentifiers(id);
for (const name in names) {
if (seenNames.has(name)) {
return false;
}
}
return true;
}

module.exports = function({ types: t }) {
let names = null;
let functionNesting = 0;
return {
name: "remove-undefined-if-possible",
visitor: {
Program: {
enter() {
names = new Set();
functionNesting = 0;
},
exit() {
names = null;
functionNesting = 0;
},
},
Function: {
enter() {
functionNesting++;
},
exit() {
functionNesting--;
Copy link
Member

Choose a reason for hiding this comment

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

I suppose you're using this only to check global ? you can use this information to check if different nested function scopes have same variable name and then solve -

function foo() {
  var x = void 0;
  function bar() {
    var x = void 0; // this is not removed now
  }
}

},
},
Identifier(path) {
names.add(path.node.name);
},
ReturnStatement(path) {
if (path.node.argument !== null) {
const rval = path.get("argument")
.evaluate();
Copy link
Member

Choose a reason for hiding this comment

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

same as above. Can you push evaluate to the same line as previous one?

if (rval.confident === true && rval.value === undefined) {
Copy link
Member

Choose a reason for hiding this comment

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

probably requires isPure check here too for

return void console.log("asdf");

path.node.argument = null;
}
}
},
VariableDeclaration(path) {
switch (path.node.kind) {
case "const":
break;
case "let":
for (const declarator of path.get("declarations")) {
removeRvalIfUndefined(declarator);
}
break;
case "var":
if (functionNesting === 0) {
// ignore global vars
break;
}
for (const declarator of path.get("declarations")) {
if (areAllBindingsNotSeen(declarator, names, t)) {
removeRvalIfUndefined(declarator);
}
}
break;
}
},
},
};
};
1 change: 1 addition & 0 deletions packages/babel-preset-babili/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"babel-plugin-minify-replace": "^0.0.1",
"babel-plugin-minify-simplify": "^0.0.3",
"babel-plugin-minify-type-constructors": "^0.0.1",
"babel-plugin-remove-undefined-if-possible": "^0.0.1",
"babel-plugin-transform-member-expression-literals": "^6.8.0",
"babel-plugin-transform-merge-sibling-variables": "^6.8.0",
"babel-plugin-transform-minify-booleans": "^6.8.0",
Expand Down
1 change: 1 addition & 0 deletions packages/babel-preset-babili/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = {
require("babel-plugin-minify-replace"),
require("babel-plugin-minify-simplify"),
require("babel-plugin-minify-type-constructors"),
require("babel-plugin-remove-undefined-if-possible"),
require("babel-plugin-transform-member-expression-literals"),
require("babel-plugin-transform-merge-sibling-variables"),
require("babel-plugin-transform-minify-booleans"),
Expand Down