Skip to content

MAINT: frontend Knip cleanups for dependencies, unused exports and duplicated constants - #4418

Open
Abhiram Shaji (abhiramshaji-cyber) wants to merge 2 commits into
microsoft:mainfrom
abhiramshaji-cyber:chore/frontend-knip-cleanups
Open

Abhiram Shaji (abhiramshaji-cyber) wants to merge 2 commits into
microsoft:mainfrom
abhiramshaji-cyber:chore/frontend-knip-cleanups

Conversation

@abhiramshaji-cyber

Copy link
Copy Markdown

Description

Knip still flagged 20 real leftovers in the frontend after #4413, and this PR fixes all but one, which is held back as a question below.

This is the second PR from #3730, following Roman Lutz (@romanlutz)'s list there. Thanks again for rerunning Knip on newer main and catching what had drifted.

What this code is for

frontend/ is the PyRIT web UI, built with Vite and tested with Jest. #4413 added frontend/knip.json so Knip skips the fixture, transformer and type false positives. What Knip reports after that is the real list this PR works through: dependencies in package.json, and exports in src/ that nothing imports.

Before this PR

A contributor runs Knip 5.88 in frontend/ with the knip.json from #4413, on main at 9a4db873.

  1. Knip reads package.json: scheduler and ts-node are listed but nothing imports them.
  2. Knip reads src/test-utils/autoRefresh.ts: it imports @jest/globals, which is not listed, only installed through jest.
  3. Knip reads src/: 16 exports have no importer, including parseChatRoute in src/utils/routeParams.ts, which nothing calls at all.
  4. BASELINE_TECHNIQUE is not reported, because ScenarioPresetEditor.test.tsx does import * as scenarioConfigForm, but the same object is still defined twice, in scenarioConfigForm.ts and scenarioLaunchForm.ts.
  5. Result: 2 unused devDependencies, 1 unlisted dependency, 16 unused exports and 1 unused type (ScenarioRunEstimator).

After this PR

Same contributor, same command.

  1. package.json lists @jest/globals at 30.5.2, the same version as jest, and no longer lists scheduler or ts-node.
  2. parseChatRoute, ScenarioRunEstimator, techniqueSetOptionLabel and techniqueSetDisplayName are gone.
  3. The constants used only in their own file are no longer exported.
  4. BASELINE_TECHNIQUE and MAX_SEED_SEARCH_LENGTH each have one definition that the other file imports.
  5. Result: 1 finding, COMMON_SCENARIO_PARAMETER_NAMES in scenarioLaunchRequest.ts, left alone on purpose (see the question at the end).
flowchart LR

classDef box fill:#eef1ff,stroke:#4c5bd4,color:#111827
classDef bad fill:#ffd6d6,stroke:#cc0000,color:#111827,stroke-width:3px
classDef good fill:#c8f0c8,stroke:#008800,color:#111827,stroke-width:3px

subgraph BEFORE["BEFORE: 20 Knip findings"]
direction TB
  B1["contributor runs knip in frontend"] --> B2["reads package.json"]
  B2 --> B3["reads every export in src"]
  B3 --> B4{"listed or exported but never imported?"}
  B4 -->|"yes for 20 items"| B5["2 deps, 1 unlisted, 16 exports, 1 type"]
  B4 -->|"no"| B6["used code, not reported"]
end

subgraph AFTER["AFTER: 1 Knip finding, held on purpose"]
direction TB
  A1["contributor runs knip in frontend"] --> A2["reads package.json"]
  A2 --> A3["reads every export in src"]
  A3 --> A4{"THE CHANGE: deps fixed, dead code removed"}
  A4 -->|"only one left"| A5["COMMON_SCENARIO_PARAMETER_NAMES"]
  A4 -->|"everything else"| A6["lint, typecheck, build still pass"]
end

B1 ~~~ A1

class B1,B2,B3,B6 box
class A1,A2,A3,A6 box
class B4,B5 bad
class A4,A5 good

linkStyle 3 stroke:#cc0000,stroke-width:5px
linkStyle 8 stroke:#008800,stroke-width:5px

style BEFORE fill:#fff5f5,stroke:#cc0000,color:#111827
style AFTER fill:#f3fff3,stroke:#008800,color:#111827
Loading

The change

