Skip to content

Commit

Permalink
Support optional new/const.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
munificent committed Feb 20, 2018
1 parent 13389c0 commit ef767a8
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.0.10

* Support optional `new`/`const` (#652).

# 1.0.9

* Updated tests. No user-facing changes.
Expand Down
2 changes: 1 addition & 1 deletion bin/format.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import 'package:dart_style/src/io.dart';
import 'package:dart_style/src/source_code.dart';

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

void main(List<String> args) {
var parser = new ArgParser(allowTrailingOptions: true);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/dart_formatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class DartFormatter {

// Parse it.
var parser = new Parser(stringSource, errorListener);
parser.enableAssertInitializer = true;
parser.enableOptionalNewAndConst = true;

AstNode node;
if (source.isCompilationUnit) {
Expand Down
3 changes: 1 addition & 2 deletions lib/src/source_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1341,8 +1341,7 @@ class SourceVisitor extends ThrowingAstVisitor {

visitInstanceCreationExpression(InstanceCreationExpression node) {
builder.startSpan();
token(node.keyword);
space();
token(node.keyword, after: space);
builder.startSpan(Cost.constructorName);

// Start the expression nesting for the argument list here, in case this
Expand Down
16 changes: 8 additions & 8 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: analyzer
url: "https://pub.dartlang.org"
source: hosted
version: "0.31.0"
version: "0.31.2-alpha.0"
args:
dependency: "direct main"
description:
Expand All @@ -21,7 +21,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.3"
version: "2.0.4"
barback:
dependency: transitive
description:
Expand Down Expand Up @@ -91,7 +91,7 @@ packages:
name: front_end
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.0-alpha.8"
version: "0.1.0-alpha.10"
glob:
dependency: transitive
description:
Expand Down Expand Up @@ -161,7 +161,7 @@ packages:
name: kernel
url: "https://pub.dartlang.org"
source: hosted
version: "0.3.0-alpha.5"
version: "0.3.0-alpha.10"
logging:
dependency: transitive
description:
Expand Down Expand Up @@ -301,7 +301,7 @@ packages:
name: stack_trace
url: "https://pub.dartlang.org"
source: hosted
version: "1.9.1"
version: "1.9.2"
stream_channel:
dependency: transitive
description:
Expand Down Expand Up @@ -329,7 +329,7 @@ packages:
name: test
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.30+1"
version: "0.12.30+3"
test_descriptor:
dependency: "direct dev"
description:
Expand Down Expand Up @@ -371,7 +371,7 @@ packages:
name: web_socket_channel
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.6"
version: "1.0.7"
yaml:
dependency: "direct dev"
description:
Expand All @@ -380,4 +380,4 @@ packages:
source: hosted
version: "2.1.13"
sdks:
dart: "2.0.0-dev.20.0"
dart: ">=2.0.0-dev.23.0 <=2.0.0-edge.d5105da9cabff92c398d90d7c80fc23b5e38e012"
24 changes: 12 additions & 12 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
name: dart_style
# Note: See tool/grind.dart for how to bump the version.
version: 1.0.9+1
version: 1.0.10
author: Dart Team <[email protected]>
description: Opinionated, automatic Dart source code formatter.
homepage: https://github.com/dart-lang/dart_style
environment:
sdk: ">=1.8.0 <2.0.0"
sdk: '>=1.8.0 <2.0.0'
dependencies:
analyzer: '>=0.30.0 <0.32.0'
analyzer: ^0.31.2-alpha.0
args: '>=0.12.1 <2.0.0'
path: '>=1.0.0 <2.0.0'
source_span: '>=1.4.0 <2.0.0'
path: ^1.0.0
source_span: ^1.4.0
dev_dependencies:
async: '>=1.0.0 <=3.0.0'
browser: '>=0.10.0 <0.11.0'
grinder: '^0.8.0'
browser: ^0.10.0
grinder: ^0.8.0
js: ^0.6.0
node_preamble: ^1.0.0
pub_semver: '^1.2.3'
test: '>=0.12.0 <0.13.0'
test_descriptor: "^1.0.0"
test_process: "^1.0.0"
yaml: '^2.0.0'
pub_semver: ^1.2.3
test: ^0.12.0
test_descriptor: ^1.0.0
test_process: ^1.0.0
yaml: ^2.0.0
executables:
dartfmt: format
dartformat: format # Allow the old name for compatibility.
Expand Down
6 changes: 5 additions & 1 deletion test/whitespace/type_arguments.stmt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ new Foo<T>();
>>> space between names
new Foo< A,B, C,D>();
<<<
new Foo<A, B, C, D>();
new Foo<A, B, C, D>();
>>> named constructor with implicit "new"
C < int ,float> .named(42);
<<<
C<int, float>.named(42);

0 comments on commit ef767a8

Please sign in to comment.