Skip to content

Commit

Permalink
chore: move to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
pikax committed Apr 3, 2021
1 parent 0d223a3 commit af9fc2b
Show file tree
Hide file tree
Showing 214 changed files with 5,189 additions and 3,508 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
"serialize-javascript": "^3.1.0",
"shelljs": "^0.8.1",
"terser": "^3.10.2",
"typescript": "^3.6.4",
"typescript": "^4.2.2",
"webpack": "~4.28.4",
"weex-js-runtime": "^0.23.6",
"weex-styler": "^0.3.0",
Expand Down
13 changes: 8 additions & 5 deletions src/compiler/codeframe.js → src/compiler/codeframe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@

const range = 2

export function generateCodeFrame (
export function generateCodeFrame(
source: string,
start: number = 0,
end: number = source.length
): string {
const lines = source.split(/\r?\n/)
let count = 0
const res = []
const res: string[] = []
for (let i = 0; i < lines.length; i++) {
count += lines[i].length + 1
if (count >= start) {
for (let j = i - range; j <= i + range || end > count; j++) {
if (j < 0 || j >= lines.length) continue
res.push(`${j + 1}${repeat(` `, 3 - String(j + 1).length)}| ${lines[j]}`)
res.push(
`${j + 1}${repeat(` `, 3 - String(j + 1).length)}| ${lines[j]}`
)
const lineLength = lines[j].length
if (j === i) {
// push underline
Expand All @@ -36,10 +38,11 @@ export function generateCodeFrame (
return res.join('\n')
}

function repeat (str, n) {
function repeat(str, n) {
let result = ''
if (n > 0) {
while (true) { // eslint-disable-line
while (true) {
// eslint-disable-line
if (n & 1) result += str
n >>>= 1
if (n <= 0) break
Expand Down
55 changes: 33 additions & 22 deletions src/compiler/codegen/events.js → src/compiler/codegen/events.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* @flow */

import { __WEEX__ } from 'typescript/weex'

const fnExpRE = /^([\w$_]+|\([^)]*?\))\s*=>|^function(?:\s+[\w$]+)?\s*\(/
const fnInvokeRE = /\([^)]*?\);*$/
const simplePathRE = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*|\['[^']*?']|\["[^"]*?"]|\[\d+]|\[[A-Za-z_$][\w$]*])*$/
Expand All @@ -14,7 +16,7 @@ const keyCodes: { [key: string]: number | Array<number> } = {
left: 37,
right: 39,
down: 40,
'delete': [8, 46]
delete: [8, 46],
}

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

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

const modifierCode: { [key: string]: string } = {
stop: '$event.stopPropagation();',
Expand All @@ -49,10 +51,10 @@ const modifierCode: { [key: string]: string } = {
meta: genGuard(`!$event.metaKey`),
left: genGuard(`'button' in $event && $event.button !== 0`),
middle: genGuard(`'button' in $event && $event.button !== 1`),
right: genGuard(`'button' in $event && $event.button !== 2`)
right: genGuard(`'button' in $event && $event.button !== 2`),
}

export function genHandlers (
export function genHandlers(
events: ASTElementHandlers,
isNative: boolean
): string {
Expand All @@ -61,6 +63,7 @@ export function genHandlers (
let dynamicHandlers = ``
for (const name in events) {
const handlerCode = genHandler(events[name])
//@ts-expect-error
if (events[name] && events[name].dynamic) {
dynamicHandlers += `${name},${handlerCode},`
} else {
Expand All @@ -77,34 +80,42 @@ export function genHandlers (

// Generate handler code with binding params on Weex
/* istanbul ignore next */
function genWeexHandler (params: Array<any>, handlerCode: string) {
function genWeexHandler(params: Array<any>, handlerCode: string) {
let innerHandlerCode = handlerCode
const exps = params.filter(exp => simplePathRE.test(exp) && exp !== '$event')
const bindings = exps.map(exp => ({ '@binding': exp }))
const exps = params.filter(
(exp) => simplePathRE.test(exp) && exp !== '$event'
)
const bindings = exps.map((exp) => ({ '@binding': exp }))
const args = exps.map((exp, i) => {
const key = `$_${i + 1}`
innerHandlerCode = innerHandlerCode.replace(exp, key)
return key
})
args.push('$event')
return '{\n' +
return (
'{\n' +
`handler:function(${args.join(',')}){${innerHandlerCode}},\n` +
`params:${JSON.stringify(bindings)}\n` +
'}'
)
}

function genHandler (handler: ASTElementHandler | Array<ASTElementHandler>): string {
function genHandler(
handler: ASTElementHandler | Array<ASTElementHandler>
): string {
if (!handler) {
return 'function(){}'
}

if (Array.isArray(handler)) {
return `[${handler.map(handler => genHandler(handler)).join(',')}]`
return `[${handler.map((handler) => genHandler(handler)).join(',')}]`
}

const isMethodPath = simplePathRE.test(handler.value)
const isFunctionExpression = fnExpRE.test(handler.value)
const isFunctionInvocation = simplePathRE.test(handler.value.replace(fnInvokeRE, ''))
const isFunctionInvocation = simplePathRE.test(
handler.value.replace(fnInvokeRE, '')
)

if (!handler.modifiers) {
if (isMethodPath || isFunctionExpression) {
Expand All @@ -120,7 +131,7 @@ function genHandler (handler: ASTElementHandler | Array<ASTElementHandler>): str
} else {
let code = ''
let genModifierCode = ''
const keys = []
const keys: string[] = []
for (const key in handler.modifiers) {
if (modifierCode[key]) {
genModifierCode += modifierCode[key]
Expand All @@ -129,11 +140,11 @@ function genHandler (handler: ASTElementHandler | Array<ASTElementHandler>): str
keys.push(key)
}
} else if (key === 'exact') {
const modifiers: ASTModifiers = (handler.modifiers: any)
const modifiers = handler.modifiers
genModifierCode += genGuard(
['ctrl', 'shift', 'alt', 'meta']
.filter(keyModifier => !modifiers[keyModifier])
.map(keyModifier => `$event.${keyModifier}Key`)
.filter((keyModifier) => !modifiers[keyModifier])
.map((keyModifier) => `$event.${keyModifier}Key`)
.join('||')
)
} else {
Expand All @@ -150,10 +161,10 @@ function genHandler (handler: ASTElementHandler | Array<ASTElementHandler>): str
const handlerCode = isMethodPath
? `return ${handler.value}.apply(null, arguments)`
: isFunctionExpression
? `return (${handler.value}).apply(null, arguments)`
: isFunctionInvocation
? `return ${handler.value}`
: handler.value
? `return (${handler.value}).apply(null, arguments)`
: isFunctionInvocation
? `return ${handler.value}`
: handler.value
/* istanbul ignore if */
if (__WEEX__ && handler.params) {
return genWeexHandler(handler.params, code + handlerCode)
Expand All @@ -162,7 +173,7 @@ function genHandler (handler: ASTElementHandler | Array<ASTElementHandler>): str
}
}

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

function genFilterCode (key: string): string {
function genFilterCode(key: string): string {
const keyVal = parseInt(key, 10)
if (keyVal) {
return `$event.keyCode!==${keyVal}`
Expand Down
Loading

0 comments on commit af9fc2b

Please sign in to comment.