Skip to content

Use path as import prefix in lib/ and test/ #3471

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 1 commit into from
Jul 26, 2023
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
6 changes: 3 additions & 3 deletions lib/src/dartdoc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions lib/src/model_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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:\\');

Expand Down Expand Up @@ -42,14 +42,14 @@ bool matchGlobs(List<String> 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<T> filterHasCanonical<T extends ModelElement>(
Expand Down
12 changes: 6 additions & 6 deletions lib/src/package_meta.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, PackageMeta?> _packageMetaCache = {};
Expand Down Expand Up @@ -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.
///
Expand Down Expand Up @@ -169,10 +169,10 @@ abstract class PubPackageMeta extends PackageMeta {

static final List<List<String>> _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;
Expand Down Expand Up @@ -375,7 +375,7 @@ File? _locate(Folder dir, List<String> 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;
}
Expand Down
12 changes: 6 additions & 6 deletions lib/src/source_linker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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]%)');

Expand Down Expand Up @@ -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%':
Expand Down
4 changes: 2 additions & 2 deletions lib/src/tool_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -240,5 +240,5 @@ class ToolRunner {

ResourceProvider get resourceProvider => toolConfiguration.resourceProvider;

p.Context get pathContext => resourceProvider.pathContext;
path.Context get pathContext => resourceProvider.pathContext;
}
40 changes: 20 additions & 20 deletions lib/src/validator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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);
Expand All @@ -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;
}
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -154,8 +154,8 @@ class Validator {
found.add(indexPath);
for (var entry in jsonData.cast<Map<String, dynamic>>()) {
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);
Expand Down Expand Up @@ -219,12 +219,12 @@ class Validator {
Set<Warnable>? 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.
Expand Down
12 changes: 6 additions & 6 deletions test/templates/enum_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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',
Expand All @@ -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');
Expand Down
12 changes: 6 additions & 6 deletions test/tool_runner_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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;
Expand All @@ -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,
Expand All @@ -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 = '''
Expand Down Expand Up @@ -93,7 +93,7 @@ echo:
});

tearDownAll(() {
tempDir?.deleteSync(recursive: true);
tempDir.deleteSync(recursive: true);
SnapshotCache.instanceFor(pubPackageMetaProvider.resourceProvider)
.dispose();
});
Expand Down