Skip to content

Commit ef767a8

Browse files
committed
Support optional new/const.
In almost all cases, an elided "new"/"const" is parsed as a normal method call by analyzer, so the existing formatting behavior for those does the right thing. The only edge case is a named constructor on a generic class. That syntax can *only* be a constructor call, so it's parsed as an instance creation expression. When we get to that code path, we need to not add an extra space before the class name if there is no preceding "new" or "const" keyword. Fix #652.
1 parent 13389c0 commit ef767a8

File tree

7 files changed

+32
-25
lines changed

7 files changed

+32
-25
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.0.10
2+
3+
* Support optional `new`/`const` (#652).
4+
15
# 1.0.9
26

37
* Updated tests. No user-facing changes.

bin/format.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import 'package:dart_style/src/io.dart';
1414
import 'package:dart_style/src/source_code.dart';
1515

1616
// Note: The following line of code is modified by tool/grind.dart.
17-
const version = "1.0.9";
17+
const version = "1.0.10";
1818

1919
void main(List<String> args) {
2020
var parser = new ArgParser(allowTrailingOptions: true);

lib/src/dart_formatter.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class DartFormatter {
103103

104104
// Parse it.
105105
var parser = new Parser(stringSource, errorListener);
106-
parser.enableAssertInitializer = true;
106+
parser.enableOptionalNewAndConst = true;
107107

108108
AstNode node;
109109
if (source.isCompilationUnit) {

lib/src/source_visitor.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,8 +1341,7 @@ class SourceVisitor extends ThrowingAstVisitor {
13411341

13421342
visitInstanceCreationExpression(InstanceCreationExpression node) {
13431343
builder.startSpan();
1344-
token(node.keyword);
1345-
space();
1344+
token(node.keyword, after: space);
13461345
builder.startSpan(Cost.constructorName);
13471346

13481347
// Start the expression nesting for the argument list here, in case this

pubspec.lock

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ packages:
77
name: analyzer
88
url: "https://pub.dartlang.org"
99
source: hosted
10-
version: "0.31.0"
10+
version: "0.31.2-alpha.0"
1111
args:
1212
dependency: "direct main"
1313
description:
@@ -21,7 +21,7 @@ packages:
2121
name: async
2222
url: "https://pub.dartlang.org"
2323
source: hosted
24-
version: "2.0.3"
24+
version: "2.0.4"
2525
barback:
2626
dependency: transitive
2727
description:
@@ -91,7 +91,7 @@ packages:
9191
name: front_end
9292
url: "https://pub.dartlang.org"
9393
source: hosted
94-
version: "0.1.0-alpha.8"
94+
version: "0.1.0-alpha.10"
9595
glob:
9696
dependency: transitive
9797
description:
@@ -161,7 +161,7 @@ packages:
161161
name: kernel
162162
url: "https://pub.dartlang.org"
163163
source: hosted
164-
version: "0.3.0-alpha.5"
164+
version: "0.3.0-alpha.10"
165165
logging:
166166
dependency: transitive
167167
description:
@@ -301,7 +301,7 @@ packages:
301301
name: stack_trace
302302
url: "https://pub.dartlang.org"
303303
source: hosted
304-
version: "1.9.1"
304+
version: "1.9.2"
305305
stream_channel:
306306
dependency: transitive
307307
description:
@@ -329,7 +329,7 @@ packages:
329329
name: test
330330
url: "https://pub.dartlang.org"
331331
source: hosted
332-
version: "0.12.30+1"
332+
version: "0.12.30+3"
333333
test_descriptor:
334334
dependency: "direct dev"
335335
description:
@@ -371,7 +371,7 @@ packages:
371371
name: web_socket_channel
372372
url: "https://pub.dartlang.org"
373373
source: hosted
374-
version: "1.0.6"
374+
version: "1.0.7"
375375
yaml:
376376
dependency: "direct dev"
377377
description:
@@ -380,4 +380,4 @@ packages:
380380
source: hosted
381381
version: "2.1.13"
382382
sdks:
383-
dart: "2.0.0-dev.20.0"
383+
dart: ">=2.0.0-dev.23.0 <=2.0.0-edge.d5105da9cabff92c398d90d7c80fc23b5e38e012"

pubspec.yaml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
name: dart_style
22
# Note: See tool/grind.dart for how to bump the version.
3-
version: 1.0.9+1
3+
version: 1.0.10
44
author: Dart Team <[email protected]>
55
description: Opinionated, automatic Dart source code formatter.
66
homepage: https://github.com/dart-lang/dart_style
77
environment:
8-
sdk: ">=1.8.0 <2.0.0"
8+
sdk: '>=1.8.0 <2.0.0'
99
dependencies:
10-
analyzer: '>=0.30.0 <0.32.0'
10+
analyzer: ^0.31.2-alpha.0
1111
args: '>=0.12.1 <2.0.0'
12-
path: '>=1.0.0 <2.0.0'
13-
source_span: '>=1.4.0 <2.0.0'
12+
path: ^1.0.0
13+
source_span: ^1.4.0
1414
dev_dependencies:
1515
async: '>=1.0.0 <=3.0.0'
16-
browser: '>=0.10.0 <0.11.0'
17-
grinder: '^0.8.0'
16+
browser: ^0.10.0
17+
grinder: ^0.8.0
1818
js: ^0.6.0
1919
node_preamble: ^1.0.0
20-
pub_semver: '^1.2.3'
21-
test: '>=0.12.0 <0.13.0'
22-
test_descriptor: "^1.0.0"
23-
test_process: "^1.0.0"
24-
yaml: '^2.0.0'
20+
pub_semver: ^1.2.3
21+
test: ^0.12.0
22+
test_descriptor: ^1.0.0
23+
test_process: ^1.0.0
24+
yaml: ^2.0.0
2525
executables:
2626
dartfmt: format
2727
dartformat: format # Allow the old name for compatibility.

test/whitespace/type_arguments.stmt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ new Foo<T>();
66
>>> space between names
77
new Foo< A,B, C,D>();
88
<<<
9-
new Foo<A, B, C, D>();
9+
new Foo<A, B, C, D>();
10+
>>> named constructor with implicit "new"
11+
C < int ,float> .named(42);
12+
<<<
13+
C<int, float>.named(42);

0 commit comments

Comments
 (0)