Skip to content

Commit 31564e5

Browse files
committed
Migrated to dart null safe
1 parent 1ba90b8 commit 31564e5

14 files changed

+260
-197
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,5 @@ fabric.properties
7979
# Android studio 3.1+ serialized cache file
8080
.idea/caches/build_file_checksums.ser
8181

82+
83+
.hooks_tools

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,8 @@
4545

4646
- Updated console reporter to report to stderr if violation severity is greater than info.
4747

48-
- Minor code improvements.
48+
- Minor code improvements.
49+
50+
## 3.0.0-nullsafety.0
51+
52+
- Updated the tool to null safety

analysis_options.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
include: package:flutter_code_style/analysis_options.yaml
2+
3+
analyzer:
4+
exclude: [ example/** ]

bin/dbstyleguidechecker.dart

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,8 @@ Future<void> main(List<String> arguments) async {
7070
CodeStyleViolationsChecker checker;
7171

7272
try {
73-
final CodeStyleViolationsReporter reporter = _createReporter(
74-
scriptArgument,
75-
);
73+
final CodeStyleViolationsReporter reporter =
74+
_createReporter(scriptArgument);
7675

7776
checker = _createChecker(scriptArgument, reporter);
7877
} on UnrecoverableException catch (exception) {
@@ -81,14 +80,18 @@ Future<void> main(List<String> arguments) async {
8180
return;
8281
}
8382

84-
runZoned<void>(checker.check, onError: (Object error, StackTrace stackTrace) {
85-
printHelpMessage(error.toString());
86-
if (error is UnrecoverableException) {
87-
exitCode = error.exitCode;
88-
} else {
89-
exitCode = exitUnexpectedError;
90-
}
91-
});
83+
await runZonedGuarded<Future<void>>(
84+
checker.check,
85+
(Object error, StackTrace stackTrace) {
86+
printHelpMessage(error.toString());
87+
88+
if (error is UnrecoverableException) {
89+
exitCode = error.exitCode;
90+
} else {
91+
exitCode = exitUnexpectedError;
92+
}
93+
},
94+
);
9295
}
9396

9497
CodeStyleViolationsChecker _createChecker(
@@ -103,15 +106,13 @@ CodeStyleViolationsChecker _createChecker(
103106
const DartAnalyzerViolationParser(),
104107
reporter,
105108
);
106-
break;
107109
case flutterProjectType:
108110
return FlutterProjectStyleGuideChecker(
109111
scriptArgument.codeStyle,
110112
scriptArgument.projectDir,
111113
const DartAnalyzerViolationParser(),
112114
reporter,
113115
);
114-
break;
115116
default:
116117
throw UnrecoverableException(
117118
'Invalid project type specified, '
@@ -128,7 +129,7 @@ CodeStyleViolationsReporter _createReporter(
128129
case reporterOfTypeConsole:
129130
return const ConsoleCodeStyleViolationsReporter();
130131
case reporterOfTypeFile:
131-
final File reporterOutputFile = scriptArgument.reporterOutputFile;
132+
final File? reporterOutputFile = scriptArgument.reporterOutputFile;
132133

133134
if (reporterOutputFile == null) {
134135
throw const UnrecoverableException(
@@ -139,7 +140,7 @@ CodeStyleViolationsReporter _createReporter(
139140
}
140141
return FileCodeStyleViolationsReporter(reporterOutputFile);
141142
case reporterOfTypeGithub:
142-
final VcsArgument vcs = scriptArgument.vcs;
143+
final VcsArgument? vcs = scriptArgument.vcs;
143144

144145
if (vcs == null) {
145146
throw const UnrecoverableException(

example/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ environment:
77
sdk: '>=2.4.0 <3.0.0'
88

99
dev_dependencies:
10-
flutter_code_style: ^1.6.2
10+
flutter_code_style: ^2.0.2

lib/src/checkers/dart_project_style_guide_checker.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,13 @@ class DartProjectStyleGuideChecker extends CodeStyleViolationsChecker {
5252
Future<String> getCodeStyleViolations() {
5353
return runPubGet().then(
5454
(_) => Process.run(
55-
'dartanalyzer',
55+
'dart',
5656
<String>[
57+
'analyze',
5758
'--format',
5859
'machine',
59-
'--options',
60-
styleGuide.path,
60+
// '--options',
61+
// styleGuide.path,
6162
projectDir.path,
6263
],
6364
runInShell: true,
@@ -89,7 +90,6 @@ class DartProjectStyleGuideChecker extends CodeStyleViolationsChecker {
8990
exitPackageUpdatedFailed,
9091
);
9192
}
92-
return;
9393
});
9494
}
9595
}

lib/src/code_style_violation.dart

Lines changed: 88 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@
3131
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3232
*/
3333

34-
import 'package:meta/meta.dart' show visibleForTesting, immutable, required;
35-
36-
// ignore_for_file: avoid_as
34+
import 'package:meta/meta.dart' show visibleForTesting, immutable;
3735

3836
/// Dart lint violation class representation.
3937
@immutable
@@ -61,32 +59,61 @@ class CodeStyleViolation {
6159

6260
/// Create a new [CodeStyleViolation].
6361
const CodeStyleViolation({
64-
@required this.severity,
65-
@required this.type,
66-
@required this.file,
67-
@required this.line,
68-
@required this.lineColumn,
69-
@required this.rule,
70-
@required this.ruleDescription,
71-
}) : assert(severity != null, "severity can't be null"),
72-
assert(type != null, "type can't be null"),
73-
assert(file != null, "file can't be null"),
74-
assert(line != null, "line can't be null"),
75-
assert(lineColumn != null, "lineColumn can't be null"),
76-
assert(rule != null, "rule can't be null"),
77-
assert(ruleDescription != null, "ruleDescription can't be null");
62+
required this.severity,
63+
required this.type,
64+
required this.file,
65+
required this.line,
66+
required this.lineColumn,
67+
required this.rule,
68+
required this.ruleDescription,
69+
});
7870

