Skip to content

Commit 81100c9

Browse files
committed
feature: no longer generate new, const
Prepare to release json_serializable 1.0.1 Fixes #275
1 parent ab724f9 commit 81100c9

File tree

73 files changed

+534
-543
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+534
-543
lines changed

.travis.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ jobs:
1919
env: PKG="example"
2020
dart: dev
2121
- stage: analyzer_and_format
22-
name: "SDK: dev - DIR: example - TASKS: dartanalyzer --fatal-infos --fatal-warnings ."
23-
script: ./tool/travis.sh dartanalyzer
22+
name: "SDK: dev - DIR: example - TASKS: dartanalyzer --fatal-warnings ."
23+
script: ./tool/travis.sh dartanalyzer_0
2424
env: PKG="example"
2525
dart: dev
2626
- stage: unit_test
@@ -35,7 +35,7 @@ jobs:
3535
dart: dev
3636
- stage: analyzer_and_format
3737
name: "SDK: dev - DIR: json_annotation - TASKS: dartanalyzer --fatal-infos --fatal-warnings ."
38-
script: ./tool/travis.sh dartanalyzer
38+
script: ./tool/travis.sh dartanalyzer_1
3939
env: PKG="json_annotation"
4040
dart: dev
4141
- stage: analyzer_and_format
@@ -45,7 +45,7 @@ jobs:
4545
dart: dev
4646
- stage: analyzer_and_format
4747
name: "SDK: dev - DIR: json_serializable - TASKS: dartanalyzer --fatal-infos --fatal-warnings ."
48-
script: ./tool/travis.sh dartanalyzer
48+
script: ./tool/travis.sh dartanalyzer_1
4949
env: PKG="json_serializable"
5050
dart: dev
5151
- stage: unit_test
@@ -70,7 +70,7 @@ stages:
7070

7171
# Only building master means that we don't run two builds for each pull request.
7272
branches:
73-
only: [master,0_5_x]
73+
only: [master]
7474

7575
cache:
7676
directories:

analysis_options.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ linter:
6969
- type_init_formals
7070
- unawaited_futures
7171
- unnecessary_brace_in_string_interps
72+
- unnecessary_const
7273
- unnecessary_getters_setters
7374
- unnecessary_lambdas
75+
- unnecessary_new
7476
- unnecessary_null_aware_assignments
7577
- unnecessary_statements
7678
- unnecessary_this

example/.mono_repo.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ dart:
55
stages:
66
- analyzer_and_format:
77
- dartfmt
8-
- dartanalyzer: --fatal-infos --fatal-warnings .
8+
# Skipping --fatal-infos until we can regenerate this package with publish
9+
# json_serializable
10+
- dartanalyzer: --fatal-warnings .
911
- unit_test:
1012
# Run the tests -- include the default-skipped presubmit tests
1113
- test: --run-skipped

example/lib/example.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ class Order {
5454
}
5555

5656
Duration _durationFromMillseconds(int milliseconds) =>
57-
new Duration(milliseconds: milliseconds);
57+
Duration(milliseconds: milliseconds);
5858
int _durationToMilliseconds(Duration duration) => duration.inMilliseconds;
5959

6060
DateTime _dateTimeFromEpochUs(int us) =>
61-
new DateTime.fromMicrosecondsSinceEpoch(us);
61+
DateTime.fromMicrosecondsSinceEpoch(us);
6262
int _dateTimeToEpochUs(DateTime dateTime) => dateTime.microsecondsSinceEpoch;
6363

6464
@JsonSerializable()

example/test/ensure_build_test.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
// TODO(kevmoo): replace with a common utility
66
// https://github.com/dart-lang/build/issues/716
7-
@Tags(const ['presubmit-only'])
7+
@Tags(['presubmit-only'])
88

99
import 'dart:convert';
1010
import 'dart:io';
@@ -20,7 +20,7 @@ void main() {
2020
var currentDir = Directory.current.resolveSymbolicLinksSync();
2121

2222
if (!p.equals(p.join(pkgRoot, 'example'), currentDir)) {
23-
throw new StateError('Expected the git root ($pkgRoot) '
23+
throw StateError('Expected the git root ($pkgRoot) '
2424
'to match the current directory ($currentDir).');
2525
}
2626
});
@@ -33,14 +33,14 @@ void main() {
3333
var result = _runProc('pub',
3434
['run', 'build_runner', 'build', '--delete-conflicting-outputs']);
3535
expect(result,
36-
contains(new RegExp(r'\[INFO\] Succeeded after \S+ with \d+ outputs')));
36+
contains(RegExp(r'\[INFO\] Succeeded after \S+ with \d+ outputs')));
3737

