代码重构

工程开发 社区

技能解读

当前原文的自动摘译,尚未人工审校。

何时使用

每当用户提出以下要求时,请激活此技能: 重构、现代化、清理或改进现有代码 将旧版 Angular 模式(构造函数注入、NgModules、BehaviorSubject)转换为当前项目标准 更新 Express 路由、添加 Zod 验证或重新组织后端逻辑 修复 TypeScript 严格模式违规

优先顺序

按此顺序应用更改以避免回归: TypeScript 类型 — 修复隐式 any、添加缺失类型、与 types.ts 对齐 角度模式 — 独立、inject()、信号 后端模式 — Zod 模式验证、身份验证中间件重用 SCSS — 平面选择器、clamp()、删除硬编码断点

角度重构

完整模式位于 ./references/angular-patterns.md。

顶级转型

从(遗留)·到(当前)constructor(private svc: Service)·private readonly svc = inject(Service)``BehaviorSubject<T>+async管道·signal<T>()+模板调用value()``NgModule声明·standalone: true+内联imports: [] this.observable$.pipe(tap(...))副作用 · effect(() => { ... })

清单——角度

[ ] 否 constructor() — 所有依赖都通过 inject() [ ] standalone: true 在每个组件上 [ ] 所有可变状态都是 signal<T>();对于在模板 [ ] 中访问的信号,派生状态是 computed() [ ] protected readonly imports: [] 中只需要 Angular 模块

流狐档案 运行与权限由流狐静态检查推断,不代表作者声明或实际测试结果。
领域
工程开发
作者声明的智能体
未找到明确声明;不据此推断已兼容或已测试
静态检查
88 / 100 · 启发式扫描,不代表运行安全
运行环境证据
未执行;当前没有隔离运行测试证据
作者 / 版本 / 许可证
@tomevault-io · 未声明许可证
上下文用量评级
低消耗
接入复杂程度
即装即用
是否需要外部接口密钥
不需要
兼容的系统
未声明(默认跨平台)
运行环境要求
未检出明确要求
检测到的文件与系统行为
  • 只读
  • 允许写入 / 修改
网络行为
允许外网请求
安装命令数
无(仅作为资料)

需要注意: 未声明工具权限范围;实际权限由运行环境决定。

作者说明文件(原文) SKILL.md

# Code Refactor — Loja Luz do Atlântico

## When to Use

Activate this skill whenever the user asks to:
- Refactor, modernize, clean up, or improve existing code
- Convert legacy Angular patterns (constructor injection, NgModules, `BehaviorSubject`) to current project standards
- Update Express routes, add Zod validation, or reorganize backend logic
- Fix TypeScript strict-mode violations
- Improve SCSS structure or responsiveness

---

## Priority Order

Apply changes in this order to avoid regressions:

1. **TypeScript types** — fix implicit `any`, add missing types, align with `types.ts`
2. **Angular patterns** — standalone, `inject()`, signals
3. **Backend patterns** — Zod schema validation, auth middleware reuse
4. **SCSS** — flat selectors, `clamp()`, remove hardcoded breakpoints

---

## Angular Refactoring

Full patterns in [./references/angular-patterns.md](./references/angular-patterns.md).

### Top transformations

| From (legacy) | To (current) |
|---------------|--------------|
| `constructor(private svc: Service)` | `private readonly svc = inject(Service)` |
| `BehaviorSubject<T>` + `async` pipe | `signal<T>()` + template call `value()` |
| `NgModule` declarations | `standalone: true` + inline `imports: []` |
| `this.observable$.pipe(tap(...))` side effects | `effect(() => { ... })` |
| `ngModel` two-way binding on non-forms | `signal()` + `(input)` event |

### Checklist — Angular

- [ ] No `constructor()` — all deps via `inject()`
- [ ] `standalone: true` on every component
- [ ] All mutable state is `signal<T>()`; derived state is `computed()`
- [ ] `protected readonly` for signals accessed in template
- [ ] Only needed Angular modules in `imports: []`
- [ ] Signals called as functions in template: `{{ value() }}`
- [ ] No implicit `any` — all signals typed explicitly

---

## Backend Refactoring

Full patterns in [./references/backend-patterns.md](./references/backend-patterns.md).

### Top transformations

| From (legacy) | To (current) |
|---------------|--------------|
| Manual `req.body` field access without validation | `schema.parse(req.body)` with Zod |
| Auth check copy-pasted into each route | Extract `requireAdmin(req, res)` guard |
| `try/catch` with `console.error` only | `try/catch` returning proper JSON `{ message: string }` |
| `req.params.id` used directly | Validated/sanitized before use |
| File upload with no MIME check | Use existing `upload` multer config with `allowedImageMimeTypes` |

### Checklist — Backend

- [ ] All incoming payloads validated with a Zod schema (`.parse()` or `.safeParse()`)
- [ ] Auth header validated for every admin route: `req.headers.authorization === \`Bearer ${adminToken}\``
- [ ] Error responses return `{ message: string }` JSON, not plain text
- [ ] No `req.params` / `req.query` values used unsanitized in file paths or SQL
- [ ] Webhook raw body handled before `express.json()` middleware

---

## TypeScript Refactoring

- Replace all `any` with concrete types from `frontend/src/app/types.ts` or inline interfaces
- Replace `||` with `??` when the intent is nullish coalescing (not falsy)
- Replace `as X` type assertions with proper narrowing (`if (x instanceof X)`, type guards)
- Ensure `noImplicitReturns` is satisfied — every branch of a function must return

---

## SCSS Refactoring

- Replace fixed `px` font sizes with `clamp()`: `font-size: clamp(1rem, 3vw, 2rem)`
- Replace deep nesting (`.parent .child .grandchild`) with flat BEM-like selectors (`.card`, `.card-title`, `.card-body`)
- Replace `@media (max-width: 768px)` breakpoints with fluid layouts using CSS Grid `auto-fit` / `minmax` where possible
- Remove `!important` — use specificity instead

---

## Refactoring Workflow

1. **Read** the file(s) to be refactored in full before changing anything
2. **Identify** which category applies (Angular / backend / TypeScript / SCSS)
3. **Apply** the matching checklist above
4. **Preserve** all existing business logic — only change structure, patterns, and syntax
5. **Verify** the project compiles without TypeScript errors after Angular changes

---
> Source: [AisleiAvila/loja](https://github.com/AisleiAvila/loja) — distributed by [TomeVault](https://tomevault.io).
<!-- tomevault:4.0:skill_md:2026-05-22 -->

讨论

基于 GitHub Discussions。登录 GitHub 即可参与讨论、点赞、订阅更新。