Paweł Krystkiewicz
All articles

7 min read

Making migrations stick

A migration only sticks if new code cannot bring the old pattern back. Two practical guards are ESLint bulk suppressions and a ratchet test.

I had a long-lived branch where I had updated every file in the product app to stop using a legacy theme import. When I merged main, lint flagged three files from main that were still using the old import.

A fourth case was worse. Git merged the file without any conflict, but main had added code using the legacy import in a file my branch had rewritten to remove it. The file no longer compiled, and there was nothing left for lint to flag.

The hard part of a migration is the file a teammate will write next week on main, using the old import, because nothing is there to stop them.

Two tools prevent this kind of backsliding. One is a lint baseline that freezes the violations you currently have and rejects any new ones. The other is a test that only lets a number go down. Since ESLint 9.24 the first capability is built in, so I'll start there. Then I'll show what I did on ESLint 8, and why that version was weaker.

The migration

The default export of @acme/theme, usually imported as Theme, is a hard-coded palette. It can't follow the host app's theme, and some of its colors differ from the theme tokens of the same name, so mixing the two gives you slightly off-brand UI.

The replacement is a useTokens() hook that reads from the active theme. Named exports from the old package are still fine, so the ban targets only the default import:

js
// eslint.config.mjs
export default [
  {
    rules: {
      'no-restricted-imports': ['error', {
        paths: [{
          name: '@acme/theme',
          importNames: ['default'],
          message: 'Use useTokens() instead of the legacy default export.',
        }],
      }],
    },
  },
];

Turning the ban on is one config entry. Turning it on without a red CI for the next month takes a baseline.

Freezing the violations you already have

Turn the rule on for everything, then record what's already broken:

sh
npx eslint --suppress-all src

That writes eslint-suppressions.json with a count per file per rule:

json
{
  "src/a.js": { "no-restricted-imports": { "count": 1 } },
  "src/b.js": { "no-restricted-imports": { "count": 1 } }
}

Commit it. From then on, plain eslint src behaves like this (I checked every row on ESLint 9.38):

ChangeResult
New file with the old importerror, exit 1
A listed file gains a second violationboth violations in that file reported, exit 1
A listed file migratedThere are suppressions left that do not occur anymore. exit 2
Listed file renamederror at the new path, exit 1

Two properties do all the work. New code can't add violations anywhere, including inside files that already have some, because the baseline is a count per file. And the baseline can't quietly go stale: migrate a file without updating it and the run fails until you run eslint --prune-suppressions.

That second property sounds like a chore, but it's what keeps the list honest. The baseline is your burn-down chart, and git log -p eslint-suppressions.json shows every step of it.

Three warnings:

  • The stale check only covers files that still exist. Delete a listed file, or rename it and clean it up in the same change, and its entry stays behind with no complaint. Prune after those changes too.
  • --pass-on-unpruned-suppressions turns the stale-entry failure off. Don't put it in CI, or you're back to a list nobody keeps honest.
  • --suppress-all happily re-baselines everything, including the violation someone added this morning. Treat a growing eslint-suppressions.json in a PR like a growing exemption list: it needs a reason.

--suppress-all only records violations of rules at error. With the rule at warn it wrote an empty file.

What I did on ESLint 8

My app was on ESLint 8, which has no suppressions. So I built the same thing by hand: the ban at error for the whole repo, plus an override that switched the rule off for the files not migrated yet.

js
// Migration burn-down: files still on the legacy import.
// Remove entries as they migrate; delete this block when the list is empty.
{
  files: [
    'src/legacy-a.js',
    'src/legacy-b.js',
  ],
  rules: { 'no-restricted-imports': 'off' },
},

That's the flat-config shape, shown for comparison. The ESLint 8 version lives in .eslintrc overrides with the same files and rules.

51 files imported the default. I migrated 5 first, then turned the ban on with the other 46 on the list:

StepFiles on the listFiles importing Theme
Before the workno rule51
First components migratedno rule46
Ban + burn-down list4646
Component-level token reads3433
Styled-component templates11
Last exemption removedblock deleted0

Three commits over three days took the list from 46 to 34 to 1 to gone. Batch size didn't matter: one commit migrated 13 component files, the next rewrote 192 token references across 28 style files, and neither tripped the rule.

See the 34 row? The code says 33. That commit migrated ProfileCard.tsx (its message even names the file), but the entry stayed on the list until the next commit. Nothing complained. ESLint doesn't care about files entries that no longer need exempting.

