mirror of
https://github.com/samiyev/puaros.git
synced 2025-12-27 23:06:54 +05:00
feat(ipuaro): add decorator extraction to initial context
Extract decorators from classes and methods for NestJS/Angular support.
Decorators are now shown in initial context:
- @Controller('users') class UserController
- @Get(':id') async getUser(id: string): Promise<User>
Changes:
- Add decorators field to FunctionInfo, MethodInfo, ClassInfo
- Update ASTParser to extract decorators from tree-sitter nodes
- Update formatFileSummary to display decorators prefix
- Add 18 unit tests for decorator extraction and formatting
This commit is contained in:
@@ -696,4 +696,140 @@ third: value3`
|
||||
expect(ast.enums).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("decorators (0.24.4)", () => {
|
||||
it("should extract class decorator", () => {
|
||||
const code = `@Controller('users')
|
||||
class UserController {}`
|
||||
const ast = parser.parse(code, "ts")
|
||||
|
||||
expect(ast.classes).toHaveLength(1)
|
||||
expect(ast.classes[0].decorators).toHaveLength(1)
|
||||
expect(ast.classes[0].decorators[0]).toBe("@Controller('users')")
|
||||
})
|
||||
|
||||
it("should extract multiple class decorators", () => {
|
||||
const code = `@Controller('api')
|
||||
@Injectable()
|
||||
@UseGuards(AuthGuard)
|
||||
class ApiController {}`
|
||||
const ast = parser.parse(code, "ts")
|
||||
|
||||
expect(ast.classes).toHaveLength(1)
|
||||
expect(ast.classes[0].decorators).toHaveLength(3)
|
||||
expect(ast.classes[0].decorators[0]).toBe("@Controller('api')")
|
||||
expect(ast.classes[0].decorators[1]).toBe("@Injectable()")
|
||||
expect(ast.classes[0].decorators[2]).toBe("@UseGuards(AuthGuard)")
|
||||
})
|
||||
|
||||
it("should extract method decorators", () => {
|
||||
const code = `class UserController {
|
||||
@Get(':id')
|
||||
@Auth()
|
||||
async getUser() {}
|
||||
}`
|
||||
const ast = parser.parse(code, "ts")
|
||||
|
||||
expect(ast.classes).toHaveLength(1)
|
||||
expect(ast.classes[0].methods).toHaveLength(1)
|
||||
expect(ast.classes[0].methods[0].decorators).toHaveLength(2)
|
||||
expect(ast.classes[0].methods[0].decorators[0]).toBe("@Get(':id')")
|
||||
expect(ast.classes[0].methods[0].decorators[1]).toBe("@Auth()")
|
||||
})
|
||||
|
||||
it("should extract exported decorated class", () => {
|
||||
const code = `@Injectable()
|
||||
export class UserService {}`
|
||||
const ast = parser.parse(code, "ts")
|
||||
|
||||
expect(ast.classes).toHaveLength(1)
|
||||
expect(ast.classes[0].isExported).toBe(true)
|
||||
expect(ast.classes[0].decorators).toHaveLength(1)
|
||||
expect(ast.classes[0].decorators[0]).toBe("@Injectable()")
|
||||
})
|
||||
|
||||
it("should extract decorator with complex arguments", () => {
|
||||
const code = `@Module({
|
||||
imports: [UserModule],
|
||||
controllers: [AppController],
|
||||
providers: [AppService]
|
||||
})
|
||||
class AppModule {}`
|
||||
const ast = parser.parse(code, "ts")
|
||||
|
||||
expect(ast.classes).toHaveLength(1)
|
||||
expect(ast.classes[0].decorators).toHaveLength(1)
|
||||
expect(ast.classes[0].decorators[0]).toContain("@Module")
|
||||
expect(ast.classes[0].decorators[0]).toContain("imports")
|
||||
})
|
||||
|
||||
it("should extract decorated class with extends", () => {
|
||||
const code = `@Entity()
|
||||
class User extends BaseEntity {}`
|
||||
const ast = parser.parse(code, "ts")
|
||||
|
||||
expect(ast.classes).toHaveLength(1)
|
||||
expect(ast.classes[0].extends).toBe("BaseEntity")
|
||||
expect(ast.classes[0].decorators).toHaveLength(1)
|
||||
expect(ast.classes[0].decorators![0]).toBe("@Entity()")
|
||||
})
|
||||
|
||||
it("should handle class without decorators", () => {
|
||||
const code = `class SimpleClass {}`
|
||||
const ast = parser.parse(code, "ts")
|
||||
|
||||
expect(ast.classes).toHaveLength(1)
|
||||
expect(ast.classes[0].decorators).toHaveLength(0)
|
||||
})
|
||||
|
||||
it("should handle method without decorators", () => {
|
||||
const code = `class SimpleClass {
|
||||
simpleMethod() {}
|
||||
}`
|
||||
const ast = parser.parse(code, "ts")
|
||||
|
||||
expect(ast.classes).toHaveLength(1)
|
||||
expect(ast.classes[0].methods).toHaveLength(1)
|
||||
expect(ast.classes[0].methods[0].decorators).toHaveLength(0)
|
||||
})
|
||||
|
||||
it("should handle function without decorators", () => {
|
||||
const code = `function simpleFunc() {}`
|
||||
const ast = parser.parse(code, "ts")
|
||||
|
||||
expect(ast.functions).toHaveLength(1)
|
||||
expect(ast.functions[0].decorators).toHaveLength(0)
|
||||
})
|
||||
|
||||
it("should handle arrow function without decorators", () => {
|
||||
const code = `const arrowFn = () => {}`
|
||||
const ast = parser.parse(code, "ts")
|
||||
|
||||
expect(ast.functions).toHaveLength(1)
|
||||
expect(ast.functions[0].decorators).toHaveLength(0)
|
||||
})
|
||||
|
||||
it("should extract NestJS controller pattern", () => {
|
||||
const code = `@Controller('users')
|
||||
export class UserController {
|
||||
@Get()
|
||||
findAll() {}
|
||||
|
||||
@Get(':id')
|
||||
findOne() {}
|
||||
|
||||
@Post()
|
||||
@Body()
|
||||
create() {}
|
||||
}`
|
||||
const ast = parser.parse(code, "ts")
|
||||
|
||||
expect(ast.classes).toHaveLength(1)
|
||||
expect(ast.classes[0].decorators).toContain("@Controller('users')")
|
||||
expect(ast.classes[0].methods).toHaveLength(3)
|
||||
expect(ast.classes[0].methods[0].decorators).toContain("@Get()")
|
||||
expect(ast.classes[0].methods[1].decorators).toContain("@Get(':id')")
|
||||
expect(ast.classes[0].methods[2].decorators).toContain("@Post()")
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1773,4 +1773,244 @@ describe("prompts", () => {
|
||||
expect(context).toContain("- enum VeryLongEnumName")
|
||||
})
|
||||
})
|
||||
|
||||
describe("decorators (0.24.4)", () => {
|
||||
it("should format function with decorators", () => {
|
||||
const structure: ProjectStructure = {
|
||||
name: "test",
|
||||
rootPath: "/test",
|
||||
files: ["controller.ts"],
|
||||
directories: [],
|
||||
}
|
||||
const asts = new Map<string, FileAST>([
|
||||
[
|
||||
"controller.ts",
|
||||
{
|
||||
imports: [],
|
||||
exports: [],
|
||||
functions: [
|
||||
{
|
||||
name: "getUser",
|
||||
lineStart: 1,
|
||||
lineEnd: 5,
|
||||
params: [
|
||||
{
|
||||
name: "id",
|
||||
type: "string",
|
||||
optional: false,
|
||||
hasDefault: false,
|
||||
},
|
||||
],
|
||||
isAsync: true,
|
||||
isExported: true,
|
||||
returnType: "Promise<User>",
|
||||
decorators: ["@Get(':id')"],
|
||||
},
|
||||
],
|
||||
classes: [],
|
||||
interfaces: [],
|
||||
typeAliases: [],
|
||||
parseError: false,
|
||||
},
|
||||
],
|
||||
])
|
||||
|
||||
const context = buildInitialContext(structure, asts)
|
||||
|
||||
expect(context).toContain("- @Get(':id') async getUser(id: string): Promise<User>")
|
||||
})
|
||||
|
||||
it("should format class with decorators", () => {
|
||||
const structure: ProjectStructure = {
|
||||
name: "test",
|
||||
rootPath: "/test",
|
||||
files: ["controller.ts"],
|
||||
directories: [],
|
||||
}
|
||||
const asts = new Map<string, FileAST>([
|
||||
[
|
||||
"controller.ts",
|
||||
{
|
||||
imports: [],
|
||||
exports: [],
|
||||
functions: [],
|
||||
classes: [
|
||||
{
|
||||
name: "UserController",
|
||||
lineStart: 1,
|
||||
lineEnd: 20,
|
||||
methods: [],
|
||||
properties: [],
|
||||
implements: [],
|
||||
isExported: true,
|
||||
isAbstract: false,
|
||||
decorators: ["@Controller('users')"],
|
||||
},
|
||||
],
|
||||
interfaces: [],
|
||||
typeAliases: [],
|
||||
parseError: false,
|
||||
},
|
||||
],
|
||||
])
|
||||
|
||||
const context = buildInitialContext(structure, asts)
|
||||
|
||||
expect(context).toContain("- @Controller('users') class UserController")
|
||||
})
|
||||
|
||||
it("should format class with multiple decorators", () => {
|
||||
const structure: ProjectStructure = {
|
||||
name: "test",
|
||||
rootPath: "/test",
|
||||
files: ["service.ts"],
|
||||
directories: [],
|
||||
}
|
||||
const asts = new Map<string, FileAST>([
|
||||
[
|
||||
"service.ts",
|
||||
{
|
||||
imports: [],
|
||||
exports: [],
|
||||
functions: [],
|
||||
classes: [
|
||||
{
|
||||
name: "UserService",
|
||||
lineStart: 1,
|
||||
lineEnd: 30,
|
||||
methods: [],
|
||||
properties: [],
|
||||
implements: ["IUserService"],
|
||||
isExported: true,
|
||||
isAbstract: false,
|
||||
decorators: ["@Injectable()", "@Singleton()"],
|
||||
},
|
||||
],
|
||||
interfaces: [],
|
||||
typeAliases: [],
|
||||
parseError: false,
|
||||
},
|
||||
],
|
||||
])
|
||||
|
||||
const context = buildInitialContext(structure, asts)
|
||||
|
||||
expect(context).toContain(
|
||||
"- @Injectable() @Singleton() class UserService implements IUserService",
|
||||
)
|
||||
})
|
||||
|
||||
it("should format function with multiple decorators", () => {
|
||||
const structure: ProjectStructure = {
|
||||
name: "test",
|
||||
rootPath: "/test",
|
||||
files: ["controller.ts"],
|
||||
directories: [],
|
||||
}
|
||||
const asts = new Map<string, FileAST>([
|
||||
[
|
||||
"controller.ts",
|
||||
{
|
||||
imports: [],
|
||||
exports: [],
|
||||
functions: [
|
||||
{
|
||||
name: "createUser",
|
||||
lineStart: 1,
|
||||
lineEnd: 10,
|
||||
params: [],
|
||||
isAsync: true,
|
||||
isExported: true,
|
||||
decorators: ["@Post()", "@Auth()", "@ValidateBody()"],
|
||||
},
|
||||
],
|
||||
classes: [],
|
||||
interfaces: [],
|
||||
typeAliases: [],
|
||||
parseError: false,
|
||||
},
|
||||
],
|
||||
])
|
||||
|
||||
const context = buildInitialContext(structure, asts)
|
||||
|
||||
expect(context).toContain("- @Post() @Auth() @ValidateBody() async createUser()")
|
||||
})
|
||||
|
||||
it("should handle function without decorators", () => {
|
||||
const structure: ProjectStructure = {
|
||||
name: "test",
|
||||
rootPath: "/test",
|
||||
files: ["utils.ts"],
|
||||
directories: [],
|
||||
}
|
||||
const asts = new Map<string, FileAST>([
|
||||
[
|
||||
"utils.ts",
|
||||
{
|
||||
imports: [],
|
||||
exports: [],
|
||||
functions: [
|
||||
{
|
||||
name: "helper",
|
||||
lineStart: 1,
|
||||
lineEnd: 5,
|
||||
params: [],
|
||||
isAsync: false,
|
||||
isExported: true,
|
||||
},
|
||||
],
|
||||
classes: [],
|
||||
interfaces: [],
|
||||
typeAliases: [],
|
||||
parseError: false,
|
||||
},
|
||||
],
|
||||
])
|
||||
|
||||
const context = buildInitialContext(structure, asts)
|
||||
|
||||
expect(context).toContain("- helper()")
|
||||
expect(context).not.toContain("@")
|
||||
})
|
||||
|
||||
it("should handle class without decorators", () => {
|
||||
const structure: ProjectStructure = {
|
||||
name: "test",
|
||||
rootPath: "/test",
|
||||
files: ["simple.ts"],
|
||||
directories: [],
|
||||
}
|
||||
const asts = new Map<string, FileAST>([
|
||||
[
|
||||
"simple.ts",
|
||||
{
|
||||
imports: [],
|
||||
exports: [],
|
||||
functions: [],
|
||||
classes: [
|
||||
{
|
||||
name: "SimpleClass",
|
||||
lineStart: 1,
|
||||
lineEnd: 10,
|
||||
methods: [],
|
||||
properties: [],
|
||||
implements: [],
|
||||
isExported: true,
|
||||
isAbstract: false,
|
||||
},
|
||||
],
|
||||
interfaces: [],
|
||||
typeAliases: [],
|
||||
parseError: false,
|
||||
},
|
||||
],
|
||||
])
|
||||
|
||||
const context = buildInitialContext(structure, asts)
|
||||
|
||||
expect(context).toContain("- class SimpleClass")
|
||||
expect(context).not.toContain("@")
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user