ts-morph#MethodDeclaration TypeScript Examples
The following examples show how to use
ts-morph#MethodDeclaration.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: configuration.ts From cli with MIT License | 6 votes |
// 获取生命周期类已有的方法声明
export function getLifeCycleClassMethodDeclaration(
source: SourceFile,
methodName: LifeCycleMethod
): MethodDeclaration {
return getExistClassMethodsDeclaration(
source,
LIFE_CYCLE_CLASS_IDENTIFIER,
methodName
);
}
Example #2
Source File: configuration.ts From cli with MIT License | 5 votes |
// 确保容器配置类的方法具有标准参数
export function ensureLifeCycleMethodArguments(
source: SourceFile,
methods: LifeCycleMethod[],
apply = true
) {
ensureLifeCycleMethods(source, methods);
const existMethodDeclarations = getExistClassMethodsDeclaration(
source,
LIFE_CYCLE_CLASS_IDENTIFIER
);
const methodsShouldBeFix: MethodDeclaration[] = [];
existMethodDeclarations.forEach((m, idx) => {
if (m.getFirstChildByKind(SyntaxKind.SyntaxList).getText() !== 'async') {
return;
}
const argsSyntaxList = m.getChildrenOfKind(SyntaxKind.SyntaxList)[1];
// 只处理参数为空的方法
if (!argsSyntaxList.getText()) {
methodsShouldBeFix.push(m);
return;
}
// 存在参数
const paramSyntaxList = argsSyntaxList.getChildrenOfKind(
SyntaxKind.Parameter
);
// 参数数量不正确 需要手动补全 因为case太多了
if (paramSyntaxList.length !== 2) {
consola.error(
`Incorrect arguments count in ${m.getName()}, expect: 2, found: ${
paramSyntaxList.length
}`
);
return;
}
});
if (!methodsShouldBeFix.length) {
return;
}
// 仅修正无参数的方法
methodsShouldBeFix.forEach(m => {
m.addParameters([
{
name: 'container',
type: 'IMidwayContainer',
hasQuestionToken: false,
initializer: null,
isReadonly: false,
isRestParameter: false,
},
{
name: 'app',
type: 'IMidwayApplication',
hasQuestionToken: true,
initializer: null,
isReadonly: false,
isRestParameter: false,
},
]);
});
apply && source.saveSync();
}