Skip to content

Commit 0d0d46f

Browse files
authored
Migrate more libraries to NNBD (#2827)
* migrate category * Migrate model_comment_reference and the parser * Fix test failure
1 parent 8d827d1 commit 0d0d46f

File tree

5 files changed

+58
-78
lines changed

5 files changed

+58
-78
lines changed

lib/src/comment_references/model_comment_reference.dart

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
// BSD-style license that can be found in the LICENSE file.
44
//
55

6-
// @dart=2.9
7-
86
import 'package:analyzer/dart/ast/ast.dart';
97
import 'package:analyzer/dart/element/element.dart';
108
import 'package:analyzer/file_system/file_system.dart';
@@ -19,7 +17,7 @@ abstract class ModelCommentReference {
1917
bool get hasConstructorHint;
2018
bool get hasCallableHint;
2119
List<String> get referenceBy;
22-
Element get staticElement;
20+
Element? get staticElement;
2321

2422
/// Construct a [ModelCommentReference] using the analyzer AST.
2523
factory ModelCommentReference(
@@ -35,45 +33,47 @@ abstract class ModelCommentReference {
3533
/// information needed for Dartdoc. Drops link to the [CommentReference]
3634
/// and [ResourceProvider] after construction.
3735
class _ModelCommentReferenceImpl implements ModelCommentReference {
38-
bool _allowUnnamedConstructor;
39-
4036
void _initAllowCache() {
4137
var referencePieces = parsed.whereType<IdentifierNode>().toList();
4238
_allowUnnamedConstructor = false;
4339
_allowUnnamedConstructorParameter = false;
4440
if (referencePieces.length >= 2) {
45-
IdentifierNode nodeLast;
46-
for (var f in referencePieces) {
47-
if (f.text == nodeLast?.text) {
48-
if (identical(referencePieces.last, f)) {
41+
for (var i = 0; i <= referencePieces.length - 2; i++) {
42+
if (referencePieces[i].text == referencePieces[i + 1].text) {
43+
if (i + 2 == referencePieces.length) {
44+
// This looks like an old-style reference to an unnamed
45+
// constructor, e.g. [lib_name.C.C].
4946
_allowUnnamedConstructor = true;
5047
} else {
48+
// This could be a reference to a parameter or type parameter of
49+
// an unnamed/new-declared constructor.
5150
_allowUnnamedConstructorParameter = true;
5251
}
5352
}
54-
nodeLast = f;
5553
}
56-
if (referencePieces.last.text == 'new') {
54+
// e.g. [C.new], which may be the unnamed constructor
55+
if (referencePieces.isNotEmpty && referencePieces.last.text == 'new') {
5756
_allowUnnamedConstructor = true;
5857
}
5958
}
6059
}
6160

61+
bool? _allowUnnamedConstructor;
6262
@override
6363
bool get allowUnnamedConstructor {
6464
if (_allowUnnamedConstructor == null) {
6565
_initAllowCache();
6666
}
67-
return _allowUnnamedConstructor;
67+
return _allowUnnamedConstructor!;
6868
}
6969

70-
bool _allowUnnamedConstructorParameter;
70+
bool? _allowUnnamedConstructorParameter;
7171
@override
7272
bool get allowUnnamedConstructorParameter {
7373
if (_allowUnnamedConstructorParameter == null) {
7474
_initAllowCache();
7575
}
76-
return _allowUnnamedConstructorParameter;
76+
return _allowUnnamedConstructorParameter!;
7777
}
7878

7979
@override
@@ -96,7 +96,7 @@ class _ModelCommentReferenceImpl implements ModelCommentReference {
9696
.toList(growable: false);
9797

9898
@override
99-
final Element staticElement;
99+
final Element? staticElement;
100100

101101
_ModelCommentReferenceImpl(
102102
CommentReference ref, ResourceProvider resourceProvider)
@@ -120,7 +120,6 @@ class _ModelCommentReferenceImpl implements ModelCommentReference {
120120
.substring(ref.offset - token.offset, ref.end - token.offset);
121121
}
122122

123-
List<CommentReferenceNode> _parsed;
124-
List<CommentReferenceNode> get parsed =>
125-
_parsed ??= CommentReferenceParser(codeRef).parse();
123+
late final List<CommentReferenceNode> parsed =
124+
CommentReferenceParser(codeRef).parse();
126125
}

lib/src/comment_references/parser.dart

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
// BSD-style license that can be found in the LICENSE file.
44
//
55

6-
// @dart=2.9
7-
86
import 'package:charcode/charcode.dart';
97
import 'package:meta/meta.dart';
108

@@ -52,7 +50,7 @@ class StringTrie {
5250
var matchChar = toMatch.codeUnitAt(index);
5351
if (children.containsKey(matchChar)) {
5452
lastValid = valid ? index : lastValid;
55-
return children[matchChar].match(toMatch, index + 1, lastValid);
53+
return children[matchChar]!.match(toMatch, index + 1, lastValid);
5654
}
5755
return valid ? index : lastValid;
5856
}
@@ -61,22 +59,19 @@ class StringTrie {
6159
var currentTrie = this;
6260
for (var i in toAdd.codeUnits) {
6361
currentTrie.children.putIfAbsent(i, () => StringTrie());
64-
currentTrie = currentTrie.children[i];
62+
currentTrie = currentTrie.children[i]!;
6563
}
6664
currentTrie.valid = true;
6765
}
6866
}
6967

70-
StringTrie _operatorParseTrie;
71-
StringTrie get operatorParseTrie {
72-
if (_operatorParseTrie == null) {
73-
_operatorParseTrie = StringTrie();
74-
for (var name in operatorNames.keys) {
75-
_operatorParseTrie.addWord(name);
76-
}
68+
late final StringTrie operatorParseTrie = () {
69+
var _operatorParseTrie = StringTrie();
70+
for (var name in operatorNames.keys) {
71+
_operatorParseTrie.addWord(name);
7772
}
7873
return _operatorParseTrie;
79-
}
74+
}();
8075

8176
/// A parser for comment references.
8277
// TODO(jcollins-g): align with [CommentReference] from analyzer AST.
@@ -114,7 +109,7 @@ class CommentReferenceParser {
114109
return [];
115110
}
116111
if (prefixResult.type == _PrefixResultType.parsedConstructorHint) {
117-
children.add(prefixResult.node);
112+
children.add(prefixResult.node!);
118113
}
119114
// [_PrefixResultType.junk] and [_PrefixResultType.missing] we can skip.
120115

@@ -130,13 +125,13 @@ class CommentReferenceParser {
130125
break;
131126
} else if (identifierResult.type ==
132127
_IdentifierResultType.parsedIdentifier) {
133-
children.add(identifierResult.node);
128+
children.add(identifierResult.node!);
134129
var typeVariablesResult = _parseTypeVariables();
135130
if (typeVariablesResult.type == _TypeVariablesResultType.endOfFile) {
136131
break;
137132
} else if (typeVariablesResult.type ==
138133
_TypeVariablesResultType.parsedTypeVariables) {
139-
children.add(typeVariablesResult.node);
134+
children.add(typeVariablesResult.node!);
140135
} else {
141136
assert(typeVariablesResult.type ==
142137
_TypeVariablesResultType.notTypeVariables);
@@ -154,7 +149,7 @@ class CommentReferenceParser {
154149
// Invalid trailing junk; reject the reference.
155150
return [];
156151
} else if (suffixResult.type == _SuffixResultType.parsedCallableHint) {
157-
children.add(suffixResult.node);
152+
children.add(suffixResult.node!);
158153
}
159154

160155
// [_SuffixResultType.junk] or [_SuffixResultType.missing] we can skip.
@@ -209,7 +204,7 @@ class CommentReferenceParser {
209204
/// Advances the index forward to the end of the operator if one is
210205
/// present and returns the operator's name. Otherwise, leaves _index
211206
/// unchanged and returns null.
212-
String _tryParseOperator() {
207+
String? _tryParseOperator() {
213208
var tryIndex = _index;
214209
if (tryIndex + _operatorKeyword.length < codeRef.length &&
215210
codeRef.substring(tryIndex, tryIndex + _operatorKeyword.length) ==
@@ -318,7 +313,6 @@ class CommentReferenceParser {
318313
bool _tryMatchLiteral(String characters,
319314
{bool acceptTrailingWhitespace = true,
320315
bool requireTrailingNonidentifier = false}) {
321-
assert(acceptTrailingWhitespace != null);
322316
if (characters.length + _index > _referenceLength) return false;
323317
int startIndex;
324318
for (startIndex = _index;
@@ -388,7 +382,7 @@ enum _PrefixResultType {
388382
class _PrefixParseResult {
389383
final _PrefixResultType type;
390384

391-
final CommentReferenceNode node;
385+
final CommentReferenceNode? node;
392386

393387
const _PrefixParseResult._(this.type, this.node);
394388

@@ -416,7 +410,7 @@ enum _IdentifierResultType {
416410
class _IdentifierParseResult {
417411
final _IdentifierResultType type;
418412

419-
final IdentifierNode node;
413+
final IdentifierNode? node;
420414

421415
const _IdentifierParseResult._(this.type, this.node);
422416

@@ -440,7 +434,7 @@ enum _TypeVariablesResultType {
440434
class _TypeVariablesParseResult {
441435
final _TypeVariablesResultType type;
442436

443-
final TypeVariablesNode node;
437+
final TypeVariablesNode? node;
444438

445439
const _TypeVariablesParseResult._(this.type, this.node);
446440

@@ -467,7 +461,7 @@ enum _SuffixResultType {
467461
class _SuffixParseResult {
468462
final _SuffixResultType type;
469463

470-
final CommentReferenceNode node;
464+
final CommentReferenceNode? node;
471465

472466
const _SuffixParseResult._(this.type, this.node);
473467

lib/src/model/category.dart

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// @dart=2.9
6-
75
import 'package:analyzer/dart/element/element.dart';
86
import 'package:analyzer/file_system/file_system.dart';
97
import 'package:dartdoc/src/comment_references/model_comment_reference.dart';
@@ -88,14 +86,13 @@ class Category extends Nameable
8886
}
8987

9088
@override
91-
// TODO(jcollins-g): make [Category] a [Warnable]?
92-
Warnable get enclosingElement => null;
89+
Warnable? get enclosingElement => null;
9390

9491
@override
95-
Element get element => null;
92+
Element? get element => null;
9693

9794
@override
98-
String get name => categoryDefinition?.displayName ?? _name;
95+
String get name => categoryDefinition.displayName;
9996

10097
@override
10198
String get sortKey => _name;
@@ -110,22 +107,18 @@ class Category extends Nameable
110107
PackageGraph get packageGraph => package.packageGraph;
111108

112109
@override
113-
Library get canonicalLibrary => null;
110+
Library get canonicalLibrary =>
111+
throw UnimplementedError('Categories can not have associated libraries.');
114112

115113
@override
116114
List<Locatable> get documentationFrom => [this];
117115

118116
@override
119117
DocumentLocation get documentedWhere => package.documentedWhere;
120118

121-
bool _isDocumented;
122-
123119
@override
124-
bool get isDocumented {
125-
_isDocumented ??= documentedWhere != DocumentLocation.missing &&
126-
documentationFile != null;
127-
return _isDocumented;
128-
}
120+
late final bool isDocumented =
121+
documentedWhere != DocumentLocation.missing && documentationFile != null;
129122

130123
@override
131124
String get fullyQualifiedName => name;
@@ -135,41 +128,34 @@ class Category extends Nameable
135128
String get filePath => 'topics/$name-topic.$_fileType';
136129

137130
@override
138-
String get href => isCanonical ? '${package.baseHref}$filePath' : null;
131+
String? get href => isCanonical ? '${package.baseHref}$filePath' : null;
139132

140133
String get categoryLabel => _categoryRenderer.renderCategoryLabel(this);
141134

142135
String get linkedName => _categoryRenderer.renderLinkedName(this);
143136

144-
int _categoryIndex;
145-
146137
/// The position in the container order for this category.
147-
int get categoryIndex {
148-
_categoryIndex ??= package.categories.indexOf(this);
149-
return _categoryIndex;
150-
}
138+
late final int categoryIndex = package.categories.indexOf(this);
151139

152-
CategoryDefinition get categoryDefinition =>
153-
config.categories.categoryDefinitions[sortKey];
140+
late final CategoryDefinition categoryDefinition =
141+
config.categories.categoryDefinitions[sortKey] ??
142+
CategoryDefinition(_name, null, null);
154143

155144
@override
156-
bool get isCanonical => categoryDefinition != null;
145+
bool get isCanonical =>
146+
config.categories.categoryDefinitions.containsKey(sortKey);
157147

158148
@override
159149
String get kind => 'Topic';
160150

161-
File _documentationFile;
162-
163151
@override
164-
File get documentationFile {
165-
if (_documentationFile == null) {
166-
if (categoryDefinition?.documentationMarkdown != null) {
167-
_documentationFile = _config.resourceProvider
168-
.getFile(categoryDefinition.documentationMarkdown);
169-
}
152+
late final File? documentationFile = () {
153+
var documentationMarkdown = categoryDefinition.documentationMarkdown;
154+
if (documentationMarkdown != null) {
155+
return _config.resourceProvider.getFile(documentationMarkdown);
170156
}
171-
return _documentationFile;
172-
}
157+
return null;
158+
}();
173159

174160
@override
175161
Iterable<Class> get classes => _classes;

lib/src/warnings.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,9 @@ const Map<PackageWarning, PackageWarningDefinition> packageWarningDefinitions =
283283
/// with an analyzer [element].
284284
mixin Warnable implements Canonicalization, CommentReferable {
285285
@override
286-
Element get element;
286+
Element? get element;
287287

288-
Warnable get enclosingElement;
288+
Warnable? get enclosingElement;
289289

290290
Package get package;
291291

@@ -450,7 +450,7 @@ class PackageWarningOptions {
450450
}
451451

452452
class PackageWarningCounter {
453-
final Map<Element, Map<PackageWarning, Set<String>>> _countedWarnings = {};
453+
final Map<Element?, Map<PackageWarning, Set<String>>> _countedWarnings = {};
454454
final _items = <Jsonable>[];
455455
final _displayedWarningCounts = <PackageWarning, int>{};
456456
final PackageGraph packageGraph;
@@ -467,7 +467,7 @@ class PackageWarningCounter {
467467

468468
/// An unmodifiable map view of all counted warnings related by their element,
469469
/// warning type, and message.
470-
UnmodifiableMapView<Element, Map<PackageWarning, Set<String>>>
470+
UnmodifiableMapView<Element?, Map<PackageWarning, Set<String>>>
471471
get countedWarnings => UnmodifiableMapView(_countedWarnings);
472472

473473
PackageWarningCounter(this.packageGraph);

test/end2end/model_special_cases_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ void main() {
311311
expect(injectSimpleHtml.documentationAsHtml,
312312
contains(' <div style="opacity: 0.5;">[HtmlInjection]</div>'));
313313
});
314+
314315
test('can inject HTML from tool', () {
315316
var envLine = RegExp(r'^Env: \{', multiLine: true);
316317
expect(envLine.allMatches(injectHtmlFromTool.documentation).length,

0 commit comments

Comments
 (0)