Skip to content

Commit ec52392

Browse files
authored
Migrate mustachio to null safety (#2801)
1 parent efe4cce commit ec52392

File tree

7 files changed

+145
-166
lines changed

7 files changed

+145
-166
lines changed

lib/src/mustachio/annotations.dart

Lines changed: 3 additions & 5 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
// See the Mustachio README at tool/mustachio/README.md for high-level
86
// documentation.
97

@@ -95,7 +93,7 @@ class RendererSpec {
9593

9694
final String standardMdTemplate;
9795

98-
final Map<TemplateFormat, Uri> standardTemplateUris;
96+
final Map<TemplateFormat, Uri?> standardTemplateUris;
9997

10098
RendererSpec(
10199
this.name,
@@ -111,8 +109,8 @@ class RendererSpec {
111109
/// Parses a URI from a String which comes from a const annotation object.
112110
///
113111
/// The String value may be the literal value, 'null'.
114-
static Uri _parseUriFromAnnotation(String unparsed) =>
115-
unparsed == 'null' || unparsed == null ? null : Uri.parse(unparsed);
112+
static Uri? _parseUriFromAnnotation(String unparsed) =>
113+
unparsed == 'null' ? null : Uri.parse(unparsed);
116114

117115
ClassElement get contextElement => contextType.element;
118116
}

lib/src/mustachio/parser.dart

Lines changed: 18 additions & 19 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
// See the Mustachio README at tool/mustachio/README.md for high-level
86
// documentation.
97

@@ -50,7 +48,7 @@ class MustachioParser {
5048
/// [sectionKey], the end tag is treated as plain text, not a tag.
5149
/// * if [sectionKey] is null, the end tag is treated as plain text, not a
5250
/// tag.
53-
List<MustachioNode> _parseBlock({String /*?*/ sectionKey}) {
51+
List<MustachioNode> _parseBlock({String? sectionKey}) {
5452
var children = <MustachioNode>[];
5553
var textStartIndex = _index;
5654
var textEndIndex = _index;
@@ -128,7 +126,7 @@ class MustachioParser {
128126
trimTextRight();
129127
}
130128
addTextNode(textStartIndex, textEndIndex);
131-
children.add(result.node);
129+
children.add(result.node!);
132130
textStartIndex = _index;
133131
continue;
134132
}
@@ -193,7 +191,7 @@ class MustachioParser {
193191
///
194192
/// [_index] should be at the character immediately following the `>`
195193
/// character which opens a possible partial tag.
196-
_TagParseResult _parsePartial({@required int tagStartIndex}) {
194+
_TagParseResult _parsePartial({required int tagStartIndex}) {
197195
var startIndex = _index;
198196
int endIndex;
199197
while (true) {
@@ -229,7 +227,7 @@ class MustachioParser {
229227
/// [_index] should be at the character immediately following the `#`
230228
/// character which opens a possible section tag.
231229
_TagParseResult _parseSection(
232-
{@required bool invert, @required int tagStartIndex}) {
230+
{required bool invert, required int tagStartIndex}) {
233231
var parsedKey = _parseKey();
234232
if (parsedKey.type == _KeyParseResultType.notKey) {
235233
return _TagParseResult.notTag;
@@ -239,6 +237,7 @@ class MustachioParser {
239237

240238
var children = _parseBlock(sectionKey: parsedKey.joinedNames);
241239
var span = _sourceFile.span(tagStartIndex, _index);
240+
var parsedKeySpan = parsedKey.span!;
242241

243242
if (parsedKey.names.length > 1) {
244243
// Desugar section with dots into nested sections.
@@ -252,7 +251,7 @@ class MustachioParser {
252251
// [three] section is the singular child node of the [two] section, and
253252
// the [two] section is the singular child of the [one] section.
254253
var lastName = parsedKey.names.last;
255-
var keySpanEndOffset = parsedKey.span.end.offset;
254+
var keySpanEndOffset = parsedKeySpan.end.offset;
256255
var lastNameSpan = _sourceFile.span(
257256
keySpanEndOffset - lastName.length, keySpanEndOffset);
258257
var section = Section([lastName], children,
@@ -262,7 +261,7 @@ class MustachioParser {
262261
// To find the start offset of the ith name, take the length of all of
263262
// the names 0 through `i - 1` re-joined with '.', and a final '.' at
264263
// the end.
265-
var sectionKeyStartOffset = parsedKey.span.start.offset +
264+
var sectionKeyStartOffset = parsedKeySpan.start.offset +
266265
(i == 0 ? 0 : parsedKey.names.take(i).join('.').length + 1);
267266
var keySpan = _sourceFile.span(
268267
sectionKeyStartOffset, sectionKeyStartOffset + sectionKey.length);
@@ -273,7 +272,7 @@ class MustachioParser {
273272
}
274273

275274
return _TagParseResult.ok(Section(parsedKey.names, children,
276-
invert: invert, span: span, keySpan: parsedKey.span));
275+
invert: invert, span: span, keySpan: parsedKeySpan));
277276
}
278277

279278
/// Tries to parse an end tag at [_index].
@@ -295,7 +294,7 @@ class MustachioParser {
295294
///
296295
/// [_index] should be at the character immediately following the `{{`
297296
/// characters which open a possible variable tag.
298-
_TagParseResult _parseVariable({@required int tagStartIndex}) {
297+
_TagParseResult _parseVariable({required int tagStartIndex}) {
299298
var escape = true;
300299
if (_thisChar == $lbrace) {
301300
escape = false;
@@ -311,7 +310,7 @@ class MustachioParser {
311310

312311
var span = _sourceFile.span(tagStartIndex, _index);
313312
return _TagParseResult.ok(Variable(parsedKey.names,
314-
escape: escape, span: span, keySpan: parsedKey.span));
313+
escape: escape, span: span, keySpan: parsedKey.span!));
315314
}
316315

317316
/// Tries to parse a key at [_index].
@@ -436,7 +435,7 @@ class Text implements MustachioNode {
436435
@override
437436
final SourceSpan span;
438437

439-
Text(this.content, {@required this.span});
438+
Text(this.content, {required this.span});
440439

441440
@override
442441
String toString() => 'Text["$content"]';
@@ -457,7 +456,7 @@ class Variable with HasMultiNamedKey implements MustachioNode {
457456
final SourceSpan keySpan;
458457

459458
Variable(this.key,
460-
{@required this.escape, @required this.span, @required this.keySpan});
459+
{required this.escape, required this.span, required this.keySpan});
461460

462461
@override
463462
String toString() => 'Variable[$key, escape=$escape]';
@@ -481,7 +480,7 @@ class Section with HasMultiNamedKey implements MustachioNode {
481480
final SourceSpan keySpan;
482481

483482
Section(this.key, this.children,
484-
{@required this.invert, @required this.span, @required this.keySpan});
483+
{required this.invert, required this.span, required this.keySpan});
485484

486485
@override
487486
String toString() => 'Section[$key, invert=$invert]';
@@ -497,7 +496,7 @@ class Partial implements MustachioNode {
497496

498497
final SourceSpan keySpan;
499498

500-
Partial(this.key, {@required this.span, @required this.keySpan});
499+
Partial(this.key, {required this.span, required this.keySpan});
501500
}
502501

503502
/// An enumeration of types of tag parse results.
@@ -519,10 +518,10 @@ class _TagParseResult {
519518
/// This field is `null` if EOF was reached, or if a tag was not parsed
520519
/// (perhaps it started out like a tag, but was malformed, and so is read as
521520
/// text).
522-
final MustachioNode /*?*/ node;
521+
final MustachioNode? node;
523522

524523
/// The key of an end tag, if an end tag was parsed.
525-
final String /*?*/ endTagKey;
524+
final String? endTagKey;
526525

527526
_TagParseResult(this.type, this.node, this.endTagKey);
528527

@@ -564,12 +563,12 @@ class _KeyParseResult {
564563

565564
/// The source span from where this key was parsed, if this represents a
566565
/// parsed key, othwerwise `null`.
567-
final SourceSpan /*?*/ span;
566+
final SourceSpan? span;
568567

569568
const _KeyParseResult._(this.type, this.names, {this.span});
570569

571570
factory _KeyParseResult(_KeyParseResultType type, String key,
572-
{@required SourceSpan span}) {
571+
{required SourceSpan span}) {
573572
if (key == '.') {
574573
return _KeyParseResult._(type, [key], span: span);
575574
} else {

0 commit comments

Comments
 (0)