Skip to content

Typescript

TypeScript-specific configuration for syntax chunking.

logger module-attribute

logger = getLogger(__name__)

TypeScriptConfig dataclass

Bases: LanguageConfig

TypeScript-specific syntax chunking configuration.

Source code in src/codemap/processor/tree_sitter/languages/typescript.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class TypeScriptConfig(LanguageConfig):
	"""TypeScript-specific syntax chunking configuration."""

	# File-level entities
	module: ClassVar[list[str]] = ["program"]
	namespace: ClassVar[list[str]] = ["export_statement", "namespace_declaration"]

	# Type definitions
	class_: ClassVar[list[str]] = ["class_declaration", "class"]
	interface: ClassVar[list[str]] = ["interface_declaration"]
	protocol: ClassVar[list[str]] = []  # TypeScript doesn't have protocols
	struct: ClassVar[list[str]] = []  # TypeScript doesn't have structs
	enum: ClassVar[list[str]] = ["enum_declaration"]
	type_alias: ClassVar[list[str]] = ["type_alias_declaration"]

	# Functions and methods
	function: ClassVar[list[str]] = [*JAVASCRIPT_CONFIG.function, "function_signature"]
	method: ClassVar[list[str]] = [*JAVASCRIPT_CONFIG.method, "method_signature"]
	property_def: ClassVar[list[str]] = [*JAVASCRIPT_CONFIG.property_def, "public_field_definition"]
	test_case: ClassVar[list[str]] = JAVASCRIPT_CONFIG.test_case
	test_suite: ClassVar[list[str]] = JAVASCRIPT_CONFIG.test_suite

	# Variables and constants
	variable: ClassVar[list[str]] = JAVASCRIPT_CONFIG.variable
	constant: ClassVar[list[str]] = JAVASCRIPT_CONFIG.constant
	class_field: ClassVar[list[str]] = [*JAVASCRIPT_CONFIG.class_field, "public_field_definition"]

	# Code organization
	import_: ClassVar[list[str]] = [*JAVASCRIPT_CONFIG.import_, "import_alias"]
	decorator: ClassVar[list[str]] = JAVASCRIPT_CONFIG.decorator

	# Documentation
	comment: ClassVar[list[str]] = JAVASCRIPT_CONFIG.comment
	docstring: ClassVar[list[str]] = JAVASCRIPT_CONFIG.docstring

	file_extensions: ClassVar[list[str]] = [".ts", ".tsx"]
	tree_sitter_name: ClassVar[str] = "typescript"

module class-attribute

module: list[str] = ['program']

namespace class-attribute

namespace: list[str] = [
	"export_statement",
	"namespace_declaration",
]

class_ class-attribute

class_: list[str] = ['class_declaration', 'class']

interface class-attribute

interface: list[str] = ['interface_declaration']

protocol class-attribute

protocol: list[str] = []

struct class-attribute

struct: list[str] = []

enum class-attribute

enum: list[str] = ['enum_declaration']

type_alias class-attribute

type_alias: list[str] = ['type_alias_declaration']

function class-attribute

function: list[str] = [*function, 'function_signature']

method class-attribute

method: list[str] = [*method, 'method_signature']

property_def class-attribute

property_def: list[str] = [
	*property_def,
	"public_field_definition",
]

test_case class-attribute

test_case: list[str] = test_case

test_suite class-attribute

test_suite: list[str] = test_suite

variable class-attribute

variable: list[str] = variable

constant class-attribute

constant: list[str] = constant

class_field class-attribute

class_field: list[str] = [
	*class_field,
	"public_field_definition",
]

import_ class-attribute

import_: list[str] = [*import_, 'import_alias']

decorator class-attribute

decorator: list[str] = decorator

comment class-attribute

comment: list[str] = comment

docstring class-attribute

docstring: list[str] = docstring

file_extensions class-attribute

file_extensions: list[str] = ['.ts', '.tsx']

tree_sitter_name class-attribute

tree_sitter_name: str = 'typescript'

TYPESCRIPT_CONFIG module-attribute

TYPESCRIPT_CONFIG = TypeScriptConfig()

