Skip to content

Commit 82382c6

Browse files
authored
tighten up analysis; add types at the api boundaries (dart-archive/source_maps#73)
1 parent 8ed4c0f commit 82382c6

16 files changed

+58
-60
lines changed

pkgs/source_maps/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 0.10.12
2+
3+
* Add additional types at API boundaries.
4+
15
# 0.10.11
26

37
* Populate the pubspec `repository` field.

pkgs/source_maps/README.md

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
[![pub package](https://img.shields.io/pub/v/source_maps.svg)](https://pub.dev/packages/source_maps)
33
[![package publisher](https://img.shields.io/pub/publisher/source_maps.svg)](https://pub.dev/packages/source_maps/publisher)
44

5-
This project implements a Dart pub package to work with source maps. The
6-
implementation is based on the [source map version 3 spec][spec] which was
5+
This project implements a Dart pub package to work with source maps.
6+
7+
## Docs and usage
8+
9+
The implementation is based on the [source map version 3 spec][spec] which was
710
originated from the [Closure Compiler][closure] and has been implemented in
811
Chrome and Firefox.
912

@@ -18,13 +21,5 @@ In this package we provide:
1821
* A parser that reads the source map format and provides APIs to read the
1922
mapping information.
2023

21-
Some upcoming features we are planning to add to this package are:
22-
23-
* A printer that lets you generate code, but record source map information in
24-
the process.
25-
* A tool that can compose source maps together. This would be useful for
26-
instance, if you have 2 tools that produce source maps and you call one with
27-
the result of the other.
28-
2924
[closure]: https://github.com/google/closure-compiler/wiki/Source-Maps
3025
[spec]: https://docs.google.com/a/google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
include: package:lints/recommended.yaml
2-
3-
linter:
4-
rules:
5-
- comment_references
1+
include: package:dart_flutter_team_lints/analysis_options.yaml

pkgs/source_maps/lib/builder.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class SourceMapBuilder {
4545
}
4646

4747
/// Encodes all mappings added to this builder as a json map.
48-
Map build(String fileUrl) {
48+
Map<String, dynamic> build(String fileUrl) {
4949
return SingleMapping.fromEntries(_entries, fileUrl).toJson();
5050
}
5151

pkgs/source_maps/lib/parser.dart

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import 'src/vlq.dart';
2525
// `)]}'` begins the string representation of the map.
2626
Mapping parse(String jsonMap,
2727
{Map<String, Map>? otherMaps, /*String|Uri*/ Object? mapUrl}) =>
28-
parseJson(jsonDecode(jsonMap), otherMaps: otherMaps, mapUrl: mapUrl);
28+
parseJson(jsonDecode(jsonMap) as Map, otherMaps: otherMaps, mapUrl: mapUrl);
2929

3030
/// Parses a source map or source map bundle directly from a json string.
3131
///
@@ -50,7 +50,7 @@ Mapping parseJsonExtended(/*List|Map*/ Object? json,
5050
return parseJson(json as Map);
5151
}
5252

53-
/// Parses a source map
53+
/// Parses a source map.
5454
///
5555
/// [mapUrl], which may be either a [String] or a [Uri], indicates the URL of
5656
/// the source map file itself. If it's passed, any URLs in the source
@@ -69,10 +69,10 @@ Mapping parseJson(Map map,
6969
throw FormatException('map containing "sections" '
7070
'cannot contain "mappings", "sources", or "names".');
7171
}
72-
return MultiSectionMapping.fromJson(map['sections'], otherMaps,
72+
return MultiSectionMapping.fromJson(map['sections'] as List, otherMaps,
7373
mapUrl: mapUrl);
7474
}
75-
return SingleMapping.fromJson(map, mapUrl: mapUrl);
75+
return SingleMapping.fromJson(map.cast<String, dynamic>(), mapUrl: mapUrl);
7676
}
7777

7878
/// A mapping parsed out of a source map.
@@ -108,21 +108,21 @@ class MultiSectionMapping extends Mapping {
108108
/// Creates a section mapping from json.
109109
MultiSectionMapping.fromJson(List sections, Map<String, Map>? otherMaps,
110110
{/*String|Uri*/ Object? mapUrl}) {
111-
for (var section in sections) {
112-
var offset = section['offset'];
111+
for (var section in sections.cast<Map>()) {
112+
var offset = section['offset'] as Map?;
113113
if (offset == null) throw FormatException('section missing offset');
114114

115-
var line = section['offset']['line'];
115+
var line = offset['line'] as int?;
116116
if (line == null) throw FormatException('offset missing line');
117117

118-
var column = section['offset']['column'];
118+
var column = offset['column'] as int?;
119119
if (column == null) throw FormatException('offset missing column');
120120

121121
_lineStart.add(line);
122122
_columnStart.add(column);
123123

124-
var url = section['url'];
125-
var map = section['map'];
124+
var url = section['url'] as String?;
125+
var map = section['map'] as Map?;
126126

127127
if (url != null && map != null) {
128128
throw FormatException("section can't use both url and map entries");
@@ -189,7 +189,7 @@ class MappingBundle extends Mapping {
189189

190190
MappingBundle.fromJson(List json, {/*String|Uri*/ Object? mapUrl}) {
191191
for (var map in json) {
192-
addMapping(parseJson(map, mapUrl: mapUrl) as SingleMapping);
192+
addMapping(parseJson(map as Map, mapUrl: mapUrl) as SingleMapping);
193193
}
194194
}
195195

@@ -342,18 +342,18 @@ class SingleMapping extends Mapping {
342342
urls.keys.toList(), names.keys.toList(), lines);
343343
}
344344

345-
SingleMapping.fromJson(Map map, {mapUrl})
346-
: targetUrl = map['file'],
347-
urls = List<String>.from(map['sources']),
348-
names = List<String>.from(map['names'] ?? []),
349-
files = List.filled(map['sources'].length, null),
350-
sourceRoot = map['sourceRoot'],
345+
SingleMapping.fromJson(Map<String, dynamic> map, {mapUrl})
346+
: targetUrl = map['file'] as String?,
347+
urls = List<String>.from(map['sources'] as List),
348+
names = List<String>.from((map['names'] as List?) ?? []),
349+
files = List.filled((map['sources'] as List).length, null),
350+
sourceRoot = map['sourceRoot'] as String?,
351351
lines = <TargetLineEntry>[],
352-
_mapUrl = mapUrl is String ? Uri.parse(mapUrl) : mapUrl,
352+
_mapUrl = mapUrl is String ? Uri.parse(mapUrl) : (mapUrl as Uri?),
353353
extensions = {} {
354354
var sourcesContent = map['sourcesContent'] == null
355355
? const <String?>[]
356-
: List<String?>.from(map['sourcesContent']);
356+
: List<String?>.from(map['sourcesContent'] as List);
357357
for (var i = 0; i < urls.length && i < sourcesContent.length; i++) {
358358
var source = sourcesContent[i];
359359
if (source == null) continue;
@@ -366,7 +366,7 @@ class SingleMapping extends Mapping {
366366
var srcLine = 0;
367367
var srcColumn = 0;
368368
var srcNameId = 0;
369-
var tokenizer = _MappingTokenizer(map['mappings']);
369+
var tokenizer = _MappingTokenizer(map['mappings'] as String);
370370
var entries = <TargetEntry>[];
371371

372372
while (tokenizer.hasTokens) {
@@ -432,7 +432,7 @@ class SingleMapping extends Mapping {
432432
///
433433
/// If [includeSourceContents] is `true`, this includes the source file
434434
/// contents from [files] in the map if possible.
435-
Map toJson({bool includeSourceContents = false}) {
435+
Map<String, dynamic> toJson({bool includeSourceContents = false}) {
436436
var buff = StringBuffer();
437437
var line = 0;
438438
var column = 0;
@@ -471,12 +471,12 @@ class SingleMapping extends Mapping {
471471
}
472472
}
473473

474-
var result = {
474+
var result = <String, dynamic>{
475475
'version': 3,
476476
'sourceRoot': sourceRoot ?? '',
477477
'sources': urls,
478478
'names': names,
479-
'mappings': buff.toString()
479+
'mappings': buff.toString(),
480480
};
481481
if (targetUrl != null) result['file'] = targetUrl!;
482482

@@ -690,6 +690,8 @@ class _MappingTokenizer implements Iterator<String> {
690690
buff.write('');
691691
try {
692692
buff.write(current);
693+
// TODO: Determine whether this try / catch can be removed.
694+
// ignore: avoid_catching_errors
693695
} on RangeError catch (_) {}
694696
buff.write('');
695697
for (var i = index + 1; i < _internal.length; i++) {

pkgs/source_maps/lib/printer.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Printer {
3636
/// adds a source map location on each new line, projecting that every new
3737
/// line in the target file (printed here) corresponds to a new line in the
3838
/// source file.
39-
void add(String str, {projectMarks = false}) {
39+
void add(String str, {bool projectMarks = false}) {
4040
var chars = str.runes.toList();
4141
var length = chars.length;
4242
for (var i = 0; i < length; i++) {
@@ -85,7 +85,7 @@ class Printer {
8585
/// [SourceSpan]. When the mark is a [SourceMapSpan] with `isIdentifier` set,
8686
/// this also records the name of the identifier in the source map
8787
/// information.
88-
void mark(mark) {
88+
void mark(Object mark) {
8989
late final SourceLocation loc;
9090
String? identifier;
9191
if (mark is SourceLocation) {
@@ -105,13 +105,13 @@ class Printer {
105105
/// including [NestedPrinter]s, and it let's you automatically indent text.
106106
///
107107
/// This class is especially useful when doing code generation, where different
108-
/// peices of the code are generated independently on separate printers, and are
108+
/// pieces of the code are generated independently on separate printers, and are
109109
/// finally put together in the end.
110110
class NestedPrinter implements NestedItem {
111111
/// Items recoded by this printer, which can be [String] literals,
112112
/// [NestedItem]s, and source map information like [SourceLocation] and
113113
/// [SourceSpan].
114-
final _items = <dynamic>[];
114+
final List<Object> _items = [];
115115

116116
/// Internal buffer to merge consecutive strings added to this printer.
117117
StringBuffer? _buff;
@@ -149,7 +149,7 @@ class NestedPrinter implements NestedItem {
149149
/// Indicate [isOriginal] when [object] is copied directly from the user code.
150150
/// Setting [isOriginal] will make this printer propagate source map locations
151151
/// on every line-break.
152-
void add(object,
152+
void add(Object object,
153153
{SourceLocation? location, SourceSpan? span, bool isOriginal = false}) {
154154
if (object is! String || location != null || span != null || isOriginal) {
155155
_flush();

pkgs/source_maps/lib/refactor.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class TextEditTransaction {
3030
/// Edit the original text, replacing text on the range [begin] and [end]
3131
/// with the [replacement]. [replacement] can be either a string or a
3232
/// [NestedPrinter].
33-
void edit(int begin, int end, replacement) {
33+
void edit(int begin, int end, Object replacement) {
3434
_edits.add(_TextEdit(begin, end, replacement));
3535
}
3636

pkgs/source_maps/lib/source_maps.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ library source_maps;
2828

2929
import 'package:source_span/source_span.dart';
3030

31-
import 'parser.dart';
3231
import 'builder.dart';
32+
import 'parser.dart';
3333

3434
export 'builder.dart';
3535
export 'parser.dart';

pkgs/source_maps/lib/src/source_map_span.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ class SourceMapFileSpan implements SourceMapSpan, FileSpan {
6161
@override
6262
int compareTo(SourceSpan other) => _inner.compareTo(other);
6363
@override
64-
String highlight({color}) => _inner.highlight(color: color);
64+
String highlight({Object? color}) => _inner.highlight(color: color);
6565
@override
6666
SourceSpan union(SourceSpan other) => _inner.union(other);
6767
@override
6868
FileSpan expand(FileSpan other) => _inner.expand(other);
6969
@override
70-
String message(String message, {color}) =>
70+
String message(String message, {Object? color}) =>
7171
_inner.message(message, color: color);
7272
@override
7373
String toString() =>

pkgs/source_maps/lib/src/utils.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ library source_maps.utils;
1010
/// and all items after `n` match too. The result is -1 when there are no
1111
/// items, 0 when all items match, and list.length when none does.
1212
// TODO(sigmund): remove this function after dartbug.com/5624 is fixed.
13-
int binarySearch(List list, bool Function(dynamic) matches) {
13+
int binarySearch<T>(List<T> list, bool Function(T) matches) {
1414
if (list.isEmpty) return -1;
1515
if (matches(list.first)) return 0;
1616
if (!matches(list.last)) return list.length;

0 commit comments

Comments
 (0)