-
-
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-void-to-nothing plugin.
Removes the 'void 0' rval for let-/var-assignments.
- Loading branch information
Shine Wang
committed
Oct 18, 2016
1 parent
a7a890a
commit c8da4f4
Showing
4 changed files
with
115 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 |
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,53 @@ | ||
# babel-plugin-transform-void-to-nothing | ||
|
||
This changes 'void 0' rval in var-/let-assignments to nothing. | ||
|
||
## Example | ||
|
||
**In** | ||
|
||
```javascript | ||
var a = void 0; | ||
let b = void 0; | ||
const c = void 0; | ||
``` | ||
|
||
**Out** | ||
|
||
```javascript | ||
var a; | ||
let b; | ||
const c = void 0; | ||
``` | ||
|
||
## Installation | ||
|
||
```sh | ||
$ npm install babel-plugin-transform-void-to-nothing | ||
``` | ||
|
||
## Usage | ||
|
||
### Via `.babelrc` (Recommended) | ||
|
||
**.babelrc** | ||
|
||
```json | ||
{ | ||
"plugins": ["babel-plugin-transform-void-to-nothing"] | ||
} | ||
``` | ||
|
||
### Via CLI | ||
|
||
```sh | ||
$ babel --plugins babel-plugin-transform-void-to-nothing script.js | ||
``` | ||
|
||
### Via Node API | ||
|
||
```javascript | ||
require("babel-core").transform("code", { | ||
plugins: ["babel-plugin-transform-void-to-nothing"] | ||
}); | ||
``` |
36 changes: 36 additions & 0 deletions
36
...plugin-transform-void-to-nothing/__tests__/babel-plugin-transform-void-to-nothing-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,36 @@ | ||
jest.autoMockOff(); | ||
|
||
const babel = require("babel-core"); | ||
const plugin = require("../src/index"); | ||
|
||
function transform(code) { | ||
return babel.transform(code, { | ||
plugins: [plugin], | ||
}).code; | ||
} | ||
|
||
describe("transform-void-to-nothing-plugin", () => { | ||
it("should transform `void 0` var-assignments to nothing", () => { | ||
const source = "var x = void 0;"; | ||
const expected = "var x;"; | ||
expect(transform(source)).toBe(expected); | ||
}); | ||
|
||
it("should transform multiple `void 0` assignments to nothing", () => { | ||
const source = "var a = void 0, b = 3, c = void 0, d;"; | ||
const expected = "var a,\n b = 3,\n c,\n d;"; | ||
expect(transform(source)).toBe(expected); | ||
}); | ||
|
||
it("should transform `void 0` let-assignments to nothing", () => { | ||
const source = "let a = void 0;"; | ||
const expected = "let a;"; | ||
expect(transform(source)).toBe(expected); | ||
}); | ||
|
||
it("should not transform `void 0` const-assignments", () => { | ||
const source = "const a = void 0;"; | ||
const expected = source; | ||
expect(transform(source)).toBe(expected); | ||
}); | ||
}); |
22 changes: 22 additions & 0 deletions
22
packages/babel-plugin-transform-void-to-nothing/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,22 @@ | ||
"use strict"; | ||
|
||
const v0 = require("../../babel-helper-is-void-0/src"); | ||
|
||
module.exports = function({ types: t }) { | ||
// "var a = void 0;" -> "var a;" | ||
const isVoid0 = v0(t); | ||
return { | ||
name: "transform-void-to-nothing", | ||
visitor: { | ||
VariableDeclaration({node: {kind: kind, declarations: declarations}}) { | ||
if (kind !== "const") { | ||
for (const declaration of declarations) { | ||
if (isVoid0(declaration.init)) { | ||
declaration.init = null; | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
}; |