Remove dead code from snippet_generator (#174440)

Randomly stumbled on code that seems to no longer have clients.
This commit is contained in:
Sigurd Meldgaard 2025-11-04 19:17:10 +01:00 committed by GitHub
parent e5d5c01850
commit 511c4e0bfc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -92,104 +92,6 @@ class SnippetGenerator {
});
}
/// Consolidates all of the snippets and the assumptions into one snippet, in
/// order to create a compilable result.
Iterable<SourceLine> consolidateSnippets(List<CodeSample> samples, {bool addMarkers = false}) {
if (samples.isEmpty) {
return <SourceLine>[];
}
final Iterable<SnippetSample> snippets = samples.whereType<SnippetSample>();
final List<SourceLine> snippetLines = <SourceLine>[...snippets.first.assumptions];
for (final SnippetSample sample in snippets) {
parseInput(sample);
snippetLines.addAll(_processBlocks(sample));
}
return snippetLines;
}
/// A RegExp that matches a Dart constructor.
static final RegExp _constructorRegExp = RegExp(r'(const\s+)?_*[A-Z][a-zA-Z0-9<>._]*\(');
/// A serial number so that we can create unique expression names when we
/// generate them.
int _expressionId = 0;
List<SourceLine> _surround(String prefix, Iterable<SourceLine> body, String suffix) {
return <SourceLine>[
if (prefix.isNotEmpty) SourceLine(prefix),
...body,
if (suffix.isNotEmpty) SourceLine(suffix),
];
}
/// Process one block of sample code (the part inside of "```" markers).
/// Splits any sections denoted by "// ..." into separate blocks to be
/// processed separately. Uses a primitive heuristic to make sample blocks
/// into valid Dart code.
List<SourceLine> _processBlocks(CodeSample sample) {
final List<SourceLine> block = sample.parts
.expand<SourceLine>((SkeletonInjection injection) => injection.contents)
.toList();
if (block.isEmpty) {
return <SourceLine>[];
}
return _processBlock(block);
}
List<SourceLine> _processBlock(List<SourceLine> block) {
final String firstLine = block.first.text;
if (firstLine.startsWith('new ') || firstLine.startsWith(_constructorRegExp)) {
_expressionId += 1;
return _surround('dynamic expression$_expressionId = ', block, ';');
} else if (firstLine.startsWith('await ')) {
_expressionId += 1;
return _surround('Future<void> expression$_expressionId() async { ', block, ' }');
} else if (block.first.text.startsWith('class ') || block.first.text.startsWith('enum ')) {
return block;
} else if ((block.first.text.startsWith('_') || block.first.text.startsWith('final ')) &&
block.first.text.contains(' = ')) {
_expressionId += 1;
return _surround('void expression$_expressionId() { ', block.toList(), ' }');
} else {
final List<SourceLine> buffer = <SourceLine>[];
int blocks = 0;
SourceLine? subLine;
final List<SourceLine> subsections = <SourceLine>[];
for (int index = 0; index < block.length; index += 1) {
// Each section of the dart code that is either split by a blank line, or with
// '// ...' is treated as a separate code block.
if (block[index].text.trim().isEmpty || block[index].text == '// ...') {
if (subLine == null) {
continue;
}
blocks += 1;
subsections.addAll(_processBlock(buffer));
buffer.clear();
assert(buffer.isEmpty);
subLine = null;
} else if (block[index].text.startsWith('// ')) {
if (buffer.length > 1) {
// don't include leading comments
// so that it doesn't start with "// " and get caught in this again
buffer.add(SourceLine('/${block[index].text}'));
}
} else {
subLine ??= block[index];
buffer.add(block[index]);
}
}
if (blocks > 0) {
if (subLine != null) {
subsections.addAll(_processBlock(buffer));
}
// Combine all of the subsections into one section, now that they've been processed.
return subsections;
} else {
return block;
}
}
}
/// Parses the input for the various code and description segments, and
/// returns a set of skeleton injections in the order found.
List<SkeletonInjection> parseInput(CodeSample sample) {