Migrate from jscpd
Convert a jscpd config to the duplicates settings of fallow with fallow migrate, check the result, and run both tools for a while.
Your project has a jscpd config with a threshold, a minimum size for a copy, and a list of ignored files. Your CI fails when the share of duplicated code goes above the threshold. fallow migrate reads that config and writes the same limits into the duplicates section of a fallow config file. Then fallow dupes fails at the same threshold. Duplication explains how fallow compares code.
Before you start
- A jscpd config:
.jscpd.json, or thejscpdfield ofpackage.json. - No fallow config file in the project.
fallow migratedoes not overwrite one.
What maps to what
Most jscpd options have a fallow key with the same name. The options that change how code is compared go to duplicates.normalization:
| jscpd | fallow |
|---|---|
threshold | duplicates.threshold |
minTokens | duplicates.minTokens |
minLines | duplicates.minLines |
mode | duplicates.mode |
skipLocal | duplicates.skipLocal |
ignore | duplicates.ignore |
ignoreIdentifiers: true or "consistent" | duplicates.normalization.ignoreIdentifiers and consistentIdentifiers, both true |
ignoreIdentifiers: "blind" | duplicates.normalization.ignoreIdentifiers: true |
ignoreLiterals: true | duplicates.normalization.ignoreStringValues and ignoreNumericValues, both true |
format | Nothing. Fallow finds the JavaScript and TypeScript files itself. |
maxLines, maxSize, ignorePattern, reporters, output, gitignore and the other options | Nothing. The migration prints a warning for each one. For ignorePattern, it proposes a range suppression. |
Steps
The steps use the demo project of these docs, with this .jscpd.json in the project root:
{ "threshold": 5, "minTokens": 60, "minLines": 6, "mode": "weak", "ignore": ["**/*.generated.ts"], "ignoreIdentifiers": true, "format": ["typescript"] }
-
Preview the config. The command writes no file:
npx fallow migrate --dry-run{ "$schema": "https://raw.githubusercontent.com/fallow-rs/fallow/main/schema.json", // Migrated from .jscpd.json "duplicates": { "minTokens": 60, "minLines": 6, "threshold": 5.0, "mode": "weak", "ignore": [ "**/*.generated.ts" ], "normalization": { "ignoreIdentifiers": true, "consistentIdentifiers": true, "ignoreStringValues": false } } } Migrated from: .jscpd.json Warnings (2): [jscpd] `mode`: jscpd's "weak" mode may differ semantically from fallow's "weak" mode. jscpd uses lexer-based tokens while fallow uses AST-based tokens. (suggestion: test with both "weak" and "mild" to find the best match) [jscpd] `format`: fallow auto-detects JS/TS filesThe config has
mode: "weak"and noignoreLiterals, so the migration addsignoreStringValues: false. What differs gives the reason. -
Write the config file:
npx fallow migrateAdd
--tomlto writefallow.toml. -
Run the analysis:
npx fallow dupes● Duplicates (2 clone groups) 17 lines 2 instances spread 2 dup:ca69f412 src/reports/export.ts:3-19 src/utils/summary.ts:7-23 17 lines 2 instances dup:06de2df9 src/reports/invoice.ts:4-20 src/reports/receipt.ts:4-20 ✗ 68 lines (40.7%) duplicated across 4 files (0.02s) Duplication (40.7%) exceeds threshold (5.0%)The migrated
thresholdof 5 percent makes the run fail.invoice.tsandreceipt.tsuse other names for the same code, and the migratedignoreIdentifierslets fallow match them. With these settings, the group ofexport.tsandsummary.tsgets another handle than in a run with the default settings.
Check the result
Compare the clone groups of fallow with the result of jscpd on the same commit. The warning about mode tells you to try a second mode. The fallow modes strict and mild (the default) give the same result, because fallow compares parsed code. To also match copies with other names, try the semantic mode:
npx fallow dupes --mode semantic
What differs
- The fallow
weakmode ignores the text of strings. When the jscpd config hasmode: "weak"withoutignoreLiterals: true, the migration addsignoreStringValues: false, so that strings stay compared. - Fallow compares parsed code, so comments and formatting never hide a copy.
- An
ignoreIdentifiersvalue other thantrue,false,"consistent"or"blind"gets a warning, and the migration skips it.
Run both tools for a while
fallow migrate does not change or delete the jscpd config, so you can run both tools in CI. When the results agree, remove jscpd and its config.
Related
- Duplication: read it next to decide which copies to merge and which to keep.
fallow migrate: each option of the command.fallow dupes: each option and the output.