-
Notifications
You must be signed in to change notification settings - Fork 17
core: add semantic collection accessor functions #221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Introduce `first()`, `last()`, `at()`, `getOrThrow()`, and `only()` functions in a new collections.ts module to replace generic `defined()` calls on immutable.js collection accessors. These semantic accessors provide better error messages and make the code's intent clearer: - `first(list)` replaces `defined(list.first())` - `last(list)` replaces `defined(list.last())` - `at(list, i)` replaces `defined(list.get(i))` - `getOrThrow(map, key)` replaces `defined(map.get(key))` - `only(set)` gets the single element from a singleton set Migrated all collection-related `defined()` calls in the diagram package to use these new accessors. Non-collection `defined()` calls (for optional properties, method return values, etc.) remain unchanged. https://claude.ai/code/session_01DsZgeTV6JEg4NhrvQ11LWW
Code Review - PR #221This PR introduces semantic collection accessor functions to improve code clarity and error messages. Overall, this is a well-executed refactoring that improves code quality. Here's my detailed feedback: Strengths
Issues & Concerns1. Missing Test Coverage (Critical)The new
Recommendation: Add 2. Type Safety Issue in
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #221 +/- ##
=======================================
Coverage 76.82% 76.82%
=======================================
Files 69 69
Lines 20523 20523
=======================================
+ Hits 15766 15767 +1
+ Misses 4757 4756 -1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Code Review for PR #221: Add semantic collection accessor functionsSummaryThis PR introduces semantic collection accessor functions to replace generic Positive Aspects✅ Excellent code clarity improvement: The semantic function names make code intent much clearer. Compare:
✅ Better error messages: Each function provides context-specific error messages:
✅ Comprehensive migration: All collection-related ✅ Type safety: Proper TypeScript generics preserve type information through the accessor functions. ✅ Clean separation: Non-collection Issues and ConcernsCritical: Missing Test CoverageThis is a blocking issue. The new
Each function should have tests covering:
Example test structure needed: // src/core/tests/collections.test.ts
import { List, Map, Set } from 'immutable';
import { first, last, at, getOrThrow, only } from '../collections';
describe('first', () => {
it('returns first element from non-empty list', () => {
const list = List([1, 2, 3]);
expect(first(list)).toBe(1);
});
it('throws with helpful message for empty list', () => {
const list = List<number>();
expect(() => first(list)).toThrow('Expected non-empty list, got size=0');
});
});
// Similar tests for last, at, getOrThrow, only...Minor: Type Safety Issue in
|
The at() function was incorrectly applied to projectHistory which is a Stack<Uint8Array>, not a List. The at() semantic accessor only works with immutable List types. Reverting to defined() for Stack.get() since Stack doesn't have the same indexed access semantics as List. https://claude.ai/code/session_01DsZgeTV6JEg4NhrvQ11LWW
3757b90 to
62b7ab6
Compare
PR Review: Semantic Collection Accessor FunctionsThis PR introduces type-safe, semantic accessor functions for immutable.js collections to replace generic ✅ Strengths
🐛 Potential Issues1. Undefined vs Null Ambiguity (Medium Severity)The functions only check for const list = List([null, "foo"]);
first(list); // Returns null without errorThis could lead to subtle bugs if calling code expects non-nullable values. Consider:
2. getOrThrow Error Messages Lack Context (Low Severity)The error message for throw new Error(`Missing key: ${key}`);For object keys or symbols, this won't provide useful information. Consider: throw new Error(`Missing key in map of size ${map.size}: ${String(key)}`);3. Type Narrowing Issue in
|
Introduce
first(),last(),at(),getOrThrow(), andonly()functions in a new collections.ts module to replace generic
defined()calls on immutable.js collection accessors.
These semantic accessors provide better error messages and make the
code's intent clearer:
first(list)replacesdefined(list.first())last(list)replacesdefined(list.last())at(list, i)replacesdefined(list.get(i))getOrThrow(map, key)replacesdefined(map.get(key))only(set)gets the single element from a singleton setMigrated all collection-related
defined()calls in the diagram packageto use these new accessors. Non-collection
defined()calls (foroptional properties, method return values, etc.) remain unchanged.
https://claude.ai/code/session_01DsZgeTV6JEg4NhrvQ11LWW