Stop mutating nodes (#5963)

* Stop mutating nodes

* Update tests

* linting
This commit is contained in:
Justin Ridgewell
2017-07-18 13:24:07 -04:00
committed by GitHub
parent aa684d1b0c
commit 28ae47a174
34 changed files with 83 additions and 275 deletions

View File

@@ -1,4 +1,6 @@
export default function({ types: t }) {
const HOISTED = new WeakSet();
const immutabilityVisitor = {
enter(path, state) {
const stop = () => {
@@ -59,15 +61,14 @@ export default function({ types: t }) {
return {
visitor: {
JSXElement(path) {
if (path.node._hoisted) return;
if (HOISTED.has(path.node)) return;
HOISTED.add(path.node);
const state = { isImmutable: true };
path.traverse(immutabilityVisitor, state);
if (state.isImmutable) {
path.hoist();
} else {
path.node._hoisted = true;
}
},
},