From 846871c495ec4a52057ebd214b1a2a59add61e2c Mon Sep 17 00:00:00 2001 From: Sam Rawlins Date: Mon, 20 Sep 2021 15:43:33 -0700 Subject: [PATCH 1/3] Migrat mustachio to null safety --- lib/src/mustachio/annotations.dart | 8 +- lib/src/mustachio/parser.dart | 37 ++++---- lib/src/mustachio/renderer_base.dart | 87 +++++++++---------- tool/mustachio/builder.dart | 42 ++++------ tool/mustachio/codegen_aot_compiler.dart | 43 +++++----- tool/mustachio/codegen_runtime_renderer.dart | 88 ++++++++++---------- tool/mustachio/utilities.dart | 2 - 7 files changed, 145 insertions(+), 162 deletions(-) diff --git a/lib/src/mustachio/annotations.dart b/lib/src/mustachio/annotations.dart index e7854432ec..a0c57e8689 100644 --- a/lib/src/mustachio/annotations.dart +++ b/lib/src/mustachio/annotations.dart @@ -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 - // See the Mustachio README at tool/mustachio/README.md for high-level // documentation. @@ -95,7 +93,7 @@ class RendererSpec { final String standardMdTemplate; - final Map standardTemplateUris; + final Map standardTemplateUris; RendererSpec( this.name, @@ -111,8 +109,8 @@ class RendererSpec { /// Parses a URI from a String which comes from a const annotation object. /// /// The String value may be the literal value, 'null'. - static Uri _parseUriFromAnnotation(String unparsed) => - unparsed == 'null' || unparsed == null ? null : Uri.parse(unparsed); + static Uri? _parseUriFromAnnotation(String unparsed) => + unparsed == 'null' ? null : Uri.parse(unparsed); ClassElement get contextElement => contextType.element; } diff --git a/lib/src/mustachio/parser.dart b/lib/src/mustachio/parser.dart index a33e3c10f7..fbd4136ef5 100644 --- a/lib/src/mustachio/parser.dart +++ b/lib/src/mustachio/parser.dart @@ -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 - // See the Mustachio README at tool/mustachio/README.md for high-level // documentation. @@ -50,7 +48,7 @@ class MustachioParser { /// [sectionKey], the end tag is treated as plain text, not a tag. /// * if [sectionKey] is null, the end tag is treated as plain text, not a /// tag. - List _parseBlock({String /*?*/ sectionKey}) { + List _parseBlock({String? sectionKey}) { var children = []; var textStartIndex = _index; var textEndIndex = _index; @@ -128,7 +126,7 @@ class MustachioParser { trimTextRight(); } addTextNode(textStartIndex, textEndIndex); - children.add(result.node); + children.add(result.node!); textStartIndex = _index; continue; } @@ -193,7 +191,7 @@ class MustachioParser { /// /// [_index] should be at the character immediately following the `>` /// character which opens a possible partial tag. - _TagParseResult _parsePartial({@required int tagStartIndex}) { + _TagParseResult _parsePartial({required int tagStartIndex}) { var startIndex = _index; int endIndex; while (true) { @@ -229,7 +227,7 @@ class MustachioParser { /// [_index] should be at the character immediately following the `#` /// character which opens a possible section tag. _TagParseResult _parseSection( - {@required bool invert, @required int tagStartIndex}) { + {required bool invert, required int tagStartIndex}) { var parsedKey = _parseKey(); if (parsedKey.type == _KeyParseResultType.notKey) { return _TagParseResult.notTag; @@ -239,6 +237,7 @@ class MustachioParser { var children = _parseBlock(sectionKey: parsedKey.joinedNames); var span = _sourceFile.span(tagStartIndex, _index); + var parsedKeySpan = parsedKey.span!; if (parsedKey.names.length > 1) { // Desugar section with dots into nested sections. @@ -252,7 +251,7 @@ class MustachioParser { // [three] section is the singular child node of the [two] section, and // the [two] section is the singular child of the [one] section. var lastName = parsedKey.names.last; - var keySpanEndOffset = parsedKey.span.end.offset; + var keySpanEndOffset = parsedKeySpan.end.offset; var lastNameSpan = _sourceFile.span( keySpanEndOffset - lastName.length, keySpanEndOffset); var section = Section([lastName], children, @@ -262,7 +261,7 @@ class MustachioParser { // To find the start offset of the ith name, take the length of all of // the names 0 through `i - 1` re-joined with '.', and a final '.' at // the end. - var sectionKeyStartOffset = parsedKey.span.start.offset + + var sectionKeyStartOffset = parsedKeySpan.start.offset + (i == 0 ? 0 : parsedKey.names.take(i).join('.').length + 1); var keySpan = _sourceFile.span( sectionKeyStartOffset, sectionKeyStartOffset + sectionKey.length); @@ -273,7 +272,7 @@ class MustachioParser { } return _TagParseResult.ok(Section(parsedKey.names, children, - invert: invert, span: span, keySpan: parsedKey.span)); + invert: invert, span: span, keySpan: parsedKeySpan)); } /// Tries to parse an end tag at [_index]. @@ -295,7 +294,7 @@ class MustachioParser { /// /// [_index] should be at the character immediately following the `{{` /// characters which open a possible variable tag. - _TagParseResult _parseVariable({@required int tagStartIndex}) { + _TagParseResult _parseVariable({required int tagStartIndex}) { var escape = true; if (_thisChar == $lbrace) { escape = false; @@ -311,7 +310,7 @@ class MustachioParser { var span = _sourceFile.span(tagStartIndex, _index); return _TagParseResult.ok(Variable(parsedKey.names, - escape: escape, span: span, keySpan: parsedKey.span)); + escape: escape, span: span, keySpan: parsedKey.span!)); } /// Tries to parse a key at [_index]. @@ -436,7 +435,7 @@ class Text implements MustachioNode { @override final SourceSpan span; - Text(this.content, {@required this.span}); + Text(this.content, {required this.span}); @override String toString() => 'Text["$content"]'; @@ -457,7 +456,7 @@ class Variable with HasMultiNamedKey implements MustachioNode { final SourceSpan keySpan; Variable(this.key, - {@required this.escape, @required this.span, @required this.keySpan}); + {required this.escape, required this.span, required this.keySpan}); @override String toString() => 'Variable[$key, escape=$escape]'; @@ -481,7 +480,7 @@ class Section with HasMultiNamedKey implements MustachioNode { final SourceSpan keySpan; Section(this.key, this.children, - {@required this.invert, @required this.span, @required this.keySpan}); + {required this.invert, required this.span, required this.keySpan}); @override String toString() => 'Section[$key, invert=$invert]'; @@ -497,7 +496,7 @@ class Partial implements MustachioNode { final SourceSpan keySpan; - Partial(this.key, {@required this.span, @required this.keySpan}); + Partial(this.key, {required this.span, required this.keySpan}); } /// An enumeration of types of tag parse results. @@ -519,10 +518,10 @@ class _TagParseResult { /// This field is `null` if EOF was reached, or if a tag was not parsed /// (perhaps it started out like a tag, but was malformed, and so is read as /// text). - final MustachioNode /*?*/ node; + final MustachioNode? node; /// The key of an end tag, if an end tag was parsed. - final String /*?*/ endTagKey; + final String? endTagKey; _TagParseResult(this.type, this.node, this.endTagKey); @@ -564,12 +563,12 @@ class _KeyParseResult { /// The source span from where this key was parsed, if this represents a /// parsed key, othwerwise `null`. - final SourceSpan /*?*/ span; + final SourceSpan? span; const _KeyParseResult._(this.type, this.names, {this.span}); factory _KeyParseResult(_KeyParseResultType type, String key, - {@required SourceSpan span}) { + {required SourceSpan span}) { if (key == '.') { return _KeyParseResult._(type, [key], span: span); } else { diff --git a/lib/src/mustachio/renderer_base.dart b/lib/src/mustachio/renderer_base.dart index 072594a264..77d1db8f69 100644 --- a/lib/src/mustachio/renderer_base.dart +++ b/lib/src/mustachio/renderer_base.dart @@ -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 - // See the Mustachio README at tool/mustachio/README.md for high-level // documentation. @@ -48,9 +46,9 @@ class Template { final Map partialTemplates; Template._( - {@required this.ast, - @required this.partials, - @required this.partialTemplates}); + {required this.ast, + required this.partials, + required this.partialTemplates}); /// Parses [file] as a Mustache template, returning a [Template]. /// @@ -69,8 +67,8 @@ class Template { /// the directory containing `p1.html`, not relative to the top-level /// template), as `/foo/partials/p2.html`. static Future