Skip to content

Migrate more libraries to NNBD #2827

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 17 additions & 18 deletions lib/src/comment_references/model_comment_reference.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// BSD-style license that can be found in the LICENSE file.
//

// @dart=2.9

import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/file_system/file_system.dart';
Expand All @@ -19,7 +17,7 @@ abstract class ModelCommentReference {
bool get hasConstructorHint;
bool get hasCallableHint;
List<String> get referenceBy;
Element get staticElement;
Element? get staticElement;

/// Construct a [ModelCommentReference] using the analyzer AST.
factory ModelCommentReference(
Expand All @@ -35,45 +33,47 @@ abstract class ModelCommentReference {
/// information needed for Dartdoc. Drops link to the [CommentReference]
/// and [ResourceProvider] after construction.
class _ModelCommentReferenceImpl implements ModelCommentReference {
bool _allowUnnamedConstructor;

void _initAllowCache() {
var referencePieces = parsed.whereType<IdentifierNode>().toList();
_allowUnnamedConstructor = false;
_allowUnnamedConstructorParameter = false;
if (referencePieces.length >= 2) {
IdentifierNode nodeLast;
for (var f in referencePieces) {
if (f.text == nodeLast?.text) {
if (identical(referencePieces.last, f)) {
for (var i = 0; i <= referencePieces.length - 2; i++) {
if (referencePieces[i].text == referencePieces[i + 1].text) {
if (i + 2 == referencePieces.length) {
// This looks like an old-style reference to an unnamed
// constructor, e.g. [lib_name.C.C].
_allowUnnamedConstructor = true;
} else {
// This could be a reference to a parameter or type parameter of
// an unnamed/new-declared constructor.
_allowUnnamedConstructorParameter = true;
}
}
nodeLast = f;
}
if (referencePieces.last.text == 'new') {
// e.g. [C.new], which may be the unnamed constructor
if (referencePieces.isNotEmpty && referencePieces.last.text == 'new') {
_allowUnnamedConstructor = true;
}
}
}

bool? _allowUnnamedConstructor;
@override
bool get allowUnnamedConstructor {
if (_allowUnnamedConstructor == null) {
_initAllowCache();
}
return _allowUnnamedConstructor;
return _allowUnnamedConstructor!;
}

bool _allowUnnamedConstructorParameter;
bool? _allowUnnamedConstructorParameter;
@override
bool get allowUnnamedConstructorParameter {
if (_allowUnnamedConstructorParameter == null) {
_initAllowCache();
}
return _allowUnnamedConstructorParameter;
return _allowUnnamedConstructorParameter!;
}

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

@override
final Element staticElement;
final Element? staticElement;

_ModelCommentReferenceImpl(
CommentReference ref, ResourceProvider resourceProvider)
Expand All @@ -120,7 +120,6 @@ class _ModelCommentReferenceImpl implements ModelCommentReference {
.substring(ref.offset - token.offset, ref.end - token.offset);
}

List<CommentReferenceNode> _parsed;
List<CommentReferenceNode> get parsed =>
_parsed ??= CommentReferenceParser(codeRef).parse();
late final List<CommentReferenceNode> parsed =
CommentReferenceParser(codeRef).parse();
}
38 changes: 16 additions & 22 deletions lib/src/comment_references/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// BSD-style license that can be found in the LICENSE file.
//

// @dart=2.9

import 'package:charcode/charcode.dart';
import 'package:meta/meta.dart';

Expand Down Expand Up @@ -52,7 +50,7 @@ class StringTrie {
var matchChar = toMatch.codeUnitAt(index);
if (children.containsKey(matchChar)) {
lastValid = valid ? index : lastValid;
return children[matchChar].match(toMatch, index + 1, lastValid);
return children[matchChar]!.match(toMatch, index + 1, lastValid);
}
return valid ? index : lastValid;
}
Expand All @@ -61,22 +59,19 @@ class StringTrie {
var currentTrie = this;
for (var i in toAdd.codeUnits) {
currentTrie.children.putIfAbsent(i, () => StringTrie());
currentTrie = currentTrie.children[i];
currentTrie = currentTrie.children[i]!;
}
currentTrie.valid = true;
}
}

StringTrie _operatorParseTrie;
StringTrie get operatorParseTrie {
if (_operatorParseTrie == null) {
_operatorParseTrie = StringTrie();
for (var name in operatorNames.keys) {
_operatorParseTrie.addWord(name);
}
late final StringTrie operatorParseTrie = () {
var _operatorParseTrie = StringTrie();
for (var name in operatorNames.keys) {
_operatorParseTrie.addWord(name);
}
return _operatorParseTrie;
}
}();

/// A parser for comment references.
// TODO(jcollins-g): align with [CommentReference] from analyzer AST.
Expand Down Expand Up @@ -114,7 +109,7 @@ class CommentReferenceParser {
return [];
}
if (prefixResult.type == _PrefixResultType.parsedConstructorHint) {
children.add(prefixResult.node);
children.add(prefixResult.node!);
}
// [_PrefixResultType.junk] and [_PrefixResultType.missing] we can skip.

Expand All @@ -130,13 +125,13 @@ class CommentReferenceParser {
break;
} else if (identifierResult.type ==
_IdentifierResultType.parsedIdentifier) {
children.add(identifierResult.node);
children.add(identifierResult.node!);
var typeVariablesResult = _parseTypeVariables();
if (typeVariablesResult.type == _TypeVariablesResultType.endOfFile) {
break;
} else if (typeVariablesResult.type ==
_TypeVariablesResultType.parsedTypeVariables) {
children.add(typeVariablesResult.node);
children.add(typeVariablesResult.node!);
} else {
assert(typeVariablesResult.type ==
_TypeVariablesResultType.notTypeVariables);
Expand All @@ -154,7 +149,7 @@ class CommentReferenceParser {
// Invalid trailing junk; reject the reference.
return [];
} else if (suffixResult.type == _SuffixResultType.parsedCallableHint) {
children.add(suffixResult.node);
children.add(suffixResult.node!);
}

// [_SuffixResultType.junk] or [_SuffixResultType.missing] we can skip.
Expand Down Expand Up @@ -209,7 +204,7 @@ class CommentReferenceParser {
/// Advances the index forward to the end of the operator if one is
/// present and returns the operator's name. Otherwise, leaves _index
/// unchanged and returns null.
String _tryParseOperator() {
String? _tryParseOperator() {
var tryIndex = _index;
if (tryIndex + _operatorKeyword.length < codeRef.length &&
codeRef.substring(tryIndex, tryIndex + _operatorKeyword.length) ==
Expand Down Expand Up @@ -318,7 +313,6 @@ class CommentReferenceParser {
bool _tryMatchLiteral(String characters,
{bool acceptTrailingWhitespace = true,
bool requireTrailingNonidentifier = false}) {
assert(acceptTrailingWhitespace != null);
if (characters.length + _index > _referenceLength) return false;
int startIndex;
for (startIndex = _index;
Expand Down Expand Up @@ -388,7 +382,7 @@ enum _PrefixResultType {
class _PrefixParseResult {
final _PrefixResultType type;

final CommentReferenceNode node;
final CommentReferenceNode? node;

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

Expand Down Expand Up @@ -416,7 +410,7 @@ enum _IdentifierResultType {
class _IdentifierParseResult {
final _IdentifierResultType type;

final IdentifierNode node;
final IdentifierNode? node;

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

Expand All @@ -440,7 +434,7 @@ enum _TypeVariablesResultType {
class _TypeVariablesParseResult {
final _TypeVariablesResultType type;

final TypeVariablesNode node;
final TypeVariablesNode? node;

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

Expand All @@ -467,7 +461,7 @@ enum _SuffixResultType {
class _SuffixParseResult {
final _SuffixResultType type;

final CommentReferenceNode node;
final CommentReferenceNode? node;

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

Expand Down
54 changes: 20 additions & 34 deletions lib/src/model/category.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// @dart=2.9

import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/file_system/file_system.dart';
import 'package:dartdoc/src/comment_references/model_comment_reference.dart';
Expand Down Expand Up @@ -88,14 +86,13 @@ class Category extends Nameable
}

@override
// TODO(jcollins-g): make [Category] a [Warnable]?
Warnable get enclosingElement => null;
Warnable? get enclosingElement => null;

@override
Element get element => null;
Element? get element => null;

@override
String get name => categoryDefinition?.displayName ?? _name;
String get name => categoryDefinition.displayName;

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

@override
Library get canonicalLibrary => null;
Library get canonicalLibrary =>
throw UnimplementedError('Categories can not have associated libraries.');

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

@override
DocumentLocation get documentedWhere => package.documentedWhere;

bool _isDocumented;

@override
bool get isDocumented {
_isDocumented ??= documentedWhere != DocumentLocation.missing &&
documentationFile != null;
return _isDocumented;
}
late final bool isDocumented =
documentedWhere != DocumentLocation.missing && documentationFile != null;

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

@override
String get href => isCanonical ? '${package.baseHref}$filePath' : null;
String? get href => isCanonical ? '${package.baseHref}$filePath' : null;

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

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

int _categoryIndex;

/// The position in the container order for this category.
int get categoryIndex {
_categoryIndex ??= package.categories.indexOf(this);
return _categoryIndex;
}
late final int categoryIndex = package.categories.indexOf(this);

CategoryDefinition get categoryDefinition =>
config.categories.categoryDefinitions[sortKey];
late final CategoryDefinition categoryDefinition =
config.categories.categoryDefinitions[sortKey] ??
CategoryDefinition(_name, null, null);

@override
bool get isCanonical => categoryDefinition != null;
bool get isCanonical =>
config.categories.categoryDefinitions.containsKey(sortKey);

@override
String get kind => 'Topic';

File _documentationFile;

@override
File get documentationFile {
if (_documentationFile == null) {
if (categoryDefinition?.documentationMarkdown != null) {
_documentationFile = _config.resourceProvider
.getFile(categoryDefinition.documentationMarkdown);
}
late final File? documentationFile = () {
var documentationMarkdown = categoryDefinition.documentationMarkdown;
if (documentationMarkdown != null) {
return _config.resourceProvider.getFile(documentationMarkdown);
}
return _documentationFile;
}
return null;
}();

@override
Iterable<Class> get classes => _classes;
Expand Down
8 changes: 4 additions & 4 deletions lib/src/warnings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,9 @@ const Map<PackageWarning, PackageWarningDefinition> packageWarningDefinitions =
/// with an analyzer [element].
mixin Warnable implements Canonicalization, CommentReferable {
@override
Element get element;
Element? get element;

Warnable get enclosingElement;
Warnable? get enclosingElement;

Package get package;

Expand Down Expand Up @@ -450,7 +450,7 @@ class PackageWarningOptions {
}

class PackageWarningCounter {
final Map<Element, Map<PackageWarning, Set<String>>> _countedWarnings = {};
final Map<Element?, Map<PackageWarning, Set<String>>> _countedWarnings = {};
final _items = <Jsonable>[];
final _displayedWarningCounts = <PackageWarning, int>{};
final PackageGraph packageGraph;
Expand All @@ -467,7 +467,7 @@ class PackageWarningCounter {

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

PackageWarningCounter(this.packageGraph);
Expand Down
1 change: 1 addition & 0 deletions test/end2end/model_special_cases_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ void main() {
expect(injectSimpleHtml.documentationAsHtml,
contains(' <div style="opacity: 0.5;">[HtmlInjection]</div>'));
});

test('can inject HTML from tool', () {
var envLine = RegExp(r'^Env: \{', multiLine: true);
expect(envLine.allMatches(injectHtmlFromTool.documentation).length,
Expand Down