From c7737ffd139716628443927c2c5bc838a47581b4 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Thu, 15 Apr 2021 17:22:15 +0200 Subject: [PATCH] Port LinksLoader to null safety With this I'm not accepting any of the Link's field to be an empty string. In that case it's better for it to be null, as the compiler will force me to deal with an empty value. --- lib/core/link.dart | 16 ++++++++++++++-- lib/core/links_loader.dart | 15 +++++++-------- test/links_loader_test.dart | 30 ++++++++++++++---------------- 3 files changed, 35 insertions(+), 26 deletions(-) diff --git a/lib/core/link.dart b/lib/core/link.dart index e6afd05f..d5bf50e4 100644 --- a/lib/core/link.dart +++ b/lib/core/link.dart @@ -1,5 +1,7 @@ import 'package:markdown/markdown.dart' as md; +// FIXME: This should be split into 2 classes, that way it would be easier +// to access to members with null safety class Link { String? publicTerm; String? filePath; @@ -13,7 +15,17 @@ class Link { required this.filePath, this.headingID, this.alt, - }); + }) { + if (publicTerm?.isEmpty == true) { + publicTerm = null; + } + if (headingID?.isEmpty == true) { + headingID = null; + } + if (alt?.isEmpty == true) { + alt = null; + } + } Link.wiki(this.wikiTerm); bool get isWikiLink => wikiTerm != null; @@ -68,7 +80,7 @@ class LinkExtractor implements md.NodeVisitor { return; } - var alt = el.attributes['title'] ?? ""; + var alt = el.attributes['title']; var title = _getText(el.children); var url = el.attributes['href']; diff --git a/lib/core/links_loader.dart b/lib/core/links_loader.dart index 516d2442..b4c9e732 100644 --- a/lib/core/links_loader.dart +++ b/lib/core/links_loader.dart @@ -1,5 +1,3 @@ -// @dart=2.9 - import 'dart:io'; import 'dart:isolate'; @@ -12,9 +10,9 @@ import 'package:synchronized/synchronized.dart'; import 'package:gitjournal/core/link.dart'; class LinksLoader { - Isolate _isolate; + Isolate? _isolate; ReceivePort _receivePort = ReceivePort(); - SendPort _sendPort; + SendPort? _sendPort; var _loadingLock = Lock(); @@ -24,7 +22,7 @@ class LinksLoader { return await _loadingLock.synchronized(() async { if (_isolate != null && _sendPort != null) return; if (_isolate != null) { - _isolate.kill(priority: Isolate.immediate); + _isolate!.kill(priority: Isolate.immediate); _isolate = null; } _isolate = await Isolate.spawn(_isolateMain, _receivePort.sendPort); @@ -35,11 +33,12 @@ class LinksLoader { }); } - Future> parseLinks({String body, String filePath}) async { + Future> parseLinks( + {required String body, required String filePath}) async { await _initIsolate(); var rec = ReceivePort(); - _sendPort.send(_LoadingMessage(body, filePath, rec.sendPort)); + _sendPort!.send(_LoadingMessage(body, filePath, rec.sendPort)); var data = await rec.first; assert(data is List); @@ -90,7 +89,7 @@ List parseLinks(String body, String filePath) { continue; } - l.filePath = p.join(parentFolderPath, p.normalize(l.filePath)); + l.filePath = p.join(parentFolderPath, p.normalize(l.filePath!)); links.add(l); } diff --git a/test/links_loader_test.dart b/test/links_loader_test.dart index f198b359..21e9c096 100644 --- a/test/links_loader_test.dart +++ b/test/links_loader_test.dart @@ -1,5 +1,3 @@ -// @dart=2.9 - import 'package:test/test.dart'; import 'package:gitjournal/core/links_loader.dart'; @@ -26,27 +24,27 @@ void main() { filePath: "/tmp/foo/file.md", ); - expect(links[0].filePath.isEmpty, true); - expect(links[0].headingID.isEmpty, true); - expect(links[0].alt.isEmpty, true); - expect(links[0].publicTerm.isEmpty, true); + expect(links[0].filePath, isNull); + expect(links[0].headingID, isNull); + expect(links[0].alt, isNull); + expect(links[0].publicTerm, isNull); expect(links[0].wikiTerm, "GitJournal"); expect(links[0].isWikiLink, true); expect(links[1].filePath, "/tmp/foo/gitjournal.md"); expect(links[1].publicTerm, "GitJournal"); - expect(links[1].alt.isEmpty, true); - expect(links[1].wikiTerm.isEmpty, true); + expect(links[1].alt, isNull); + expect(links[1].wikiTerm, isNull); expect(links[2].filePath, "/tmp/foo/gitjournal.md"); expect(links[2].publicTerm, "GitJournal"); - expect(links[2].alt.isEmpty, true); - expect(links[2].wikiTerm.isEmpty, true); + expect(links[2].alt, isNull); + expect(links[2].wikiTerm, isNull); expect(links[3].filePath, "/tmp/foo/gitjournal"); expect(links[3].publicTerm, "GitJournal"); expect(links[3].alt, "alt-text"); - expect(links[3].wikiTerm.isEmpty, true); + expect(links[3].wikiTerm, isNull); /* expect(links[4].filePath, "/tmp/foam.md"); @@ -78,13 +76,13 @@ Foam is licensed under the [MIT license](license). expect(links.length, 5); expect(links[0].filePath, "/tmp/foo.md"); - expect(links[0].alt.isEmpty, true); + expect(links[0].alt, isNull); expect(links[0].headingID, "#contributors-"); - expect(links[0].publicTerm.isEmpty, true); + expect(links[0].publicTerm, isNull); expect(links[1].filePath, "/tmp/license"); - expect(links[1].alt.isEmpty, true); - expect(links[1].headingID.isEmpty, true); + expect(links[1].alt, isNull); + expect(links[1].headingID, isNull); expect(links[1].publicTerm, "MIT license"); expect(links[2].filePath, "/tmp/foo.md"); @@ -96,7 +94,7 @@ Foam is licensed under the [MIT license](license). // FIXME: link-references for wiki Links // expect(links[3].filePath.isEmpty, true); // expect(links[3].isWikiLink, true); - expect(links[3].headingID.isEmpty, true); + expect(links[3].headingID, isNull); expect(links[3].alt, "Wiki Links"); expect(links[4].filePath, "/tmp/foo.md");