How to use the visitNode function from typescript

Find comprehensive JavaScript typescript.visitNode code examples handpicked from public code repositorys.

457
458
459
460
461
462
463
464
465
466
 * @type {import("typescript").TransformerFactory<import("typescript").SourceFile>}
 */
// eslint-disable-next-line no-unused-vars
const transformer = (context) => {
    return (sourceFile) =>
        ts.visitNode(sourceFile, (node) => {
            if (ts.isSourceFile(node)) {
                // All non-export-statements stay at the root level, the rest is wrapped in `declare global`
                const exportStatements = node.statements.filter(s => s.modifiers && s.modifiers.some(m => m.kind === ts.SyntaxKind.ExportKeyword));
                const otherStatements = node.statements.filter(s => !exportStatements.includes(s));
fork icon116
star icon294
watch icon29

+ 3 other calls in file

38
39
40
41
42
43
44
45
46
47
48
        ]);
        return ts.factory.createCallExpression(logFunc, undefined, newArgs);
      }
      return ts.visitEachChild(node, visit, context);
    }
    return ts.visitNode(sourceFile, visit);
  };
}


/**
fork icon0
star icon0
watch icon1

+ 37 other calls in file

72
73
74
75
76
77
78
79
80
81
82
83
    }


    return ts.visitEachChild(node, visitor, context);
  };


  return ts.visitNode(sourceFile, visitor);
};


module.exports = transformerProgram;
fork icon0
star icon0
watch icon1

24
25
26
27
28
29
30
31
32
33
};
return (sourceFile) => {
    if (!shouldTransform(sourceFile.fileName)) {
        return sourceFile;
    }
    const updatedSourceFile = ts.visitNode(sourceFile, visitNode);
    if (resourceImportDeclarations.length) {
        // Add resource imports
        return ts.updateSourceFileNode(updatedSourceFile, ts.setTextRange(ts.createNodeArray([
            ...resourceImportDeclarations,
fork icon0
star icon0
watch icon1

61
62
63
64
65
66
67
68
69
70
};
// Don't visit the sourcefile at all if we don't have ops for it.
if (ops.length === 0) {
    return sf;
}
const result = ts.visitNode(sf, visitor);
// If we removed any decorators, we need to clean up the decorator arrays.
if (removeOps.some(op => op.target.kind === ts.SyntaxKind.Decorator)) {
    cleanupDecorators(result);
}
fork icon0
star icon0
watch icon1

119
120
121
122
123
124
125
126
127
128
    updatedStatements.splice(uIndex, oldStatementsLength, ...newStatement);
    // When having more than a single new statement
    // we need to update the update Index
    uIndex += (newStatement ? newStatement.length - 1 : 0);
}
const result = ts.visitNode(currentStatement, visitor);
if (result !== currentStatement) {
    if (!updatedStatements) {
        updatedStatements = statements.slice();
    }
fork icon0
star icon0
watch icon1

71
72
73
74
75
76
77
78
79
80
                    return undefined;
                }
                // Otherwise return node as is.
                return ts.visitEachChild(node, visitor, context);
            };
            return ts.visitNode(sf, visitor);
        };
        return transformer;
    };
}
fork icon0
star icon0
watch icon1

Other functions in typescript

Sorted by popularity

function icon

typescript.SyntaxKind is the most popular function in typescript (82777 examples)