Permalink
Browse files

FIX: Edge-case means we aren't quite ready to move initilizers into '…

…lets' just yet

ALL 1391 PASSING
  • Loading branch information...
shanebdavis committed Jan 10, 2019
1 parent e9c0c54 commit 23637f0e764f7676e75fbd59af3439d7937df65f
@@ -59,8 +59,7 @@ upToButNotEol = /[^\n]*/y

while (match = matchBlock source, offset)
endOffset = offset
{matchLength} = match
offset += matchLength
offset += match.matchLength

expressionSource = source.slice originalOffset, endOffset
parentNode.subparse
@@ -116,45 +116,53 @@ UniqueIdentifierHandle = &UniqueIdentifierHandle
getSourceNodeForAutoLetsWithStatements: (statementsStn, toSourceNodeOptions) ->
toSourceNodeOptions extract? returnAction

if (statementsStn extract statements)[0]?.type == :Assignment && @haveAutoLets
autoLetIdentifiers = object @requiredIdentifierLets with true
assignedAutoLetStns = []
find statement, i in statements
statement extract type, propName
if type == :Assignment && autoLetIdentifiers[propName] && (!returnAction || i < statements.length - 1)
assignedAutoLetStns.push statement
autoLetIdentifiers[propName] = false
false
else true # 'found' a non-match; stop

if 0 < numAssignsConsumed = assignedAutoLetStns.length
letSourceNodes =
array v, k in autoLetIdentifiers when v with k into
array v in assignedAutoLetStns with v.toSourceNode()

[]
"let "

array letSourceNode, i in letSourceNodes into out = []
out.push ', ' if i > 0
letSourceNode

if statements.length - numAssignsConsumed > 0
[]
"; "
statementsStn.toSourceNodeWithCustomChildren
statements.slice numAssignsConsumed, statementsStn.length
toSourceNodeOptions
else
";"

else
[]
@autoLetsForSourceNode
statementsStn.toSourceNode toSourceNodeOptions
##
This isn't quite ready for prime-time yet. The problem is
we need to detect if the rValue of the assign has dependencies on
variables we haven't let yet.

Ex: while a do b = c; c = 0
Bad code, I know, but it should generate legal JS:
while (a) {let b, c; b = c; c = 0;}

Another special case problem: while a do b = c = 0

if (statementsStn extract statements)[0]?.type == :Assignment && @haveAutoLets
autoLetIdentifiers = object @requiredIdentifierLets with true
assignedAutoLetStns = []
find statement, i in statements
statement extract type, propName
if type == :Assignment && autoLetIdentifiers[propName] && (!returnAction || i < statements.length - 1)
assignedAutoLetStns.push statement
autoLetIdentifiers[propName] = false
false
else true # 'found' a non-match; stop

if false # 0 < numAssignsConsumed = assignedAutoLetStns?.length
# letSourceNodes =
# array v, k in autoLetIdentifiers when v with k into
# array v in assignedAutoLetStns with v.toSourceNode()

# []
# "let "

# array letSourceNode, i in letSourceNodes into out = []
# out.push ', ' if i > 0
# letSourceNode

# if statements.length - numAssignsConsumed > 0
# []
# "; "
# statementsStn.toSourceNodeWithCustomChildren
# statements.slice numAssignsConsumed, statementsStn.length
# toSourceNodeOptions
# else
# ";"

else
statementsStn.toSourceNode toSourceNodeOptions
[]
@autoLetsForSourceNode
statementsStn.toSourceNode toSourceNodeOptions


@getter
@@ -66,7 +66,6 @@ Caf.defMod(module, () => {
match,
m,
endOffset,
matchLength,
expressionSource;
({ nextOffset, source } = parentNode);
offset = nextOffset;
@@ -78,8 +77,7 @@ Caf.defMod(module, () => {
(() => {
while ((match = matchBlock(source, offset))) {
endOffset = offset;
({ matchLength } = match);
offset += matchLength;
offset += match.matchLength;
}
})(),
(expressionSource = source.slice(originalOffset, endOffset)),
@@ -183,75 +183,16 @@ Caf.defMod(module, () => {
statementsStn,
toSourceNodeOptions
) {
let returnAction,
statements,
autoLetIdentifiers,
assignedAutoLetStns,
numAssignsConsumed,
letSourceNodes,
out,
base;
let returnAction;
if (Caf.exists(toSourceNodeOptions)) {
returnAction = toSourceNodeOptions.returnAction;
}
return (Caf.exists(
(base = (statements = statementsStn.statements)[0])
) && base.type) === "Assignment" && this.haveAutoLets
? ((autoLetIdentifiers = Caf.object(
this.requiredIdentifierLets,
() => true
)),
(assignedAutoLetStns = []),
Caf.find(statements, (statement, i) => {
let type, propName;
type = statement.type;
propName = statement.propName;
return type === "Assignment" &&
autoLetIdentifiers[propName] &&
(!returnAction || i < statements.length - 1)
? (assignedAutoLetStns.push(statement),
(autoLetIdentifiers[propName] = false),
false)
: true;
}),
0 < (numAssignsConsumed = assignedAutoLetStns.length)
? ((letSourceNodes = Caf.array(
autoLetIdentifiers,
(v, k) => k,
(v, k) => v,
Caf.array(assignedAutoLetStns, v => v.toSourceNode())
)),
[
"let ",
Caf.array(
letSourceNodes,
(letSourceNode, i) => {
if (i > 0) {
out.push(", ");
}
return letSourceNode;
},
null,
(out = [])
),
statements.length - numAssignsConsumed > 0
? [
"; ",
statementsStn.toSourceNodeWithCustomChildren(
statements.slice(
numAssignsConsumed,
statementsStn.length
),
toSourceNodeOptions
)
]
: ";"
])
: [
this.autoLetsForSourceNode,
statementsStn.toSourceNode(toSourceNodeOptions)
])
: statementsStn.toSourceNode(toSourceNodeOptions);
return false
? undefined
: [
this.autoLetsForSourceNode,
statementsStn.toSourceNode(toSourceNodeOptions)
];
};
this.getter({
autoLetsForSourceNode: function() {
Oops, something went wrong.

0 comments on commit 23637f0

Please sign in to comment.