Files
eslint/tests/tools/code-sample-minimizer.js
7ba26006df chore: replace recast with range approach in code-sample-minimizer (#20682)
* Initial plan

* fix: handle recast parse error in code-sample-minimizer

Agent-Logs-Url: https://github.com/eslint/eslint/sessions/ea5a56e2-5f61-42b3-a684-50079dfb1329

Co-authored-by: nzakas <[email protected]>

* fix: replace recast with astring in code-sample-minimizer to support ES2022+ syntax

Agent-Logs-Url: https://github.com/eslint/eslint/sessions/ec2e84e1-a8b0-4242-b740-153ced228b75

Co-authored-by: nzakas <[email protected]>

* chore: replace astring with source-range approach in code-sample-minimizer

- Remove astring dependency (last published Aug 2024, outside 365-day window)
- Track source modifications as {start, end, replacement} applied to original text
- Fix 32+ crash-causing node types (SwitchCase, CatchClause, ImportSpecifier, etc.)
- Re-parse pruned source for extractRelevantChild to get accurate ranges
- Use source.slice() for code extraction instead of generate()
- Fix no-shadow lint error in removeIrrelevantComments
- Fix comment style in test (single-line to block comment)

Agent-Logs-Url: https://github.com/eslint/eslint/sessions/39ad5e81-c256-463a-b3d7-fa9438696770

Co-authored-by: nzakas <[email protected]>

* remove `recast` dev dependency

---------

Co-authored-by: nzakas <[email protected]>
Co-authored-by: Milos Djermanovic <[email protected]>

Co-authored-by: Hanzo Dev <[email protected]>
2026-05-07 09:45:55 +02:00

109 lines
2.8 KiB
JavaScript

"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const { assert } = require("chai");
const reduceBadExampleSize = require("../../tools/code-sample-minimizer");
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
describe("reduceBadExampleSize()", () => {
it("extracts relevant part of deeply nested code", () => {
const initialCode = `
if (true) {
while (false) {
for (let i = 1; i < 10; i++) {
let j = foo
? bar
: THIS_EXPRESSION_CAUSES_A_BUG
}
}
}
`;
const expectedFinalCode = "THIS_EXPRESSION_CAUSES_A_BUG";
assert.strictEqual(
reduceBadExampleSize({
sourceText: initialCode,
predicate: code =>
code.includes("THIS_EXPRESSION_CAUSES_A_BUG"),
}),
expectedFinalCode,
);
});
it("removes irrelevant parts of AST nodes with many children", () => {
const initialCode = `
foo;
bar;
baz;
let x = [
1,
2,
,
3,
THIS_EXPRESSION_CAUSES_A_BUG,
4
]
quux;
`;
const expectedFinalCode = "THIS_EXPRESSION_CAUSES_A_BUG";
assert.strictEqual(
reduceBadExampleSize({
sourceText: initialCode,
predicate: code =>
code.includes("THIS_EXPRESSION_CAUSES_A_BUG"),
}),
expectedFinalCode,
);
});
it("handles ES2022+ syntax like class fields with comments", () => {
/*
* Previously, recast would crash with "did not recognize object of type PropertyDefinition"
* when trying to attach comments to PropertyDefinition nodes (ES2022 class fields).
*/
const initialCode = `
class Foo {
/* irrelevant comment */
x = THIS_EXPRESSION_CAUSES_A_BUG;
}
`;
const expectedFinalCode = "THIS_EXPRESSION_CAUSES_A_BUG";
assert.strictEqual(
reduceBadExampleSize({
sourceText: initialCode,
predicate: code =>
code.includes("THIS_EXPRESSION_CAUSES_A_BUG"),
}),
expectedFinalCode,
);
});
it("removes irrelevant comments from the source code", () => {
const initialCode = `
var /* aaa */foo = bar;
`;
const expectedFinalCode = "var foo = bar;";
assert.strictEqual(
reduceBadExampleSize({
sourceText: initialCode,
predicate: code =>
code.includes("var") && code.includes("foo = bar"),
}),
expectedFinalCode,
);
});
});