TypeScriptSyntaxHandler

Bases: JavaScriptSyntaxHandler

TypeScript-specific syntax handling logic.

Inherits from JavaScript handler to reuse common logic.

Source code in src/codemap/processor/tree_sitter/languages/typescript.py
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
class TypeScriptSyntaxHandler(JavaScriptSyntaxHandler):
	"""
	TypeScript-specific syntax handling logic.

	Inherits from JavaScript handler to reuse common logic.

	"""

	def __init__(self) -> None:
		"""Initialize with TypeScript configuration."""
		# Revert to super() and ignore potential linter false positive
		super().__init__(TYPESCRIPT_CONFIG)  # type: ignore[call-arg] # pylint: disable=too-many-function-args

	def get_entity_type(self, node: Node, parent: Node | None, content_bytes: bytes) -> EntityType:
		"""
		Determine the EntityType for a TypeScript node.

		Args:
		    node: The tree-sitter node
		    parent: The parent node (if any)
		    content_bytes: Source code content as bytes

		Returns:
		    The entity type

		"""
		node_type = node.type
		logger.debug(
			"Getting entity type for TypeScript node: type=%s, parent_type=%s",
			node_type,
			parent.type if parent else None,
		)

		# Check for TypeScript specific types first
		if node_type in self.config.interface:
			return EntityType.INTERFACE
		if node_type in self.config.type_alias:
			return EntityType.TYPE_ALIAS
		if node_type == "enum_declaration":
			return EntityType.ENUM
		if node_type == "module":  # TS internal modules/namespaces
			return EntityType.NAMESPACE
		if node_type == "namespace_declaration":
			return EntityType.NAMESPACE
		if node_type == "method_signature":
			return EntityType.METHOD
		if node_type == "property_signature":
			return EntityType.PROPERTY

		# Use the JavaScript logic for common types
		return super().get_entity_type(node, parent, content_bytes)

	def extract_name(self, node: Node, content_bytes: bytes) -> str:
		"""
		Extract the name identifier from a definition node.

		Args:
		    node: The tree-sitter node
		    content_bytes: Source code content as bytes

		Returns:
		    The extracted name

		"""
		# Handle TypeScript-specific node types first
		name_node = None

		if node.type in [
			"interface_declaration",
			"enum_declaration",
			"type_alias_declaration",
			"namespace_declaration",
		] or node.type in ["method_signature", "property_signature"]:
			name_node = node.child_by_field_name("name")

		if name_node:
			try:
				return content_bytes[name_node.start_byte : name_node.end_byte].decode("utf-8", errors="ignore")
			except (UnicodeDecodeError, IndexError, AttributeError) as e:
				logger.warning("Failed to decode TypeScript name: %s", e)
				return f"<decoding-error-{node.type}>"

		# Fall back to JavaScript name extraction
		return super().extract_name(node, content_bytes)

	def get_body_node(self, node: Node) -> Node | None:
		"""
		Get the node representing the 'body' of a definition.

		Args:
		    node: The tree-sitter node

		Returns:
		    The body node if available, None otherwise

		"""
		if node.type in ("interface_declaration", "function_signature", "method_signature"):
			return None  # Interfaces and signatures have no body block
		return super().get_body_node(node)

	def get_children_to_process(self, node: Node, body_node: Node | None) -> list[Node]:
		"""
		Get the list of child nodes that should be recursively processed.

		Args:
		    node: The tree-sitter node
		    body_node: The body node if available

		Returns:
		    List of child nodes to process

		"""
		# TypeScript-specific handling
		if node.type == "type_alias_declaration":
			# Type aliases don't have children to process
			return []

		# Fall back to JavaScript children processing
		return super().get_children_to_process(node, body_node)

	def extract_imports(self, node: Node, content_bytes: bytes) -> list[str]:
		"""
		Extract imported module names from a TypeScript import statement.

		Args:
		    node: The tree-sitter node representing an import statement
		    content_bytes: Source code content as bytes

		Returns:
		    List of imported module names as strings

		"""
		# TypeScript import statements are the same as JavaScript
		return super().extract_imports(node, content_bytes)

	def get_enclosing_node_of_type(self, node: Node, target_type: EntityType) -> Node | None:
		"""
		Find the first ancestor node matching the target TypeScript entity type.

		Handles INTERFACE specifically and falls back to the JavaScript handler
		for other types (CLASS, FUNCTION, METHOD, MODULE).

		Args:
		    node: The starting node.
		    target_type: The EntityType to search for in ancestors.

		Returns:
		    The ancestor node if found, otherwise None.

		"""
		if target_type == EntityType.INTERFACE:
			target_node_types = ["interface_declaration"]
			current = node.parent
			while current:
				if current.type in target_node_types:
					return current
				current = current.parent
			return None
		# Fall back to JS handler for other types (CLASS, FUNCTION, METHOD, MODULE)
		return super().get_enclosing_node_of_type(node, target_type)