7971
///
80-
CodeStyleViolation.fromJson(final Map<String, Object> json)
81-
: type = json['type'] as String,
82-
file = json['file'] as String,
83-
line = json['line'] as int,
84-
lineColumn = json['lineColumn'] as int,
85-
rule = json['rule'] as String,
86-
ruleDescription = json['ruleDescription'] as String,
87-
severity = ViolationSeverity.fromMap(
88-
json['severity'] as Map<String, Object>,
89-
);
72+
factory CodeStyleViolation.fromJson(final Map<String, Object> json) {
73+
final Object? type = json['type'];
74+
final Object? file = json['file'];
75+
final Object? line = json['line'];
76+
final Object? lineColumn = json['lineColumn'];
77+
final Object? rule = json['rule'];
78+
final Object? ruleDescription = json['ruleDescription'];
79+
final Object? severity = json['severity'];
80+
81+
return CodeStyleViolation(
82+
type: type is String
83+
? type
84+
: throw ArgumentError.value(type, 'type', 'invalid violation type'),
85+
file: file is String
86+
? file
87+
: throw ArgumentError.value(type, 'file', 'invalid file path'),
88+
line: line is int
89+
? line
90+
: throw ArgumentError.value(line, 'line', 'invalid line number'),
91+
lineColumn: lineColumn is int
92+
? lineColumn
93+
: throw ArgumentError.value(
94+
lineColumn,
95+
'lineColumn',
96+
'invalid line column',
97+
),
98+
rule: rule is String
99+
? rule
100+
: throw ArgumentError.value(rule, 'rule', 'invalid rule name'),
101+
ruleDescription: ruleDescription is String
102+
? ruleDescription
103+
: throw ArgumentError.value(
104+
ruleDescription,
105+
'ruleDescription',
106+
'invalid rule description',
107+
),
108+
severity: severity is Map<String, Object>
109+
? ViolationSeverity.fromMap(severity)
110+
: throw ArgumentError.value(
111+
severity,
112+
'severity',
113+
'is not a map of string to object',
114+
),
115+
);
116+
}
90117

