Codiga has joined Datadog!

Read the Blog
Skip to main content

VariableDeclaration AST JavaScript Object

The VariableDeclaration object matches a JavaScript import.

The astType value for this node is variabledeclaration.

Code Pattern

This object catches the following code patterns:

const foo = 42; // javascript
const foo: number = 42; // typescript only

Usage

The following code checks if a variable declaration uses the any type. This is useful for TypeScript.

function visit(node) {
if (
node &&
node.type &&
node.type.astType === "string" &&
node.type.value === "any"
) {
const error = buildError(
node.type.start.line,
node.type.start.col,
node.type.end.line,
node.type.end.col,
"do not use any type",
"CRITICAL",
"SAFETY"
);
addError(error);
}
}

Attributes

  • modifier (AstString): the modifier (let, const, var)
  • name (AstString): the name of the variable
  • type TypeScript only (AstElement, it can be different elements, from a string to more complex objects): the type of the variable. Can be a Type, a TypeOperation or any other node type.
  • value (array of AstElement): the value of the variable