Skip to content

Commit

Permalink
[License] Enable prefer_equal_for_default_values lint
Browse files Browse the repository at this point in the history
  • Loading branch information
cbracken committed Dec 20, 2018
1 parent 1d7285b commit 4703a7f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 18 deletions.
1 change: 1 addition & 0 deletions tools/licenses/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ linter:
- one_member_abstracts
- overridden_fields
- package_prefixed_library_names
- prefer_equal_for_default_values
- prefer_final_locals
- prefer_is_not_empty
- prefer_single_quotes
Expand Down
48 changes: 35 additions & 13 deletions tools/licenses/lib/licenses.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ abstract class LicenseSource {
}

abstract class License implements Comparable<License> {
factory License.unique(String body, LicenseType type, { bool reformatted: false, String origin, bool yesWeKnowWhatItLooksLikeButItIsNot: false }) {
factory License.unique(String body, LicenseType type, {
bool reformatted = false,
String origin,
bool yesWeKnowWhatItLooksLikeButItIsNot = false
}) {
if (!reformatted)
body = _reformat(body);
final License result = _registry.putIfAbsent(body, () => UniqueLicense._(body, type, origin: origin, yesWeKnowWhatItLooksLikeButItIsNot: yesWeKnowWhatItLooksLikeButItIsNot));
Expand All @@ -118,7 +122,10 @@ abstract class License implements Comparable<License> {
return result;
}

factory License.template(String body, LicenseType type, { bool reformatted: false, String origin }) {
factory License.template(String body, LicenseType type, {
bool reformatted = false,
String origin
}) {
if (!reformatted)
body = _reformat(body);
final License result = _registry.putIfAbsent(body, () => TemplateLicense._(body, type, origin: origin));
Expand All @@ -130,7 +137,10 @@ abstract class License implements Comparable<License> {
return result;
}

factory License.message(String body, LicenseType type, { bool reformatted: false, String origin }) {
factory License.message(String body, LicenseType type, {
bool reformatted = false,
String origin
}) {
if (!reformatted)
body = _reformat(body);
final License result = _registry.putIfAbsent(body, () => MessageLicense._(body, type, origin: origin));
Expand All @@ -157,7 +167,10 @@ abstract class License implements Comparable<License> {
return _registry.putIfAbsent(body, () => UniqueLicense._(body, type));
}

factory License.fromBodyAndType(String body, LicenseType type, { bool reformatted: false, String origin }) {
factory License.fromBodyAndType(String body, LicenseType type, {
bool reformatted = false,
String origin
}) {
if (!reformatted)
body = _reformat(body);
final License result = _registry.putIfAbsent(body, () {
Expand Down Expand Up @@ -271,7 +284,10 @@ abstract class License implements Comparable<License> {
return _registry.putIfAbsent(body, () => License.fromBodyAndType(body, type, origin: origin));
}

License._(String body, this.type, { this.origin, bool yesWeKnowWhatItLooksLikeButItIsNot: false }) : body = body, authors = _readAuthors(body) {
License._(String body, this.type, {
this.origin,
bool yesWeKnowWhatItLooksLikeButItIsNot = false
}) : body = body, authors = _readAuthors(body) {
assert(_reformat(body) == body);
assert(() {
try {
Expand Down Expand Up @@ -500,7 +516,7 @@ Iterable<_LineRange> _walkLinesBackwards(String body, int start) sync* {
yield _LineRange(start, end, body);
}

Iterable<_LineRange> _walkLinesForwards(String body, { int start: 0, int end }) sync* {
Iterable<_LineRange> _walkLinesForwards(String body, { int start = 0, int end }) sync* {
int startIndex = start == 0 || body[start-1] == '\n' ? start : null;
int endIndex = startIndex ?? start;
end ??= body.length;
Expand All @@ -526,7 +542,7 @@ class _SplitLicense {
String getConditions() => _split >= _body.length ? '' : _body.substring(_split == 0 ? 0 : _split + 1);
}

_SplitLicense _splitLicense(String body, { bool verifyResults: true }) {
_SplitLicense _splitLicense(String body, { bool verifyResults = true }) {
final Iterator<_LineRange> lines = _walkLinesForwards(body).iterator;
if (!lines.moveNext())
throw 'tried to split empty license';
Expand Down Expand Up @@ -613,7 +629,7 @@ class _PartialLicenseMatch {
final bool hasCopyrights;
}

Iterable<_PartialLicenseMatch> _findLicenseBlocks(String body, RegExp pattern, int firstPrefixIndex, int indentPrefixIndex, { bool needsCopyright: true }) sync* {
Iterable<_PartialLicenseMatch> _findLicenseBlocks(String body, RegExp pattern, int firstPrefixIndex, int indentPrefixIndex, { bool needsCopyright = true }) sync* {
// I tried doing this with one big RegExp initially, but that way lay madness.
for (Match match in pattern.allMatches(body)) {
assert(match.groupCount >= firstPrefixIndex);
Expand Down Expand Up @@ -709,7 +725,11 @@ Iterable<_PartialLicenseMatch> _findLicenseBlocks(String body, RegExp pattern, i
}

class _LicenseMatch {
_LicenseMatch(this.license, this.start, this.end, { this.debug: '', this.isDuplicate: false, this.missingCopyrights: false });
_LicenseMatch(this.license, this.start, this.end, {
this.debug = '',
this.isDuplicate = false,
this.missingCopyrights = false
});
final License license;
final int start;
final int end;
Expand All @@ -718,7 +738,7 @@ class _LicenseMatch {
final bool missingCopyrights;
}

Iterable<_LicenseMatch> _expand(License template, String copyright, String body, int start, int end, { String debug: '', String origin }) sync* {
Iterable<_LicenseMatch> _expand(License template, String copyright, String body, int start, int end, { String debug = '', String origin }) sync* {
final List<License> results = template.expandTemplate(_reformat(copyright), body, origin: origin).toList();
if (results.isEmpty)
throw 'license could not be expanded';
Expand Down Expand Up @@ -780,7 +800,7 @@ Iterable<_LicenseMatch> _tryReferenceByFilename(String body, LicenseFileReferenc
}
}

Iterable<_LicenseMatch> _tryReferenceByType(String body, RegExp pattern, LicenseSource parentDirectory, { String origin, bool needsCopyright: true }) sync* {
Iterable<_LicenseMatch> _tryReferenceByType(String body, RegExp pattern, LicenseSource parentDirectory, { String origin, bool needsCopyright = true }) sync* {
for (_PartialLicenseMatch match in _findLicenseBlocks(body, pattern, 1, 2, needsCopyright: needsCopyright)) {
final LicenseType type = convertLicenseNameToType(match.group(3));
final License template = parentDirectory.nearestLicenseOfType(type);
Expand Down Expand Up @@ -965,8 +985,10 @@ class MultiLicense extends License {

// the kind of license that should not be combined with separate copyright notices
class UniqueLicense extends License {
UniqueLicense._(String body, LicenseType type, { String origin, bool yesWeKnowWhatItLooksLikeButItIsNot: false })
: super._(body, type, origin: origin, yesWeKnowWhatItLooksLikeButItIsNot: yesWeKnowWhatItLooksLikeButItIsNot);
UniqueLicense._(String body, LicenseType type, {
String origin,
bool yesWeKnowWhatItLooksLikeButItIsNot = false
}) : super._(body, type, origin: origin, yesWeKnowWhatItLooksLikeButItIsNot: yesWeKnowWhatItLooksLikeButItIsNot);
@override
Iterable<License> expandTemplate(String copyright, String licenseBody, { String origin }) sync* {
throw 'attempted to expand non-template license with "$copyright"\ntemplate was: $this';
Expand Down
6 changes: 3 additions & 3 deletions tools/licenses/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1097,13 +1097,13 @@ class _RepositoryDirectory extends _RepositoryEntry implements LicenseSource {
return null;
}

License _fullWalkUpForLicenseWithName(String name, { String authors, bool ignoreCase: false }) {
License _fullWalkUpForLicenseWithName(String name, { String authors, bool ignoreCase = false }) {
return _canGoUp(authors)
? parent._fullWalkUpForLicenseWithName(name, authors: authors, ignoreCase: ignoreCase)
: _fullWalkDownForLicenseWithName(name, authors: authors, ignoreCase: ignoreCase);
}

License _fullWalkDownForLicenseWithName(String name, { String authors, bool ignoreCase: false }) {
License _fullWalkDownForLicenseWithName(String name, { String authors, bool ignoreCase = false }) {
License result = _localLicenseWithName(name, authors: authors, ignoreCase: ignoreCase);
if (result == null) {
for (_RepositoryDirectory directory in _subdirectories) {
Expand Down Expand Up @@ -1140,7 +1140,7 @@ class _RepositoryDirectory extends _RepositoryEntry implements LicenseSource {
/// to the LICENSE in the root of the repo.
bool get isLicenseRootException => false;

License _localLicenseWithName(String name, { String authors, bool ignoreCase: false }) {
License _localLicenseWithName(String name, { String authors, bool ignoreCase = false }) {
Map<String, _RepositoryEntry> map;
if (ignoreCase) {
// we get here if we're trying a last-ditch effort at finding a file.
Expand Down
20 changes: 18 additions & 2 deletions tools/licenses/lib/patterns.dart
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,15 @@ final List<RegExp> csAttribution = <RegExp>[
// REFERENCES TO OTHER FILES

class LicenseFileReferencePattern {
LicenseFileReferencePattern({ this.firstPrefixIndex, this.indentPrefixIndex, this.copyrightIndex, this.authorIndex, this.fileIndex, this.pattern, this.needsCopyright: true });
LicenseFileReferencePattern({
this.firstPrefixIndex,
this.indentPrefixIndex,
this.copyrightIndex,
this.authorIndex,
this.fileIndex,
this.pattern,
this.needsCopyright = true
});
final int firstPrefixIndex;
final int indentPrefixIndex;
final int copyrightIndex;
Expand Down Expand Up @@ -571,7 +579,15 @@ final List<RegExp> csReferencesByTypeNoCopyright = <RegExp>[
];

class MultipleVersionedLicenseReferencePattern {
MultipleVersionedLicenseReferencePattern({ this.firstPrefixIndex, this.indentPrefixIndex, this.licenseIndices, this.versionIndicies, this.checkLocalFirst: true, this.pattern });
MultipleVersionedLicenseReferencePattern({
this.firstPrefixIndex,
this.indentPrefixIndex,
this.licenseIndices,
this.versionIndicies,
this.checkLocalFirst = true,
this.pattern
});

final int firstPrefixIndex;
final int indentPrefixIndex;
final List<int> licenseIndices;
Expand Down

0 comments on commit 4703a7f

Please sign in to comment.