-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathwarnings.dart
674 lines (608 loc) · 24 KB
/
warnings.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
// Copyright (c) 2017, 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 'dart:io';
import 'dart:math' as math;
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/file_system/file_system.dart';
import 'package:collection/collection.dart';
import 'package:dartdoc/src/dartdoc_options.dart';
import 'package:dartdoc/src/logging.dart';
import 'package:dartdoc/src/model/comment_referable.dart';
import 'package:dartdoc/src/model/model.dart';
import 'package:dartdoc/src/package_meta.dart';
import 'package:dartdoc/src/utils.dart';
const _namePlaceholder = '@@name@@';
mixin PackageWarningOptionContext implements DartdocOptionContextBase {
bool get allowNonLocalWarnings =>
optionSet['allowNonLocalWarnings'].valueAt(context);
// allowWarningsInPackages, ignoreWarningsInPackages, errors, warnings, and
// ignore are only used indirectly via the synthetic packageWarningOptions
// option.
PackageWarningOptions get packageWarningOptions =>
optionSet['packageWarningOptions'].valueAt(context);
bool get verboseWarnings => optionSet['verboseWarnings'].valueAt(context);
}
List<DartdocOption<Object?>> createPackageWarningOptions(
PackageMetaProvider packageMetaProvider,
) {
var resourceProvider = packageMetaProvider.resourceProvider;
return [
DartdocOptionArgOnly<bool>('allowNonLocalWarnings', false, resourceProvider,
negatable: true,
help: 'Show warnings from packages we are not documenting locally.'),
// Options for globally enabling/disabling all warnings and errors
// for individual packages are command-line only. This will allow
// meta-packages like Flutter to control whether warnings are displayed for
// packages they don't control.
DartdocOptionArgOnly<List<String>?>(
'allowWarningsInPackages', null, resourceProvider,
splitCommas: true,
help:
'Package names to display warnings for (ignore all others if set)'),
DartdocOptionArgOnly<List<String>?>(
'allowErrorsInPackages', null, resourceProvider,
splitCommas: true,
help: 'Package names to display errors for (ignore all others if set)'),
DartdocOptionArgOnly<List<String>?>(
'ignoreWarningsInPackages', null, resourceProvider,
splitCommas: true,
help: 'Package names to ignore warnings for. Takes priority over '
'allow-warnings-in-packages'),
DartdocOptionArgOnly<List<String>?>(
'ignoreErrorsInPackages', null, resourceProvider,
splitCommas: true,
help: 'Package names to ignore errors for. Takes priority over '
'allow-errors-in-packages'),
// Options for globally enabling/disabling warnings and errors across
// packages. Loaded from dartdoc_options.yaml, but command line arguments
// will override.
DartdocOptionArgFile<List<String>?>('errors', null, resourceProvider,
splitCommas: true,
help: 'Additional warning names to force as errors. Specify an empty '
'list to force defaults (overriding dartdoc_options.yaml)\n'
'Defaults:\n${PackageWarningMode.error._warningsListHelpText}'),
DartdocOptionArgFile<List<String>?>('ignore', null, resourceProvider,
splitCommas: true,
help: 'Additional warning names to ignore. Specify an empty list to '
'force defaults (overriding dartdoc_options.yaml).\n'
'Defaults:\n${PackageWarningMode.ignore._warningsListHelpText}'),
DartdocOptionArgFile<List<String>?>('warnings', null, resourceProvider,
splitCommas: true,
help:
'Additional warning names to show as warnings (instead of error or '
'ignore, if not warning by default).\n'
'Defaults:\n${PackageWarningMode.warn._warningsListHelpText}'),
// Synthetic option uses a factory to build a PackageWarningOptions from all
// the above flags.
DartdocOptionSyntheticOnly<PackageWarningOptions>(
'packageWarningOptions',
(DartdocSyntheticOption<PackageWarningOptions> option, Folder dir) =>
PackageWarningOptions.fromOptions(option, dir, packageMetaProvider),
resourceProvider,
),
];
}
/// Something that package warnings can be reported on. Optionally associated
/// with an analyzer [element].
mixin Warnable implements CommentReferable, Documentable, Locatable {
Element2? get element;
void warn(
PackageWarning kind, {
String? message,
Iterable<Locatable> referredFrom = const [],
Iterable<String> extendedDebug = const [],
}) {
packageGraph.warnOnElement(this, kind,
message: message,
referredFrom: referredFrom,
extendedDebug: extendedDebug);
}
}
/// The kinds of warnings that can be displayed when documenting a package.
enum PackageWarning implements Comparable<PackageWarning> {
ambiguousDocReference(
'ambiguous-doc-reference',
'ambiguous doc reference {0}',
shortHelp:
'A comment reference could refer to two or more different objects',
),
// Fix these warnings by adding the original library exporting the symbol with
// `--include`, by using `--auto-include-dependencies`, or by using
// `--exclude` to hide one of the libraries involved.
ambiguousReexport(
'ambiguous-reexport',
'ambiguous reexport of {0}, canonicalization candidates: {1}',
shortHelp:
'A symbol is exported from private to public in more than one library '
'and dartdoc can not determine which one is canonical',
longHelp: "Use {@canonicalFor $_namePlaceholder} in the desired library's "
"documentation to resolve the ambiguity and/or override dartdoc's "
'decision, or structure your package so the reexport is less '
'ambiguous. The symbol will still be referenced in all candidates -- '
'this only controls the location where it will be written and which '
'library will be displayed in navigation for the relevant pages. The '
'flag `--ambiguous-reexport-scorer-min-confidence` allows you to set '
'the threshold at which this warning will appear.',
),
ignoredCanonicalFor(
'ignored-canonical-for',
"library says it is {@canonicalFor {0}} but {0} can't be canonical "
'there',
shortHelp:
'A @canonicalFor tag refers to a library which this symbol can not be '
'canonical for',
),
// Fix these warnings by adding libraries with `--include`, or by using
// `--auto-include-dependencies`.
// TODO(jcollins-g): pipeline references through `linkedName` for error
// messages and warn for non-public canonicalization errors.
noCanonicalFound(
'no-canonical-found',
'no canonical library found for {0}, not linking',
shortHelp:
'A symbol is part of the public interface for this package, but no '
'library documented with this package documents it so dartdoc cannot '
'link to it',
),
noDocumentableLibrariesInPackage(
'no-documentable-libraries',
'{0} has no documentable libraries',
shortHelp:
'The package is to be documented but has no Dart libraries to document',
longHelp: 'Dartdoc could not find any public libraries to document in '
"'$_namePlaceholder', but documentation was requested. This might be "
'expected for an asset-only package, in which case, disable this '
'warning in your dartdoc_options.yaml file.',
),
noLibraryLevelDocs(
'no-library-level-docs',
'{0} has no library level documentation comments',
shortHelp: 'There are no library level docs for this library',
),
packageOrderGivesMissingPackageName(
'category-order-gives-missing-package-name',
"--package-order gives invalid package name: '{0}'",
shortHelp:
'The category-order flag on the command line was given the name of a '
'nonexistent package',
),
unresolvedDocReference(
'unresolved-doc-reference',
'unresolved doc reference [{0}]',
shortHelp:
'A comment reference could not be found in parameters, enclosing '
'class, enclosing library, or at the top level of any documented '
'library with the package',
referredFromPrefix: 'in documentation inherited from',
),
unknownMacro(
'unknown-macro',
'undefined macro [{0}]',
shortHelp: 'A comment reference contains an unknown macro',
),
unknownHtmlFragment(
'unknown-html-fragment',
'undefined HTML fragment identifier [{0}]',
shortHelp:
'Dartdoc attempted to inject an unknown block of HTML, indicating a '
'bug in Dartdoc',
),
brokenLink(
'broken-link',
'dartdoc generated a broken link to: {0}',
shortHelp: 'Dartdoc generated a link to a non-existent file',
warnablePrefix: 'to element',
referredFromPrefix: 'linked to from',
),
duplicateFile(
'duplicate-file',
'file already written at "{0}"',
shortHelp:
'Dartdoc is trying to write to a duplicate filename based on the names '
'of Dart symbols.',
longHelp:
'Dartdoc generates a path and filename to write to for each symbol. '
"'$_namePlaceholder' conflicts with another symbol in the generated "
'path, and therefore can not be written out. Changing the name, '
'library name, or class name (if appropriate) of one of the '
'conflicting items can resolve the conflict. Alternatively, use the '
"`@nodoc` directive in one symbol's documentation comment to hide it.",
warnablePrefix: 'for symbol',
referredFromPrefix: 'conflicting with file already generated by',
defaultWarningMode: PackageWarningMode.error,
),
orphanedFile(
'orphaned-file',
'dartdoc generated a file orphan: {0}',
shortHelp: 'Dartdoc generated files that are unreachable from the index',
),
unknownFile(
'unknown-file',
'dartdoc detected an unknown file in the doc tree: {0}',
shortHelp:
'A leftover file exists in the tree that dartdoc did not write in this '
'pass',
),
missingFromSearchIndex(
'missing-from-search-index',
'dartdoc generated a file not in the search index: {0}',
shortHelp: 'A file generated by dartdoc is not present in the generated '
'index.json file',
),
// The message for this warning can contain many punctuation and other
// symbols, so bracket with a triple quote for defense.
typeAsHtml(
'type-as-html',
'generic type handled as HTML: """{0}"""',
shortHelp:
'Use of <> in a comment for type parameters is being treated as HTML '
'by Markdown',
defaultWarningMode: PackageWarningMode.ignore,
),
invalidParameter(
'invalid-parameter',
'invalid parameter to dartdoc directive: {0}',
shortHelp: 'A parameter given to a dartdoc directive was invalid.',
defaultWarningMode: PackageWarningMode.error,
),
toolError(
'tool-error',
'tool execution failed: {0}',
shortHelp: 'Unable to execute external tool.',
defaultWarningMode: PackageWarningMode.error,
),
deprecated(
'deprecated',
'deprecated dartdoc usage: {0}',
shortHelp: 'A dartdoc directive has a deprecated format.',
),
// TODO(kallentu): Remove this warning.
missingCodeBlockLanguage(
'missing-code-block-language',
'missing code block language: {0}',
shortHelp: '(Deprecated: Use `missing_code_block_language_in_doc_comment` '
'lint) A fenced code block is missing a specified language.',
longHelp: '(Deprecated: Use `missing_code_block_language_in_doc_comment` '
'lint) To enable proper syntax highlighting of Markdown code blocks, '
'Dartdoc requires code blocks to specify the language used after the '
'initial declaration. As an example, to specify Dart you would open '
'the Markdown code block with ```dart or ~~~dart.',
defaultWarningMode: PackageWarningMode.ignore,
isDeprecated: true,
);
/// The name which can be used at the command line to enable this warning.
final String _flagName;
/// The message template, with substitutions.
final String _template;
final String _warnablePrefix;
final String _referredFromPrefix;
final String _shortHelp;
final String _longHelp;
final bool _isDeprecated;
final PackageWarningMode _defaultWarningMode;
const PackageWarning(
this._flagName,
this._template, {
required String shortHelp,
String longHelp = '',
String warnablePrefix = 'from',
String referredFromPrefix = 'referred to by',
bool isDeprecated = false,
PackageWarningMode defaultWarningMode = PackageWarningMode.warn,
}) : _shortHelp = shortHelp,
_longHelp = longHelp,
_warnablePrefix = warnablePrefix,
_referredFromPrefix = referredFromPrefix,
_isDeprecated = isDeprecated,
_defaultWarningMode = defaultWarningMode;
static PackageWarning? _byName(String name) =>
PackageWarning.values.firstWhereOrNull((w) => w._flagName == name);
@override
int compareTo(PackageWarning other) {
return _flagName.compareTo(other._flagName);
}
String messageFor(List<String> messageParts) {
var message = _template;
for (var i = 0; i < messageParts.length; i++) {
message = message.replaceAll('{$i}', messageParts[i]);
}
return message;
}
String messageForWarnable(Warnable warnable) =>
'$_warnablePrefix ${warnable.safeWarnableName}: ${warnable.location}';
String messageForReferral(Locatable referral) =>
'$_referredFromPrefix ${referral.safeWarnableName}: ${referral.location}';
}
/// Used to declare defaults for a particular package warning.
enum PackageWarningMode {
ignore,
warn,
error;
String get _warningsListHelpText {
return (PackageWarning.values
.where((w) => w._defaultWarningMode == this)
.toList(growable: false)
..sort())
.map((w) => ' ${w._flagName}: ${w._shortHelp}')
.join('\n');
}
}
/// Warnings which are OK to skip if we can determine the warnable isn't
/// documented.
/// In particular, this set should not include warnings around public/private
/// or canonicalization problems, because those can break the `isDocumented()`
/// check.
const Set<PackageWarning> skipWarningIfNotDocumentedFor = {
PackageWarning.unresolvedDocReference,
PackageWarning.typeAsHtml
};
class PackageWarningOptions {
final Map<PackageWarning, PackageWarningMode> warningModes = {};
PackageWarningOptions() {
for (var definition in PackageWarning.values) {
switch (definition._defaultWarningMode) {
case PackageWarningMode.warn:
warn(definition);
case PackageWarningMode.error:
error(definition);
case PackageWarningMode.ignore:
ignore(definition);
}
}
}
static PackageWarningOptions fromOptions(
DartdocSyntheticOption<PackageWarningOptions> option,
Folder dir,
PackageMetaProvider packageMetaProvider,
) {
// First, initialize defaults.
var newOptions = PackageWarningOptions();
var packageMeta = packageMetaProvider.fromDir(dir)!;
// Interpret errors/warnings/ignore options. In the event of conflict,
// warning overrides error and ignore overrides warning.
var errorsForDir =
option.parent.getValueAs<List<String>?>('errors', dir) ?? [];
for (var warningName in errorsForDir) {
var packageWarning = PackageWarning._byName(warningName);
if (packageWarning != null) {
newOptions.writeErrorOnDeprecation(packageWarning);
newOptions.error(packageWarning);
}
}
var warningsForDir =
option.parent.getValueAs<List<String>?>('warnings', dir) ?? [];
for (var warningName in warningsForDir) {
var packageWarning = PackageWarning._byName(warningName);
if (packageWarning != null) {
newOptions.writeErrorOnDeprecation(packageWarning);
newOptions.warn(packageWarning);
}
}
var ignoredForDir =
option.parent.getValueAs<List<String>?>('ignore', dir) ?? [];
for (var warningName in ignoredForDir) {
var packageWarning = PackageWarning._byName(warningName);
if (packageWarning != null) {
newOptions.ignore(packageWarning);
}
}
// Check whether warnings are allowed at all in this package.
var allowWarningsInPackages =
option.parent.getValueAs<List<String>?>('allowWarningsInPackages', dir);
var allowErrorsInPackages =
option.parent.getValueAs<List<String>?>('allowErrorsInPackages', dir);
var ignoreWarningsInPackages = option.parent
.getValueAs<List<String>?>('ignoreWarningsInPackages', dir);
var ignoreErrorsInPackages =
option.parent.getValueAs<List<String>?>('ignoreErrorsInPackages', dir);
void ignoreWarning(PackageWarning kind) {
newOptions.ignore(kind);
}
if (allowWarningsInPackages != null &&
!allowWarningsInPackages.contains(packageMeta.name)) {
PackageWarning.values.forEach(ignoreWarning);
}
if (allowErrorsInPackages != null &&
!allowErrorsInPackages.contains(packageMeta.name)) {
PackageWarning.values.forEach(ignoreWarning);
}
if (ignoreWarningsInPackages != null &&
ignoreWarningsInPackages.contains(packageMeta.name)) {
PackageWarning.values.forEach(ignoreWarning);
}
if (ignoreErrorsInPackages != null &&
ignoreErrorsInPackages.contains(packageMeta.name)) {
PackageWarning.values.forEach(ignoreWarning);
}
return newOptions;
}
void error(PackageWarning kind) =>
warningModes[kind] = PackageWarningMode.error;
void ignore(PackageWarning kind) =>
warningModes[kind] = PackageWarningMode.ignore;
void warn(PackageWarning kind) =>
warningModes[kind] = PackageWarningMode.warn;
void writeErrorOnDeprecation(PackageWarning warning) {
if (warning._isDeprecated) {
stderr.writeln("The warning '${warning._flagName}' is deprecated.");
}
}
PackageWarningMode getMode(PackageWarning kind) => warningModes[kind]!;
}
class PackageWarningCounter {
final Map<Element2?, Map<PackageWarning, Set<String>>> _countedWarnings = {};
final _items = <Jsonable>[];
final _displayedWarningCounts = <PackageWarning, int>{};
final PackageGraph packageGraph;
int _errorCount = 0;
/// The total amount of errors this package has experienced.
int get errorCount => _errorCount;
int _warningCount = 0;
/// The total amount of warnings this package has experienced.
int get warningCount => _warningCount;
/// An unmodifiable map view of all counted warnings related by their element,
/// warning type, and message.
UnmodifiableMapView<Element2?, Map<PackageWarning, Set<String>>>
get countedWarnings => UnmodifiableMapView(_countedWarnings);
PackageWarningCounter(this.packageGraph);
/// Logs [packageWarning].
///
/// Assumes it is already counted with [addWarning].
void _writeWarning(PackageWarning packageWarning, PackageWarningMode? mode,
bool verboseWarnings, String name, String fullMessage) {
if (mode == PackageWarningMode.ignore) {
return;
}
var type = switch (mode) {
PackageWarningMode.error => 'error',
PackageWarningMode.warn => 'warning',
_ => null,
};
if (type != null) {
var entry = ' $type: $fullMessage';
var displayedWarningCount =
_displayedWarningCounts.increment(packageWarning);
if (displayedWarningCount == 1 &&
verboseWarnings &&
packageWarning._longHelp.isNotEmpty) {
// First time we've seen this warning. Give a little extra info.
var longHelpLines = packageWarning._longHelp
.split('\n')
.map((line) => line.replaceAll(_namePlaceholder, name))
.map((line) =>
_wrapText(line, prefix: ' ', width: _messageWidth));
var verboseOut = longHelpLines.join('\n');
entry = '$entry\n$verboseOut';
}
assert(entry == entry.trimRight());
_items.add(_JsonWarning(type, packageWarning, fullMessage, entry));
}
for (var item in _items) {
logWarning(item.toString());
}
_items.clear();
}
/// If this package has had any warnings counted.
bool get hasWarnings => _countedWarnings.isNotEmpty;
/// Whether we've already warned for this combination of [e], [kind],
/// and [messageFragment].
bool hasWarning(
Warnable? e, PackageWarning kind, String messageFragment) {
if (e == null) {
return false;
}
final warning = _countedWarnings[e.element];
if (warning != null) {
final messages = warning[kind];
return messages != null &&
messages.any((message) => message.contains(messageFragment));
}
return false;
}
/// Adds the warning to the counter, and writes out the fullMessage string
/// if configured to do so.
void addWarning(Warnable? e
, PackageWarning kind, String message,
String fullMessage) {
assert(!hasWarning(e, kind, message));
// TODO(jcollins-g): Make addWarning not accept nulls for element.
PackageWarningOptionContext config =
e?.config ?? packageGraph.defaultPackage.config;
var isLocal = e?.package.isLocal ?? true;
var warningMode = !isLocal && !config.allowNonLocalWarnings
? PackageWarningMode.ignore
: config.packageWarningOptions.getMode(kind);
if (warningMode == PackageWarningMode.warn) {
_warningCount += 1;
} else if (warningMode == PackageWarningMode.error) {
_errorCount += 1;
}
var elementName = e == null ? '<global>' : e.fullyQualifiedName;
_countedWarnings
.putIfAbsent(e?.element, () => {})
.putIfAbsent(kind, () => {})
.add(message);
_writeWarning(
kind,
warningMode,
config.verboseWarnings,
elementName,
fullMessage,
);
}
@override
String toString() {
final errors = '$errorCount ${pluralize('error', errorCount)}';
final warnings = '$warningCount ${pluralize('warning', warningCount)}';
return '$errors, $warnings';
}
}
/// Wraps [text] to the given [width], if provided.
///
/// This function is taken from the 'dartdev' package, which has tests.
String _wrapText(String text, {required int width, String prefix = ''}) {
// For convenience, the caller specifies the line width, but effectively, we
// subtract out the width of the prefix.
width = width - prefix.length;
var buffer = StringBuffer(prefix);
var lineMaxEndIndex = width;
var lineStartIndex = 0;
while (true) {
if (lineMaxEndIndex >= text.length) {
buffer.write(text.substring(lineStartIndex, text.length));
break;
} else {
var lastSpaceIndex = text.lastIndexOf(' ', lineMaxEndIndex);
if (lastSpaceIndex == -1 || lastSpaceIndex <= lineStartIndex) {
// No space between [lineStartIndex] and [lineMaxEndIndex]. Get the
// _next_ space.
lastSpaceIndex = text.indexOf(' ', lineMaxEndIndex);
if (lastSpaceIndex == -1) {
// No space at all after [lineStartIndex].
lastSpaceIndex = text.length;
buffer.write(text.substring(lineStartIndex, lastSpaceIndex));
break;
}
}
buffer.write(text.substring(lineStartIndex, lastSpaceIndex));
buffer.writeln();
buffer.write(prefix);
lineStartIndex = lastSpaceIndex + 1;
}
lineMaxEndIndex = lineStartIndex + width;
}
return buffer.toString();
}
/// The width that messages in the terminal should be limited to.
///
/// If `stdout` has a terminal, use that terminal's width capped to 120
/// characters wide. Otherwise, use a width of 80.
final _messageWidth =
stdout.hasTerminal ? math.min(stdout.terminalColumns, 120) : 80;
class _JsonWarning extends Jsonable {
final String type;
final PackageWarning kind;
final String message;
@override
final String text;
_JsonWarning(this.type, this.kind, this.message, this.text);
@override
Map<String, dynamic> toJson() => {
'type': type,
'kind': kind._flagName,
'message': message,
'text': text,
};
}
extension on Map<PackageWarning, int> {
int increment(PackageWarning kind) {
if (this[kind] == null) {
this[kind] = 1;
return 1;
} else {
this[kind] = this[kind]! + 1;
return this[kind]!;
}
}
}