Skip to content

Commit 5b92bb4

Browse files
authored
Migrate markdown_processor and matching_link_result to NNBD (#2835)
* Migrate element_type to nnbd * Migrate markdown_processor and matching_link_result
1 parent 53aa4bc commit 5b92bb4

File tree

4 files changed

+40
-39
lines changed

4 files changed

+40
-39
lines changed

lib/src/markdown_processor.dart

Lines changed: 31 additions & 29 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
/// Utility code to convert markdown comments to html.
86
library dartdoc.markdown_processor;
97

@@ -162,7 +160,7 @@ class _IterableBlockParser extends md.BlockParser {
162160
/// Return false if the passed [referable] is an unnamed [Constructor],
163161
/// or if it is shadowing another type of element, or is a parameter of
164162
/// one of the above.
165-
bool _rejectUnnamedAndShadowingConstructors(CommentReferable referable) {
163+
bool _rejectUnnamedAndShadowingConstructors(CommentReferable? referable) {
166164
if (referable is Constructor) {
167165
if (referable.isUnnamedConstructor) return false;
168166
if (referable.enclosingElement
@@ -175,11 +173,11 @@ bool _rejectUnnamedAndShadowingConstructors(CommentReferable referable) {
175173

176174
/// Return false unless the passed [referable] represents a callable object.
177175
/// Allows constructors but does not require them.
178-
bool _requireCallable(CommentReferable referable) =>
176+
bool _requireCallable(CommentReferable? referable) =>
179177
referable is ModelElement && referable.isCallable;
180178

181179
/// Return false unless the passed [referable] represents a constructor.
182-
bool _requireConstructor(CommentReferable referable) =>
180+
bool _requireConstructor(CommentReferable? referable) =>
183181
referable is Constructor;
184182

185183
/// Implements _getMatchingLinkElement via [CommentReferable.referenceBy].
@@ -188,40 +186,42 @@ MatchingLinkResult _getMatchingLinkElementCommentReferable(
188186
var commentReference =
189187
warnable.commentRefs[codeRef] ?? ModelCommentReference.synthetic(codeRef);
190188

191-
bool Function(CommentReferable) filter;
192-
bool Function(CommentReferable) allowTree;
189+
late final bool Function(CommentReferable?) filter;
190+
late final bool Function(CommentReferable?) allowTree;
193191

194192
// Constructor references are pretty ambiguous by nature since they can be
195193
// declared with the same name as the class they are constructing, and even
196194
// if they don't use field-formal parameters, sometimes have parameters
197195
// named the same as members.
198196
// Maybe clean this up with inspiration from constructor tear-off syntax?
199197
if (commentReference.allowUnnamedConstructor) {
198+
allowTree = (_) => true;
200199
// Neither reject, nor require, a default constructor in the event
201200
// the comment reference structure implies one. (We can not require it
202201
// in case a library name is the same as a member class name and the class
203202
// is the intended lookup). For example, [FooClass.FooClass] structurally
204203
// "looks like" a default constructor, so we should allow it here.
205-
filter = commentReference.hasCallableHint ? _requireCallable : null;
204+
filter = commentReference.hasCallableHint ? _requireCallable : (_) => true;
206205
} else if (commentReference.hasConstructorHint &&
207206
commentReference.hasCallableHint) {
207+
allowTree = (_) => true;
208208
// This takes precedence over the callable hint if both are present --
209209
// pick a constructor if and only constructor if we see `new`.
210210
filter = _requireConstructor;
211211
} else if (commentReference.hasCallableHint) {
212+
allowTree = (_) => true;
212213
// Trailing parens indicate we are looking for a callable.
213214
filter = _requireCallable;
214215
} else {
215-
// Without hints, reject unnamed constructors and their parameters to force
216-
// resolution to the class.
217-
filter = _rejectUnnamedAndShadowingConstructors;
218-
219216
if (!commentReference.allowUnnamedConstructorParameter) {
220217
allowTree = _rejectUnnamedAndShadowingConstructors;
218+
} else {
219+
allowTree = (_) => true;
221220
}
221+
// Without hints, reject unnamed constructors and their parameters to force
222+
// resolution to the class.
223+
filter = _rejectUnnamedAndShadowingConstructors;
222224
}
223-
allowTree ??= (_) => true;
224-
filter ??= (_) => true;
225225
var lookupResult = warnable.referenceBy(commentReference.referenceBy,
226226
allowTree: allowTree, filter: filter);
227227

@@ -239,7 +239,10 @@ md.Node _makeLinkNode(String codeRef, Warnable warnable) {
239239
if (linkedElement is ModelElement && linkedElement.isDeprecated) {
240240
anchor.attributes['class'] = 'deprecated';
241241
}
242-
anchor.attributes['href'] = linkedElement.href;
242+
var href = linkedElement.href;
243+
if (href != null) {
244+
anchor.attributes['href'] = href;
245+
}
243246
return anchor;
244247
}
245248
// else this would be linkedElement.linkedName, but link bodies are slightly
@@ -250,9 +253,8 @@ md.Node _makeLinkNode(String codeRef, Warnable warnable) {
250253
// current element.
251254
warnable.warn(PackageWarning.unresolvedDocReference,
252255
message: codeRef,
253-
referredFrom: warnable.documentationIsLocal
254-
? null
255-
: warnable.documentationFrom);
256+
referredFrom:
257+
warnable.documentationIsLocal ? [] : warnable.documentationFrom);
256258
}
257259
}
258260

@@ -328,8 +330,8 @@ Iterable<int> findFreeHangingGenericsPositions(String string) sync* {
328330
}
329331

330332
class MarkdownDocument extends md.Document {
331-
factory MarkdownDocument.withElementLinkResolver(Canonicalization element) {
332-
md.Node /*?*/ linkResolver(String name, [String /*?*/ _]) {
333+
factory MarkdownDocument.withElementLinkResolver(Warnable element) {
334+
md.Node? linkResolver(String name, [String? _]) {
333335
if (name.isEmpty) {
334336
return null;
335337
}
@@ -343,11 +345,11 @@ class MarkdownDocument extends md.Document {
343345
}
344346

345347
MarkdownDocument(
346-
{Iterable<md.BlockSyntax> blockSyntaxes,
347-
Iterable<md.InlineSyntax> inlineSyntaxes,
348-
md.ExtensionSet extensionSet,
349-
md.Resolver linkResolver,
350-
md.Resolver imageLinkResolver})
348+
{Iterable<md.BlockSyntax>? blockSyntaxes,
349+
Iterable<md.InlineSyntax>? inlineSyntaxes,
350+
md.ExtensionSet? extensionSet,
351+
md.Resolver? linkResolver,
352+
md.Resolver? imageLinkResolver})
351353
: super(
352354
blockSyntaxes: blockSyntaxes,
353355
inlineSyntaxes: inlineSyntaxes,
@@ -362,7 +364,7 @@ class MarkdownDocument extends md.Document {
362364
String text, bool processFullText) {
363365
var hasExtendedContent = false;
364366
var lines = LineSplitter.split(text).toList();
365-
md.Node firstNode;
367+
md.Node? firstNode;
366368
var nodes = <md.Node>[];
367369
for (var node in _IterableBlockParser(lines, this).parseLinesGenerator()) {
368370
if (firstNode != null) {
@@ -387,7 +389,7 @@ class MarkdownDocument extends md.Document {
387389
nodes.insertAll(i, inlineNodes);
388390
i += inlineNodes.length - 1;
389391
} else if (node is md.Element && node.children != null) {
390-
_parseInlineContent(node.children);
392+
_parseInlineContent(node.children!);
391393
}
392394
}
393395
}
@@ -407,7 +409,7 @@ class _InlineCodeSyntax extends md.InlineSyntax {
407409

408410
@override
409411
bool onMatch(md.InlineParser parser, Match match) {
410-
var element = md.Element.text('code', _htmlEscape.convert(match[1] /*!*/));
412+
var element = md.Element.text('code', _htmlEscape.convert(match[1]!));
411413
parser.addNode(element);
412414
return true;
413415
}
@@ -416,7 +418,7 @@ class _InlineCodeSyntax extends md.InlineSyntax {
416418
class _AutolinkWithoutScheme extends md.AutolinkSyntax {
417419
@override
418420
bool onMatch(md.InlineParser parser, Match match) {
419-
var url = match[1] /*!*/;
421+
var url = match[1]!;
420422
var text = _htmlEscape.convert(url).replaceFirst(_hideSchemes, '');
421423
var anchor = md.Element.text('a', text);
422424
anchor.attributes['href'] = url;

lib/src/matching_link_result.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
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:dartdoc/src/model/comment_referable.dart';
86
import 'package:dartdoc/src/model/model.dart';
97
import 'package:dartdoc/src/quiver.dart';
108

119
class MatchingLinkResult {
12-
final CommentReferable commentReferable;
10+
final CommentReferable? commentReferable;
1311
final bool warn;
1412

1513
MatchingLinkResult(this.commentReferable, {this.warn = true});

lib/src/quiver.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ Iterable<T> concat<T>(Iterable<Iterable<T>> iterables) =>
1515
// From lib/src/core/hash.dart:
1616

1717
/// Generates a hash code for two objects.
18-
int hash2(Object a, Object b) =>
18+
int hash2(Object? a, Object? b) =>
1919
_finish(_combine(_combine(0, a.hashCode), b.hashCode));
2020

2121
/// Generates a hash code for three objects.
22-
int hash3(Object a, Object b, Object c) => _finish(
22+
int hash3(Object? a, Object? b, Object? c) => _finish(
2323
_combine(_combine(_combine(0, a.hashCode), b.hashCode), c.hashCode));
2424

2525
/// Generates a hash code for four objects.
26-
int hash4(Object a, Object b, Object c, Object d) => _finish(_combine(
26+
int hash4(Object? a, Object? b, Object? c, Object? d) => _finish(_combine(
2727
_combine(_combine(_combine(0, a.hashCode), b.hashCode), c.hashCode),
2828
d.hashCode));
2929

testing/test_package_experiments/lib/constructor_tearoffs.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ library constructor_tearoffs;
1111

1212
abstract class A {
1313
final int number;
14+
1415
/// Even though this is abstract, dartdoc should still allow referring to
1516
/// [A.new].
1617
A.new(this.number);
@@ -59,11 +60,11 @@ typedef Fstring = F<String>;
5960
typedef NotAClass = Function;
6061

6162
/// Mixins don't have constructors either, so disallow `M.new`.
62-
mixin M<T> on C {
63-
}
63+
mixin M<T> on C {}
6464

6565
void func() {}
66-
void funcTypeParams<T extends String, U extends num>(T something, U different) {}
66+
void funcTypeParams<T extends String, U extends num>(
67+
T something, U different) {}
6768

6869
const aFunc = func;
6970
const aFuncParams = funcTypeParams;
@@ -78,4 +79,4 @@ const aTearOffDefaultConstructorArgs = F<String>.new;
7879
const aTearOffDefaultConstructorTypedef = Fstring.new;
7980

8081
// TODO(jcollins-g): does not work @ analyzer 2.2
81-
//const aTearOffDefaultConstructorArgsTypedef = Ft<String>.new;
82+
//const aTearOffDefaultConstructorArgsTypedef = Ft<String>.new;

0 commit comments

Comments
 (0)