-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ability to generate if-else blocks #834
Comments
The problem with builders for statements is there are so many, and it is hard to cover everything. Happy to accept PRs though for statements you feel add value! |
Hmm, good point. Maybe there a way to just let folks built their own statements (like the one above) in a safer way than right now? Right now, I do something like this: final conditionString = condition.accept(_dartEmitter).toString();
final returnStatement = literal(returnValue).returned.statement;
final returnString = returnStatement.accept(_dartEmitter).toString();
return Code('if ($conditionString) { $returnString }'); ( Would it be better to have something like: final buf = CodeBuffer()
..addString('if (')
..addExpression(condition)
..addString(') {')
..addStatement(literal(returnValue).returned)
..addString('}');
return buf.build(); Eh. Not sure if this is any better. What I was driving at is a way not to use |
Just came across this issue and I was thinking about something like this: CompountStatementBuilder()
..addStatement(refer('myBool').eq(literal('1')).asIf())
..addBlock(BlockBuilder()
..addStatement(literal(returnValue).returned.statement)
)
..followedBy(CompountStatementBuilder()
..addStatement(literalElse)
..addBlock(/* .. */));
CompountStatementBuilder()
..addStatement(refer('myArray').asFor(refer('item')))
..addBlock(/* .. */);
CompountStatementBuilder()
..addStatement(literalTry)
..addBlock(/* .. */)
// Maybe use builder callback directly for ease of use?
..followedBy((b) => b
..addStatement(literalCatch)
..addBlock(/* .. */)); Happy to setup a PR. |
I created a helper method like so: Block ifStatement(
Expression conditional,
Code body, [
bool singleLine = false,
]) =>
Block.of([
Code('if ('),
conditional.code,
Code(') {'),
body,
Code('}'),
]); I even have one for try catch thats a bit more complicated Block tryStatement({
required Code try$,
List<CatchClause> clauses = const [],
Catch? catch$,
Code? finally$,
}) {
if (clauses.isEmpty && catch$ == null && finally$ == null) {
throw ArgumentError(
r'Must provide at least one clause such as in `clauses`, `catch$`, or `finally$`');
}
return Block((b) {
b.statements.add(Code('try {'));
b.statements.add(try$);
b.statements.add(Code('}'));
// on X {}
// on X catch (e) {}
// on X catch (e, s) {}
// catch (e) {}
// catch (e, s) {}
for (final clause in clauses) {
if (clause.on case final on) {
b.statements.add(Code('on '));
b.statements.add(on.code);
}
if (clause.catch$) {
b.statements.add(Code('catch (e, s)'));
}
b.statements.add(Code('{'));
b.statements.add(clause.body(refer('e'), refer('s')));
b.statements.add(Code('}'));
}
if (catch$ != null) {
b.statements.add(Code('catch (e, s) {'));
b.statements.add(catch$.body(refer('e'), refer('s')));
b.statements.add(Code('}'));
}
if (finally$ != null) {
b.statements.add(Code('finally {'));
b.statements.add(finally$);
b.statements.add(Code('}'));
}
});
}
class CatchClause {
final CatchBodyBuilder body;
final Reference on;
final bool catch$;
CatchClause(
Code body, {
required this.on,
}) : catch$ = false,
body = ((_, __) => body);
const CatchClause.catch$(
this.body, {
required this.on,
}) : catch$ = true;
}
class Catch {
final CatchBodyBuilder body;
const Catch(this.body);
static const rethrow$ = Catch(_rethrowBody);
static Code _rethrowBody(_, __) => refer('rethrow').statement;
}
typedef CatchBodyBuilder = Code Function(Reference error, Reference stackTrace); (note, i did not implement an option that excludes the stacktrace option) |
why did this get moved? I'm a bit confused. |
@TekExplorer code_builder got moved into this repo, see #693 and dart-archive/code_builder#467 |
A previous version had
.asIf()
for building statements likeAs far as I know, the only way to do these with current API is to write this manually via
Code(...)
.The text was updated successfully, but these errors were encountered: