Skip to content

Commit

Permalink
Source mapping for for-in
Browse files Browse the repository at this point in the history
  • Loading branch information
wcjohnson committed Jun 18, 2017
1 parent 966dc9f commit ee93ac2
Show file tree
Hide file tree
Showing 37 changed files with 88 additions and 202 deletions.
92 changes: 45 additions & 47 deletions src/for.lsc
Original file line number Diff line number Diff line change
@@ -1,74 +1,72 @@
import t from './types'
import { ensureBlockBody } from './blocks'
import is from './is'
import { undeclaredRef } from './ref'

import { getLoc, placeAtLoc as atLoc, placeAtNode as atNode, span, placeTreeAtLocWhenUnplaced as allAtLoc } from 'ast-loc-utils'

generateForInIterator(path, type: "array" | "object") ->
idx = path.node.idx || path.scope.generateUidIdentifier("i")
{ node } = path
forKwdLoc = node~getLoc()~span(3)
iterable = node[type]
iterableLoc = iterable~getLoc()

idx = node.idx || path.scope.generateUidIdentifier("i")~t.clone()~atLoc(forKwdLoc)

// _i = 0
initDeclarations = [
t.variableDeclarator(idx, t.numericLiteral(0))
];
t.variableDeclarator(idx, t.numericLiteral(0)~atNode(idx))~atNode(idx)
]

// (_obj|_arr) = <expr>
refId = if (is("Identifier", path.node[type])):
path.node[type];
else:
// if the target of iteration is a complex expression,
// create a reference so it only evaluates once
const refName = type === "object" ? "obj" : "arr";
const refId = path.scope.generateUidIdentifier(refName);
initDeclarations.unshift(
t.variableDeclarator(
refId,
path.node[type]
)
)
refId
{ ref: iterableRef, declarator, isComplex } = undeclaredRef(path, iterable, type === "object" ? "obj" : "arr")
if isComplex: initDeclarations.unshift(declarator)

// _keys = Object.keys(_obj)
let keys;
if (type === "object") {
now keys = path.scope.generateUidIdentifier("keys");
now keys = path.scope.generateUidIdentifier("keys")~t.clone()~atLoc(iterableLoc);
initDeclarations.push(
t.variableDeclarator(keys,
t.callExpression(
t.memberExpression(
t.identifier("Object"),
t.identifier("keys")),
[refId]
)
)
);
t.identifier("Object")~atLoc(iterableLoc),
t.identifier("keys")~atLoc(iterableLoc)
)~atLoc(iterableLoc)
[iterableRef]
)~atLoc(iterableLoc)
)~atLoc(iterableLoc)
)
}

// _len = (_keys | _arr).length
len = path.scope.generateUidIdentifier("len")
len = path.scope.generateUidIdentifier("len")~t.clone()~atLoc(iterableLoc)
initDeclarations.push(
t.variableDeclarator(
len,
t.memberExpression(
type === "object" ? keys : refId,
t.identifier("length")
)
)
);
type === "object" ? keys : iterableRef,
t.identifier("length")~atLoc(iterableLoc)
)~atLoc(iterableLoc)
)~atLoc(iterableLoc)
)

const init = t.variableDeclaration("let", initDeclarations);
init = t.variableDeclaration("let", initDeclarations)~atLoc(forKwdLoc)
// _i < _len
const test = t.binaryExpression("<", idx, len);
test = t.binaryExpression("<", idx, len)~atLoc(forKwdLoc)
// _i++
const update = t.updateExpression("++", idx);
update = t.updateExpression("++", idx)~atLoc(forKwdLoc)

ensureBlockBody(path);
const innerDeclarations = [];
ensureBlockBody(path)
innerDeclarations = []
if (type === "object") {
// _k = _keys[_i]
const key = path.node.key || path.scope.generateUidIdentifier("k");
key = path.node.key || path.scope.generateUidIdentifier("k")~t.clone()~atLoc(iterableLoc)
innerDeclarations.push(
t.variableDeclaration("const", [
t.variableDeclarator(key, t.memberExpression(keys, idx, true))
])
t.variableDeclarator(key,
t.memberExpression(keys, idx, true)
)
])~allAtLoc(iterableLoc)
);

// val = _obj[_k]
Expand All @@ -77,10 +75,10 @@ generateForInIterator(path, type: "array" | "object") ->
t.variableDeclaration("const", [
t.variableDeclarator(
path.node.val,
t.memberExpression(refId, key, true)
t.memberExpression(iterableRef, key, true)
)
])
);
])~allAtLoc(iterableLoc)
)
}
} else {
// elem = _arr[_i]
Expand All @@ -89,18 +87,18 @@ generateForInIterator(path, type: "array" | "object") ->
t.variableDeclaration("const", [
t.variableDeclarator(
path.node.elem,
t.memberExpression(refId, idx, true)
t.memberExpression(iterableRef, idx, true)
)
])
);
])~allAtLoc(iterableLoc)
)
}
}

if (innerDeclarations.length > 0) {
path.get("body").unshiftContainer("body", innerDeclarations);
path.get("body").unshiftContainer("body", innerDeclarations)
}

t.forStatement(init, test, update, path.node.body)
t.forStatement(init, test, update, path.node.body)~atNode(node)

