Skip to content

Commit

Permalink
Variable initialization do not eval last expression
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentDamour committed Jul 9, 2016
1 parent 4c01976 commit ff49011
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/selectors/parse_expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const DELIMITER_MAP = {
']': '[',
'`': '`'
};
const VARIABLE_IDENTIFIERS = ['var', 'const', 'let'];

const findDelimiters = ({ column }, lineContents) =>
_.intersection(_.takeRight(lineContents, lineContents.length - column), OPEN_DELIMITERS).length
Expand All @@ -23,6 +24,7 @@ const parseExpressions = (code) => {

const parens = { '(': 0, '{': 0, '[': 0 };
let wasOpen = false;
let isVariableInitialization = false;
const exp = _.reduce(tokenized, (expressions, { value, loc: { end } }, index) => {
const lineNumber = end.line;
const lineContents = codeByLine[lineNumber - 1];
Expand All @@ -40,6 +42,10 @@ const parseExpressions = (code) => {
parens[DELIMITER_MAP[value]] -= 1;
}

if (VARIABLE_IDENTIFIERS.includes(value)) {
isVariableInitialization = true;
}

if (!lineHasMoreDelimiters && wasOpen && _.every(parens, count => count === 0)) {
wasOpen = false;
expressions[lineNumber] = _.take(codeByLine, lineNumber).join('\n');
Expand All @@ -48,7 +54,11 @@ const parseExpressions = (code) => {
}

if (!lineHasMoreDelimiters && _.every(parens, count => count === 0)) {
expressions[lineNumber] = _.take(codeByLine, lineNumber).join('\n');
if (!isVariableInitialization) {
expressions[lineNumber] = _.take(codeByLine, lineNumber).join('\n');
} else if(endOfLine || value === ';') {
isVariableInitialization = false;
}

return expressions;
}
Expand Down

0 comments on commit ff49011

Please sign in to comment.