-
-
Notifications
You must be signed in to change notification settings - Fork 222
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
Changes from 1 commit
2509f7c
9202a29
0f57a44
494ce86
735e9f0
9393126
5e2d216
06c4195
712922a
03884c7
580c677
26120cc
42a9414
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
src | ||
__tests__ | ||
node_modules | ||
*.log |
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,78 @@ | ||
jest.autoMockOff(); | ||
|
||
const babel = require("babel-core"); | ||
const plugin = require("../src/index"); | ||
|
||
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 = ` | ||
function foo() { | ||
const a = undefined; | ||
return undefined; | ||
}`; | ||
const expected = ` | ||
function foo() { | ||
const a = undefined; | ||
return; | ||
}`; | ||
expect(transform(source)).toBe(expected); | ||
}); | ||
|
||
it("should remove var declarations in functions", () => { | ||
const source = ` | ||
function foo() { | ||
var a = undefined; | ||
}`; | ||
const expected = ` | ||
function foo() { | ||
var 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 = ` | ||
for (var a = undefined;;) { | ||
var b = undefined; | ||
}`; | ||
expect(transform(source)).toBe(source); | ||
}); | ||
|
||
it("should not remove var-assignments if referenced before", () => { | ||
const source = ` | ||
function foo() { | ||
a = 3; | ||
var a = undefined; | ||
}`; | ||
expect(transform(source)).toBe(source); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. test cases for
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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;
}
}
} |
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
"use strict"; | ||
|
||
function isInLocalLoop(path, t) { | ||
if (path === null) { | ||
return false; | ||
} else if (t.isLoop(path)) { | ||
return true; | ||
} else if (t.isFunction(path)) { | ||
return false; | ||
} else { | ||
return isInLocalLoop(path.parentPath, t); | ||
} | ||
} | ||
|
||
function removeRvalIfUndefined(declaratorPath) { | ||
const rval = declaratorPath.get("init") | ||
.evaluate(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 isAnyLvalReferencedBefore(declaratorPath, seenIds, t) { | ||
const id = declaratorPath.get("id"); | ||
if (t.isIdentifier(id)) { | ||
return seenIds.has(id.node.name); | ||
} | ||
let hasReference = false; | ||
id.traverse({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for non simple declarations, you have |
||
Identifier(path) { | ||
if (seenIds.has(path.node.name)) { | ||
hasReference = true; | ||
} | ||
} | ||
}); | ||
return hasReference; | ||
} | ||
|
||
function removeUndefinedAssignments(functionPath, t) { | ||
const seenIds = new Set(); | ||
functionPath.traverse({ | ||
Function(path) { | ||
path.skip(); | ||
}, | ||
Identifier(path) { | ||
// potential optimization: only store ids that are lvals of assignments. | ||
seenIds.add(path.node.name); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't it also consider this as seen ? let a = 1;
{
let a = undefined;
} ? |
||
}, | ||
VariableDeclaration(path) { | ||
switch (path.node.kind) { | ||
case "const": | ||
break; | ||
case "let": | ||
path.node.declarations.forEach((declarator, index) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you need declarationPath, you can simply do |
||
const declaratorPath = path.get("declarations")[index]; | ||
removeRvalIfUndefined(declaratorPath); | ||
}); | ||
break; | ||
case "var": | ||
if (!t.isFunction(functionPath)) { | ||
break; | ||
} | ||
if (isInLocalLoop(path.parentPath, t)) { | ||
break; | ||
} | ||
path.node.declarations.forEach((declarator, index) => { | ||
const declaratorPath = path.get("declarations")[index]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as previous. Since you need declarationPath, you can simply do |
||
if (!isAnyLvalReferencedBefore(declaratorPath, seenIds, t)) { | ||
removeRvalIfUndefined(declaratorPath); | ||
} | ||
}); | ||
break; | ||
} | ||
}, | ||
}); | ||
} | ||
|
||
module.exports = function({ types: t }) { | ||
return { | ||
name: "remove-undefined-if-possible", | ||
visitor: { | ||
FunctionParent(path) { | ||
removeUndefinedAssignments(path, t); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since this creates 2 more levels of traverse for some paths, can you run this with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I ran the timing script after adding this to the preset list: for file in ./scripts/fixtures/*.js; do
echo $file
./scripts/plugin-timing.js $file
done ./scripts/fixtures/backbone.jspluginAlias time(ms) # visits time/visit(ms) ./scripts/fixtures/flot.jspluginAlias time(ms) # visits time/visit(ms) ./scripts/fixtures/jquery.jspluginAlias time(ms) # visits time/visit(ms) ./scripts/fixtures/react.jspluginAlias time(ms) # visits time/visit(ms) |
||
}, | ||
ReturnStatement(path) { | ||
if (path.node.argument !== null) { | ||
const rval = path.get("argument") | ||
.evaluate(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably requires return void console.log("asdf"); |
||
path.node.argument = null; | ||
} | ||
} | ||
}, | ||
}, | ||
}; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is how all other tests are aligned.