/* @flow */

import { dirRE } from './parser/index'

// operators like typeof, instanceof and in are allowed
const prohibitedKeywordRE = new RegExp('\\b' + (
  'do,if,for,let,new,try,var,case,else,with,await,break,catch,class,const,' +
  'super,throw,while,yield,delete,export,import,return,switch,default,' +
  'extends,finally,continue,debugger,function,arguments'
).split(',').join('\\b|\\b') + '\\b')
// check valid identifier for v-for
const identRE = /[A-Za-z_$][\w$]*/

// detect problematic expressions in a template
export function detectErrors (ast: ?ASTNode): Array<string> {
  const errors: Array<string> = []
  if (ast) {
    checkNode(ast, errors)
  }
  return errors
}

function checkNode (node: ASTNode, errors: Array<string>) {
  if (node.type === 1) {
    for (const name in node.attrsMap) {
      if (dirRE.test(name)) {
        const value = node.attrsMap[name]
        if (value) {
          if (name === 'v-for') {
            checkFor(node, `v-for="${value}"`, errors)
          } else {
            checkExpression(value, `${name}="${value}"`, errors)
          }
        }
      }
    }
    if (node.children) {
      for (let i = 0; i < node.children.length; i++) {
        checkNode(node.children[i], errors)
      }
    }
  } else if (node.type === 2) {
    checkExpression(node.expression, node.text, errors)
  }
}

function checkFor (node: ASTElement, text: string, errors: Array<string>) {
  checkExpression(node.for || '', text, errors)
  checkIdentifier(node.alias, 'v-for alias', text, errors)
  checkIdentifier(node.iterator1, 'v-for iterator', text, errors)
  checkIdentifier(node.iterator2, 'v-for iterator', text, errors)
}

function checkIdentifier (ident: ?string, type: string, text: string, errors: Array<string>) {
  if (typeof ident === 'string' && !identRE.test(ident)) {
    errors.push(`- invalid ${type} "${ident}" in expression: ${text}`)
  }
}

function checkExpression (exp: string, text: string, errors: Array<string>) {
  exp = stripToString(exp)
  const keywordMatch = exp.match(prohibitedKeywordRE)
  if (keywordMatch) {
    errors.push(
      `- avoid using JavaScript keyword as property name: ` +
      `"${keywordMatch[0]}" in expression ${text}`
    )
  } else {
    try {
      new Function(`return ${exp}`)
    } catch (e) {
      errors.push(`- invalid expression: ${text}`)
    }
  }
}

function stripToString (exp) {
  return exp.replace(/^_s\((.*)\)$/, '$1')
}

@@ -468,16 +468,167 @@
 %5D%5B%5Cw$%5D*/
+%0A// strip strings in expressions%0Aconst stripStringRE = /'(?:%5B%5E'%5C%5C%5D%7C%5C%5C.)*'%7C%22(?:%5B%5E%22%5C%5C%5D%7C%5C%5C.)*%22%7C%60(?:%5B%5E%60%5C%5C%5D%7C%5C%5C.)*%5C$%5C%7B%7C%5C%7D(?:%5B%5E%60%5C%5C%5D%7C%5C%5C.)*%60%7C%60(?:%5B%5E%60%5C%5C%5D%7C%5C%5C.)*%60/g
 %0A%0A// det
@@ -2123,33 +2123,66 @@
 %7B%0A  
-exp = stripToString(
+try %7B%0A    new Function(%60return $%7B
 exp
+%7D%60
 )%0A
+  %7D catch (e) %7B%0A  
   co
@@ -2204,16 +2204,43 @@
 h = exp.
+replace(stripStringRE, '').
 match(pr
@@ -2258,16 +2258,18 @@
 wordRE)%0A
+  
   if (ke
@@ -2278,32 +2278,34 @@
 ordMatch) %7B%0A    
+  
 errors.push(%0A   
@@ -2307,16 +2307,18 @@
 (%0A      
+ 
+ 
 %60- avoid
@@ -2371,16 +2371,18 @@
 +%0A      
+  
 %60%22$%7Bkeyw
@@ -2426,221 +2426,84 @@
     
-)%0A  %7D else %7B%0A    try %7B%0A      new Function(%60return $%7Bexp%7D%60)%0A    %7D catch (e) %7B%0A      errors.push(%60- invalid expression: $%7Btext%7D%60)%0A    %7D%0A  %7D%0A%7D%0A%0Afunction stripToString (exp) %7B%0A  return exp.replace(/%5E_s%5C((.*)%5C)$/, '$1')
+  )%0A    %7D else %7B%0A      errors.push(%60- invalid expression: $%7Btext%7D%60)%0A    %7D%0A  %7D
 %0A%7D%0A

fix error detection on string interpolations (fix #3137)