Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(propDefinition): 函数匹配支持参数表和异步函数 #39

Merged
merged 1 commit into from
Jun 23, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions src/plugin/lib/ScriptFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,52 @@ function parseScriptFile(file: string, type: string, prop: string) {
let locs: Location[] = []

let reg: RegExp | null = null
let s = '\\s*'
let b = `\\(${s}\\)`
/**
* 空白符正则
*/
const s = '\\s*'

if (type === 'prop') {
reg = new RegExp(`^${s}` + prop + `${s}:`, 'gm')
} else if (type === 'method') {
// prop: () => {}
// prop: function() {}
// prop: function xxx() {}
// prop() {}
reg = new RegExp(`^${s}` + prop + `(${s}:${b}${s}=>|${s}:${s}function|${s}${b}${s}\\{)`, 'gm')
// prop: (e:{}) => {}
// reg = new RegExp(`^${s}` + prop + `(${s}:${b}${s}=>|${s}:${s}function|${s}${b}${s}\\{)`, 'gm')

/**
* 函数参数表正则
* 允许参数跨行
* - 无参数`()`
* - 有参数`( e )`
* - 参数跨行
* ```ts
* (
* e: event
* )
* ```
*/
const param = `\\([\\s\\S]*?\\)`
const async = `(async\\s+)?`
/**
* 方法定义正则
* - 普通方法`prop(...){`
* - 异步方法`async prop(...){`
*/
const methodReg = `${async}${prop}${s}${param}${s}\\{`
/**
* 属性式函数定义 正则
* - 箭头函数`prop: (...) =>`
* - 异步箭头函数`prop: async (...) =>`
* - 普通函数声明`prop: function...`
* - 异步函数声明`prop: async function...`
*/
const propFuncReg = `${prop}${s}:${async}(${param}${s}=>|function\\W)`
reg = new RegExp(`^${s}(${methodReg}|${propFuncReg})`, 'gm')
}

match(content, reg as any).forEach(mat => {
match(content, reg!).forEach(mat => {
let pos = getPositionFromIndex(content, mat.index + mat[0].indexOf(prop))
let endPos = new Position(pos.line, pos.character + prop.length)
locs.push(new Location(Uri.file(file), new Range(pos, endPos)))
Expand Down