export transformForInArrayStatement(path) ->
path.replaceWith(generateForInIterator(path, "array"))
Expand Down
6 changes: 2 additions & 4 deletions test/fixtures/arrow-function-expressions/oneline/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
return 1;
});() => {
for (let _i = 0, _keys = Object.keys(obj), _len = _keys.length; _i < _len; _i++) {
const k = _keys[_i];
f(k);
const k = _keys[_i];f(k);
}
};() => {
throw new Error();
Expand All @@ -23,7 +22,6 @@ const f = () => {

() => {
for (let _i2 = 0, _keys2 = Object.keys(obj), _len2 = _keys2.length; _i2 < _len2; _i2++) {
const k = _keys2[_i2];
f(k);
const k = _keys2[_i2];f(k);
}
};func();
2 changes: 0 additions & 2 deletions test/fixtures/comprehensions/assignment-expr/expected.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
(() => {
const _arr = [];

for (let _arr2 = Array(10), i = 0, _len = _arr2.length; i < _len; i++) {
_arr.push(x = f(i));
}

return _arr;
})();
10 changes: 2 additions & 8 deletions test/fixtures/comprehensions/closure-nested-deep/expected.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
(() => {
const _arr = [];

for (let _arr2 = Array(10), i = 0, _len = _arr2.length; i < _len; i++) {
for (let j = 0, _len2 = a.length; j < _len2; j++) {
if (i < 5) {
function f() {
return (() => {
const _obj = {};

for (let _arr3 = Array(10), k = 0, _len3 = _arr3.length; k < _len3; k++) {
if (k > 7) {
_obj[k] = function g() {
Expand All @@ -16,16 +14,12 @@
};
};
}
}

return _obj;
}return _obj;
})();
}

_arr.push(f);
}
}
}

return _arr;
}return _arr;
})();
5 changes: 1 addition & 4 deletions test/fixtures/comprehensions/closure-nested/expected.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
(() => {
const _arr = [];

for (let _arr2 = Array(10), i = 0, _len = _arr2.length; i < _len; i++) {
function f() {
return function g() {
return i;
};
}
_arr.push(f);
}

return _arr;
}return _arr;
})();
5 changes: 1 addition & 4 deletions test/fixtures/comprehensions/closure/expected.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
(() => {
const _arr = [];

for (let _arr2 = Array(10), i = 0, _len = _arr2.length; i < _len; i++) {
function f() {
return i;
}
_arr.push(f);
}

return _arr;
}return _arr;
})();
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
const closures = (() => {
const _arr = [];

for (let _arr2 = Array(3), i = 0, _len = _arr2.length; i < _len; i++) {
const x = g(i);
function g(x) {
return x + 1;
}
_arr.push(g);
}

return _arr;
}return _arr;
})();
8 changes: 2 additions & 6 deletions test/fixtures/comprehensions/object-basic/expected.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
(() => {
const _obj = {};

for (let _i = 0, _len = arr.length; _i < _len; _i++) {
const x = arr[_i];
_obj[x] = f(x);
}

return _obj;
const x = arr[_i];_obj[x] = f(x);
}return _obj;
})();
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
(() => {
const _obj = {};

for (let _arr = Array(3), i = 0, _len = _arr.length; i < _len; i++) {
const x = g(i);

_obj[i] = function g(x) {
return x + 1;
};
}

return _obj;
}return _obj;
})();
8 changes: 2 additions & 6 deletions test/fixtures/comprehensions/object-no-parens/expected.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
(() => {
const _obj = {};

for (let _i = 0, _len = arr.length; _i < _len; _i++) {
const x = arr[_i];
_obj[x] = f(x);
}

return _obj;
const x = arr[_i];_obj[x] = f(x);
}return _obj;
})();
7 changes: 2 additions & 5 deletions test/fixtures/comprehensions/variable-decl/expected.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
(() => {
const _arr = [];

for (let _arr2 = Array(10), i = 0, _len = _arr2.length; i < _len; i++) {
const x = f(i);

_arr.push(x);
}

return _arr;
})();
}return _arr;
})();
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
(() => {
const _arr = [];

for (let _arr2 = Array(10), i = 0, _len = _arr2.length; i < _len; i++) {
_arr.push(x = f(i));
}

return _arr;
})();

This file was deleted.

10 changes: 0 additions & 10 deletions test/fixtures/enhanced-comprehensions/basic-object/expected.js

This file was deleted.

6 changes: 1 addition & 5 deletions test/fixtures/enhanced-comprehensions/basic/expected.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
(() => {
const _arr = [];

for (let _i = 0, _len = y.length; _i < _len; _i++) {
const x = y[_i];

_arr.push(x);
}

return _arr;
}return _arr;
})();
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
(() => {
const _arr = [];

for (let _arr2 = Array(10), i = 0, _len = _arr2.length; i < _len; i++) {
for (let j = 0, _len2 = a.length; j < _len2; j++) {
if (i < 5) {
function f() {
return (() => {
const _obj = {};

for (let _arr3 = Array(10), k = 0, _len3 = _arr3.length; k < _len3; k++) {
if (k > 7) {
_obj[k] = function g() {
Expand All @@ -16,16 +14,12 @@
};
};
}
}

return _obj;
}return _obj;
})();
}

_arr.push(f);
}
}
}

return _arr;
}return _arr;
})();
Loading

0 comments on commit ee93ac2

Please sign in to comment.