-
-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented transform-remove-undefined plugin. (#197)
* Implemented transform-void-to-nothing plugin. Removes the 'void 0' rval for let-/var-assignments. * addressed PR comments - used t.getBindingIdentifiers - changed from forEach to for-loop - added tests * Optimized visitor to be O(N) in the number of AST nodes instead of O(N^2). * Lint and added package.json. * Bug fix: should capture inner functions' usages of var-declared identifiers too. * Renamed babel-plugin-remove-undefined-if-possible -> babel-plugin-transform-remove-undefined. * Track using violations There's no way we can visit every identifier. Instead, figure out which constant violations hamper the minification. Oh, and some test cases. Those should probably be cleaned up. 😁 * Find the function's function's references * Fixed many things: - purity-check of rvals - added unit tests - refactoring * renamed tests
- Loading branch information
Showing
7 changed files
with
375 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
59
packages/babel-plugin-transform-remove-undefined/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
}); | ||
``` |
174 changes: 174 additions & 0 deletions
174
...ugin-transform-remove-undefined/__tests__/babel-plugin-transform-remove-undefined-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
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 in 1 statement", () => { | ||
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 not remove const-assignments to undefined", () => { | ||
const source = "const a = undefined;"; | ||
expect(transform(source)).toBe(source); | ||
}); | ||
|
||
it("should remove undefined return value", () => { | ||
const source = unpad(` | ||
function foo() { | ||
return undefined; | ||
}`); | ||
const expected = unpad(` | ||
function foo() { | ||
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 remove var-assignments in loops if no modify", () => { | ||
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 if modify", () => { | ||
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 lval is reference before via a 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 not remove 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
16
packages/babel-plugin-transform-remove-undefined/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": {} | ||
} |
120 changes: 120 additions & 0 deletions
120
packages/babel-plugin-transform-remove-undefined/src/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
"use strict"; | ||
|
||
function isPureAndUndefined(rval) { | ||
if (rval.isIdentifier() && | ||
rval.node.name === "undefined") { | ||
return true; | ||
} | ||
if (!rval.isPure()) { | ||
return false; | ||
} | ||
const evaluation = rval.evaluate(); | ||
return evaluation.confident === true && evaluation.value === undefined; | ||
} | ||
|
||
function getLoopParent(path, scopeParent) { | ||
const parent = path.findParent((p) => p.isLoop() || p === scopeParent); | ||
// don't traverse higher than the function the var is defined in. | ||
return parent === scopeParent ? null : parent; | ||
} | ||
|
||
function getFunctionParent(path, scopeParent) { | ||
const parent = path.findParent((p) => p.isFunction()); | ||
// don't traverse higher than the function the var is defined in. | ||
return parent === scopeParent ? null : parent; | ||
} | ||
|
||
function getFunctionReferences(path, scopeParent, references = new Set()) { | ||
for (let func = getFunctionParent(path, scopeParent); func; func = getFunctionParent(func, scopeParent)) { | ||
const id = func.node.id; | ||
const binding = id && func.scope.getBinding(id.name); | ||
|
||
if (!binding) { | ||
continue; | ||
} | ||
|
||
binding.referencePaths.forEach((path) => { | ||
if (!references.has(path)) { | ||
references.add(path); | ||
getFunctionReferences(path, scopeParent, references); | ||
} | ||
}); | ||
} | ||
return references; | ||
} | ||
|
||
function hasViolation(declarator, scope, start) { | ||
const binding = scope.getBinding(declarator.node.id.name); | ||
if (!binding) { | ||
return true; | ||
} | ||
|
||
const scopeParent = declarator.getFunctionParent(); | ||
|
||
const violation = binding.constantViolations.some((v) => { | ||
// return 'true' if we cannot guarantee the violation references | ||
// the initialized identifier after | ||
const violationStart = v.node.start; | ||
if (violationStart === undefined || violationStart < start) { | ||
return true; | ||
} | ||
|
||
const references = getFunctionReferences(v, scopeParent); | ||
for (const ref of references) { | ||
if (ref.node.start === undefined || ref.node.start < start) { | ||
return true; | ||
} | ||
} | ||
|
||
for (let loop = getLoopParent(declarator, scopeParent); loop; loop = getLoopParent(loop, scopeParent)) { | ||
if (loop.node.end === undefined || loop.node.end > violationStart) { | ||
return true; | ||
} | ||
} | ||
}); | ||
|
||
return violation; | ||
} | ||
|
||
module.exports = function() { | ||
return { | ||
name: "transform-remove-undefined", | ||
visitor: { | ||
ReturnStatement(path) { | ||
if (path.node.argument !== null) { | ||
if (isPureAndUndefined(path.get("argument"))) { | ||
path.node.argument = null; | ||
} | ||
} | ||
}, | ||
|
||
VariableDeclaration(path) { | ||
switch (path.node.kind) { | ||
case "const": | ||
break; | ||
case "let": | ||
for (const declarator of path.get("declarations")) { | ||
if (isPureAndUndefined(declarator.get("init"))) { | ||
declarator.node.init = null; | ||
} | ||
} | ||
break; | ||
case "var": | ||
const start = path.node.start; | ||
if (start === undefined) { | ||
// This is common for plugin-generated nodes | ||
break; | ||
} | ||
const scope = path.scope; | ||
for (const declarator of path.get("declarations")) { | ||
if (isPureAndUndefined(declarator.get("init")) && | ||
!hasViolation(declarator, scope, start)) { | ||
declarator.node.init = null; | ||
} | ||
} | ||
break; | ||
} | ||
}, | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters