Skip to content

remove dartdoc's dep on package:quiver #2305

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 2 commits into from
Aug 17, 2020
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
4 changes: 2 additions & 2 deletions lib/src/generator/resource_loader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ library dartdoc.resource_loader;
import 'dart:async' show Future;
import 'dart:convert' show utf8;

import 'package:resource/resource.dart';
import 'package:resource/resource.dart' as resource;

/// Loads a `package:` resource as a String.
Future<String> loadAsString(String path) async {
Expand All @@ -29,5 +29,5 @@ Future<List<int>> loadAsBytes(String path) async {
}

var uri = Uri.parse(path);
return await ResourceLoader.defaultLoader.readAsBytes(uri);
return await resource.ResourceLoader.defaultLoader.readAsBytes(uri);
}
16 changes: 13 additions & 3 deletions lib/src/model/class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import 'package:dartdoc/src/element_type.dart';
import 'package:dartdoc/src/model/extension_target.dart';
import 'package:dartdoc/src/model/model.dart';
import 'package:dartdoc/src/model_utils.dart' as model_utils;
import 'package:dartdoc/src/quiver.dart' as quiver;
import 'package:meta/meta.dart';
import 'package:quiver/iterables.dart' as quiver;

/// A [Container] defined with a `class` declaration in Dart.
///
Expand Down Expand Up @@ -81,6 +81,7 @@ class Class extends Container
quiver.concat([super.instanceOperators, inheritedOperators]);

List<ModelElement> _allModelElements;

@override
List<ModelElement> get allModelElements {
_allModelElements ??= List.from(
Expand Down Expand Up @@ -129,6 +130,7 @@ class Class extends Container
bool get hasPublicConstructors => publicConstructorsSorted.isNotEmpty;

List<Constructor> _publicConstructorsSorted;

List<Constructor> get publicConstructorsSorted =>
_publicConstructorsSorted ??= publicConstructors.toList()..sort(byName);

Expand Down Expand Up @@ -165,7 +167,9 @@ class Class extends Container
model_utils.findCanonicalFor(packageGraph.implementors[href] ?? []));
}

/*lazy final*/ List<Method> _inheritedMethods;
/*lazy final*/
List<Method> _inheritedMethods;

Iterable<Method> get inheritedMethods {
if (_inheritedMethods == null) {
_inheritedMethods = <Method>[];
Expand All @@ -192,7 +196,9 @@ class Class extends Container

bool get hasPublicInheritedMethods => publicInheritedMethods.isNotEmpty;

/*lazy final*/ List<Operator> _inheritedOperators;
/*lazy final*/
List<Operator> _inheritedOperators;

Iterable<Operator> get inheritedOperators {
if (_inheritedOperators == null) {
_inheritedOperators = [];
Expand Down Expand Up @@ -352,6 +358,7 @@ class Class extends Container
}

List<Field> _allFields;

List<Field> get allFields {
if (_allFields == null) {
_allFields = [];
Expand Down Expand Up @@ -463,13 +470,15 @@ class Class extends Container
}

Iterable<Method> _declaredMethods;

@override
Iterable<Method> get declaredMethods =>
_declaredMethods ??= element.methods.map((e) {
return ModelElement.from(e, library, packageGraph) as Method;
});

List<TypeParameter> _typeParameters;

// a stronger hash?
@override
List<TypeParameter> get typeParameters {
Expand All @@ -481,6 +490,7 @@ class Class extends Container
}

Iterable<Field> _instanceFields;

@override
Iterable<Field> get instanceFields =>
_instanceFields ??= allFields.where((f) => !f.isStatic);
Expand Down
4 changes: 2 additions & 2 deletions lib/src/model/comment_processable.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:io';

import 'package:args/args.dart';
import 'package:crypto/crypto.dart';
import 'package:crypto/crypto.dart' as crypto;
import 'package:dartdoc/src/logging.dart';
import 'package:dartdoc/src/model/model.dart';
import 'package:dartdoc/src/render/model_element_renderer.dart';
Expand Down Expand Up @@ -502,7 +502,7 @@ mixin CommentProcessable on Documentable, Warnable, Locatable, SourceCodeMixin {
if (!config.injectHtml) return rawDocs;
return rawDocs.replaceAllMapped(_htmlPattern, (match) {
var fragment = match[1];
var digest = sha1.convert(fragment.codeUnits).toString();
var digest = crypto.sha1.convert(fragment.codeUnits).toString();
packageGraph.addHtmlFragment(digest, fragment);
// The newlines are so that Markdown will pass this through without
// touching it.
Expand Down
2 changes: 1 addition & 1 deletion lib/src/model/container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import 'package:analyzer/dart/element/element.dart';
import 'package:dartdoc/src/model/model.dart';
import 'package:dartdoc/src/model_utils.dart' as model_utils;
import 'package:dartdoc/src/quiver.dart' as quiver;
import 'package:meta/meta.dart';
import 'package:quiver/iterables.dart' as quiver;

/// A [Container] represents a Dart construct that can contain methods,
/// operators, and fields, such as [Class], [Enum], or [Extension].
Expand Down
2 changes: 1 addition & 1 deletion lib/src/model/extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:analyzer/dart/element/element.dart';
import 'package:dartdoc/src/element_type.dart';
import 'package:dartdoc/src/model/extension_target.dart';
import 'package:dartdoc/src/model/model.dart';
import 'package:quiver/iterables.dart' as quiver;
import 'package:dartdoc/src/quiver.dart' as quiver;

/// Extension methods
class Extension extends Container
Expand Down
2 changes: 1 addition & 1 deletion lib/src/model/library.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import 'package:analyzer/src/dart/element/inheritance_manager3.dart';
import 'package:analyzer/src/generated/sdk.dart';
import 'package:dartdoc/src/model/model.dart';
import 'package:dartdoc/src/package_meta.dart' show PackageMeta;
import 'package:dartdoc/src/quiver.dart' as quiver;
import 'package:dartdoc/src/warnings.dart';
import 'package:path/path.dart' as path;
import 'package:quiver/iterables.dart' as quiver;

/// Find all hashable children of a given element that are defined in the
/// [LibraryElement] given at initialization.
Expand Down
2 changes: 1 addition & 1 deletion lib/src/model/package_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ import 'package:dartdoc/src/logging.dart';
import 'package:dartdoc/src/model/model.dart';
import 'package:dartdoc/src/package_meta.dart'
show PackageMeta, pubPackageMetaProvider;
import 'package:dartdoc/src/quiver.dart' as quiver;
import 'package:dartdoc/src/render/renderer_factory.dart';
import 'package:dartdoc/src/special_elements.dart';
import 'package:package_config/discovery.dart' as package_config;
import 'package:path/path.dart' as path;
import 'package:quiver/iterables.dart' as quiver;

/// Everything you need to instantiate a PackageGraph object for documenting.
abstract class PackageBuilder {
Expand Down
42 changes: 42 additions & 0 deletions lib/src/quiver.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// 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.

/// Methods in-lined from package:quiver.

// From lib/iterables.dart:

/// Returns the concatenation of the input iterables.
///
/// The returned iterable is a lazily-evaluated view on the input iterables.
Iterable<T> concat<T>(Iterable<Iterable<T>> iterables) =>
iterables.expand((x) => x);

// From lib/src/core/hash.dart:

/// Generates a hash code for two objects.
int hash2(Object a, Object b) =>
_finish(_combine(_combine(0, a.hashCode), b.hashCode));

/// Generates a hash code for three objects.
int hash3(Object a, Object b, Object c) => _finish(
_combine(_combine(_combine(0, a.hashCode), b.hashCode), c.hashCode));

/// Generates a hash code for four objects.
int hash4(Object a, Object b, Object c, Object d) => _finish(_combine(
_combine(_combine(_combine(0, a.hashCode), b.hashCode), c.hashCode),
d.hashCode));

// Jenkins hash functions

int _combine(int hash, int value) {
hash = 0x1fffffff & (hash + value);
hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10));
return hash ^ (hash >> 6);
}

int _finish(int hash) {
hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3));
hash = hash ^ (hash >> 11);
return 0x1fffffff & (hash + ((0x00003fff & hash) << 15));
}
11 changes: 6 additions & 5 deletions lib/src/tuple.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// file 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.

import 'package:quiver/core.dart';
import 'package:dartdoc/src/quiver.dart' as quiver;

/// Represents a 2-tuple, or pair.
class Tuple2<T1, T2> {
Expand All @@ -26,7 +26,7 @@ class Tuple2<T1, T2> {
o is Tuple2 && o.item1 == item1 && o.item2 == item2;

@override
int get hashCode => hash2(item1.hashCode, item2.hashCode);
int get hashCode => quiver.hash2(item1.hashCode, item2.hashCode);
}

/// Represents a 3-tuple, or triple.
Expand All @@ -51,7 +51,8 @@ class Tuple3<T1, T2, T3> {
o is Tuple3 && o.item1 == item1 && o.item2 == item2 && o.item3 == item3;

@override
int get hashCode => hash3(item1.hashCode, item2.hashCode, item3.hashCode);
int get hashCode =>
quiver.hash3(item1.hashCode, item2.hashCode, item3.hashCode);
}

/// Represents a 4-tuple, or quadruple.
Expand Down Expand Up @@ -83,6 +84,6 @@ class Tuple4<T1, T2, T3, T4> {
o.item4 == item4;

@override
int get hashCode =>
hash4(item1.hashCode, item2.hashCode, item3.hashCode, item4.hashCode);
int get hashCode => quiver.hash4(
item1.hashCode, item2.hashCode, item3.hashCode, item4.hashCode);
}
7 changes: 3 additions & 4 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ environment:
sdk: '>=2.7.0 <3.0.0'

dependencies:
analyzer: ^0.39.12
analyzer: ^0.39.16
args: '>=1.5.0 <2.0.0'
collection: ^1.2.0
cli_util: ^0.1.3+2
cli_util: '>=0.1.4 <0.3.0'
crypto: ^2.0.6
html: '>=0.12.1 <0.15.0'
# We don't use http_parser directly; this dep exists to ensure that we get at
Expand All @@ -24,7 +24,6 @@ dependencies:
package_config: '>=0.1.5 <2.0.0'
path: ^1.3.0
pub_semver: ^1.3.7
quiver: ^2.0.0
resource: ^2.1.2
stack_trace: ^1.4.2
yaml: ^2.1.0
Expand All @@ -34,7 +33,7 @@ dev_dependencies:
build: ^1.3.0
build_runner: ^1.10.0
build_version: ^2.0.1
coverage: ^0.13.0
coverage: ^0.14.0
dhttpd: ^3.0.0
glob: ^1.1.5
grinder: ^0.8.2
Expand Down
72 changes: 72 additions & 0 deletions test/quiver_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// 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.

import 'package:test/test.dart';
import 'package:dartdoc/src/quiver.dart';

void main() {
group('concat', () {
test('should handle empty input iterables', () {
expect(concat([]), isEmpty);
});

test('should handle single input iterables', () {
expect(
concat([
[1, 2, 3]
]),
[1, 2, 3]);
});

test('should chain multiple input iterables', () {
expect(
concat([
[1, 2, 3],
[-1, -2, -3]
]),
[1, 2, 3, -1, -2, -3]);
});

test('should throw for null input', () {
expect(() => concat(null), throwsNoSuchMethodError);
});

test('should throw if any input is null', () {
expect(
() => concat([
[1, 2],
null,
[3, 4]
]).toList(),
throwsNoSuchMethodError);
});

test('should reflectchanges in the inputs', () {
var a = [1, 2];
var b = [4, 5];
var ab = concat([a, b]);
expect(ab, [1, 2, 4, 5]);
a.add(3);
b.add(6);
expect(ab, [1, 2, 3, 4, 5, 6]);
});
});

group('hash', () {
test('hash2 should return an int', () {
var h = hash2('123', 456);
expect(h, isA<int>());
});

test('hash3 should return an int', () {
var h = hash3('123', 456, true);
expect(h, isA<int>());
});

test('hash4 should return an int', () {
var h = hash4('123', 456, true, []);
expect(h, isA<int>());
});
});
}