Skip to content

Commit af9fc2b

Browse files
committed
chore: move to typescript
1 parent 0d223a3 commit af9fc2b

File tree

214 files changed

+5189
-3508
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

214 files changed

+5189
-3508
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@
136136
"serialize-javascript": "^3.1.0",
137137
"shelljs": "^0.8.1",
138138
"terser": "^3.10.2",
139-
"typescript": "^3.6.4",
139+
"typescript": "^4.2.2",
140140
"webpack": "~4.28.4",
141141
"weex-js-runtime": "^0.23.6",
142142
"weex-styler": "^0.3.0",
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@
22

33
const range = 2
44

5-
export function generateCodeFrame (
5+
export function generateCodeFrame(
66
source: string,
77
start: number = 0,
88
end: number = source.length
99
): string {
1010
const lines = source.split(/\r?\n/)
1111
let count = 0
12-
const res = []
12+
const res: string[] = []
1313
for (let i = 0; i < lines.length; i++) {
1414
count += lines[i].length + 1
1515
if (count >= start) {
1616
for (let j = i - range; j <= i + range || end > count; j++) {
1717
if (j < 0 || j >= lines.length) continue
18-
res.push(`${j + 1}${repeat(` `, 3 - String(j + 1).length)}| ${lines[j]}`)
18+
res.push(
19+
`${j + 1}${repeat(` `, 3 - String(j + 1).length)}| ${lines[j]}`
20+
)
1921
const lineLength = lines[j].length
2022
if (j === i) {
2123
// push underline
@@ -36,10 +38,11 @@ export function generateCodeFrame (
3638
return res.join('\n')
3739
}
3840

39-
function repeat (str, n) {
41+
function repeat(str, n) {
4042
let result = ''
4143
if (n > 0) {
42-
while (true) { // eslint-disable-line
44+
while (true) {
45+
// eslint-disable-line
4346
if (n & 1) result += str
4447
n >>>= 1
4548
if (n <= 0) break
Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* @flow */
22

3+
import { __WEEX__ } from 'typescript/weex'
4+
35
const fnExpRE = /^([\w$_]+|\([^)]*?\))\s*=>|^function(?:\s+[\w$]+)?\s*\(/
46
const fnInvokeRE = /\([^)]*?\);*$/
57
const simplePathRE = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*|\['[^']*?']|\["[^"]*?"]|\[\d+]|\[[A-Za-z_$][\w$]*])*$/
@@ -14,7 +16,7 @@ const keyCodes: { [key: string]: number | Array<number> } = {
1416
left: 37,
1517
right: 39,
1618
down: 40,
17-
'delete': [8, 46]
19+
delete: [8, 46],
1820
}
1921

2022
// KeyboardEvent.key aliases
@@ -31,13 +33,13 @@ const keyNames: { [key: string]: string | Array<string> } = {
3133
right: ['Right', 'ArrowRight'],
3234
down: ['Down', 'ArrowDown'],
3335
// #9112: IE11 uses `Del` for Delete key name.
34-
'delete': ['Backspace', 'Delete', 'Del']
36+
delete: ['Backspace', 'Delete', 'Del'],
3537
}
3638

3739
// #4868: modifiers that prevent the execution of the listener
3840
// need to explicitly return null so that we can determine whether to remove
3941
// the listener for .once
40-
const genGuard = condition => `if(${condition})return null;`
42+
const genGuard = (condition) => `if(${condition})return null;`
4143

4244
const modifierCode: { [key: string]: string } = {
4345
stop: '$event.stopPropagation();',
@@ -49,10 +51,10 @@ const modifierCode: { [key: string]: string } = {
4951
meta: genGuard(`!$event.metaKey`),
5052
left: genGuard(`'button' in $event && $event.button !== 0`),
5153
middle: genGuard(`'button' in $event && $event.button !== 1`),
52-
right: genGuard(`'button' in $event && $event.button !== 2`)
54+
right: genGuard(`'button' in $event && $event.button !== 2`),
5355
}
5456

55-
export function genHandlers (
57+
export function genHandlers(
5658
events: ASTElementHandlers,
5759
isNative: boolean
5860
): string {
@@ -61,6 +63,7 @@ export function genHandlers (
6163
let dynamicHandlers = ``
6264
for (const name in events) {
6365
const handlerCode = genHandler(events[name])
66+
//@ts-expect-error
6467
if (events[name] && events[name].dynamic) {
6568
dynamicHandlers += `${name},${handlerCode},`
6669
} else {
@@ -77,34 +80,42 @@ export function genHandlers (
7780

7881
// Generate handler code with binding params on Weex
7982
/* istanbul ignore next */
80-
function genWeexHandler (params: Array<any>, handlerCode: string) {
83+
function genWeexHandler(params: Array<any>, handlerCode: string) {
8184
let innerHandlerCode = handlerCode
82-
const exps = params.filter(exp => simplePathRE.test(exp) && exp !== '$event')
83-
const bindings = exps.map(exp => ({ '@binding': exp }))
85+
const exps = params.filter(
86+
(exp) => simplePathRE.test(exp) && exp !== '$event'
87+
)
88+
const bindings = exps.map((exp) => ({ '@binding': exp }))
8489
const args = exps.map((exp, i) => {
8590
const key = `$_${i + 1}`
8691
innerHandlerCode = innerHandlerCode.replace(exp, key)
8792
return key
8893
})
8994
args.push('$event')
90-
return '{\n' +
95+
return (
96+
'{\n' +
9197
`handler:function(${args.join(',')}){${innerHandlerCode}},\n` +
9298
`params:${JSON.stringify(bindings)}\n` +
9399
'}'
100+
)
94101
}
95102

96-
function genHandler (handler: ASTElementHandler | Array<ASTElementHandler>): string {
103+
function genHandler(
104+
handler: ASTElementHandler | Array<ASTElementHandler>
105+
): string {
97106
if (!handler) {
98107
return 'function(){}'
99108
}
100109

101110
if (Array.isArray(handler)) {
102-
return `[${handler.map(handler => genHandler(handler)).join(',')}]`
111+
return `[${handler.map((handler) => genHandler(handler)).join(',')}]`
103112
}
104113

105114
const isMethodPath = simplePathRE.test(handler.value)
106115
const isFunctionExpression = fnExpRE.test(handler.value)
107-
const isFunctionInvocation = simplePathRE.test(handler.value.replace(fnInvokeRE, ''))
116+
const isFunctionInvocation = simplePathRE.test(
117+
handler.value.replace(fnInvokeRE, '')
118+
)
108119

109120
if (!handler.modifiers) {
110121
if (isMethodPath || isFunctionExpression) {
@@ -120,7 +131,7 @@ function genHandler (handler: ASTElementHandler | Array<ASTElementHandler>): str
120131
} else {
121132
let code = ''
122133
let genModifierCode = ''
123-
const keys = []
134+
const keys: string[] = []
124135
for (const key in handler.modifiers) {
125136
if (modifierCode[key]) {
126137
genModifierCode += modifierCode[key]
@@ -129,11 +140,11 @@ function genHandler (handler: ASTElementHandler | Array<ASTElementHandler>): str
129140
keys.push(key)
130141
}
131142
} else if (key === 'exact') {
132-
const modifiers: ASTModifiers = (handler.modifiers: any)
143+
const modifiers = handler.modifiers
133144
genModifierCode += genGuard(
134145
['ctrl', 'shift', 'alt', 'meta']
135-
.filter(keyModifier => !modifiers[keyModifier])
136-
.map(keyModifier => `$event.${keyModifier}Key`)
146+
.filter((keyModifier) => !modifiers[keyModifier])
147+
.map((keyModifier) => `$event.${keyModifier}Key`)
137148
.join('||')
138149
)
139150
} else {
@@ -150,10 +161,10 @@ function genHandler (handler: ASTElementHandler | Array<ASTElementHandler>): str
150161
const handlerCode = isMethodPath
151162
? `return ${handler.value}.apply(null, arguments)`
152163
: isFunctionExpression
153-
? `return (${handler.value}).apply(null, arguments)`
154-
: isFunctionInvocation
155-
? `return ${handler.value}`
156-
: handler.value
164+
? `return (${handler.value}).apply(null, arguments)`
165+
: isFunctionInvocation
166+
? `return ${handler.value}`
167+
: handler.value
157168
/* istanbul ignore if */
158169
if (__WEEX__ && handler.params) {
159170
return genWeexHandler(handler.params, code + handlerCode)
@@ -162,7 +173,7 @@ function genHandler (handler: ASTElementHandler | Array<ASTElementHandler>): str
162173
}
163174
}
164175

165-
function genKeyFilter (keys: Array<string>): string {
176+
function genKeyFilter(keys: Array<string>): string {
166177
return (
167178
// make sure the key filters only apply to KeyboardEvents
168179
// #9441: can't use 'keyCode' in $event because Chrome autofill fires fake
@@ -172,7 +183,7 @@ function genKeyFilter (keys: Array<string>): string {
172183
)
173184
}
174185

175-
function genFilterCode (key: string): string {
186+
function genFilterCode(key: string): string {
176187
const keyVal = parseInt(key, 10)
177188
if (keyVal) {
178189
return `$event.keyCode!==${keyVal}`

0 commit comments

Comments
 (0)