initial update to allow an optional parameter to retain required fields#767
initial update to allow an optional parameter to retain required fields#767elisadinsmore wants to merge 1 commit intomainfrom
Conversation
WalkthroughThe pull request updates the Changes
Sequence Diagram(s)sequenceDiagram
participant Plugin as viewMappedPlugin
participant Listener as Job Completed Event Listener
participant UFields as unmappedDestinationFields
participant MFields as mappedFields
Listener->>Plugin: Invoke viewMappedPlugin on job:completed
Plugin->>Listener: Check if options.includeRequired is true
alt options.includeRequired is true
Plugin->>UFields: Iterate through fields
UFields-->>Plugin: Identify fields with 'required' constraint
Plugin->>MFields: Add required fields to mappedFields
end
Plugin->>Listener: Proceed with updating workbook metadata
Possibly related PRs
Suggested reviewers
✨ Finishing Touches
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (3)
plugins/view-mapped/src/view-mapped.ts (3)
75-100: Consider simplifying the nested loop structure for required fields.While the implementation is correct, the deeply nested loops and conditional checks make the code harder to read and could impact performance for large datasets.
Here's a more concise implementation using array methods:
//If the includeRequired bool value is set to true, we will add all required fields to the mappedFields array if (includeRequired) { - for ( - let i = 0; - i < jobPlan.data.plan.unmappedDestinationFields.length; - i++ - ) { - for ( - let j = 0; - j < - jobPlan.data.plan.unmappedDestinationFields[i].destinationField - .constraints?.length; - j++ - ) { - if ( - jobPlan.data.plan.unmappedDestinationFields[i] - .destinationField.constraints[j].type === 'required' - ) { - const requiredFieldKey = - jobPlan.data.plan.unmappedDestinationFields[i] - .destinationField.key - mappedFields.push(requiredFieldKey) - } - } - } + jobPlan.data.plan.unmappedDestinationFields.forEach(field => { + const hasRequiredConstraint = field.destinationField.constraints?.some( + constraint => constraint.type === 'required' + ); + + if (hasRequiredConstraint) { + mappedFields.push(field.destinationField.key); + } + }); }
13-14: Consider adding JSDoc documentation for the options parameter.Adding JSDoc would provide better developer experience for users of this plugin.
Add JSDoc documentation:
/** * This plugin allows you to make the post-mapping sheet only display mapped data + * @param options - Configuration options for the plugin + * @param options.includeRequired - When true, unmapped required fields will also be displayed */ export function viewMappedPlugin(options?: { includeRequired?: boolean }) { const { includeRequired = false } = options
75-75: Fix typo in comment.There's a small typo in the comment.
- //If the includeRequired bool value is set to true, we will add all required fields to the mappedFields array + // If the includeRequired bool value is set to true, we will add all required fields to the mappedFields array
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
plugins/view-mapped/src/view-mapped.ts(3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
- GitHub Check: Setup
🔇 Additional comments (3)
plugins/view-mapped/src/view-mapped.ts (3)
13-14: Good job on parameter addition with backward compatibility.The addition of an optional parameter with a default value is a clean approach that maintains backward compatibility while adding new functionality.
116-116: Accurate comment update reflecting the new behavior.The comment has been properly updated to reflect that both mapped fields and required fields (when
includeRequiredis true) will have their metadata updated.
13-14:Details
❓ Verification inconclusive
Verify that this change works with the PR objectives.
The PR objectives indicate that this update should allow required fields to be retained even if they're not mapped. The implementation logically adds unmapped required fields to the
mappedFieldsarray, which should achieve this goal.Also applies to: 75-100
🏁 Script executed:
#!/bin/bash # Description: Verify that the implementation aligns with PR objectives # Check if the implementation correctly identifies and adds required fields ast-grep --pattern $'if (includeRequired) { $$$ mappedFields.push($field) $$$ }' # Check if there are unit tests for this new functionality rg -A 3 -B 3 "includeRequired.*true" --type=testLength of output: 167
Below is a revised verification approach to double-check that the new logic for including unmapped required fields is correctly implemented and covered by tests. The previous shell commands didn’t return output due to file type mismatches. Please run the scripts below to confirm:
#!/bin/bash # Verify that the includeRequired logic is present and pushes unmapped required fields echo "Searching for the includeRequired branch in plugins/view-mapped/src/view-mapped.ts..." rg -n "if\s*\(includeRequired\)" plugins/view-mapped/src/view-mapped.ts echo "Searching for mappedFields.push usage (indicating required field addition)..." rg -n "mappedFields\.push" plugins/view-mapped/src/view-mapped.ts echo "Searching for unit tests related to includeRequired functionality..." rg -A 5 -B 5 "includeRequired" --glob "*spec.ts"Please verify manually that:
- The file
plugins/view-mapped/src/view-mapped.tscontains the logic checkingif (includeRequired)and pushing the required field intomappedFields.- Unit tests (typically in files matching
*spec.ts) exist that exercise the functionality, confirming that unmapped required fields are retained whenincludeRequiredis set to true.Once you’ve confirmed these points, it’ll ensure that the change aligns with the PR objectives of retaining unmapped required fields.
| mappedFields.push(requiredFieldKey) | ||
| } |
There was a problem hiding this comment.
🛠️ Refactor suggestion
Prevent duplicate field keys in mappedFields array.
If a field is both mapped and required, it could be added to the mappedFields array twice, which might lead to unexpected behavior.
Add a check to prevent duplicates:
- mappedFields.push(requiredFieldKey)
+ if (!mappedFields.includes(requiredFieldKey)) {
+ mappedFields.push(requiredFieldKey)
+ }📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| mappedFields.push(requiredFieldKey) | |
| } | |
| if (!mappedFields.includes(requiredFieldKey)) { | |
| mappedFields.push(requiredFieldKey) | |
| } | |
| } |
| //If the includeRequired bool value is set to true, we will add all required fields to the mappedFields array | ||
| if (includeRequired) { | ||
| for ( | ||
| let i = 0; | ||
| i < jobPlan.data.plan.unmappedDestinationFields.length; | ||
| i++ | ||
| ) { | ||
| for ( | ||
| let j = 0; | ||
| j < | ||
| jobPlan.data.plan.unmappedDestinationFields[i].destinationField | ||
| .constraints?.length; | ||
| j++ | ||
| ) { | ||
| if ( | ||
| jobPlan.data.plan.unmappedDestinationFields[i] | ||
| .destinationField.constraints[j].type === 'required' | ||
| ) { | ||
| const requiredFieldKey = | ||
| jobPlan.data.plan.unmappedDestinationFields[i] | ||
| .destinationField.key | ||
| mappedFields.push(requiredFieldKey) | ||
| } | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Add null-safety checks for constraints property.
The current implementation might cause runtime errors if constraints is undefined for any field.
Add proper null-safety checks with optional chaining:
if (includeRequired) {
for (
let i = 0;
i < jobPlan.data.plan.unmappedDestinationFields.length;
i++
) {
+ const constraints = jobPlan.data.plan.unmappedDestinationFields[i]?.destinationField?.constraints;
+ if (!constraints || constraints.length === 0) continue;
+
for (
let j = 0;
- j <
- jobPlan.data.plan.unmappedDestinationFields[i].destinationField
- .constraints?.length;
+ j < constraints.length;
j++
) {
if (
- jobPlan.data.plan.unmappedDestinationFields[i]
- .destinationField.constraints[j].type === 'required'
+ constraints[j].type === 'required'
) {
const requiredFieldKey =
jobPlan.data.plan.unmappedDestinationFields[i]
.destinationField.key
mappedFields.push(requiredFieldKey)
}
}
}
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| //If the includeRequired bool value is set to true, we will add all required fields to the mappedFields array | |
| if (includeRequired) { | |
| for ( | |
| let i = 0; | |
| i < jobPlan.data.plan.unmappedDestinationFields.length; | |
| i++ | |
| ) { | |
| for ( | |
| let j = 0; | |
| j < | |
| jobPlan.data.plan.unmappedDestinationFields[i].destinationField | |
| .constraints?.length; | |
| j++ | |
| ) { | |
| if ( | |
| jobPlan.data.plan.unmappedDestinationFields[i] | |
| .destinationField.constraints[j].type === 'required' | |
| ) { | |
| const requiredFieldKey = | |
| jobPlan.data.plan.unmappedDestinationFields[i] | |
| .destinationField.key | |
| mappedFields.push(requiredFieldKey) | |
| } | |
| } | |
| } | |
| } | |
| //If the includeRequired bool value is set to true, we will add all required fields to the mappedFields array | |
| if (includeRequired) { | |
| for ( | |
| let i = 0; | |
| i < jobPlan.data.plan.unmappedDestinationFields.length; | |
| i++ | |
| ) { | |
| const constraints = jobPlan.data.plan.unmappedDestinationFields[i]?.destinationField?.constraints; | |
| if (!constraints || constraints.length === 0) continue; | |
| for ( | |
| let j = 0; | |
| j < constraints.length; | |
| j++ | |
| ) { | |
| if (constraints[j].type === 'required') { | |
| const requiredFieldKey = | |
| jobPlan.data.plan.unmappedDestinationFields[i] | |
| .destinationField.key | |
| mappedFields.push(requiredFieldKey) | |
| } | |
| } | |
| } | |
| } |
Please explain how to summarize this PR for the Changelog:
Adding an optional parameter to the plugin that allows the ability to retain required fields if they weren't mapped by the user
Tell code reviewer how and what to test:
Go through the mapping process on a sheet with at least one required field and do not map that field. Ensure it still is displayed in the review stage.