That's where the hand-made version is weaker:

  • It goes stale silently. Suppressions would have failed the run on ProfileCard.tsx.
  • off is all or nothing. An exempt file can collect any number of new violations. Suppressions count per file.
  • off, not warn. My CI ran eslint --max-warnings=0, so exempt files had to be off. The common opposite is a global warn with CI running lint without --max-warnings. Then the ban blocks nothing. It's a suggestion.

Still on ESLint 8, or using a rule at warn? The override list works. Keep it sorted, one path per line, and check now and then that every listed file still fails the rule.

Ratcheting down non-lint metrics

Lint baselines only cover lint rules. Some migrations are measured by something else.

In the component library, every component's token tree exists once per theme, and ideally every theme has the same keys. They didn't, and a component reading a key that one theme lacks gets undefined in that theme. Hundreds of mismatched keys don't get fixed in one PR. But you can stop the number from growing. This is a toy version of the real test, which covers every component family:

js
// drift.ratchet.test.js
const themeA = require('./theme-a.json');
const themeB = require('./theme-b.json');

// Frozen drift per family: [onlyInA, onlyInB] leaf keys.
// Shrink an entry when you fix drift, never grow one. Delete it at [0, 0].
const KNOWN_DRIFT = {
  button: [1, 1],
  input: [0, 1],
};

const leaves = (node, prefix = '') =>
  node && typeof node === 'object'
    ? Object.entries(node).flatMap(([key, value]) => leaves(value, `${prefix}.${key}`))
    : [prefix];

function measureDrift() {
  const drift = {};
  for (const family of new Set([...Object.keys(themeA), ...Object.keys(themeB)])) {
    const a = leaves(themeA[family] ?? {});
    const b = leaves(themeB[family] ?? {});
    const onlyA = a.filter((path) => !b.includes(path)).length;
    const onlyB = b.filter((path) => !a.includes(path)).length;
    if (onlyA + onlyB > 0) drift[family] = [onlyA, onlyB];
  }
  return drift;
}

test('per-family drift matches the frozen table', () => {
  expect(measureDrift()).toEqual(KNOWN_DRIFT);
});

It's the same idea as the suppressions file, for a metric ESLint can't see. The important choice is toEqual, not toBeLessThanOrEqual:

  • Drift goes up: the test fails. You introduced a mismatch, fix it.
  • Drift goes down: the test fails too. You fixed something, so lower the number in the same PR, or delete the line at [0, 0].

With <=, a fix goes unrecorded, and the next person can spend that slack on new drift without anyone noticing. With exact equality, the number only moves down, in the PR that earned it, where the reviewer sees it. That's exactly what ESLint's exit code 2 does for suppressions.

Freeze per family, not one total. If a change fixes one family and breaks another, the total doesn't move and a total-only test passes. The per-family table fails, and the Jest diff shows both rows.

If you need this for more than one metric, Betterer is a tool built around exactly this kind of ratchet, though its last stable release is from 2022, so check its state first. For one table, a test file is plenty.

Honest caveat: I don't have a burn-down to show for this table. It was the last guard to land. What I can show is that it fails for a new mismatched key, for a fix without a table update, for an entry left at [0, 0], and for one family fixed while another regressed. That's what it's for.

Common pitfalls

Merges git doesn't report. The dangerous merges are the clean ones, like the file from the intro. Land the rule and the baseline on main in the first PR, so main stops producing new violations while you migrate. I did my burn-down on a long-lived branch without merging main and cleaned up four files afterwards.

Growing the baseline to dodge the rule. Nothing technical stops it, with either approach. Make the baseline visible in review: an owner's approval for eslint-suppressions.json or the lint config, and an agreement that it only shrinks.

Lint only sees imports. After the merge I found ten color props typed as legacy palette names (keyof typeof theme.colors). They need no import, so the rule can't see them. It doesn't see require('@acme/theme') or a dynamic import() either. Count what the rule can't see separately.

Deleting the override block at the end. On the ESLint 8 route, delete the whole block when the last file migrates. ESLint makes you: an empty files: [] is a config error in both legacy and flat config.

What I would do differently

I'd put the rule and its baseline on main on day one, before migrating a single file. A ban with a 46-file baseline looks a bit silly for about a week. After that, it's what keeps the migration from sliding back.