From f66eb72d26a1dface3da89f40e14c4bfc8f56863 Mon Sep 17 00:00:00 2001 From: Sam Rawlins Date: Wed, 26 Jul 2023 09:02:43 -0700 Subject: [PATCH] Use path as import prefix in lib/ and test/ (#3471) --- lib/src/dartdoc.dart | 6 +++--- lib/src/model_utils.dart | 8 +++---- lib/src/package_meta.dart | 12 +++++------ lib/src/source_linker.dart | 12 +++++------ lib/src/tool_runner.dart | 4 ++-- lib/src/validator.dart | 40 +++++++++++++++++------------------ test/templates/enum_test.dart | 12 +++++------ test/tool_runner_test.dart | 12 +++++------ 8 files changed, 53 insertions(+), 53 deletions(-) diff --git a/lib/src/dartdoc.dart b/lib/src/dartdoc.dart index 425fd69bce..0b8da45e32 100644 --- a/lib/src/dartdoc.dart +++ b/lib/src/dartdoc.dart @@ -23,7 +23,7 @@ import 'package:dartdoc/src/validator.dart'; import 'package:dartdoc/src/version.dart'; import 'package:dartdoc/src/warnings.dart'; import 'package:meta/meta.dart'; -import 'package:path/path.dart' as p; +import 'package:path/path.dart' as path; const String programName = 'dartdoc'; // Update when pubspec version changes by running `pub run build_runner build` @@ -72,7 +72,7 @@ class DartdocFileWriter implements FileWriter { }) { _validateMaxWriteStats(filePath, content.length); // Replace '/' separators with proper separators for the platform. - var outFile = p.joinAll(filePath.split('/')); + var outFile = path.joinAll(filePath.split('/')); if (!allowOverwrite) { _warnAboutOverwrite(outFile, null); @@ -91,7 +91,7 @@ class DartdocFileWriter implements FileWriter { _validateMaxWriteStats(filePath, bytes.length); // Replace '/' separators with proper separators for the platform. - var outFile = p.joinAll(filePath.split('/')); + var outFile = path.joinAll(filePath.split('/')); _warnAboutOverwrite(outFile, element); _fileElementMap[outFile] = element; diff --git a/lib/src/model_utils.dart b/lib/src/model_utils.dart index c42ed395d6..384edf455a 100644 --- a/lib/src/model_utils.dart +++ b/lib/src/model_utils.dart @@ -11,7 +11,7 @@ import 'package:analyzer/file_system/file_system.dart'; import 'package:dartdoc/src/failure.dart'; import 'package:dartdoc/src/model/model.dart'; import 'package:glob/glob.dart'; -import 'package:path/path.dart' as p; +import 'package:path/path.dart' as path; final _driveLetterMatcher = RegExp(r'^\w:\\'); @@ -42,14 +42,14 @@ bool matchGlobs(List globs, String fullName, {bool? isWindows}) { if (!driveGlob.hasMatch(glob)) continue; // `C:\` => `\` for rejoining via posix. glob = glob.replaceFirst(_driveLetterMatcher, r'/'); - filteredGlobs.add(p.posix.joinAll(p.windows.split(glob))); + filteredGlobs.add(path.posix.joinAll(path.windows.split(glob))); } } else { filteredGlobs.addAll(globs); } - return filteredGlobs.any( - (g) => Glob(g, context: windows ? p.windows : p.posix).matches(fullName)); + return filteredGlobs.any((g) => + Glob(g, context: windows ? path.windows : path.posix).matches(fullName)); } Iterable filterHasCanonical( diff --git a/lib/src/package_meta.dart b/lib/src/package_meta.dart index cd4f5e45ec..e5169a9e43 100644 --- a/lib/src/package_meta.dart +++ b/lib/src/package_meta.dart @@ -14,7 +14,7 @@ import 'package:analyzer/src/generated/sdk.dart' show DartSdk; import 'package:dartdoc/src/dartdoc_options.dart'; import 'package:dartdoc/src/failure.dart'; import 'package:meta/meta.dart'; -import 'package:path/path.dart' as p; +import 'package:path/path.dart' as path; import 'package:yaml/yaml.dart'; final Map _packageMetaCache = {}; @@ -121,7 +121,7 @@ abstract class PackageMeta { @override int get hashCode => pathContext.hash(pathContext.absolute(dir.path)); - p.Context get pathContext => resourceProvider.pathContext; + path.Context get pathContext => resourceProvider.pathContext; /// Returns true if this represents a 'Dart' SDK. /// @@ -169,10 +169,10 @@ abstract class PubPackageMeta extends PackageMeta { static final List> _sdkDirFilePaths = Platform.isWindows ? [ - for (var paths in _sdkDirFilePathsPosix) + for (var filePaths in _sdkDirFilePathsPosix) [ - for (var path in paths) - p.joinAll(p.Context(style: p.Style.posix).split(path)), + for (var filePath in filePaths) + path.joinAll(path.posix.split(filePath)), ], ] : _sdkDirFilePathsPosix; @@ -375,7 +375,7 @@ File? _locate(Folder dir, List fileNames) { for (var name in fileNames) { for (var f in files) { - var baseName = p.basename(f.path).toLowerCase(); + var baseName = path.basename(f.path).toLowerCase(); if (baseName == name) return f; if (baseName.startsWith(name)) return f; } diff --git a/lib/src/source_linker.dart b/lib/src/source_linker.dart index 709651fba5..9bda3d0bd5 100644 --- a/lib/src/source_linker.dart +++ b/lib/src/source_linker.dart @@ -8,7 +8,7 @@ library dartdoc.source_linker; import 'package:analyzer/file_system/file_system.dart'; import 'package:dartdoc/src/dartdoc_options.dart'; import 'package:dartdoc/src/model/model.dart'; -import 'package:path/path.dart' as p; +import 'package:path/path.dart' as path; final _uriTemplateRegExp = RegExp(r'(%[frl]%)'); @@ -106,16 +106,16 @@ class SourceLinker { if (root == null || uriTemplate == null) { return ''; } - if (!p.isWithin(root, sourceFileName) || - excludes.any((String exclude) => p.isWithin(exclude, sourceFileName))) { + if (!path.isWithin(root, sourceFileName) || + excludes + .any((String exclude) => path.isWithin(exclude, sourceFileName))) { return ''; } return uriTemplate.replaceAllMapped(_uriTemplateRegExp, (match) { switch (match[1]) { case '%f%': - var urlContext = p.Context(style: p.Style.url); - return urlContext - .joinAll(p.split(p.relative(sourceFileName, from: root))); + return path.url + .joinAll(path.split(path.relative(sourceFileName, from: root))); case '%r%': return revision!; case '%l%': diff --git a/lib/src/tool_runner.dart b/lib/src/tool_runner.dart index 1db1e912a3..51423908fe 100644 --- a/lib/src/tool_runner.dart +++ b/lib/src/tool_runner.dart @@ -10,7 +10,7 @@ import 'package:analyzer/file_system/file_system.dart'; import 'package:dartdoc/src/dartdoc_options.dart'; import 'package:dartdoc/src/io_utils.dart'; import 'package:dartdoc/src/tool_definition.dart'; -import 'package:path/path.dart' as p; +import 'package:path/path.dart' as path; typedef ToolErrorCallback = void Function(String message); typedef FakeResultCallback = String Function(String tool, @@ -240,5 +240,5 @@ class ToolRunner { ResourceProvider get resourceProvider => toolConfiguration.resourceProvider; - p.Context get pathContext => resourceProvider.pathContext; + path.Context get pathContext => resourceProvider.pathContext; } diff --git a/lib/src/validator.dart b/lib/src/validator.dart index cc1bf1cde3..e8340b3e72 100644 --- a/lib/src/validator.dart +++ b/lib/src/validator.dart @@ -13,7 +13,7 @@ import 'package:dartdoc/src/model/package_graph.dart'; import 'package:dartdoc/src/runtime_stats.dart'; import 'package:dartdoc/src/warnings.dart'; import 'package:html/parser.dart' show parse; -import 'package:path/path.dart' as p; +import 'package:path/path.dart' as path; class Validator { final PackageGraph _packageGraph; @@ -28,7 +28,7 @@ class Validator { Validator(this._packageGraph, this._config, String origin, this._writtenFiles, this._onCheckProgress) - : _origin = p.normalize(origin), + : _origin = path.normalize(origin), _hrefs = _packageGraph.allHrefs; /// Don't call this method more than once, and only after you've @@ -45,7 +45,7 @@ class Validator { } void _collectLinks(String pathToCheck, [String? source, String? fullPath]) { - fullPath ??= p.join(_origin, pathToCheck); + fullPath ??= path.join(_origin, pathToCheck); final pageLinks = _getLinksAndBaseHref(fullPath); if (pageLinks == null) { @@ -67,16 +67,16 @@ class Validator { // the stack without this. final toVisit = <(String newPathToCheck, String newFullPath)>{}; final pathDirectory = baseHref == null - ? p.dirname(pathToCheck) - : '${p.dirname(pathToCheck)}/$baseHref'; + ? path.dirname(pathToCheck) + : '${path.dirname(pathToCheck)}/$baseHref'; for (final href in links) { final uri = Uri.tryParse(href); if (uri == null || !uri.hasAuthority && !uri.hasFragment) { var linkPath = '$pathDirectory/$href'; - linkPath = p.normalize(linkPath); - final newFullPath = p.join(_origin, linkPath); + linkPath = path.normalize(linkPath); + final newFullPath = path.join(_origin, linkPath); if (!_visited.contains(newFullPath)) { toVisit.add((linkPath, newFullPath)); _visited.add(newFullPath); @@ -91,13 +91,13 @@ class Validator { } void _checkForOrphans() { - final staticAssets = p.join(_origin, 'static-assets', ''); - final indexJson = p.join(_origin, 'index.json'); + final staticAssets = path.join(_origin, 'static-assets', ''); + final indexJson = path.join(_origin, 'index.json'); var foundIndexJson = false; void checkDirectory(Folder dir) { for (final child in dir.getChildren()) { - final fullPath = p.normalize(child.path); + final fullPath = path.normalize(child.path); if (_visited.contains(fullPath)) { continue; } @@ -108,12 +108,12 @@ class Validator { if (fullPath.startsWith(staticAssets)) { continue; } - if (p.equals(fullPath, indexJson)) { + if (path.equals(fullPath, indexJson)) { foundIndexJson = true; _onCheckProgress.add(fullPath); continue; } - final relativeFullPath = p.relative(fullPath, from: _origin); + final relativeFullPath = path.relative(fullPath, from: _origin); if (!_writtenFiles.contains(relativeFullPath)) { // This isn't a file we wrote (this time); don't claim we did. _warn(PackageWarning.unknownFile, fullPath, _origin); @@ -138,8 +138,8 @@ class Validator { } void _checkSearchIndex() { - final fullPath = p.join(_origin, 'index.json'); - final indexPath = p.join(_origin, 'index.html'); + final fullPath = path.join(_origin, 'index.json'); + final indexPath = path.join(_origin, 'index.html'); final file = _config.resourceProvider.getFile(fullPath); if (!file.exists) { return; @@ -154,8 +154,8 @@ class Validator { found.add(indexPath); for (var entry in jsonData.cast>()) { if (entry.containsKey('href')) { - final entryPath = - p.joinAll([_origin, ...p.posix.split(entry['href'] as String)]); + final entryPath = path + .joinAll([_origin, ...path.posix.split(entry['href'] as String)]); if (!_visited.contains(entryPath)) { _warn(PackageWarning.brokenLink, entryPath, _origin, referredFrom: fullPath); @@ -219,12 +219,12 @@ class Validator { Set? warnOnElements; // Make all paths relative to origin. - if (p.isWithin(origin, warnOn)) { - warnOn = p.relative(warnOn, from: origin); + if (path.isWithin(origin, warnOn)) { + warnOn = path.relative(warnOn, from: origin); } if (referredFrom != null) { - if (p.isWithin(origin, referredFrom)) { - referredFrom = p.relative(referredFrom, from: origin); + if (path.isWithin(origin, referredFrom)) { + referredFrom = path.relative(referredFrom, from: origin); } final hrefReferredFrom = _hrefs[referredFrom]; // Source paths are always relative. diff --git a/test/templates/enum_test.dart b/test/templates/enum_test.dart index b7b8e0dbfa..bd0de62e50 100644 --- a/test/templates/enum_test.dart +++ b/test/templates/enum_test.dart @@ -5,7 +5,7 @@ import 'package:analyzer/file_system/memory_file_system.dart'; import 'package:dartdoc/src/dartdoc.dart'; import 'package:dartdoc/src/model/model.dart'; -import 'package:path/path.dart' as p; +import 'package:path/path.dart' as path; import 'package:test/test.dart'; import '../src/test_descriptor_utils.dart' as d; @@ -106,7 +106,7 @@ enum EnumWithDefaultConstructor { '--input', packagePath, '--output', - p.join(packagePath, 'doc'), + path.join(packagePath, 'doc'), '--sdk-dir', packageMetaProvider.defaultSdkDir.path, '--no-link-to-remote', @@ -124,20 +124,20 @@ enum EnumWithDefaultConstructor { ); await (await Dartdoc.fromContext(context, packageBuilder)).generateDocs(); eLines = resourceProvider - .getFile(p.join(packagePath, 'doc', 'lib', 'E.html')) + .getFile(path.join(packagePath, 'doc', 'lib', 'E.html')) .readAsStringSync() .split('\n'); eRightSidebarLines = resourceProvider - .getFile(p.join(packagePath, 'doc', 'lib', 'E-enum-sidebar.html')) + .getFile(path.join(packagePath, 'doc', 'lib', 'E-enum-sidebar.html')) .readAsStringSync() .split('\n'); enumWithDefaultConstructorLines = resourceProvider - .getFile(p.join( + .getFile(path.join( packagePath, 'doc', 'lib', 'EnumWithDefaultConstructor.html')) .readAsStringSync() .split('\n'); enumWithDefaultConstructorRightSidebarLines = resourceProvider - .getFile(p.join(packagePath, 'doc', 'lib', + .getFile(path.join(packagePath, 'doc', 'lib', 'EnumWithDefaultConstructor-enum-sidebar.html')) .readAsStringSync() .split('\n'); diff --git a/test/tool_runner_test.dart b/test/tool_runner_test.dart index 86cf5c4634..99caeabc72 100644 --- a/test/tool_runner_test.dart +++ b/test/tool_runner_test.dart @@ -10,7 +10,7 @@ import 'package:dartdoc/src/dartdoc_options.dart'; import 'package:dartdoc/src/package_meta.dart'; import 'package:dartdoc/src/tool_definition.dart'; import 'package:dartdoc/src/tool_runner.dart'; -import 'package:path/path.dart' as p; +import 'package:path/path.dart' as path; import 'package:test/test.dart'; import 'package:yaml/yaml.dart'; @@ -19,7 +19,7 @@ final Directory _toolExecutableDir = Directory('testing/tool_executables'); void main() { ToolConfiguration toolMap; - Directory? tempDir; + late Directory tempDir; late File setupFile; late ToolRunner runner; @@ -29,7 +29,7 @@ void main() { setUpAll(() async { ProcessResult? result; tempDir = Directory.systemTemp.createTempSync('tool_runner_test_'); - var snapshotFile = p.join(tempDir!.path, 'drill.snapshot'); + var snapshotFile = path.join(tempDir.path, 'drill.snapshot'); try { result = Process.runSync( Platform.resolvedExecutable, @@ -49,10 +49,10 @@ void main() { stderr.writeln(result.stderr); } expect(result?.exitCode, equals(0)); - setupFile = File(p.join(tempDir!.path, 'setup.stamp')); + setupFile = File(path.join(tempDir.path, 'setup.stamp')); var nonDartName = Platform.isWindows ? 'non_dart.bat' : 'non_dart.sh'; var nonDartExecutable = - p.join(_toolExecutableDir.absolute.path, nonDartName); + path.join(_toolExecutableDir.absolute.path, nonDartName); // Have to replace backslashes on Windows with double-backslashes, to // escape them for YAML parser. var yamlMap = ''' @@ -93,7 +93,7 @@ echo: }); tearDownAll(() { - tempDir?.deleteSync(recursive: true); + tempDir.deleteSync(recursive: true); SnapshotCache.instanceFor(pubPackageMetaProvider.resourceProvider) .dispose(); });