Skip to content

Commit a23b22e

Browse files
defccyyx990803
authored andcommitted
Improve flow type for codegen module (vuejs#4723)
* update flow * specify any type to ASTNode
1 parent c660917 commit a23b22e

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

src/compiler/codegen/events.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const fnExpRE = /^\s*([\w$_]+|\([^)]*?\))\s*=>|^function\s*\(/
44
const simplePathRE = /^\s*[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*|\['.*?']|\[".*?"]|\[\d+]|\[[A-Za-z_$][\w$]*])*\s*$/
55

66
// keyCode aliases
7-
const keyCodes: { [k: any]: number | [number, number] } = {
7+
const keyCodes: { [key: string]: number | Array<number> } = {
88
esc: 27,
99
tab: 9,
1010
enter: 13,
@@ -16,7 +16,7 @@ const keyCodes: { [k: any]: number | [number, number] } = {
1616
'delete': [8, 46]
1717
}
1818

19-
const modifierCode: { [k: string]: string } = {
19+
const modifierCode: { [key: string]: string } = {
2020
stop: '$event.stopPropagation();',
2121
prevent: '$event.preventDefault();',
2222
self: 'if($event.target !== $event.currentTarget)return;',
@@ -70,7 +70,7 @@ function genKeyFilter (keys: Array<string>): string {
7070
return `if(${keys.map(genFilterCode).join('&&')})return;`
7171
}
7272

73-
function genFilterCode (key: number | string): string {
73+
function genFilterCode (key: string): string {
7474
const keyVal = parseInt(key, 10)
7575
if (keyVal) {
7676
return `$event.keyCode!==${keyVal}`

src/compiler/codegen/index.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ function genInlineTemplate (el: ASTElement): ?string {
268268
}
269269
}
270270

271-
function genScopedSlots (slots) {
271+
function genScopedSlots (slots: { [key: string]: ASTElement }): string {
272272
return `scopedSlots:{${
273273
Object.keys(slots).map(key => genScopedSlot(key, slots[key])).join(',')
274274
}}`
@@ -306,32 +306,35 @@ function genChildren (el: ASTElement, checkSkip?: boolean): string | void {
306306
// 0: no normalization needed
307307
// 1: simple normalization needed (possible 1-level deep nested array)
308308
// 2: full normalization needed
309-
function getNormalizationType (children): number {
309+
function getNormalizationType (children: Array<ASTNode>): number {
310310
let res = 0
311311
for (let i = 0; i < children.length; i++) {
312-
const el: any = children[i]
312+
const el: ASTNode = children[i]
313+
if (el.type !== 1) {
314+
continue
315+
}
313316
if (needsNormalization(el) ||
314-
(el.if && el.ifConditions.some(c => needsNormalization(c.block)))) {
317+
(el.ifConditions && el.ifConditions.some(c => needsNormalization(c.block)))) {
315318
res = 2
316319
break
317320
}
318321
if (maybeComponent(el) ||
319-
(el.if && el.ifConditions.some(c => maybeComponent(c.block)))) {
322+
(el.ifConditions && el.ifConditions.some(c => maybeComponent(c.block)))) {
320323
res = 1
321324
}
322325
}
323326
return res
324327
}
325328

326-
function needsNormalization (el: ASTElement) {
327-
return el.for || el.tag === 'template' || el.tag === 'slot'
329+
function needsNormalization (el: ASTElement): boolean {
330+
return el.for !== undefined || el.tag === 'template' || el.tag === 'slot'
328331
}
329332

330-
function maybeComponent (el: ASTElement) {
331-
return el.type === 1 && !isPlatformReservedTag(el.tag)
333+
function maybeComponent (el: ASTElement): boolean {
334+
return !isPlatformReservedTag(el.tag)
332335
}
333336

334-
function genNode (node: ASTNode) {
337+
function genNode (node: ASTNode): string {
335338
if (node.type === 1) {
336339
return genElement(node)
337340
} else {
@@ -365,7 +368,7 @@ function genSlot (el: ASTElement): string {
365368
}
366369
367370
// componentName is el.component, take it as argument to shun flow's pessimistic refinement
368-
function genComponent (componentName, el): string {
371+
function genComponent (componentName: string, el: ASTElement): string {
369372
const children = el.inlineTemplate ? null : genChildren(el, true)
370373
return `_c(${componentName},${genData(el)}${
371374
children ? `,${children}` : ''

0 commit comments

Comments
 (0)