Skip to content

[code-simplifier] refactor(policy): simplify native plugin handling after PR #141 #143

@github-actions

Description

@github-actions

Code Simplification - 2026-05-11

This PR simplifies code from #141 (native PolicyPlugin exports) to improve clarity and reduce duplication while preserving all functionality.

Files Simplified

  • packages/core/src/services/policy.ts — extracted resolveNativePlugin() helper to deduplicate repeated validate+normalize pattern
  • packages/core/src/services/policy/loader.ts — removed redundant meta spread (normalization already done by loadPolicy)
  • packages/core/src/services/readiness/index.ts — replaced options parameter reassignment with a local shadow boolean

Improvements Made

  1. Reduced Duplication

    • The validateNativePlugin(config, source); return normalizeNativePlugin(config) pair appeared identically twice in loadPolicy (local file path branch and npm package branch). Extracted into a private resolveNativePlugin() helper.
  2. Removed Redundant Code

    • In loadPluginChain, native plugins returned by loadPolicy were being spread again with sourceType: "module" and trust: "trusted-code" — but loadPolicy already unconditionally sets these via normalizeNativePlugin. The redundant spread is removed; plugins.push(loaded) is sufficient.
  3. Cleaner State Management

    • runReadinessReport was reassigning the options function parameter (options = { ...options, shadow: true }) to avoid mutating the caller's object. Replaced with a local shadow boolean initialized from options.shadow ?? false and set to true when native plugins are encountered — same semantics, no parameter mutation.

Changes Based On

Recent changes from:

Testing

  • ✅ No functional changes — all code paths produce identical behavior (manually traced)
  • ⚠️ Node/npm execution is sandboxed in this environment; CI will run the full test suite on this PR

Generated by Code Simplifier · ● 4.8M ·

  • expires on May 12, 2026, 11:54 AM UTC

Note

This was originally intended as a pull request, but GitHub Actions is not permitted to create or approve pull requests in this repository.
The changes have been pushed to branch simplify/policy-native-plugin-2026-05-11-3344a8cef306257f.

Click here to create the pull request

To fix the permissions issue, go to SettingsActionsGeneral and enable Allow GitHub Actions to create and approve pull requests. See also: gh-aw FAQ

Show patch preview (138 of 138 lines)
From 43a857e987f3abe77a605988b098a6bcfb355f34 Mon Sep 17 00:00:00 2001
From: "github-actions[bot]" <github-actions[bot]@users.noreply.github.com>
Date: Mon, 11 May 2026 11:52:38 +0000
Subject: [PATCH] refactor(policy): simplify native plugin handling after PR
 #141

- Extract resolveNativePlugin() helper in policy.ts to deduplicate
  the repeated validateNativePlugin + normalizeNativePlugin call pair
- Remove redundant meta spread in loadPluginChain (loadPolicy already
  normalizes sourceType/trust via normalizeNativePlugin before returning)
- Replace options parameter reassignment in runReadinessReport with a
  local shadow boolean variable for clearer intent

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
---
 packages/core/src/services/policy.ts          | 17 +++++++++++------
 packages/core/src/services/policy/loader.ts   | 14 +++-----------
 packages/core/src/services/readiness/index.ts | 13 ++++---------
 3 files changed, 18 insertions(+), 26 deletions(-)

diff --git a/packages/core/src/services/policy.ts b/packages/core/src/services/policy.ts
index 3a299e2..d276eb2 100644
--- a/packages/core/src/services/policy.ts
+++ b/packages/core/src/services/policy.ts
@@ -178,6 +178,15 @@ function normalizeNativePlugin(plugin: PolicyPlugin): PolicyPlugin {
   };
 }
 
+/**
+ * Validate and normalize a module export that passed the isNativePlugin check.
+ * Throws if the plugin is structurally invalid; otherwise returns normalized form.
+ */
+function resolveNativePlugin(config: PolicyPlugin, source: string): PolicyPlugin {
+  validateNativePlugin(config, source);
+  return normalizeNativePlugin(config);
+}
+
 /**
  * Load a policy from a file path or npm specifier.
  *
@@ -219,11 +228,8 @@ export async function loadPolicy(
         // import() treats as a URL scheme (c:), causing ERR_UNSUPPORTED_ESM_URL_SCHEME.
         const mod = (await import(pathToFileURL(resolved).href)) as Record<string, unknown>;
         const config = (mod.default ?? mod) as
... (truncated)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions