INNER CODE UNIT · TypeScript

addMethod

maciejtreder/ng-toolkit · schematics/_utils/index.ts:272

export function addMethod(tree: Tree, filePath: string, body: string): void {
    const sourceText = getFileContent(tree, filePath);
    let changePos;
    const changeRecorder = tree.beginUpdate(filePath);


    if (body.indexOf('constructor') >= 0 || body.indexOf('public') >= 0) {
        let match = sourceText.match(/(?:export|) class.*?{[\s\S]*?((?:constructor|public|private|)[\w\s]*?\()/);
        if (match) {
            changePos = (match['index'] || 0) + match[0].length - match[1].length;
        } else {
            changePos = sourceText.lastIndexOf("}") - 1;
        }
    } else {
        changePos = sourceText.lastIndexOf("}") - 1;
    }
    changeRecorder.insertLeft(changePos, `\n${body}\n`);
    tree.commitUpdate(changeRecorder);

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…