MCPcopy
hub / github.com/eslint/eslint / reduceBadExampleSize

Function reduceBadExampleSize

tools/code-sample-minimizer.js:44–298  ·  view source on GitHub ↗

* Given "bad" source text (e.g. an code sample that causes a rule to crash), tries to return a smaller * piece of source text which is also "bad", to make it easier for a human to figure out where the * problem is. * @param {Object} options Options to process * @param {string} options.sourceText

({
	sourceText,
	predicate,
	parser = {
		parse: (code, options) =>
			espree.parse(code, {
				...options,
				loc: true,
				range: true,
				raw: true,
				tokens: true,
				comment: true,
				eslintVisitorKeys: true,
				eslintScopeManager: true,
				ecmaVersion: espree.latestEcmaVersion,
				sourceType: "script",
			}),
	},
	visitorKeys = evk.KEYS,
})

Source from the content-addressed store, hash-verified

42 * @returns {string} Another piece of "bad" source text, which may or may not be smaller than the original source text.
43 */
44function reduceBadExampleSize({
45 sourceText,
46 predicate,
47 parser = {
48 parse: (code, options) =>
49 espree.parse(code, {
50 ...options,
51 loc: true,
52 range: true,
53 raw: true,
54 tokens: true,
55 comment: true,
56 eslintVisitorKeys: true,
57 eslintScopeManager: true,
58 ecmaVersion: espree.latestEcmaVersion,
59 sourceType: "script",
60 }),
61 },
62 visitorKeys = evk.KEYS,
63}) {
64 let counter = 0;
65
66 /**
67 * Returns a new unique identifier
68 * @returns {string} A name for a new identifier
69 */
70 function generateNewIdentifierName() {
71 return `$${counter++}`;
72 }
73
74 /**
75 * Determines whether a source text sample is "bad"
76 * @param {string} updatedSourceText The sample
77 * @returns {boolean} `true` if the sample is "bad"
78 */
79 function reproducesBadCase(updatedSourceText) {
80 try {
81 parser.parse(updatedSourceText);
82 } catch {
83 return false;
84 }
85
86 return predicate(updatedSourceText);
87 }
88
89 assert(
90 reproducesBadCase(sourceText),
91 "Original source text should reproduce issue",
92 );
93 const ast = parser.parse(sourceText);
94
95 /*
96 * Track committed source-text modifications as { start, end, replacement } objects.
97 * All modifications are non-overlapping — the algorithm only modifies a subtree after
98 * deciding not to remove its ancestor, so ranges at different recursion levels are disjoint.
99 */
100 const modifications = [];
101

Callers 1

Calls 6

reproducesBadCaseFunction · 0.85
pruneIrrelevantSubtreesFunction · 0.85
buildSourceFunction · 0.85
extractRelevantChildFunction · 0.85
removeIrrelevantCommentsFunction · 0.85
assertFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…