How to use the updateCall function from typescript

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

typescript.updateCall updates a CallExpression node in an Abstract Syntax Tree (AST) created by the TypeScript compiler API.

56
57
58
59
60
61
62
63
64
65
66
    if (/\.(less|css|scss|sass|svg|png|html)/.test(file)) {
        const sourceFileDir = path.dirname(sourceFile.path);
        const abs = path.join(sourceFileDir, file);
        const absSource = path.join(options.outDir, path.relative(options.baseUrl, sourceFileDir));
        const relFile = path.relative(absSource, abs).replaceAll(path.sep, '/');
        return ts.updateCall(importNode, importNode.expression, undefined, [ts.createStringLiteral(relFile)]);
    }
}


const lessToStringTransformer = function (context) {
fork icon0
star icon2
watch icon1

+ 19 other calls in file

281
282
283
284
285
286
287
288
289
290
]));
let arg = ts.createObjectLiteral();
if (exportAssignment) {
    arg = ts.createBinary(exportAssignment, ts.SyntaxKind.BarBarToken, arg);
}
const updatedIife = ts.updateCall(iife, ts.updateParen(iife.expression, updatedFunction), iife.typeArguments, [arg]);
let value = ast_utils_1.addPureComment(updatedIife);
if (exportAssignment) {
    value = ts.createBinary(exportAssignment, ts.SyntaxKind.FirstAssignment, updatedIife);
}
fork icon0
star icon0
watch icon1

How does typescript.updateCall work?

typescript.updateCall is a function in the TypeScript Compiler API that creates a new CallExpression node with updated arguments, type arguments, and expression. It takes a CallExpression node and an array of new arguments as input and returns a new CallExpression node with the updated information. If there are no changes to the CallExpression, the original node is returned.

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import ts from "typescript";

const sourceFile = ts.createSourceFile(
  "example.ts",
  "console.log(add(1, 2));",
  ts.ScriptTarget.Latest
);

const visitor = (node: ts.Node): ts.Node => {
  if (ts.isCallExpression(node)) {
    const expression = node.expression.getText();
    if (expression === "add") {
      const newArgs = ts.createNodeArray([
        ts.createNumericLiteral("10"),
        ts.createNumericLiteral("20"),
      ]);
      return ts.updateCall(node, node.expression, undefined, newArgs);
    }
  }
  return node;
};

const updatedFile = ts.transform(sourceFile, [visitor]);
console.log(updatedFile.transformed[0].getText());

In this example, we're using the typescript.updateCall() function to replace the arguments of a function call. We first create a source file with a console.log() statement that calls a function named add() with two arguments. We then define a visitor function that looks for CallExpression nodes with an add identifier and replaces the arguments with two new NumericLiteral nodes. Finally, we transform the source file with the visitor function and output the updated source code.

Other functions in typescript

Sorted by popularity

function icon

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