Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make dart_style mostly strong mode clean.
Running into one case of: dart-lang/sdk#25698

R=kevmoo@google.com

Review URL: https://codereview.chromium.org//1758453003 .
  • Loading branch information
munificent committed Mar 2, 2016
1 parent fd89540 commit 700b86d
Show file tree
Hide file tree
Showing 13 changed files with 208 additions and 96 deletions.
2 changes: 2 additions & 0 deletions .analysis_options
@@ -0,0 +1,2 @@
analyzer:
strong-mode: true
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
# 0.2.7

* Make it strong mode clean.

# 0.2.6

* Support deploying an npm package exporting a formatCode method.
Expand Down
6 changes: 3 additions & 3 deletions bin/format.dart
Expand Up @@ -52,7 +52,7 @@ void main(List<String> args) {
negatable: false,
help: "Unused flag for compability with the old formatter.");

var argResults;
ArgResults argResults;
try {
argResults = parser.parse(args);
} on FormatException catch (err) {
Expand All @@ -70,7 +70,7 @@ void main(List<String> args) {
}

// Can only preserve a selection when parsing from stdin.
var selection;
List<int> selection;

if (argResults["preserve"] != null && argResults.rest.isNotEmpty) {
usageError(parser, "Can only use --preserve when reading from stdin.");
Expand Down Expand Up @@ -192,7 +192,7 @@ void formatStdin(FormatterOptions options, List<int> selection) {
var output = formatter.formatSource(source);
options.reporter
.afterFile(null, "<stdin>", output, changed: source != output);
return true;
return;
} on FormatterException catch (err) {
stderr.writeln(err.message());
exitCode = 65; // sysexits.h: EX_DATAERR
Expand Down
2 changes: 1 addition & 1 deletion lib/src/argument_list_visitor.dart
Expand Up @@ -295,7 +295,7 @@ class ArgumentSublist {
arguments.takeWhile((arg) => arg is! NamedExpression).toList();
var named = arguments.skip(positional.length).toList();

var collections = {};
var collections = <Expression, Token>{};
for (var argument in arguments) {
var bracket = _getCollectionBracket(argument);
if (bracket != null) collections[argument] = bracket;
Expand Down
10 changes: 6 additions & 4 deletions lib/src/call_chain_visitor.dart
Expand Up @@ -94,7 +94,7 @@ class CallChainVisitor {
var target;

// Recursively walk the chain of calls and turn the tree into a list.
var calls = [];
var calls = <Expression>[];
flatten(expression) {
target = expression;

Expand Down Expand Up @@ -130,19 +130,21 @@ class CallChainVisitor {
// address.street.number
// .toString()
// .length;
var properties = [];
var properties = <Expression>[];
if (target is SimpleIdentifier) {
properties = calls.takeWhile((call) {
// Step into index expressions to see what the index is on.
while (call is IndexExpression) call = call.target;
while (call is IndexExpression) {
call = (call as IndexExpression).target;
}
return call is! MethodInvocation;
}).toList();
}

calls.removeRange(0, properties.length);

// Separate out the block calls, if there are any.
var blockCalls;
List<Expression> blockCalls;
var hangingCall;

var inBlockCalls = false;
Expand Down
9 changes: 4 additions & 5 deletions lib/src/debug.dart
Expand Up @@ -71,19 +71,18 @@ void dumpChunks(int start, List<Chunk> chunks) {

// Show the spans as vertical bands over their range (unless there are too
// many).
var spans = new Set();
addSpans(chunks) {
var spanSet = new Set<Span>();
addSpans(List<Chunk> chunks) {
for (var chunk in chunks) {
spans.addAll(chunk.spans);
spanSet.addAll(chunk.spans);

if (chunk.isBlock) addSpans(chunk.block.chunks);
}
}

addSpans(chunks);

spans = spans.toList();

var spans = spanSet.toList();
var rules =
chunks.map((chunk) => chunk.rule).where((rule) => rule != null).toSet();

Expand Down
8 changes: 4 additions & 4 deletions lib/src/formatter_options.dart
Expand Up @@ -32,17 +32,17 @@ class FormatterOptions {
abstract class OutputReporter {
/// Prints only the names of files whose contents are different from their
/// formatted version.
static final dryRun = new _DryRunReporter();
static final OutputReporter dryRun = new _DryRunReporter();

/// Prints the formatted results of each file to stdout.
static final print = new _PrintReporter();
static final OutputReporter print = new _PrintReporter();

/// Prints the formatted result and selection info of each file to stdout as
/// a JSON map.
static final printJson = new _PrintJsonReporter();
static final OutputReporter printJson = new _PrintJsonReporter();

/// Overwrites each file with its formatted result.
static final overwrite = new _OverwriteReporter();
static final OutputReporter overwrite = new _OverwriteReporter();

/// Describe the directory whose contents are about to be processed.
void showDirectory(String path) {}
Expand Down
10 changes: 5 additions & 5 deletions lib/src/line_splitting/solve_state.dart
Expand Up @@ -195,7 +195,7 @@ class SolveState {
for (var value = 1; value < rule.numValues; value++) {
var boundRules = unsplitRules.clone();

var mustSplitRules;
List<Rule> mustSplitRules;
var valid = boundRules.tryBind(_splitter.rules, rule, value, (rule) {
if (mustSplitRules == null) mustSplitRules = [];
mustSplitRules.add(rule);
Expand Down Expand Up @@ -453,9 +453,9 @@ class SolveState {
void _ensureBoundRulesInUnboundLines() {
if (_boundRulesInUnboundLines != null) return;

_boundRulesInUnboundLines = new Set();
_boundRulesInUnboundLines = new Set<Rule>();

var boundInLine = new Set();
var boundInLine = new Set<Rule>();
var hasUnbound = false;

for (var i = 0; i < _splitter.chunks.length - 1; i++) {
Expand Down Expand Up @@ -528,7 +528,7 @@ class SolveState {
_unboundConstraints = {};

for (var unbound in _unboundRules) {
var disallowedValues;
Set<int> disallowedValues;

for (var bound in unbound.constrainedRules) {
if (!_boundRules.contains(bound)) continue;
Expand All @@ -552,7 +552,7 @@ class SolveState {
}

if (disallowedValues == null) {
disallowedValues = new Set();
disallowedValues = new Set<int>();
_unboundConstraints[unbound] = disallowedValues;
}

Expand Down
7 changes: 4 additions & 3 deletions lib/src/rule/rule.dart
Expand Up @@ -35,7 +35,8 @@ class Rule extends FastHash {
/// By convention, this is the highest of the range of allowed values.
int get fullySplitValue => numValues - 1;

final int cost;
int get cost => _cost;
final int _cost;

/// During line splitting [LineSplitter] sets this to the index of this
/// rule in its list of rules.
Expand Down Expand Up @@ -74,10 +75,10 @@ class Rule extends FastHash {
/// rules.
bool get splitsOnInnerRules => true;

Rule([int cost]) : cost = cost ?? Cost.normal;
Rule([int cost]) : _cost = cost ?? Cost.normal;

/// Creates a new rule that is already fully split.
Rule.hard() : cost = 0 {
Rule.hard() : _cost = 0 {
// Set the cost to zero since it will always be applied, so there's no
// point in penalizing it.
//
Expand Down
25 changes: 13 additions & 12 deletions lib/src/source_visitor.dart
Expand Up @@ -342,10 +342,13 @@ class SourceVisitor implements AstVisitor {
// consistent names but for now we require all sections to have the same
// method name.
for (var expression in sections) {
if (expression is! MethodInvocation) return false;
if (name == null) {
name = expression.methodName.name;
} else if (name != expression.methodName.name) {
if (expression is MethodInvocation) {
if (name == null) {
name = expression.methodName.name;
} else if (name != expression.methodName.name) {
return false;
}
} else {
return false;
}
}
Expand Down Expand Up @@ -410,9 +413,8 @@ class SourceVisitor implements AstVisitor {
twoNewlines();
} else if (member is MethodDeclaration) {
// Add a blank line after non-empty block methods.
var method = member as MethodDeclaration;
if (method.body is BlockFunctionBody) {
var body = method.body as BlockFunctionBody;
if (member.body is BlockFunctionBody) {
var body = member.body as BlockFunctionBody;
needsDouble = body.block.statements.isNotEmpty;
}
}
Expand Down Expand Up @@ -492,9 +494,8 @@ class SourceVisitor implements AstVisitor {
needsDouble = true;
} else if (declaration is FunctionDeclaration) {
// Add a blank line after non-empty block functions.
var function = declaration as FunctionDeclaration;
if (function.functionExpression.body is BlockFunctionBody) {
var body = function.functionExpression.body as BlockFunctionBody;
if (declaration.functionExpression.body is BlockFunctionBody) {
var body = declaration.functionExpression.body as BlockFunctionBody;
needsDouble = body.block.statements.isNotEmpty;
}
}
Expand Down Expand Up @@ -1724,7 +1725,7 @@ class SourceVisitor implements AstVisitor {
void _visitMemberDeclaration(
/* FunctionDeclaration|MethodDeclaration */ node,
/* FunctionExpression|MethodDeclaration */ function) {
visitMetadata(node.metadata);
visitMetadata(node.metadata as NodeList<Annotation>);

// Nest the signature in case we have to split between the return type and
// name.
Expand Down Expand Up @@ -2254,7 +2255,7 @@ class SourceVisitor implements AstVisitor {

var tokenLine = _startLine(token);

var comments = [];
var comments = <SourceComment>[];
while (comment != null) {
var commentLine = _startLine(comment);

Expand Down

0 comments on commit 700b86d

Please sign in to comment.