Skip to content

Commit b89b572

Browse files
authored
Support optional new/const. (#669)
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 96c65d9 commit b89b572

File tree

7 files changed

+27
-20
lines changed

7 files changed

+27
-20
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: 3 additions & 3 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.1"
10+
version: "0.31.2-alpha.0"
1111
args:
1212
dependency: "direct main"
1313
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.9"
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.9"
164+
version: "0.3.0-alpha.10"
165165
logging:
166166
dependency: transitive
167167
description:

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
@@ -14,4 +14,8 @@ new Foo<void, void Function()>();
1414
>>> void as a generic method type argument
1515
list.map< void,void Function()>();
1616
<<<
17-
list.map<void, void Function()>();
17+
list.map<void, void Function()>();
18+
>>> named constructor with implicit "new"
19+
C < int ,float> .named(42);
20+
<<<
21+
C<int, float>.named(42);

0 commit comments

Comments
 (0)