babel-plugin-transform-rename-properties
Advanced tools
Comparing version 0.0.1 to 0.0.2
31
index.js
@@ -15,5 +15,36 @@ module.exports = function({ types: t }, options = {}) { | ||
const replacePropertyOrMethod = { | ||
exit(path) { | ||
const node = path.node; | ||
let name; | ||
if (t.isIdentifier(node.key)) { | ||
name = node.key.name; | ||
} else if (t.isStringLiteral(node.key)) { | ||
name = node.key.value; | ||
} else { | ||
return; | ||
} | ||
const newName = nameMap.get(name); | ||
if (newName === undefined) { | ||
return; | ||
} | ||
const newNode = t.cloneNode(node, false); | ||
if (t.isIdentifier(node.key) && t.isValidIdentifier(newName)) { | ||
newNode.key = t.identifier(newName); | ||
} else { | ||
newNode.key = t.stringLiteral(newName); | ||
} | ||
path.replaceWith(newNode); | ||
path.skip(); | ||
} | ||
}; | ||
return { | ||
name: "transform-rename-properties", | ||
visitor: { | ||
Property: replacePropertyOrMethod, | ||
Method: replacePropertyOrMethod, | ||
MemberExpression: { | ||
@@ -20,0 +51,0 @@ exit(path) { |
{ | ||
"name": "babel-plugin-transform-rename-properties", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Rename JavaScript properties", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -16,3 +16,12 @@ # babel-plugin-transform-rename-properties | ||
```js | ||
function foo(obj) { | ||
const obj = { | ||
foo: { | ||
bar: 1 | ||
}, | ||
quux: 2 | ||
}; | ||
const { foo } = obj; | ||
function quux(obj) { | ||
return obj.foo.bar + obj.quux; | ||
@@ -43,3 +52,12 @@ } | ||
```js | ||
function foo(obj) { | ||
const obj = { | ||
__FOO__: { | ||
bar: 1 | ||
}, | ||
"I HAVE SPACES": 2 | ||
}; | ||
const { __FOO__: foo } = obj; | ||
function quux(obj) { | ||
return obj.__FOO__.bar + obj["I HAVE SPACES"]; | ||
@@ -46,0 +64,0 @@ } |
4540
70
68