Skip to content

Latest commit

 

History

History
92 lines (71 loc) · 4.49 KB

v1.md

File metadata and controls

92 lines (71 loc) · 4.49 KB
title Codemod kit v1
description Author a fixture-driven codemod and verify its plan with the installed Offing CLI.

Codemod kit v1

A codemod takes a scan result and plans unified diffs for findings it can repair exactly. A reviewed built-in codemod uses defineCodemod({ id, rules, plan }). Outside this repository, the installed CLI accepts a declarative codemod.json and tests it against local fixtures.

Plan contract

The planner receives the root and the findings that match its declared rules. Each planned patch has a repository-relative path and a unified diff that Git can apply. A refusal identifies the finding and explains why it needs manual judgment. A matched rule with an expected replacement must still name that replacement. Offing reports a refusal when the replacement changes. The codemod may further check the exact source shape and evidence before proposing an edit.

Planning reads input files and returns results. It must be deterministic and idempotent. It must never write files, use the network, run commands, or execute scanned code. An ambiguous file or finding must stay unchanged and carry a refusal with a concrete reason.

External declaration

Create a folder containing codemod.json and a cases/ directory. The example declaration contains the complete v1 shape.

Field Value
schemaVersion offing.codemod.v1
id A lowercase kebab-case identifier with an initial letter
ruleId The exact Ebb rule ID
expectedReplacement The exact replacement string the finding must report
path An exact repository-relative path, using letters, digits, underscores, hyphens, dots and forward slashes
before, after Two different complete lines as literal strings, without line endings

The declaration targets the exact path and line in the finding. Offing checks the replacement and compares the complete line before proposing a patch. A recognized finding with a different line is refused unless the line already matches the expected result. The v1 interpreter supports a single text edit on each matching finding's line. It has no operations for imports, shell commands, writes, or network access.

Fixture layout

Each named case contains before/, after/ and expect.json. Both trees contain the full fixture repo. For built-ins, cases live directly in tests/fixtures/codemods/<id>/.

my-codemod/
  codemod.json
  cases/
    refusable-shape/
      before/
        mise.toml.fixture
      after/
        mise.toml.fixture
      expect.json

The runner removes the .fixture suffix when it materializes a case. Store Dockerfiles as image.fixture as specified in decision 0027. Use ci.yml.workflow.fixture for a neutral workflow basename. The runner restores these names in its temporary copy. Trees may contain regular files and directories with portable names. Git metadata and symlinks are rejected.

expect.json contains refusals, an array of objects with ruleId, path, optional one-based line, and reason. Order follows the plan. Use {"refusals":[]} for a positive case with no refusals. An optional lowercase hex patchSha256 fixes the precise rendered patch bytes.

Conformance

Run the installed CLI with Git on your path and Node.js 22.15 or newer. The standalone Offing binary uses the Node.js executable on your path to start the conformance process.

offing codemod test ./my-codemod

The runner copies each case into temporary storage, scans and plans it twice, checks determinism and refusals, runs git apply --check, and applies the patch to the temporary copy. It compares the output with the after/ tree byte for byte and rejects a second plan with new edits. A case failure exits with status 1 and names the failing case. The directory you pass stays untouched.

Conformance runs reviewed planner code in a dedicated Node process. The Node permission model denies filesystem writes and child processes. Loading networking modules and calling fetch are blocked and checked in that process. This is a regression guard for reviewed code. External declarations are parsed as data and the CLI has no way to load external code given to the command.

The standalone packed-package runner checks a valid edit, an ambiguous line, and rule drift in a clean consumer directory. It needs Node.js, Git and the installed Offing executable.

node path/to/docs/spec/codemod/conformance/check.mjs "$(command -v offing)"