Skip to content

Commit 4c37421

Browse files
author
Eric Wheeler
committed
feat: add support for namespace declarations in tree-sitter TypeScript parser
Added query pattern for namespace declarations (internal_module nodes) in the TypeScript parser. This allows the parser to identify and extract namespace declarations from TypeScript code. - Added test case to verify namespace parsing functionality - Added query pattern to capture namespace declarations in typescript.ts Signed-off-by: Eric Wheeler <[email protected]>
1 parent d3f0ef5 commit 4c37421

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/services/tree-sitter/__tests__/parseSourceCodeDefinitions.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,44 @@ describe("treeParserDebug", () => {
458458
expect(result).toContain("case 0:")
459459
expect(result).toContain("case 25:")
460460
})
461+
462+
it("should parse namespace declarations", async function () {
463+
const namespaceContent = `
464+
/**
465+
* Validation namespace containing various validation functions
466+
* @namespace
467+
* @description Contains reusable validation logic
468+
*/
469+
namespace Validation {
470+
/**
471+
* Validates email addresses according to RFC 5322
472+
* @param email - The email address to validate
473+
* @returns boolean indicating if the email is valid
474+
*/
475+
export function isValidEmail(email: string): boolean {
476+
// Email validation logic
477+
return true;
478+
}
479+
480+
/**
481+
* Validates phone numbers in international format
482+
* @param phone - The phone number to validate
483+
* @returns boolean indicating if the phone number is valid
484+
*/
485+
export function isValidPhone(phone: string): boolean {
486+
// Phone validation logic
487+
return true;
488+
}
489+
}
490+
`
491+
mockedFs.readFile.mockResolvedValue(Buffer.from(namespaceContent))
492+
493+
const result = await testParseSourceCodeDefinitions("/test/namespace.tsx", namespaceContent)
494+
expect(result).toBeDefined()
495+
expect(result).toContain("namespace Validation")
496+
expect(result).toContain("isValidEmail")
497+
expect(result).toContain("isValidPhone")
498+
})
461499
})
462500

463501
describe("parseSourceCodeDefinitions", () => {

src/services/tree-sitter/queries/typescript.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- arrow functions (lambda functions)
88
- switch/case statements with complex case blocks
99
- enum declarations with members
10+
- namespace declarations
1011
*/
1112
export default `
1213
(function_signature
@@ -61,4 +62,8 @@ export default `
6162
; Enum declarations
6263
(enum_declaration
6364
name: (identifier) @name.definition.enum) @definition.enum
65+
66+
; Namespace declarations
67+
(internal_module
68+
name: (identifier) @name.definition.namespace) @definition.namespace
6469
`

0 commit comments

Comments
 (0)