-
-
Notifications
You must be signed in to change notification settings - Fork 225
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
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2509f7c
Implemented transform-void-to-nothing plugin.
9202a29
addressed PR comments
0f57a44
Optimized visitor to be O(N) in the number of AST nodes instead of O(…
494ce86
Merge branch 'master' into transform-void-to-nothing
735e9f0
Lint and added package.json.
9393126
Bug fix: should capture inner functions' usages of var-declared ident…
5e2d216
Renamed babel-plugin-remove-undefined-if-possible -> babel-plugin-tra…
06c4195
Track using violations
jridgewell 712922a
Find the function's function's references
jridgewell 03884c7
Merge pull request #1 from jridgewell/pr/197
shinew 580c677
Fixed many things:
26120cc
Merge branch 'master' into transform-void-to-nothing
42a9414
renamed tests
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"] | ||
}); | ||
``` |
200 changes: 200 additions & 0 deletions
200
...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,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 ...", () => { | ||
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
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": {} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should this be more descriptive? or just make this instead of the previous test?