__init__

__init__() -> None

Initialize with TypeScript configuration.

Source code in src/codemap/processor/tree_sitter/languages/typescript.py
68
69
70
71
def __init__(self) -> None:
	"""Initialize with TypeScript configuration."""
	# Revert to super() and ignore potential linter false positive
	super().__init__(TYPESCRIPT_CONFIG)  # type: ignore[call-arg] # pylint: disable=too-many-function-args

get_entity_type

get_entity_type(
	node: Node, parent: Node | None, content_bytes: bytes
) -> EntityType

Determine the EntityType for a TypeScript node.

Parameters:

Name Type Description Default
node Node

The tree-sitter node

required
parent Node | None

The parent node (if any)

required
content_bytes bytes

Source code content as bytes

required

Returns:

Type Description
EntityType

The entity type

Source code in src/codemap/processor/tree_sitter/languages/typescript.py
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
def get_entity_type(self, node: Node, parent: Node | None, content_bytes: bytes) -> EntityType:
	"""
	Determine the EntityType for a TypeScript node.

	Args:
	    node: The tree-sitter node
	    parent: The parent node (if any)
	    content_bytes: Source code content as bytes

	Returns:
	    The entity type

	"""
	node_type = node.type
	logger.debug(
		"Getting entity type for TypeScript node: type=%s, parent_type=%s",
		node_type,
		parent.type if parent else None,
	)

	# Check for TypeScript specific types first
	if node_type in self.config.interface:
		return EntityType.INTERFACE
	if node_type in self.config.type_alias:
		return EntityType.TYPE_ALIAS
	if node_type == "enum_declaration":
		return EntityType.ENUM
	if node_type == "module":  # TS internal modules/namespaces
		return EntityType.NAMESPACE
	if node_type == "namespace_declaration":
		return EntityType.NAMESPACE
	if node_type == "method_signature":
		return EntityType.METHOD
	if node_type == "property_signature":
		return EntityType.PROPERTY

	# Use the JavaScript logic for common types
	return super().get_entity_type(node, parent, content_bytes)

extract_name

extract_name(node: Node, content_bytes: bytes) -> str

Extract the name identifier from a definition node.

Parameters:

Name Type Description Default
node Node

The tree-sitter node

required
content_bytes bytes

Source code content as bytes

required

Returns:

Type Description
str

The extracted name

Source code in src/codemap/processor/tree_sitter/languages/typescript.py
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
def extract_name(self, node: Node, content_bytes: bytes) -> str:
	"""
	Extract the name identifier from a definition node.

	Args:
	    node: The tree-sitter node
	    content_bytes: Source code content as bytes

	Returns:
	    The extracted name

	"""
	# Handle TypeScript-specific node types first
	name_node = None

	if node.type in [
		"interface_declaration",
		"enum_declaration",
		"type_alias_declaration",
		"namespace_declaration",
	] or node.type in ["method_signature", "property_signature"]:
		name_node = node.child_by_field_name("name")

	if name_node:
		try:
			return content_bytes[name_node.start_byte : name_node.end_byte].decode("utf-8", errors="ignore")
		except (UnicodeDecodeError, IndexError, AttributeError) as e:
			logger.warning("Failed to decode TypeScript name: %s", e)
			return f"<decoding-error-{node.type}>"

	# Fall back to JavaScript name extraction
	return super().extract_name(node, content_bytes)

