Skip to content

Conversation

Copy link

Copilot AI commented Nov 18, 2025

Analysis identified 608 code clones with 5.04% duplication (11,201 lines, 81,426 tokens). Targeted the two largest duplications for extraction.

Changes

1. Codegen toObjCType Function (56 lines, 443 tokens eliminated)

  • Extracted duplicated type conversion logic from serializeConstantsStruct.js and serializeRegularStruct.js into Utils.js
  • Parameterized to support both std::vector and facebook::react::LazyVector array types

Before:

// Duplicated in both serializeConstantsStruct.js and serializeRegularStruct.js
function toObjCType(hasteModuleName, nullableTypeAnnotation, isOptional = false) {
  const [typeAnnotation, nullable] = unwrapNullable(nullableTypeAnnotation);
  const isRequired = !nullable && !isOptional;
  switch (typeAnnotation.type) {
    case 'ArrayTypeAnnotation':
      return wrapCxxOptional(`std::vector<${toObjCType(...)}>`, isRequired);
    // ... 70+ lines of duplicate switch cases
  }
}

After:

// Utils.js - shared implementation
function toObjCType(hasteModuleName, nullableTypeAnnotation, isOptional = false, arrayTypeWrapper = 'std::vector') {
  // ... unified implementation with parameterized array wrapper
}

// serializeConstantsStruct.js
function toObjCType(hasteModuleName, nullableTypeAnnotation, isOptional = false) {
  return sharedToObjCType(hasteModuleName, nullableTypeAnnotation, isOptional, 'std::vector');
}

2. Version Replacement Scripts (3 clones, ~44 lines eliminated)

  • Created replace-build-utils.js centralizing validation, configuration checking, and tarball extraction logic
  • Refactored replace-rncore-version.js and replace_dependencies_version.js to use shared utilities
  • Eliminated duplicate implementations of validateBuildConfiguration, validateVersion, shouldReplaceConfiguration, and updateLastBuildConfiguration

Impact:

  • Net -17 lines while eliminating major duplications
  • Improved maintainability through centralized logic
  • All 2,936 codegen tests passing
Original prompt

Find and refactor duplicated code


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Refactor duplicated code for improved maintainability Refactor: Extract duplicated code into shared utility modules Nov 18, 2025
Copilot finished work on behalf of coado November 18, 2025 14:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants