-
Notifications
You must be signed in to change notification settings - Fork 66
New features for 1.0.0-beta #52
Changes from all commits
fa33745
bf57e67
a751518
9693d9e
ef1b68f
8913c77
ca3e9d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ import 'package:code_builder/src/builders/reference.dart'; | |
import 'package:code_builder/src/builders/shared.dart'; | ||
import 'package:code_builder/src/builders/statement.dart'; | ||
import 'package:code_builder/src/builders/statement/if.dart'; | ||
import 'package:code_builder/src/builders/statement/while.dart'; | ||
import 'package:code_builder/src/builders/type.dart'; | ||
import 'package:code_builder/src/tokens.dart'; | ||
|
||
|
@@ -35,6 +36,9 @@ final _null = astFactory.nullLiteral(new KeywordToken(Keyword.NULL, 0)); | |
final _true = | ||
astFactory.booleanLiteral(new KeywordToken(Keyword.TRUE, 0), true); | ||
|
||
/// A reference to `super`. | ||
ExpressionBuilder get superRef => new _SuperExpression(); | ||
|
||
/// Returns a pre-defined literal expression of [value]. | ||
/// | ||
/// Only primitive values are allowed. | ||
|
@@ -123,6 +127,24 @@ abstract class AbstractExpressionMixin implements ExpressionBuilder { | |
); | ||
} | ||
|
||
@override | ||
ExpressionBuilder operator >(ExpressionBuilder other) { | ||
return new _AsBinaryExpression( | ||
this, | ||
other, | ||
$gt, | ||
); | ||
} | ||
|
||
@override | ||
ExpressionBuilder operator <(ExpressionBuilder other) { | ||
return new _AsBinaryExpression( | ||
this, | ||
other, | ||
$lt, | ||
); | ||
} | ||
|
||
@override | ||
ExpressionBuilder and(ExpressionBuilder other) { | ||
return new _AsBinaryExpression( | ||
|
@@ -176,6 +198,11 @@ abstract class AbstractExpressionMixin implements ExpressionBuilder { | |
@override | ||
StatementBuilder asYieldStar() => new _AsYield(this, true); | ||
|
||
@override | ||
WhileStatementBuilder asWhile({bool asDo: false}) { | ||
return new WhileStatementBuilder(asDo, this); | ||
} | ||
|
||
@override | ||
Statement buildStatement([Scope scope]) { | ||
return asStatement().buildStatement(scope); | ||
|
@@ -200,6 +227,9 @@ abstract class AbstractExpressionMixin implements ExpressionBuilder { | |
return new _CascadeExpression(this, create(reference('.'))); | ||
} | ||
|
||
@override | ||
ExpressionBuilder decrement() => new _DecrementExpression(this); | ||
|
||
@override | ||
ExpressionBuilder equals(ExpressionBuilder other) { | ||
return new _AsBinaryExpression( | ||
|
@@ -209,9 +239,14 @@ abstract class AbstractExpressionMixin implements ExpressionBuilder { | |
); | ||
} | ||
|
||
@override | ||
ExpressionBuilder increment([bool prefix = false]) { | ||
return new _IncrementExpression(this, prefix); | ||
} | ||
|
||
@override | ||
ExpressionBuilder identical(ExpressionBuilder other) { | ||
return lib$core.identical.call([ | ||
return lib$core.identical.call(<ExpressionBuilder>[ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this fixing an analyzer warning? I'd be surprised if this wasn't inferred in strong mode... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it could not infer the type and warned as such. |
||
this, | ||
other, | ||
]); | ||
|
@@ -275,6 +310,12 @@ abstract class ExpressionBuilder | |
/// Returns as an [ExpressionBuilder] dividing by [other]. | ||
ExpressionBuilder operator /(ExpressionBuilder other); | ||
|
||
/// Returns as an [ExpressionBuilder] `<` by [other]. | ||
ExpressionBuilder operator <(ExpressionBuilder other); | ||
|
||
/// Returns as an [ExpressionBuilder] `>` by [other]. | ||
ExpressionBuilder operator >(ExpressionBuilder other); | ||
|
||
/// Returns as an [ExpressionBuilder] `&&` [other]. | ||
ExpressionBuilder and(ExpressionBuilder other); | ||
|
||
|
@@ -323,6 +364,9 @@ abstract class ExpressionBuilder | |
/// Returns as a [StatementBuilder] yielding this one. | ||
StatementBuilder asYieldStar(); | ||
|
||
/// Returns as a [WhileStatementBuilder] with this as the condition. | ||
WhileStatementBuilder asWhile({bool asDo: false}); | ||
|
||
/// Returns an [Expression] AST representing the builder. | ||
Expression buildExpression([Scope scope]); | ||
|
||
|
@@ -337,12 +381,18 @@ abstract class ExpressionBuilder | |
Iterable<ExpressionBuilder> create(ExpressionBuilder self), | ||
); | ||
|
||
/// Returns as an [ExpressionBuilder] decrementing this expression. | ||
ExpressionBuilder decrement(); | ||
|
||
/// Returns as an [ExpressionBuilder] comparing using `==` against [other]. | ||
ExpressionBuilder equals(ExpressionBuilder other); | ||
|
||
/// Returns as an [ExpressionBuilder] comparing using `identical`. | ||
ExpressionBuilder identical(ExpressionBuilder other); | ||
|
||
/// Returns as an [ExpressionBuilder] incrementing this expression. | ||
ExpressionBuilder increment([bool prefix = false]); | ||
|
||
/// Returns as an [InvocationBuilder] on [method] of this expression. | ||
InvocationBuilder invoke( | ||
String method, | ||
|
@@ -476,6 +526,15 @@ ExpressionBuilder _expressionify(v) { | |
throw new ArgumentError('Could not expressionify $v'); | ||
} | ||
|
||
class _SuperExpression extends Object | ||
with AbstractExpressionMixin, TopLevelMixin { | ||
@override | ||
AstNode buildAst([Scope scope]) => buildExpression(scope); | ||
|
||
@override | ||
Expression buildExpression([_]) => astFactory.superExpression($super); | ||
} | ||
|
||
class _TypedListExpression extends Object | ||
with AbstractExpressionMixin, TopLevelMixin { | ||
static List<ExpressionBuilder> _toExpression(Iterable values) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -171,7 +171,7 @@ ConstructorBuilder _constructorImpl({ | |
throw new StateError('Invalid AST type: ${member.runtimeType}'); | ||
} | ||
} | ||
final constructor = new ConstructorBuilder(name); | ||
final constructor = new ConstructorBuilder(name: name); | ||
_addFunctions.forEach((a) => a(constructor)); | ||
return constructor; | ||
} | ||
|
@@ -186,7 +186,20 @@ abstract class ConstructorBuilder | |
HasStatements, | ||
ValidClassMember { | ||
/// Create a new [ConstructorBuilder], optionally with a [name]. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doc comment is no longer complete. This is a case where we can add useful information explaining the 'super' arguments ;) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Acknowledged. |
||
factory ConstructorBuilder([String name]) = _NormalConstructorBuilder; | ||
/// | ||
/// Can invoke `super` if [invokeSuper] is set, using super.[superName]. | ||
factory ConstructorBuilder({ | ||
String name, | ||
String superName, | ||
List<ExpressionBuilder> invokeSuper, | ||
}) = _NormalConstructorBuilder; | ||
|
||
/// Adds a field initializer to this constructor. | ||
void addInitializer( | ||
String fieldName, { | ||
ExpressionBuilder toExpression, | ||
String toParameter, | ||
}); | ||
|
||
@override | ||
void addNamed(ParameterBuilder parameter, {bool asField: false}); | ||
|
@@ -530,18 +543,69 @@ class _NamedParameterWrapper | |
class _NormalConstructorBuilder extends Object | ||
with HasAnnotationsMixin, HasParametersMixin, HasStatementsMixin | ||
implements ConstructorBuilder { | ||
final _initializers = <String, ExpressionBuilder>{}; | ||
final String _name; | ||
final String _superName; | ||
final List<ExpressionBuilder> _superInvocation; | ||
|
||
_NormalConstructorBuilder([this._name]); | ||
_NormalConstructorBuilder({ | ||
List<ExpressionBuilder> invokeSuper, | ||
String name, | ||
String superName, | ||
}) | ||
: _name = name, | ||
_superInvocation = invokeSuper, | ||
_superName = superName; | ||
|
||
@override | ||
void addInitializer( | ||
String fieldName, { | ||
ExpressionBuilder toExpression, | ||
String toParameter, | ||
}) { | ||
_initializers[fieldName] = toExpression ?? reference(toParameter); | ||
} | ||
|
||
@override | ||
ConstructorDeclaration buildAst([Scope scope]) { | ||
throw new UnsupportedError('Can only be built as part of a class.'); | ||
} | ||
|
||
@override | ||
ConstructorDeclaration buildConstructor(TypeBuilder returnType, | ||
[Scope scope]) { | ||
ConstructorDeclaration buildConstructor( | ||
TypeBuilder returnType, [ | ||
Scope scope, | ||
]) { | ||
List<ConstructorInitializer> initializers; | ||
if (_initializers.isNotEmpty) { | ||
initializers ??= []; | ||
initializers.addAll( | ||
_initializers.keys.map((fieldName) { | ||
return astFactory.constructorFieldInitializer( | ||
null, | ||
null, | ||
astFactory.simpleIdentifier(stringToken(fieldName)), | ||
$equals, | ||
_initializers[fieldName].buildExpression(scope), | ||
); | ||
}), | ||
); | ||
} | ||
if (_superInvocation != null) { | ||
initializers ??= []; | ||
initializers.add(astFactory.superConstructorInvocation( | ||
$super, | ||
_superName != null ? $period : null, | ||
_superName != null | ||
? astFactory.simpleIdentifier(stringToken(_superName)) | ||
: null, | ||
astFactory.argumentList( | ||
$openParen, | ||
_superInvocation.map((e) => e.buildExpression(scope)).toList(), | ||
$closeParen, | ||
), | ||
)); | ||
} | ||
return astFactory.constructorDeclaration( | ||
null, | ||
buildAnnotations(scope), | ||
|
@@ -552,8 +616,8 @@ class _NormalConstructorBuilder extends Object | |
_name != null ? $period : null, | ||
_name != null ? stringIdentifier(_name) : null, | ||
buildParameterList(scope), | ||
null, | ||
null, | ||
initializers != null && initializers.isNotEmpty ? $semicolon : null, | ||
initializers, | ||
null, | ||
!hasStatements | ||
? astFactory.emptyFunctionBody($semicolon) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nit] how are you choosing between
=>
andreturn
? Why is this one different fromdecrement
above?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trying not to use
=>
when it creates a new line, but probably inconsistent.