Skip to content
This repository was archived by the owner on Nov 1, 2024. It is now read-only.

Enforce and fix prefer_typing_uninitialized_variables #100

Merged
merged 1 commit into from
Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ linter:
rules:
- prefer_equal_for_default_values
- prefer_generic_function_type_aliases
- prefer_typing_uninitialized_variables
- slash_for_doc_comments
- unnecessary_const
- unnecessary_new
22 changes: 11 additions & 11 deletions lib/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ class _Parser {
_next();

// Page name
var name;
Identifier name;
if (_peekIdentifier()) {
name = identifier();
}
Expand Down Expand Up @@ -666,7 +666,7 @@ class _Parser {
// '}'
_next();

var name;
dynamic name;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is kind of a fun one - I wanted to avoid changing any behavior so I only gave real types where I could. This surfaced the fact that _peekIdentifier() must always return false for this code path. If it doesn't then name would be an Identifier, since identifier always returns a non-null Identifier. However the StyletDirective constructor expects name to be a String, so the only value that works is null. There is almost certainly a logic bug here that was masked by overly dynamic code, but I don't know exactly what it is to fix it... The null safe Dart migration won't be fun for this package...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤷‍♂️

This isn't making things worse in any case.

if (_peekIdentifier()) {
name = identifier();
}
Expand Down Expand Up @@ -897,7 +897,7 @@ class _Parser {
}
} else if (mixinParameter && _peekToken.kind == TokenKind.VAR_DEFINITION) {
_next();
var definedName;
Identifier definedName;
if (_peekIdentifier()) definedName = identifier();

Expressions exprs;
Expand All @@ -917,7 +917,7 @@ class _Parser {
// @include IDENT [(args,...)];
_next();

var name;
Identifier name;
if (_peekIdentifier()) {
name = identifier();
}
Expand All @@ -931,7 +931,7 @@ class _Parser {
// the first has 3 terms and the second has 1 term.
if (_maybeEat(TokenKind.LPAREN)) {
var terms = <Expression>[];
var expr;
dynamic expr;
var keepGoing = true;
while (keepGoing && (expr = processTerm()) != null) {
// VarUsage is returns as a list
Expand Down Expand Up @@ -960,7 +960,7 @@ class _Parser {
_next(); // '@-moz-document'
var functions = <LiteralTerm>[];
do {
var function;
LiteralTerm function;

// Consume function token: IDENT '('
var ident = identifier();
Expand Down Expand Up @@ -1431,7 +1431,7 @@ class _Parser {
// than the error message for element. Should consolidate the
// code.
// TODO(terry): Need to handle attribute namespace too.
var first;
dynamic first;
var start = _peekToken.span;
switch (_peek()) {
case TokenKind.ASTERISK:
Expand Down Expand Up @@ -1632,7 +1632,7 @@ class _Parser {
var expressions = <Expression>[];

Token termToken;
var value;
dynamic value;

var keepParsing = true;
while (keepParsing) {
Expand Down Expand Up @@ -1719,7 +1719,7 @@ class _Parser {
op = TokenKind.NO_MATCH;
}

var value;
dynamic value;
if (op != TokenKind.NO_MATCH) {
// Operator hit so we require a value too.
if (_peekIdentifier()) {
Expand Down Expand Up @@ -2158,7 +2158,7 @@ class _Parser {
var expressions = Expressions(_makeSpan(start));

var keepGoing = true;
var expr;
dynamic expr;
while (keepGoing && (expr = processTerm(ieFilter)) != null) {
Expression op;

Expand Down Expand Up @@ -2242,7 +2242,7 @@ class _Parser {
[bool ieFilter = false]) {
var start = _peekToken.span;
Token t; // token for term's value
var value; // value of term (numeric values)
dynamic value; // value of term (numeric values)

var unary = '';
switch (_peek()) {
Expand Down
6 changes: 3 additions & 3 deletions lib/src/analyzer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ class _TopLevelIncludeReplacer extends Visitor {
/// Utility function to match an include to a list of either Declarations or
/// RuleSets, depending on type of mixin (ruleset or declaration). The include
/// can be an include in a declaration or an include directive (top-level).
int _findInclude(List list, var node) {
int _findInclude(List list, TreeNode node) {
IncludeDirective matchNode =
(node is IncludeMixinAtDeclaration) ? node.include : node;

Expand Down Expand Up @@ -844,13 +844,13 @@ class DeclarationIncludes extends Visitor {

/// @include as a top-level with ruleset(s).
class _IncludeReplacer extends Visitor {
final _include;
final TreeNode _include;
final List<TreeNode> _newDeclarations;

/// Look for the [ruleSet] inside of a @media directive; if found then replace
/// with the [newRules].
static void replace(
StyleSheet ss, var include, List<TreeNode> newDeclarations) {
StyleSheet ss, TreeNode include, List<TreeNode> newDeclarations) {
var visitor = _IncludeReplacer(include, newDeclarations);
visitor.visitStyleSheet(ss);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/polyfill.dart
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class _VarDefAndUsage extends Visitor {
var result = <Expression>[];

var varDef = _knownVarDefs[usage.name];
var expressions;
List<Expression> expressions;
if (varDef == null) {
// VarDefinition not found try the defaultValues.
expressions = usage.defaultValues;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/token.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Token {

/// A token containing a parsed literal value.
class LiteralToken extends Token {
var value;
dynamic value;
LiteralToken(int kind, FileSpan span, this.value) : super(kind, span);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/tokenizer_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ abstract class TokenizerBase {
}

Token _makeRawStringToken(bool isMultiline) {
var s;
String s;
if (isMultiline) {
// Skip initial newline in multiline strings
var start = _startIndex + 4;
Expand Down
8 changes: 4 additions & 4 deletions lib/src/tree.dart
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class SimpleSelectorSequence extends TreeNode {
// All other selectors (element, #id, .class, attribute, pseudo, negation,
// namespace, *) are derived from this selector.
abstract class SimpleSelector extends TreeNode {
final _name; // Wildcard, ThisOperator, Identifier, Negation, others?
final dynamic _name; // Wildcard, ThisOperator, Identifier, Negation, others?

SimpleSelector(this._name, SourceSpan span) : super(span);

Expand Down Expand Up @@ -200,7 +200,7 @@ class ElementSelector extends SimpleSelector {

// namespace|element
class NamespaceSelector extends SimpleSelector {
final _namespace; // null, Wildcard or Identifier
final dynamic _namespace; // null, Wildcard or Identifier

NamespaceSelector(this._namespace, var name, SourceSpan span)
: super(name, span);
Expand Down Expand Up @@ -1689,7 +1689,7 @@ class BorderExpression extends BoxExpression {
}

class HeightExpression extends DartStyleExpression {
final height;
final dynamic height;

HeightExpression(SourceSpan span, this.height)
: super(DartStyleExpression.heightStyle, span);
Expand All @@ -1712,7 +1712,7 @@ class HeightExpression extends DartStyleExpression {
}

class WidthExpression extends DartStyleExpression {
final width;
final dynamic width;

WidthExpression(SourceSpan span, this.width)
: super(DartStyleExpression.widthStyle, span);
Expand Down