feat(client/codegen): aliases for methods
This commit is contained in:
parent
8acad15d7c
commit
c0103441d3
1 changed files with 81 additions and 63 deletions
|
@ -48,7 +48,7 @@ async function addSingleMethod(state, fileName) {
|
||||||
return getLeadingComments(ast)
|
return getLeadingComments(ast)
|
||||||
.split('\n')
|
.split('\n')
|
||||||
.map((i) => i.replace(/^(\/\/|\s*\*+|\/\*\*+\s*)/g, '').trim())
|
.map((i) => i.replace(/^(\/\/|\s*\*+|\/\*\*+\s*)/g, '').trim())
|
||||||
.some((i) => i.startsWith(flag))
|
.find((i) => i.startsWith(flag))
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const stmt of program.statements) {
|
for (const stmt of program.statements) {
|
||||||
|
@ -108,6 +108,15 @@ async function addSingleMethod(state, fileName) {
|
||||||
(mod) => mod.kind === 92 /* ExportKeyword */
|
(mod) => mod.kind === 92 /* ExportKeyword */
|
||||||
)
|
)
|
||||||
const isInitialize = checkForFlag(stmt, '@initialize')
|
const isInitialize = checkForFlag(stmt, '@initialize')
|
||||||
|
const aliases = (function() {
|
||||||
|
const flag = checkForFlag(stmt, '@alias')
|
||||||
|
if (!flag) return []
|
||||||
|
|
||||||
|
const [, aliases] = flag.split('=')
|
||||||
|
if (!aliases || !aliases.length) return []
|
||||||
|
|
||||||
|
return aliases.split(',')
|
||||||
|
})()
|
||||||
|
|
||||||
if (!isExported && !isPrivate) {
|
if (!isExported && !isPrivate) {
|
||||||
throwError(
|
throwError(
|
||||||
|
@ -186,6 +195,7 @@ async function addSingleMethod(state, fileName) {
|
||||||
isPrivate,
|
isPrivate,
|
||||||
func: stmt,
|
func: stmt,
|
||||||
comment: getLeadingComments(stmt),
|
comment: getLeadingComments(stmt),
|
||||||
|
aliases
|
||||||
})
|
})
|
||||||
|
|
||||||
const module = `./${relPath.replace(/\.ts$/, '')}`
|
const module = `./${relPath.replace(/\.ts$/, '')}`
|
||||||
|
@ -286,7 +296,7 @@ async function main() {
|
||||||
|
|
||||||
const printer = ts.createPrinter()
|
const printer = ts.createPrinter()
|
||||||
|
|
||||||
state.methods.list.forEach(({ name, isPrivate, func, comment }) => {
|
state.methods.list.forEach(({ name: origName, isPrivate, func, comment, aliases }) => {
|
||||||
// create method that calls that function and passes `this`
|
// create method that calls that function and passes `this`
|
||||||
// first let's determine the signature
|
// first let's determine the signature
|
||||||
const returnType = func.type ? ': ' + func.type.getText() : ''
|
const returnType = func.type ? ': ' + func.type.getText() : ''
|
||||||
|
@ -338,15 +348,22 @@ async function main() {
|
||||||
return it.getFullText()
|
return it.getFullText()
|
||||||
}).join(', ')
|
}).join(', ')
|
||||||
|
|
||||||
// write comment, but remove @internal mark and set default values for parameters
|
// remove @internal mark and set default values for parameters
|
||||||
comment = comment
|
comment = comment
|
||||||
|
.replace(/^\s*\/\/+\s*@alias.*$/m, '')
|
||||||
.replace(/(\n^|\/\*)\s*\*\s*@internal.*/m, '')
|
.replace(/(\n^|\/\*)\s*\*\s*@internal.*/m, '')
|
||||||
.replace(/((?:\n^|\/\*)\s*\*\s*@param )([^\s]+?)($|\s+)/gm, (_, pref, arg, post) => {
|
.replace(/((?:\n^|\/\*)\s*\*\s*@param )([^\s]+?)($|\s+)/gm, (_, pref, arg, post) => {
|
||||||
const param = rawParams.find(it => it.name.escapedText === arg)
|
const param = rawParams.find(it => it.name.escapedText === arg)
|
||||||
if (!param) return _
|
if (!param) return _
|
||||||
if (!param._savedDefault) return _
|
if (!param._savedDefault) return _
|
||||||
|
if (post) {
|
||||||
return `${pref}${arg}${post}(default: \`${param._savedDefault.trim()}\`) `
|
return `${pref}${arg}${post}(default: \`${param._savedDefault.trim()}\`) `
|
||||||
|
} else {
|
||||||
|
return `${pref}${arg}\n* (default: \`${param._savedDefault.trim()}\`)`
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
for (const name of [origName, ...aliases]) {
|
||||||
if (!comment.match(/\/\*\*?\s*\*\//))
|
if (!comment.match(/\/\*\*?\s*\*\//))
|
||||||
// empty comment, no need to write it
|
// empty comment, no need to write it
|
||||||
output.write(comment)
|
output.write(comment)
|
||||||
|
@ -357,11 +374,12 @@ async function main() {
|
||||||
}${name}${generics}(${parameters})${returnType}${
|
}${name}${generics}(${parameters})${returnType}${
|
||||||
func.body
|
func.body
|
||||||
? `{
|
? `{
|
||||||
return ${name}.apply(this, arguments)
|
return ${origName}.apply(this, arguments)
|
||||||
}`
|
}`
|
||||||
: ''
|
: ''
|
||||||
}`
|
}`
|
||||||
)
|
)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
output.untab()
|
output.untab()
|
||||||
output.write('}')
|
output.write('}')
|
||||||
|
|
Loading…
Reference in a new issue