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 8, 2016
1 parent 4c01976 commit f786a30
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions 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,6 +54,14 @@ const parseExpressions = (code) => {
}

if (!lineHasMoreDelimiters && _.every(parens, count => count === 0)) {
if (isVariableInitialization) {
if (endOfLine || value === ';') {
isVariableInitialization = false;
}

return expressions;
}

expressions[lineNumber] = _.take(codeByLine, lineNumber).join('\n');

return expressions;
Expand Down

0 comments on commit f786a30

Please sign in to comment.