Просмотр исходного кода

Improve flow type for codegen module (#4723)

* update flow

* specify any type to ASTNode
chengchao 9 лет назад
Родитель
Сommit
a23b22e91d
2 измененных файлов с 17 добавлено и 14 удалено
  1. 3 3
      src/compiler/codegen/events.js
  2. 14 11
      src/compiler/codegen/index.js

+ 3 - 3
src/compiler/codegen/events.js

@@ -4,7 +4,7 @@ const fnExpRE = /^\s*([\w$_]+|\([^)]*?\))\s*=>|^function\s*\(/
 const simplePathRE = /^\s*[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*|\['.*?']|\[".*?"]|\[\d+]|\[[A-Za-z_$][\w$]*])*\s*$/
 
 // keyCode aliases
-const keyCodes: { [k: any]: number | [number, number] } = {
+const keyCodes: { [key: string]: number | Array<number> } = {
   esc: 27,
   tab: 9,
   enter: 13,
@@ -16,7 +16,7 @@ const keyCodes: { [k: any]: number | [number, number] } = {
   'delete': [8, 46]
 }
 
-const modifierCode: { [k: string]: string } = {
+const modifierCode: { [key: string]: string } = {
   stop: '$event.stopPropagation();',
   prevent: '$event.preventDefault();',
   self: 'if($event.target !== $event.currentTarget)return;',
@@ -70,7 +70,7 @@ function genKeyFilter (keys: Array<string>): string {
   return `if(${keys.map(genFilterCode).join('&&')})return;`
 }
 
-function genFilterCode (key: number | string): string {
+function genFilterCode (key: string): string {
   const keyVal = parseInt(key, 10)
   if (keyVal) {
     return `$event.keyCode!==${keyVal}`

+ 14 - 11
src/compiler/codegen/index.js

@@ -268,7 +268,7 @@ function genInlineTemplate (el: ASTElement): ?string {
   }
 }
 
-function genScopedSlots (slots) {
+function genScopedSlots (slots: { [key: string]: ASTElement }): string {
   return `scopedSlots:{${
     Object.keys(slots).map(key => genScopedSlot(key, slots[key])).join(',')
   }}`
@@ -306,32 +306,35 @@ function genChildren (el: ASTElement, checkSkip?: boolean): string | void {
 // 0: no normalization needed
 // 1: simple normalization needed (possible 1-level deep nested array)
 // 2: full normalization needed
-function getNormalizationType (children): number {
+function getNormalizationType (children: Array<ASTNode>): number {
   let res = 0
   for (let i = 0; i < children.length; i++) {
-    const el: any = children[i]
+    const el: ASTNode = children[i]
+    if (el.type !== 1) {
+      continue
+    }
     if (needsNormalization(el) ||
-        (el.if && el.ifConditions.some(c => needsNormalization(c.block)))) {
+        (el.ifConditions && el.ifConditions.some(c => needsNormalization(c.block)))) {
       res = 2
       break
     }
     if (maybeComponent(el) ||
-        (el.if && el.ifConditions.some(c => maybeComponent(c.block)))) {
+        (el.ifConditions && el.ifConditions.some(c => maybeComponent(c.block)))) {
       res = 1
     }
   }
   return res
 }
 
-function needsNormalization (el: ASTElement) {
-  return el.for || el.tag === 'template' || el.tag === 'slot'
+function needsNormalization (el: ASTElement): boolean {
+  return el.for !== undefined || el.tag === 'template' || el.tag === 'slot'
 }
 
-function maybeComponent (el: ASTElement) {
-  return el.type === 1 && !isPlatformReservedTag(el.tag)
+function maybeComponent (el: ASTElement): boolean {
+  return !isPlatformReservedTag(el.tag)
 }
 
-function genNode (node: ASTNode) {
+function genNode (node: ASTNode): string {
   if (node.type === 1) {
     return genElement(node)
   } else {
@@ -365,7 +368,7 @@ function genSlot (el: ASTElement): string {
 }
 
 // componentName is el.component, take it as argument to shun flow's pessimistic refinement
-function genComponent (componentName, el): string {
+function genComponent (componentName: string, el: ASTElement): string {
   const children = el.inlineTemplate ? null : genChildren(el, true)
   return `_c(${componentName},${genData(el)}${
     children ? `,${children}` : ''