Two commits:

  • package.json and package-lock.json: add @jest/globals 30.5.2, remove ts-node and scheduler (done with npm). The lockfile only drops ts-node and the packages it pulled in. scheduler 0.28.0 stays installed through react-dom, so the /scheduler/ chunk match in vite.config.ts still finds it. As you suggested, the ts-node removal lives in this PR, in the dependency commit separate from the source edits, so a revert is cheap if CI disagrees.
  • src/:
    • BASELINE_TECHNIQUE is removed from scenarioLaunchForm.ts, and ScenarioDetail.tsx now imports it from ./scenarioConfigForm, which still uses and exports it, so the import * as scenarioConfigForm test keeps working.
    • services/api.ts now imports MAX_SEED_SEARCH_LENGTH from seedExampleQuery.ts instead of keeping its own copy. That's the first import from components inside services, so if you'd rather keep that direction clean I can just unexport it and leave the two copies.
    • Deleted: parseChatRoute (utils/routeParams.ts), ScenarioRunEstimator (types/index.ts), techniqueSetOptionLabel and techniqueSetDisplayName, which only techniqueSetOptionLabel called (scenarioTechniqueSets.ts).
    • No longer exported: MODALITY_ORDER, DEFAULT_DATASET_SORT, MIN_SEED_PAGE_SIZE, MAX_SEED_PAGE_SIZE, isSeedType, HIERARCHY_MAX_NODES, HIERARCHY_CHILD_PAGE_SIZE, ANALYTICS_GROUP_LIMIT, ANALYTICS_MAX_PREDICATES, ANALYTICS_ASR_DEFINITION, ANALYTICS_ALL_ASR_DEFINITION.

Locally, npm run lint, npm run type-check and npm run build pass. On Node 22 with ts-node gone, Jest still loads jest.config.ts and runs the full suite. The only failure I saw was App.drafts.test.tsx "dirty draft confirmation", which was flaky for me on unmodified main too.

What changes for a user

  • Nothing in the UI. No values change. The constants that lost export and the shared copies keep the same values.
  • Fewer dev packages to install (ts-node and its dependencies).
  • Knip goes from 20 findings to 1.

A question on COMMON_SCENARIO_PARAMETER_NAMES

For COMMON_SCENARIO_PARAMETER_NAMES I left both copies as they are. The preset copy in scenarioConfigForm.ts looks like a snapshot of the old ScenarioDetail set from #2931. dataset_overrides (#3143), local_technique_factory (#3449) and scoring_target (#3794) were only added to the launch copy afterwards. Would you like the preset editor to import the shared set from scenarioLaunchRequest.ts, or should presets still be able to edit any of those three?

The two functions that read each set:

dynamicScenarioParameters and its callers
dynamicScenarioParameters, which filters by the smaller set, and its 6 preset editor callers, Functions view in Architect

scenarioDynamicParameters and its callers
scenarioDynamicParameters, which filters by the larger set, and its callers (the launch form and a test helper), Functions view in Architect

Side note: both sets also leave out operation_id and incompatible_technique_policy, which Scenario._common_scenario_parameters declares. I didn't touch that here, just mentioning it in case it's useful for that decision.

Related: #3730, #4413

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the frontend dependency and export cleanup.

No open issues 🎉

1 file not reviewed
  • frontend/package-lock.json: Generated file
What changed in this PR

This PR follows #4413’s Knip configuration work by cleaning up unused dependencies and exports in PyRIT’s frontend, without intended UI behavior changes.

Changes:

  • Add @jest/globals explicitly and remove direct scheduler and ts-node dependencies.
  • Remove unused helpers and a type, and make file-local constants and validation helpers private.
  • Reuse existing baseline-technique and seed-search-limit definitions; leave COMMON_SCENARIO_PARAMETER_NAMES unchanged pending clarification.
File Description
frontend/​src/​utils/​routeParams.ts Remove unused parseChatRoute.
frontend/​src/​utils/​attackAnalytics.ts Make internal analytics constants private.
frontend/​src/​types/​index.ts Remove unused ScenarioRunEstimator type.
frontend/​src/​services/​api.ts Import the existing seed-search limit instead of duplicating it.
frontend/​src/​hooks/​useScenarioResultHierarchy.ts Make hierarchy limits private.
frontend/​src/​components/​Scenarios/​scenarioTechniqueSets.ts Remove unused technique-label helpers.
frontend/​src/​components/​Scenarios/​scenarioLaunchForm.ts Remove the duplicate baseline-technique constant.
frontend/​src/​components/​Scenarios/​ScenarioDetail.tsx Import the baseline technique from scenarioConfigForm.
frontend/​src/​components/​Datasets/​seedExampleQuery.ts Make page-size bounds and seed-type validation private.
frontend/​src/​components/​Datasets/​datasetQuery.ts Make the default dataset sort private.
frontend/​src/​components/​Config/​targetFilters.ts Make modality ordering private.
frontend/​package.json Update direct development dependencies.
frontend/​package-lock.json Sync dependency changes and remove ts-node-only packages.

Review effort: Balanced

This branch has not been deployed

No deployments
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