3838
// 3 - get a list of modified `.g.dart` files - should still be empty
3939
expect(_changedGeneratedFiles(), isEmpty);
4040
});
4141
}
4242

43-
final _whitespace = new RegExp(r'\s');
43+
final _whitespace = RegExp(r'\s');
4444

4545
Set<String> _changedGeneratedFiles() {
4646
var output = _runProc('git', ['status', '--porcelain']);
@@ -55,7 +55,7 @@ String _runProc(String proc, List<String> args) {
5555
var result = Process.runSync(proc, args);
5656

5757
if (result.exitCode != 0) {
58-
throw new ProcessException(
58+
throw ProcessException(
5959
proc, args, result.stderr as String, result.exitCode);
6060
}
6161

example/test/example_test.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@ import 'package:test/test.dart';
99

1010
void main() {
1111
test('JsonSerializable', () {
12-
final person = new Person('Inigo', 'Montoya', new DateTime(1560, 5, 5))
13-
..orders = [
14-
new Order(new DateTime.now())..item = (new Item()..count = 42)
15-
];
12+
final person = Person('Inigo', 'Montoya', DateTime(1560, 5, 5))
13+
..orders = [Order(DateTime.now())..item = (Item()..count = 42)];
1614

1715
final personJson = _encode(person);
1816

1917
final person2 =
20-
new Person.fromJson(json.decode(personJson) as Map<String, dynamic>);
18+
Person.fromJson(json.decode(personJson) as Map<String, dynamic>);
2119

2220
expect(person.firstName, person2.firstName);
2321
expect(person.lastName, person2.lastName);

example/test/readme_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ void main() {
1313

1414
void _expect(String fileName) {
1515
test(fileName, () {
16-
final file = new File(fileName);
16+
final file = File(fileName);
1717
expect(file.readAsStringSync(), contains(_pubspecContent));
1818
});
1919
}

json_annotation/lib/src/allowed_keys_helpers.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ void $checkKeys(Map map,
1515
var invalidKeys =
1616
map.keys.cast<String>().where((k) => !allowedKeys.contains(k)).toList();
1717
if (invalidKeys.isNotEmpty) {
18-
throw new UnrecognizedKeysException(invalidKeys, map, allowedKeys);
18+
throw UnrecognizedKeysException(invalidKeys, map, allowedKeys);
1919
}
2020
}
2121

2222
if (requiredKeys != null) {
2323
var missingKeys = requiredKeys.where((k) => !map.keys.contains(k)).toList();
2424
if (missingKeys.isNotEmpty) {
25-
throw new MissingRequiredKeysException(missingKeys, map);
25+
throw MissingRequiredKeysException(missingKeys, map);
2626
}
2727
}
2828

@@ -34,7 +34,7 @@ void $checkKeys(Map map,
3434
.toList();
3535

3636
if (nullValuedKeys.isNotEmpty) {
37-
throw new DisallowedNullValueException(nullValuedKeys, map);
37+
throw DisallowedNullValueException(nullValuedKeys, map);
3838
}
3939
}
4040
}

json_annotation/lib/src/checked_helpers.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ T $checkedNew<T>(String className, Map map, T constructor(),
2828
} else if (error is DisallowedNullValueException) {
2929
key = error.keysWithNullValues.first;
3030
}
31-
throw new CheckedFromJsonException._(error, stack, map, key,
31+
throw CheckedFromJsonException._(error, stack, map, key,
3232
className: className);
3333
}
3434
}
@@ -43,7 +43,7 @@ T $checkedConvert<T>(Map map, String key, T castFunc(Object value)) {
4343
} on CheckedFromJsonException {
4444
rethrow;
4545
} catch (error, stack) {
46-
throw new CheckedFromJsonException._(error, stack, map, key);
46+
throw CheckedFromJsonException._(error, stack, map, key);
4747
}
4848
}
4949

json_annotation/lib/src/wrapper_helpers.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,30 @@ abstract class $JsonMapWrapper extends UnmodifiableMapBase<String, dynamic> {}
1616
/// Should not be used directly.
1717
Map<String, dynamic> $wrapMap<K, V>(
1818
Map<K, V> source, dynamic converter(V key)) =>
19-
new _MappingMap(source, converter);
19+
_MappingMap(source, converter);
2020

2121
/// Helper function used in generated code when
2222
/// `JsonSerializableGenerator.useWrappers` is `true`.
2323
///
2424
/// Should not be used directly.
2525
Map<String, dynamic> $wrapMapHandleNull<K, V>(
2626
Map<K, V> source, dynamic converter(V key)) =>
27-
source == null ? null : new _MappingMap(source, converter);
27+
source == null ? null : _MappingMap(source, converter);
2828

2929
/// Helper function used in generated code when
3030
/// `JsonSerializableGenerator.useWrappers` is `true`.
3131
///
3232
/// Should not be used directly.
3333
List<dynamic> $wrapList<T>(List<T> source, dynamic converter(T key)) =>
34-
new _MappingList(source, converter);
34+
_MappingList(source, converter);
3535

3636
/// Helper function used in generated code when
3737
/// `JsonSerializableGenerator.useWrappers` is `true`.
3838
///
3939
/// Should not be used directly.
4040
List<dynamic> $wrapListHandleNull<T>(
4141
List<T> source, dynamic converter(T key)) =>
42-
source == null ? null : new _MappingList(source, converter);
42+
source == null ? null : _MappingList(source, converter);
4343

4444
typedef dynamic _Convert<S>(S value);
4545

@@ -53,13 +53,13 @@ class _MappingList<S> extends ListBase<dynamic> {
5353
dynamic operator [](int index) => _converter(_source[index]);
5454

5555
@override
56-
operator []=(int index, dynamic value) => throw new UnsupportedError('');
56+
operator []=(int index, dynamic value) => throw UnsupportedError('');
5757

5858
@override
5959
int get length => _source.length;
6060

6161
@override
62-
set length(int value) => throw new UnsupportedError('');
62+
set length(int value) => throw UnsupportedError('');
6363
}
6464

6565
class _MappingMap<K, V> extends UnmodifiableMapBase<String, dynamic> {

json_annotation/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: json_annotation
2-
version: 1.0.0
2+
version: 1.0.1-dev
33
description: >-
44
Classes and helper functions that support JSON code generation via the
55
`json_serializable` package.

json_serializable/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.1.0
2+
3+
* Explicit `new` and `const` are no longer generated.
4+
15
## 1.0.0
26

37
* **BREAKING** By default, code generated to support `toJson` now creates

json_serializable/example/example.g.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

json_serializable/lib/builder.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import 'src/json_part_builder.dart';
2323
Builder jsonSerializable(BuilderOptions options) {
2424
// Paranoid copy of options.config - don't assume it's mutable or needed
2525
// elsewhere.
26-
var optionsMap = new Map<String, dynamic>.from(options.config);
26+
var optionsMap = Map<String, dynamic>.from(options.config);
2727

2828
var builder = jsonPartBuilder(
2929
useWrappers: optionsMap.remove('use_wrappers') as bool,
@@ -36,7 +36,7 @@ Builder jsonSerializable(BuilderOptions options) {
3636

3737
if (optionsMap.isNotEmpty) {
3838
if (log == null) {
39-
throw new StateError('Upgrade `build_runner` to at least 0.8.2.');
39+
throw StateError('Upgrade `build_runner` to at least 0.8.2.');
4040
} else {
4141
log.warning('These options were ignored: `$optionsMap`.');
4242
}

json_serializable/lib/src/decode_helper.dart

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import 'type_helper.dart';
1111
import 'utils.dart';
1212

1313
abstract class DecodeHelper implements HelperCore {
14-
final StringBuffer _buffer = new StringBuffer();
14+
final StringBuffer _buffer = StringBuffer();
1515

1616
String createFactory(Map<String, FieldElement> accessibleFields,
1717
Map<String, String> unavailableReasons) {
@@ -68,15 +68,15 @@ abstract class DecodeHelper implements HelperCore {
6868
_buffer.write('''\n return val;
6969
}''');
7070

71-
var fieldKeyMap = new Map.fromEntries(fieldsSetByFactory
72-
.map((k) => new MapEntry(k, nameAccess(accessibleFields[k])))
71+
var fieldKeyMap = Map.fromEntries(fieldsSetByFactory
72+
.map((k) => MapEntry(k, nameAccess(accessibleFields[k])))
7373
.where((me) => me.key != me.value));
7474

7575
String fieldKeyMapArg;
7676
if (fieldKeyMap.isEmpty) {
7777
fieldKeyMapArg = '';
7878
} else {
79-
var mapLiteral = jsonMapAsDart(fieldKeyMap, true);
79+
var mapLiteral = jsonMapAsDart(fieldKeyMap);
8080
fieldKeyMapArg = ', fieldKeyMap: $mapLiteral';
8181
}
8282

@@ -122,8 +122,8 @@ abstract class DecodeHelper implements HelperCore {
122122
var args = <String>[];
123123

124124
if (classAnnotation.disallowUnrecognizedKeys) {
125-
var allowKeysLiteral = jsonLiteralAsDart(
126-
accessibleFields.values.map(nameAccess).toList(), true);
125+
var allowKeysLiteral =
126+
jsonLiteralAsDart(accessibleFields.values.map(nameAccess).toList());
127127

128128
args.add('allowedKeys: $allowKeysLiteral');
129129
}
@@ -132,7 +132,7 @@ abstract class DecodeHelper implements HelperCore {
132132
accessibleFields.values.where((fe) => jsonKeyFor(fe).required).toList();
133133
if (requiredKeys.isNotEmpty) {
134134
var requiredKeyLiteral =
135-
jsonLiteralAsDart(requiredKeys.map(nameAccess).toList(), true);
135+
jsonLiteralAsDart(requiredKeys.map(nameAccess).toList());
136136

137137
args.add('requiredKeys: $requiredKeyLiteral');
138138
}
@@ -142,7 +142,7 @@ abstract class DecodeHelper implements HelperCore {
142142
.toList();
143143
if (disallowNullKeys.isNotEmpty) {
144144
var dissallowNullKeyLiteral =
145-
jsonLiteralAsDart(disallowNullKeys.map(nameAccess).toList(), true);
145+
jsonLiteralAsDart(disallowNullKeys.map(nameAccess).toList());
146146

147147
args.add('disallowNullValues: $dissallowNullKeyLiteral');
148148
}
@@ -211,11 +211,11 @@ _ConstructorData _writeConstructorInvocation(
211211
var ctor = classElement.unnamedConstructor;
212212
if (ctor == null) {
213213
// TODO(kevmoo): support using another ctor - dart-lang/json_serializable#50
214-
throw new UnsupportedError(
214+
throw UnsupportedError(
215215
'The class `$className` has no default constructor.');
216216
}
217217

218-
var usedCtorParamsAndFields = new Set<String>();
218+
var usedCtorParamsAndFields = Set<String>();
219219
var constructorArguments = <ParameterElement>[];
220220
var namedConstructorArguments = <ParameterElement>[];
221221

@@ -231,7 +231,7 @@ _ConstructorData _writeConstructorInvocation(
231231
msg = '$msg $additionalInfo';
232232
}
233233

234-
throw new UnsupportedError(msg);
234+
throw UnsupportedError(msg);
235235
}
236236

237237
continue;
@@ -253,8 +253,8 @@ _ConstructorData _writeConstructorInvocation(
253253
var remainingFieldsForInvocationBody =
254254
writeableFields.toSet().difference(usedCtorParamsAndFields);
255255

256-
var buffer = new StringBuffer();
257-
buffer.write('new $className${genericClassArguments(classElement, false)}(');
256+
var buffer = StringBuffer();
257+
buffer.write('$className${genericClassArguments(classElement, false)}(');
258258
if (constructorArguments.isNotEmpty) {
259259
buffer.writeln();
260260
buffer.writeAll(constructorArguments.map((paramElement) {
@@ -279,8 +279,8 @@ _ConstructorData _writeConstructorInvocation(
279279

280280
usedCtorParamsAndFields.addAll(remainingFieldsForInvocationBody);
281281

282-
return new _ConstructorData(buffer.toString(),
283-
remainingFieldsForInvocationBody, usedCtorParamsAndFields);
282+
return _ConstructorData(buffer.toString(), remainingFieldsForInvocationBody,
283+
usedCtorParamsAndFields);
284284
}
285285

286286
class _ConstructorData {

0 commit comments

Comments
 (0)