get_body_node

get_body_node(node: Node) -> Node | None

Get the node representing the 'body' of a definition.

Parameters:

Name Type Description Default
node Node

The tree-sitter node

required

Returns:

Type Description
Node | None

The body node if available, None otherwise

Source code in src/codemap/processor/tree_sitter/languages/typescript.py
145
146
147
148
149
150
151
152
153
154
155
156
157
158
def get_body_node(self, node: Node) -> Node | None:
	"""
	Get the node representing the 'body' of a definition.

	Args:
	    node: The tree-sitter node

	Returns:
	    The body node if available, None otherwise

	"""
	if node.type in ("interface_declaration", "function_signature", "method_signature"):
		return None  # Interfaces and signatures have no body block
	return super().get_body_node(node)

get_children_to_process

get_children_to_process(
	node: Node, body_node: Node | None
) -> list[Node]

Get the list of child nodes that should be recursively processed.

Parameters:

Name Type Description Default
node Node

The tree-sitter node

required
body_node Node | None

The body node if available

required

Returns:

Type Description
list[Node]

List of child nodes to process

Source code in src/codemap/processor/tree_sitter/languages/typescript.py
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
def get_children_to_process(self, node: Node, body_node: Node | None) -> list[Node]:
	"""
	Get the list of child nodes that should be recursively processed.

	Args:
	    node: The tree-sitter node
	    body_node: The body node if available

	Returns:
	    List of child nodes to process

	"""
	# TypeScript-specific handling
	if node.type == "type_alias_declaration":
		# Type aliases don't have children to process
		return []

	# Fall back to JavaScript children processing
	return super().get_children_to_process(node, body_node)

extract_imports

extract_imports(
	node: Node, content_bytes: bytes
) -> list[str]

Extract imported module names from a TypeScript import statement.

Parameters:

Name Type Description Default
node Node

The tree-sitter node representing an import statement

required
content_bytes bytes

Source code content as bytes

required

Returns:

Type Description
list[str]

List of imported module names as strings

Source code in src/codemap/processor/tree_sitter/languages/typescript.py
180
181
182
183
184
185
186
187
188
189
190
191
192
193
def extract_imports(self, node: Node, content_bytes: bytes) -> list[str]:
	"""
	Extract imported module names from a TypeScript import statement.

	Args:
	    node: The tree-sitter node representing an import statement
	    content_bytes: Source code content as bytes

	Returns:
	    List of imported module names as strings

	"""
	# TypeScript import statements are the same as JavaScript
	return super().extract_imports(node, content_bytes)

get_enclosing_node_of_type

get_enclosing_node_of_type(
	node: Node, target_type: EntityType
) -> Node | None

Find the first ancestor node matching the target TypeScript entity type.

Handles INTERFACE specifically and falls back to the JavaScript handler for other types (CLASS, FUNCTION, METHOD, MODULE).

Parameters:

Name Type Description Default
node Node

The starting node.

required
target_type EntityType

The EntityType to search for in ancestors.

required

Returns:

Type Description
Node | None

The ancestor node if found, otherwise None.

Source code in src/codemap/processor/tree_sitter/languages/typescript.py
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
def get_enclosing_node_of_type(self, node: Node, target_type: EntityType) -> Node | None:
	"""
	Find the first ancestor node matching the target TypeScript entity type.

	Handles INTERFACE specifically and falls back to the JavaScript handler
	for other types (CLASS, FUNCTION, METHOD, MODULE).

	Args:
	    node: The starting node.
	    target_type: The EntityType to search for in ancestors.

	Returns:
	    The ancestor node if found, otherwise None.

	"""
	if target_type == EntityType.INTERFACE:
		target_node_types = ["interface_declaration"]
		current = node.parent
		while current:
			if current.type in target_node_types:
				return current
			current = current.parent
		return None
	# Fall back to JS handler for other types (CLASS, FUNCTION, METHOD, MODULE)
	return super().get_enclosing_node_of_type(node, target_type)