Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

error-detector.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* @flow */
  2. import { dirRE, onRE } from './parser/index'
  3. type Range = { start?: number, end?: number };
  4. // these keywords should not appear inside expressions, but operators like
  5. // typeof, instanceof and in are allowed
  6. const prohibitedKeywordRE = new RegExp('\\b' + (
  7. 'do,if,for,let,new,try,var,case,else,with,await,break,catch,class,const,' +
  8. 'super,throw,while,yield,delete,export,import,return,switch,default,' +
  9. 'extends,finally,continue,debugger,function,arguments'
  10. ).split(',').join('\\b|\\b') + '\\b')
  11. // these unary operators should not be used as property/method names
  12. const unaryOperatorsRE = new RegExp('\\b' + (
  13. 'delete,typeof,void'
  14. ).split(',').join('\\s*\\([^\\)]*\\)|\\b') + '\\s*\\([^\\)]*\\)')
  15. // strip strings in expressions
  16. const stripStringRE = /'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"|`(?:[^`\\]|\\.)*\$\{|\}(?:[^`\\]|\\.)*`|`(?:[^`\\]|\\.)*`/g
  17. // detect problematic expressions in a template
  18. export function detectErrors (ast: ?ASTNode, warn: Function) {
  19. if (ast) {
  20. checkNode(ast, warn)
  21. }
  22. }
  23. function checkNode (node: ASTNode, warn: Function) {
  24. if (node.type === 1) {
  25. for (const name in node.attrsMap) {
  26. if (dirRE.test(name)) {
  27. const value = node.attrsMap[name]
  28. if (value) {
  29. const range = node.rawAttrsMap[name]
  30. if (name === 'v-for') {
  31. checkFor(node, `v-for="${value}"`, warn, range)
  32. } else if (onRE.test(name)) {
  33. checkEvent(value, `${name}="${value}"`, warn, range)
  34. } else {
  35. checkExpression(value, `${name}="${value}"`, warn, range)
  36. }
  37. }
  38. }
  39. }
  40. if (node.children) {
  41. for (let i = 0; i < node.children.length; i++) {
  42. checkNode(node.children[i], warn)
  43. }
  44. }
  45. } else if (node.type === 2) {
  46. checkExpression(node.expression, node.text, warn, node)
  47. }
  48. }
  49. function checkEvent (exp: string, text: string, warn: Function, range?: Range) {
  50. const stipped = exp.replace(stripStringRE, '')
  51. const keywordMatch: any = stipped.match(unaryOperatorsRE)
  52. if (keywordMatch && stipped.charAt(keywordMatch.index - 1) !== '$') {
  53. warn(
  54. `avoid using JavaScript unary operator as property name: ` +
  55. `"${keywordMatch[0]}" in expression ${text.trim()}`,
  56. range
  57. )
  58. }
  59. checkExpression(exp, text, warn, range)
  60. }
  61. function checkFor (node: ASTElement, text: string, warn: Function, range?: Range) {
  62. checkExpression(node.for || '', text, warn, range)
  63. checkIdentifier(node.alias, 'v-for alias', text, warn, range)
  64. checkIdentifier(node.iterator1, 'v-for iterator', text, warn, range)
  65. checkIdentifier(node.iterator2, 'v-for iterator', text, warn, range)
  66. }
  67. function checkIdentifier (
  68. ident: ?string,
  69. type: string,
  70. text: string,
  71. warn: Function,
  72. range?: Range
  73. ) {
  74. if (typeof ident === 'string') {
  75. try {
  76. new Function(`var ${ident}=_`)
  77. } catch (e) {
  78. warn(`invalid ${type} "${ident}" in expression: ${text.trim()}`, range)
  79. }
  80. }
  81. }
  82. function checkExpression (exp: string, text: string, warn: Function, range?: Range) {
  83. try {
  84. new Function(`return ${exp}`)
  85. } catch (e) {
  86. const keywordMatch = exp.replace(stripStringRE, '').match(prohibitedKeywordRE)
  87. if (keywordMatch) {
  88. warn(
  89. `avoid using JavaScript keyword as property name: ` +
  90. `"${keywordMatch[0]}"\n Raw expression: ${text.trim()}`,
  91. range
  92. )
  93. } else {
  94. warn(
  95. `invalid expression: ${e.message} in\n\n` +
  96. ` ${exp}\n\n` +
  97. ` Raw expression: ${text.trim()}\n`,
  98. range
  99. )
  100. }
  101. }
  102. }