Skip to content

Commit

Permalink
feat: Add some extensions function for CXXTerraNode
Browse files Browse the repository at this point in the history
  • Loading branch information
littleGnAl committed Mar 7, 2024
1 parent 4ab49c2 commit 5e87246
Show file tree
Hide file tree
Showing 2 changed files with 209 additions and 15 deletions.
107 changes: 93 additions & 14 deletions cxx-parser/__tests__/unit_test/cxx_terra_node_ext.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,101 @@
import '../../src/cxx_terra_node_ext';
import { SimpleType, SimpleTypeKind } from '../../src/cxx_terra_node';
import {
CXXFile,
Clazz,
Constructor,
EnumConstant,
Enumz,
IncludeDirective,
MemberFunction,
MemberVariable,
SimpleType,
SimpleTypeKind,
Struct,
TypeAlias,
Variable,
// Import the index file to check if there are any issues with the order of
// interface declarations and exports.
} from '../../src/index';

describe('cxx_terra_node_ext', () => {
it('lenOfArrayType can get len of array type', () => {
let simpleType = new SimpleType();
simpleType.kind = SimpleTypeKind.array_t;
simpleType.source = 'int[10]';
describe('lenOfArrayType', () => {
it('lenOfArrayType can get len of array type', () => {
let simpleType = new SimpleType();
simpleType.kind = SimpleTypeKind.array_t;
simpleType.source = 'int[10]';

let len = simpleType.lenOfArrayType();
expect(len).toBe('10');
let len = simpleType.lenOfArrayType();
expect(len).toBe('10');
});

it('lenOfArrayType can get len of array type from std type', () => {
let simpleType = new SimpleType();
simpleType.kind = SimpleTypeKind.array_t;
simpleType.source = 'uint8_t[10]';

let len = simpleType.lenOfArrayType();
expect(len).toBe('10');
});
});

it('lenOfArrayType can get len of array type from std type', () => {
let simpleType = new SimpleType();
simpleType.kind = SimpleTypeKind.array_t;
simpleType.source = 'uint8_t[10]';
describe('CXXTerraNode', () => {
it('isCXXFile', () => {
let node = new CXXFile();
expect(node.isCXXFile()).toBe(true);
});

it('isIncludeDirective', () => {
let node = new IncludeDirective();
expect(node.isIncludeDirective()).toBe(true);
});

it('isTypeAlias', () => {
let node = new TypeAlias();
expect(node.isTypeAlias()).toBe(true);
});

it('isClazz', () => {
let node = new Clazz();
expect(node.isClazz()).toBe(true);
});

it('isStruct', () => {
let node = new Struct();
expect(node.isStruct()).toBe(true);
});

it('isConstructor', () => {
let node = new Constructor();
expect(node.isConstructor()).toBe(true);
});

it('isMemberFunction', () => {
let node = new MemberFunction();
expect(node.isMemberFunction()).toBe(true);
});

it('isVariable', () => {
let node = new Variable();
expect(node.isVariable()).toBe(true);
});

it('isSimpleType', () => {
let node = new SimpleType();
expect(node.isSimpleType()).toBe(true);
});

it('isMemberVariable', () => {
let node = new MemberVariable();
expect(node.isMemberVariable()).toBe(true);
});

it('isEnumConstant', () => {
let node = new EnumConstant();
expect(node.isEnumConstant()).toBe(true);
});

let len = simpleType.lenOfArrayType();
expect(len).toBe('10');
it('isEnumz', () => {
let node = new Enumz();
expect(node.isEnumz()).toBe(true);
});
});
});
117 changes: 116 additions & 1 deletion cxx-parser/src/cxx_terra_node_ext.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,77 @@
import { strict as assert } from 'assert';

import { SimpleType, SimpleTypeKind } from './cxx_terra_node';
import {
CXXTYPE,
CXXTerraNode,
SimpleType,
SimpleTypeKind,
} from './cxx_terra_node';

export {};

declare module '@agoraio-extensions/cxx-parser' {
export interface CXXTerraNode {
/**
* Checks if this node is a `CXXFile`.
*/
isCXXFile(): boolean;

/**
* Checks if this node is an `IncludeDirective`.
*/
isIncludeDirective(): boolean;

/**
* Checks if this node is a `TypeAlias`.
*/
isTypeAlias(): boolean;

/**
* Checks if this node is a `Clazz`.
*/
isClazz(): boolean;

/**
* Checks if this node is a `Struct`.
*/
isStruct(): boolean;

/**
* Checks if this node is a `Constructor`.
*/
isConstructor(): boolean;

/**
* Checks if this node is a `MemberFunction`.
*/
isMemberFunction(): boolean;

/**
* Checks if this node is a `Variable`.
*/
isVariable(): boolean;

/**
* Checks if this node is a `SimpleType`.
*/
isSimpleType(): boolean;

/**
* Checks if this node is a `MemberVariable`.
*/
isMemberVariable(): boolean;

/**
* Checks if this node is a `EnumConstant`.
*/
isEnumConstant(): boolean;

/**
* Checks if this node is a `Enumz`.
*/
isEnumz(): boolean;
}

export interface SimpleType {
/**
* The length of the array type. For example, "int[10]" returns "10".
Expand Down Expand Up @@ -65,3 +132,51 @@ SimpleType.prototype.lenOfArrayType = function (): string {

assert(false); // Should not reach here
};

CXXTerraNode.prototype.isCXXFile = function (): boolean {
return this.__TYPE === CXXTYPE.CXXFile;
};

CXXTerraNode.prototype.isIncludeDirective = function (): boolean {
return this.__TYPE === CXXTYPE.IncludeDirective;
};

CXXTerraNode.prototype.isTypeAlias = function (): boolean {
return this.__TYPE === CXXTYPE.TypeAlias;
};

CXXTerraNode.prototype.isClazz = function (): boolean {
return this.__TYPE === CXXTYPE.Clazz;
};

CXXTerraNode.prototype.isStruct = function (): boolean {
return this.__TYPE === CXXTYPE.Struct;
};

CXXTerraNode.prototype.isConstructor = function (): boolean {
return this.__TYPE === CXXTYPE.Constructor;
};

CXXTerraNode.prototype.isMemberFunction = function (): boolean {
return this.__TYPE === CXXTYPE.MemberFunction;
};

CXXTerraNode.prototype.isVariable = function (): boolean {
return this.__TYPE === CXXTYPE.Variable;
};

CXXTerraNode.prototype.isSimpleType = function (): boolean {
return this.__TYPE === CXXTYPE.SimpleType;
};

CXXTerraNode.prototype.isMemberVariable = function (): boolean {
return this.__TYPE === CXXTYPE.MemberVariable;
};

CXXTerraNode.prototype.isEnumConstant = function (): boolean {
return this.__TYPE === CXXTYPE.EnumConstant;
};

CXXTerraNode.prototype.isEnumz = function (): boolean {
return this.__TYPE === CXXTYPE.Enumz;
};

0 comments on commit 5e87246

Please sign in to comment.