Skip to content

Commit d39275d

Browse files
authored
Make check_code_samples.dart‎ support the standard example directives, and migrate some @tool directives (flutter#189670)
This PR consists of two parts of changes. ## Migrate some `@tool` directives As part of flutter#189629, this PR migrates specific templates used by the `material_ui` and `cupertino_ui` packages from `{@tool}` directives to standard dartdoc to ensure they render correctly within these packages. This migration is limited to only these essential `{@tool}` instances, as Dartdoc does not currently provide native support for Dartpad. Note that Dartpad notices ("_To see it in action, copy and run this code snippet on Dartpad_") are expanded, unlike in flutter/packages#12120 where they're macros. This is necessary because Dartdoc can't correctly handle nested templates and macros, preventing inner macros from expanding correctly. This PR will be followed by reverting flutter/packages#12271, which constitutes the final step of flutter#189629. I have verified that the package docs will render correctly after the revert. ## Make `check_code_samples.dart‎` support the standard example directives Since the examples now consist of two styles, the legacy style "See code in" and the standard style `@example`, `check_code_samples.dart‎` needs to be updated to recognize the standard `@example` directives so that it won't incorrectly report that these examples files are not used by any docs. ## Pre-launch Checklist - [ ] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [ ] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [ ] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [ ] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [ ] I signed the [CLA]. - [ ] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [ ] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent b4058f9 commit d39275d

15 files changed

Lines changed: 326 additions & 137 deletions

‎dev/bots/check_code_samples.dart‎

Lines changed: 65 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ class SampleChecker {
134134
final Directory flutterRoot;
135135
final FileSystem filesystem;
136136

137-
// The `exampleBase` is where the paths in "See code in" are relative to.
138-
// Defaults to <flutter_root>/packages/flutter.
137+
// The `exampleBase` is where the paths in "See code in" or "{@example}" are
138+
// relative to. Defaults to <flutter_root>/packages/flutter.
139139
Directory get exampleBase => examples.parent.parent;
140140

141141
bool checkCodeSamples() {
@@ -145,14 +145,25 @@ class SampleChecker {
145145
final List<File> exampleFilenames = getExampleFilenames(examples);
146146

147147
// Get a list of all the example link paths that appear in the source files.
148-
final (Set<String> exampleLinks, Set<LinkInfo> malformedLinks) = getExampleLinks(packages);
148+
final (
149+
Set<String> exampleLinks,
150+
Set<LinkInfo> legacyMalformedLinks,
151+
Set<LinkInfo> standardMalformedLinks,
152+
) = getExampleLinks(
153+
packages,
154+
);
149155
// Also add in any that might be found in the dart:ui directory.
150-
final (Set<String> uiExampleLinks, Set<LinkInfo> uiMalformedLinks) = getExampleLinks(
156+
final (
157+
Set<String> uiExampleLinks,
158+
Set<LinkInfo> uiLegacyMalformedLinks,
159+
Set<LinkInfo> uiStandardMalformedLinks,
160+
) = getExampleLinks(
151161
dartUIPath,
152162
);
153163

154164
exampleLinks.addAll(uiExampleLinks);
155-
malformedLinks.addAll(uiMalformedLinks);
165+
legacyMalformedLinks.addAll(uiLegacyMalformedLinks);
166+
standardMalformedLinks.addAll(uiStandardMalformedLinks);
156167

157168
// Get a list of the filenames that were not found in the source files.
158169
final List<String> missingFilenames = checkForMissingLinks(exampleFilenames, exampleLinks);
@@ -165,7 +176,10 @@ class SampleChecker {
165176
// generate new examples.
166177
missingFilenames.removeWhere((String file) => _knownUnlinkedExamples.contains(file));
167178

168-
if (missingFilenames.isEmpty && missingTests.isEmpty && malformedLinks.isEmpty) {
179+
if (missingFilenames.isEmpty &&
180+
missingTests.isEmpty &&
181+
legacyMalformedLinks.isEmpty &&
182+
standardMalformedLinks.isEmpty) {
169183
return true;
170184
}
171185

@@ -188,16 +202,30 @@ class SampleChecker {
188202
foundError(buffer.toString().split('\n'));
189203
}
190204

191-
if (malformedLinks.isNotEmpty) {
205+
if (legacyMalformedLinks.isNotEmpty) {
192206
final buffer = StringBuffer(
193207
'The following malformed links were found in API doc comments:\n',
194208
);
195-
for (final link in malformedLinks) {
209+
for (final link in legacyMalformedLinks) {
196210
buffer.writeln(' $link');
197211
}
198212
buffer.write(
199213
'Correct the formatting of these links so that they match the exact pattern:\n'
200-
r" r'\*\* See code in (?<path>.+) \*\*'",
214+
r" '\*\* See code in (?<path>.+) \*\*'",
215+
);
216+
foundError(buffer.toString().split('\n'));
217+
}
218+
219+
if (standardMalformedLinks.isNotEmpty) {
220+
final buffer = StringBuffer(
221+
'The following malformed links were found in API doc comments:\n',
222+
);
223+
for (final link in standardMalformedLinks) {
224+
buffer.writeln(' $link');
225+
}
226+
buffer.write(
227+
'Correct the formatting of these links so that they match the exact pattern:\n'
228+
" '{@example /<path>}' or '{@example /<path>#<region>}'",
201229
);
202230
foundError(buffer.toString().split('\n'));
203231
}
@@ -233,36 +261,50 @@ class SampleChecker {
233261
return getFiles(directory.childDirectory('lib'), RegExp(r'\d+\.dart$'));
234262
}
235263

236-
(Set<String>, Set<LinkInfo>) getExampleLinks(Directory searchDirectory) {
264+
(Set<String>, Set<LinkInfo>, Set<LinkInfo>) getExampleLinks(Directory searchDirectory) {
237265
final List<File> files = getFiles(searchDirectory, RegExp(r'\.dart$'));
238266
final searchStrings = <String>{};
239-
final malformedStrings = <LinkInfo>{};
240-
final validExampleRe = RegExp(r'\*\* See code in (?<path>.+) \*\*');
267+
final legacyMalformedStrings = <LinkInfo>{};
268+
final standardMalformedStrings = <LinkInfo>{};
269+
final legacyValidExampleRe = RegExp(r'\*\* See code in (?<path>.+) \*\*');
270+
final standardValidExampleRe = RegExp(r'{@example /(?<path>[^#}]+)(?:#.+)?}');
241271
// Looks for some common broken versions of example links. This looks for
242-
// something that is at minimum "///*seecode<something>*" to indicate that it
243-
// looks like an example link. It should be narrowed if we start getting false
244-
// positives.
245-
final malformedLinkRe = RegExp(
272+
// something that is at minimum "///*seecode<something>*" or
273+
// "///{@example<something>}" to indicate that it looks like an example
274+
// link. It should be narrowed if we start getting false positives.
275+
final legacyMalformedLinkRe = RegExp(
246276
r'^(?<malformed>\s*///\s*\*\*?\s*[sS][eE][eE]\s*[Cc][Oo][Dd][Ee].+\*\*?)',
247277
);
278+
final standardMalformedLinkRe = RegExp(r'^(?<malformed>\s*///\s*{@example.*)');
248279
for (final file in files) {
249280
final String contents = file.readAsStringSync();
250281
final List<String> lines = contents.split('\n');
251282
var count = 0;
252283
for (final line in lines) {
253284
count += 1;
254-
final RegExpMatch? validMatch = validExampleRe.firstMatch(line);
255-
if (validMatch != null) {
256-
searchStrings.add(validMatch.namedGroup('path')!);
285+
final RegExpMatch? legacyValidMatch = legacyValidExampleRe.firstMatch(line);
286+
final RegExpMatch? standardValidMatch = standardValidExampleRe.firstMatch(line);
287+
if (legacyValidMatch != null) {
288+
searchStrings.add(legacyValidMatch.namedGroup('path')!);
289+
} else if (standardValidMatch != null) {
290+
searchStrings.add(standardValidMatch.namedGroup('path')!);
257291
}
258-
final RegExpMatch? malformedMatch = malformedLinkRe.firstMatch(line);
292+
final RegExpMatch? legacyMalformedMatch = legacyMalformedLinkRe.firstMatch(line);
293+
final RegExpMatch? standardMalformedMatch = standardMalformedLinkRe.firstMatch(line);
259294
// It's only malformed if it doesn't match the valid RegExp.
260-
if (malformedMatch != null && validMatch == null) {
261-
malformedStrings.add(LinkInfo(malformedMatch.namedGroup('malformed')!, file, count));
295+
if (legacyMalformedMatch != null && legacyValidMatch == null) {
296+
legacyMalformedStrings.add(
297+
LinkInfo(legacyMalformedMatch.namedGroup('malformed')!, file, count),
298+
);
299+
}
300+
if (standardMalformedMatch != null && standardValidMatch == null) {
301+
standardMalformedStrings.add(
302+
LinkInfo(standardMalformedMatch.namedGroup('malformed')!, file, count),
303+
);
262304
}
263305
}
264306
}
265-
return (searchStrings, malformedStrings);
307+
return (searchStrings, legacyMalformedStrings, standardMalformedStrings);
266308
}
267309

268310
List<String> checkForMissingLinks(List<File> exampleFilenames, Set<String> searchStrings) {

‎dev/bots/docs.sh‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ function generate_docs() {
121121
# Install and activate dartdoc.
122122
# When updating to a new dartdoc version, please also update
123123
# `dartdoc_options.yaml` to include newly introduced error and warning types.
124-
"$DART" pub global activate dartdoc 9.0.8
124+
"$DART" pub global activate dartdoc 9.0.9
125125

126126
# Build and install the snippets tool, which resides in
127127
# the dev/docs/snippets directory.

0 commit comments

Comments
 (0)