91118
///
92119
Map<String, Object> toJson() {
@@ -119,15 +146,15 @@ class CodeStyleViolation {
119146
@override
120147
bool operator ==(Object other) =>
121148
identical(this, other) ||
122-
other is CodeStyleViolation &&
123-
runtimeType == other.runtimeType &&
124-
severity == other.severity &&
125-
type == other.type &&
126-
rule == other.rule &&
127-
ruleDescription == other.ruleDescription &&
128-
file == other.file &&
129-
line == other.line &&
130-
lineColumn == other.lineColumn;
149+
other is CodeStyleViolation &&
150+
runtimeType == other.runtimeType &&
151+
severity == other.severity &&
152+
type == other.type &&
153+
rule == other.rule &&
154+
ruleDescription == other.ruleDescription &&
155+
file == other.file &&
156+
line == other.line &&
157+
lineColumn == other.lineColumn;
131158

132159
@override
133160
int get hashCode =>
@@ -172,9 +199,17 @@ class ViolationSeverity {
172199
final String id;
173200

174201
/// Create [ViolationSeverity] from the parameter [json].
175-
ViolationSeverity.fromMap(final Map<String, Object> json)
176-
: level = json['level'] as int,
177-
id = json['id'] as String;
202+
factory ViolationSeverity.fromMap(final Map<String, Object?> json) {
203+
final Object? level = json['level'];
204+
final Object? id = json['id'];
205+
206+
return ViolationSeverity.private(
207+
level is int
208+
? level
209+
: throw ArgumentError.value(level, 'level', 'invalid level'),
210+
id is String ? id : throw ArgumentError.value(id, 'id', 'invalid id'),
211+
);
212+
}
178213

179214
/// Transform [ViolationSeverity] to a [Map].
180215
Map<String, Object> toJson() {
@@ -183,18 +218,20 @@ class ViolationSeverity {
183218

184219
/// Create [ViolationSeverity] with [level] and [id].
185220
@visibleForTesting
186-
const ViolationSeverity.private(this.level, this.id)
187-
: assert(level != null, "severity can't be null"),
188-
assert(id != null, "id can't be null");
221+
const ViolationSeverity.private(this.level, this.id);
189222

190223
/// Create [ViolationSeverity] from the specified [id].
191224
///
192225
/// Notes: the id need to be one of the items in [supportedSeverities].
193226
factory ViolationSeverity.withId(final String id) {
194-
final ViolationSeverity lintSeverity = supportedSeverities.firstWhere(
195-
(ViolationSeverity element) => element.id == id,
196-
orElse: () => null,
197-
);
227+
ViolationSeverity? lintSeverity;
228+
229+
for (final ViolationSeverity severity in supportedSeverities) {
230+
if (severity.id == id) {
231+
lintSeverity = severity;
232+
break;
233+
}
234+
}
198235

199236
if (lintSeverity == null) {
200237
throw ArgumentError.value(
@@ -210,10 +247,10 @@ class ViolationSeverity {
210247
@override
211248
bool operator ==(Object other) =>
212249
identical(this, other) ||
213-
other is ViolationSeverity &&
214-
runtimeType == other.runtimeType &&
215-
level == other.level &&
216-
id == other.id;
250+
other is ViolationSeverity &&
251+
runtimeType == other.runtimeType &&
252+
level == other.level &&
253+
id == other.id;
217254

218255
@override
219256
int get hashCode => level.hashCode ^ id.hashCode;
@@ -223,5 +260,5 @@ class ViolationSeverity {
223260

224261
/// Contains the list of supported lint rules severities.
225262
static final List<ViolationSeverity> supportedSeverities =
226-
<ViolationSeverity>[info, warning, error, invalid];
263+
<ViolationSeverity>[info, warning, error, invalid];
227264
}

lib/src/parsers/dart_analyzer_violation_parser.dart

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ class DartAnalyzerViolationParser extends CodeStyleViolationsParser {
5555
);
5656

5757
@override
58-
Future<List<CodeStyleViolation>> parse(String violations,
59-
String projectDir,) async {
58+
Future<List<CodeStyleViolation>> parse(
59+
String violations,
60+
String projectDir,
61+
) async {
6062
final List<CodeStyleViolation> parsedViolations = <CodeStyleViolation>[];
6163

6264
for (final String violation in LineSplitter.split(violations)) {
@@ -74,8 +76,9 @@ class DartAnalyzerViolationParser extends CodeStyleViolationsParser {
7476
///
7577
/// [projectDir] containing this file.
7678
@visibleForTesting
77-
CodeStyleViolation parseStyleGuideViolation(final String violation, [
78-
String projectDir,
79+
CodeStyleViolation parseStyleGuideViolation(
80+
final String violation, [
81+
String? projectDir,
7982
]) {
8083
if (violation.isEmpty) {
8184
throw const UnrecoverableException(
@@ -104,19 +107,20 @@ class DartAnalyzerViolationParser extends CodeStyleViolationsParser {
104107
} else {
105108
final RegExpMatch result = matches.single;
106109

107-
final ViolationSeverity severity = ViolationSeverity.withId(result[1]);
108-
final String type = result[2];
109-
final String rule = result[3];
110+
// starting from one because it's the
111+
final ViolationSeverity severity = ViolationSeverity.withId(result[1]!);
112+
final String type = result[2]!;
113+
final String rule = result[3]!;
110114

111-
String filePath = result[4];
115+
String filePath = result[4]!;
112116

113117
if (projectDir != null) {
114118
filePath = getFileRelativePath(filePath, projectDir);
115119
}
116120

117-
final int lineNumber = int.parse(result[5]);
118-
final int lineColumn = int.parse(result[6]);
119-
final String lintRuleDescription = result[8];
121+
final int lineNumber = int.parse(result[5]!);
122+
final int lineColumn = int.parse(result[6]!);
123+
final String lintRuleDescription = result[8]!;
120124

121125
return CodeStyleViolation(
122126
severity: severity,

0 commit comments

Comments
 (0)