Skip to content

Consolidate plugin resolution logic into single utility #3941

@ntotten

Description

@ntotten

Problem

Plugin path resolution logic is duplicated in two places within PrettierEditService.ts:

  1. resolvePlugins() (lines 47-100) - used during formatting
  2. getSelectors() (lines 102-129) - used for language discovery

Both need to handle:

  • Plugin objects (pass through)
  • URLs (file://, data:// - pass through)
  • Absolute paths (convert to file:// for v3 ESM)
  • Relative paths (resolve relative to file being formatted)
  • Package names (resolve via createRequire)

Proposed Solution

Create a single PluginResolver class or utility module that:

  1. Handles all plugin path resolution logic in one place
  2. Provides version-aware URL conversion (file:// for v3 ESM, absolute paths for v2)
  3. Caches resolved plugins per workspace to avoid repeated resolution
  4. Exposes clear methods for both formatting and selector use cases

Example Structure

class PluginResolver {
  private cache = new Map<string, ResolvedPlugin[]>();
  
  async resolvePlugins(
    plugins: PrettierOptions['plugins'],
    filePath: string,
    prettierVersion: string
  ): Promise<ResolvedPlugin[]> {
    // Unified resolution logic
  }
  
  clearCache(workspacePath?: string): void {
    // Clear cache on config changes
  }
}

Benefits

  • Single source of truth for plugin resolution
  • Easier to maintain and test
  • Consistent behavior across all plugin usage
  • Caching improves performance

Relevant Files

  • src/PrettierEditService.ts - resolvePlugins() and getSelectors() methods
  • Consider creating new file: src/PluginResolver.ts

Metadata

Metadata

Assignees

No one assigned

    Labels

    staleNo recent activitytriage-pendingAwaiting initial triage

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions