@@ -2718,24 +2718,28 @@ abstract class ModelElement extends Canonicalization
27182718 return docFrom;
27192719 }
27202720
2721- String __documentationLocal () {
2721+ String _documentationLocal () {
27222722 if (config.dropTextFrom.contains (element.library.name)) {
27232723 return '' ;
27242724 }
27252725 String _rawDocs = computeDocumentationComment ?? '' ;
27262726 _rawDocs = stripComments (_rawDocs) ?? '' ;
27272727 _rawDocs = _injectExamples (_rawDocs);
27282728 _rawDocs = _stripMacroTemplatesAndAddToIndex (_rawDocs);
2729- _rawDocs = _injectMacros (_rawDocs);
27302729 return _rawDocs;
27312730 }
27322731
2733- String get _documentationLocal => _memoizer.memoized (__documentationLocal);
2732+ /// Returns the documentation for this literal element unless
2733+ /// [config.dropTextFrom] indicates it should not be returned. Macro
2734+ /// definitions are stripped, but macros themselves are not injected. This
2735+ /// is a two stage process to avoid ordering problems.
2736+ String get documentationLocal => _memoizer.memoized (_documentationLocal);
27342737
27352738 /// Returns the docs, stripped of their leading comments syntax.
27362739 @override
27372740 String get documentation {
2738- return documentationFrom.map ((e) => e._documentationLocal).join ('<p>' );
2741+ return _injectMacros (
2742+ documentationFrom.map ((e) => e.documentationLocal).join ('<p>' ));
27392743 }
27402744
27412745 Library get definingLibrary => package.findOrCreateLibraryFor (element);
@@ -3376,7 +3380,7 @@ abstract class ModelElement extends Canonicalization
33763380 ///
33773381 /// Example:
33783382 ///
3379- /// You define the template anywhere in the comments like:
3383+ /// You define the template in any comment for a documentable entity like:
33803384 ///
33813385 /// {@template foo}
33823386 /// Foo contents!
@@ -3397,7 +3401,11 @@ abstract class ModelElement extends Canonicalization
33973401 String _injectMacros (String rawDocs) {
33983402 final macroRegExp = new RegExp (r'{@macro\s+([^}]+)}' );
33993403 return rawDocs.replaceAllMapped (macroRegExp, (match) {
3400- return package.getMacro (match[1 ]);
3404+ String macro = package.getMacro (match[1 ]);
3405+ if (macro == null ) {
3406+ warn (PackageWarning .unknownMacro, message: match[1 ]);
3407+ }
3408+ return macro ;
34013409 });
34023410 }
34033411
@@ -3414,7 +3422,7 @@ abstract class ModelElement extends Canonicalization
34143422 r'[ ]*{@template\s+(.+?)}([\s\S]+?){@endtemplate}[ ]*\n?' ,
34153423 multiLine: true );
34163424 return rawDocs.replaceAllMapped (templateRegExp, (match) {
3417- package.addMacro (match[1 ].trim (), match[2 ].trim ());
3425+ package._addMacro (match[1 ].trim (), match[2 ].trim ());
34183426 return "" ;
34193427 });
34203428 }
@@ -3690,6 +3698,7 @@ class Package extends Canonicalization with Nameable, Warnable, Memoizeable {
36903698 final Map <Element , Library > _elementToLibrary = {};
36913699 final Map <String , String > _macros = {};
36923700 bool allLibrariesAdded = false ;
3701+ bool _macrosAdded = false ;
36933702
36943703 Package (Iterable <LibraryElement > libraryElements, this .packageMeta,
36953704 this ._packageWarningOptions, this .context,
@@ -3712,10 +3721,10 @@ class Package extends Canonicalization with Nameable, Warnable, Memoizeable {
37123721 });
37133722
37143723 _implementors.values.forEach ((l) => l.sort ());
3715- // Go through docs of every model element in package to prebuild the macros index
3716- // TODO(jcollins-g): move index building into a cached-on-demand generation
3717- // like most other bits in [Package].
3718- allCanonicalModelElements. forEach ((m) => m.documentation) ;
3724+ // Go through docs of every model element in package to prebuild the macros
3725+ // index.
3726+ allModelElements. forEach ((m) => m.documentationLocal);
3727+ _macrosAdded = true ;
37193728 }
37203729
37213730 Set <String > _allRootDirs () =>
@@ -3877,6 +3886,9 @@ class Package extends Canonicalization with Nameable, Warnable, Memoizeable {
38773886 }
38783887 referredFromPrefix = 'in documentation inherited from' ;
38793888 break ;
3889+ case PackageWarning .unknownMacro:
3890+ warningMessage = "undefined macro [${message }]" ;
3891+ break ;
38803892 case PackageWarning .brokenLink:
38813893 warningMessage = 'dartdoc generated a broken link to: ${message }' ;
38823894 warnablePrefix = 'to element' ;
@@ -4325,9 +4337,13 @@ class Package extends Canonicalization with Nameable, Warnable, Memoizeable {
43254337 Iterable <ModelElement > get allCanonicalModelElements =>
43264338 _memoizer.memoized (_allCanonicalModelElements);
43274339
4328- String getMacro (String name) => _macros[name];
4340+ String getMacro (String name) {
4341+ assert (_macrosAdded);
4342+ return _macros[name];
4343+ }
43294344
4330- void addMacro (String name, String content) {
4345+ void _addMacro (String name, String content) {
4346+ assert (! _macrosAdded);
43314347 _macros[name] = content;
43324348 }
43334349}
0 commit comments