TsSimpleAst

Class Declarations

Class declarations can be retrieved from source files, namespaces, or function bodies:

const classes = sourceFile.getClasses();
const class1 = sourceFile.getClass("Class1");
const firstClassWithConstructor = sourceFile.getClass(c => c.getConstructor() !== undefined);

Extends expression

const extendsExpression = classDeclaration.getExtendsExpression();

Will return ExpressionWithTypeArguments | undefined.

Implements expressions

const implementsExpressions = classDeclaration.getImplementsExpressions();

Will return ExpressionWithTypeArguments[].

Constructor

If one exists, it can be retrieved via getConstructor:

const ctor = classDeclaration.getConstructor();

Methods

Get the instance methods using getInstanceMethods():

const instanceMethods = classDeclaration.getInstanceMethods();

Get the static methods using getStaticMethods():

const staticMethods = classDeclaration.getStaticMethods();

Properties

Get the instance properties using getInstanceProperties():

const instanceProperties = classDeclaration.getInstanceProperties();

Get the static properties using getStaticProperties():

const staticProperties = classDeclaration.getStaticProperties();

Get members

Get all static and instance members:

const allMembers = classDeclaration.getAllMembers();

Get instance members:

const instanceMembers = classDeclaration.getInstanceMembers();

Get static members:

const staticMembers = classDeclaration.getStaticMembers();

Abstract

Nodes on a class may be abstract.

Get if it’s abstract:

method.getIsAbstract(); // returns: boolean

Get the abstract keyword:

method.getAbstractKeyword(); // returns: node | undefined

Set if abstract:

method.setIsAbstract(true);  // set as abstract
method.setIsAbstract(false); // set as not abstract

Constructors

Constructors implement common functions found on function like declarations, but also include a scope.

Methods

Explore the functionality available via auto-complete.

Properties

Explore the functionality available via auto-complete.

Get Accessors

If it exists, you can get the corresponding set accessor:

const setAccessor = getAccessor.getSetAccessor();

Set Accessors

If it exists, you can get the corresponding get accessor:

const getAccessor = setAccessor.getGetAccessor();