[Feature] Improve type safety by reducing 'any' type usage in CDP message handlers
Maintainer thường phản hồi trong vòng 1 ngày
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 35/100
- Loại issue
- Tính năng
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- typescript
- Lĩnh vực
- developer-experience, devtools, tooling
Hướng nghiên cứu
Bắt đầu bằng cách đọc các handler trong src/cdp-proxy/CDPMessageHandlers/ và interface ICDPMessageHandler, sau đó kiểm tra xem đã có các kiểu của giao thức CDP hay chưa. Định nghĩa các kiểu theo từng bước cho các dạng message được liệt kê, thêm guard khi cần và cập nhật các handler để thay thế những chỗ sử dụng any đã xác định, đồng thời giữ nguyên các message đã được xử lý và hành vi của chúng.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Summary
The CDP (Chrome DevTools Protocol) message handler files use any type extensively, reducing type safety and making the code harder to maintain and debug.
Current Situation
High usage of any type in CDP handlers:
src/cdp-proxy/CDPMessageHandlers/hermesCDPMessageHandler.ts:
- processDebuggerCDPMessage(event: any)
- processApplicationCDPMessage(event: any)
- handleCallFunctionOnEvent(event: any): any
- handleFunctionTypeResult(event: any): any
- handlePausedEvent(event: any): any
- handleBreakpointSetting(event: any): any
src/cdp-proxy/CDPMessageHandlers/iOSDirectCDPMessageHandler.ts:
- processDebuggerCDPMessage(event: any)
- processApplicationCDPMessage(event: any)
- processDeprecatedConsoleMessage(event: any)
- params: any = {}
src/cdp-proxy/CDPMessageHandlers/rnCDPMessageHandler.ts:
- processDebuggerCDPMessage(event: any)
- processApplicationCDPMessage(event: any)
- handleAppBundleFirstPauseEvent(event: IProtocolCommand): any
- params: any = event.params
Interface definition:
interface ICDPMessageHandler {
processDebuggerCDPMessage: (event: any) => ProcessedCDPMessage;
processApplicationCDPMessage: (event: any) => ProcessedCDPMessage;
}
Problems with Current Approach
- No compile-time type checking: Can't catch CDP message structure errors at build time
- Poor IntelliSense: No autocomplete for CDP message properties
- Runtime errors: Easy to access non-existent properties without warning
- Maintainability: Harder to understand expected message structure
- Refactoring risk: Changes can break code without TypeScript catching it
Proposed Changes
Phase 1: Define CDP Message Types
Create type definitions for CDP protocol messages:
// cdp-proxy/types/CDPMessages.ts
interface CDPEvent {
method: string;
params?: Record<string, unknown>;
}
interface CDPCallFunctionOnEvent extends CDPEvent {
method: 'Runtime.callFunctionOn';
params: {
functionDeclaration: string;
objectId?: string;
arguments?: any[];
returnByValue?: boolean;
};
}
interface CDPPausedEvent extends CDPEvent {
method: 'Debugger.paused';
params: {
callFrames: CallFrame[];
reason: string;
data?: Record<string, unknown>;
};
}
interface CallFrame {
callFrameId: string;
functionName: string;
location: Location;
scopeChain: Scope[];
}
// ... more type definitions
Phase 2: Update Handler Interfaces
Replace any with proper types:
interface ICDPMessageHandler {
processDebuggerCDPMessage: (event: CDPEvent) => ProcessedCDPMessage;
processApplicationCDPMessage: (event: CDPEvent) => ProcessedCDPMessage;
}
Phase 3: Add Type Guards
Implement type guards for runtime type checking:
function isCDPCallFunctionOnEvent(event: CDPEvent): event is CDPCallFunctionOnEvent {
return event.method === 'Runtime.callFunctionOn';
}
function isCDPPausedEvent(event: CDPEvent): event is CDPPausedEvent {
return event.method === 'Debugger.paused';
}
Phase 4: Update Handler Implementations
Use type guards in handlers:
public processDebuggerCDPMessage(event: CDPEvent): ProcessedCDPMessage {
if (isCDPCallFunctionOnEvent(event)) {
return this.handleCallFunctionOnEvent(event);
}
if (isCDPPausedEvent(event)) {
return this.handlePausedEvent(event);
}
// ...
}
private handleCallFunctionOnEvent(event: CDPCallFunctionOnEvent): ProcessedCDPMessage {
// event.params is now properly typed
const { functionDeclaration, objectId } = event.params;
// ...
}
Benefits
- Type safety: Catch CDP message structure errors at compile time
- Better IntelliSense: Autocomplete for CDP message properties
- Self-documenting: Types serve as documentation for CDP protocol usage
- Refactoring safety: TypeScript catches breaking changes
- Debugging: Easier to identify message structure issues
- Maintenance: Clearer code intent and expected structures
Implementation Strategy
- Start with commonly used CDP message types
- Add type definitions incrementally (one handler at a time)
- Use union types for messages with variants
- Leverage TypeScript's discriminated unions for type narrowing
- Consider using existing CDP protocol type definitions if available
Potential Challenges
- CDP protocol has many message types
- Some messages have dynamic/flexible structures
- Need to balance type safety with flexibility
- Requires understanding CDP protocol specification
Alternative Approaches
- Use
@types/chrome-devtools-protocol: Leverage existing type definitions - Generate types from protocol JSON: Use CDP protocol specification
- Gradual typing: Start with loose types, refine incrementally
- Unknown over any: Use
unknowninstead ofanyfor better safety
References
- CDP Message Handlers:
src/cdp-proxy/CDPMessageHandlers/ - Chrome DevTools Protocol: https://chromedevtools.github.io/devtools-protocol/
- Potential types package:
@types/chrome-devtools-protocol - TypeScript discriminated unions: https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html#discriminating-unions
- Ngôn ngữ chính
- TypeScript
- Star
- 2.7k
- Fork
- 298
- Merge trung bình
- 14 giờ 54 phút
- Pull request đã merge (30 ngày)
- 27
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của microsoft/vscode-react-native
-
[Feature] Avoid duplicate error notificationsCó thể đã có người làm @erenbati đã nhận 2 ngày trước. Đang mởenhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
microsoft/vscode-react-native#2963 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[Feature] Stop Expo Go installation when Quick Pick is canceledCó thể đã có người làm @erenbati đã nhận 17 ngày trước. Đang mởenhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
microsoft/vscode-react-native#2959 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
enhancement follow-up
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
microsoft/vscode-react-native#2951 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[Feature] Update the smoke test VSIX and remove outdated packages during releaseCó thể đã có người làm @EmmaYuan1015 đã nhận 1 ngày trước. Đang mởenhancement
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 35/100
microsoft/vscode-react-native#2997 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[Feature] Expo Doctor output is lost when diagnostics failCó thể đã có người làm @erenbati đã nhận 17 ngày trước. Đang mởenhancement
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 72/100
microsoft/vscode-react-native#2957 ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của microsoft/vscode-react-native
Issue tương tự
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 85/100
-
area:cowork area:desktop documentation enhancement platform:macos
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
anthropics/claude-code#101720 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 77/100
apache/rocketmq-dashboard#6610 ·
Maintainer thường phản hồi trong vòng 3 ngày
-
Add: MS NOWĐang mởcheck:passed streams:add
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Global update drops overrideAccess, so Local API updateGlobal fails filterOptions validation (3.90 regression)Có thể đã có người làm @PINYOPATTANAWASANPORN đã nhận hôm nay. Đang mởarea: core Bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
payloadcms/payload#18674 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày