krzysztofzablocki / SourceryPro-Feedback

Repository for discussing https://merowing.info/sourcery-pro/
12 stars 0 forks source link

Crash of code generator 2 #14

Open rocketnik opened 2 years ago

rocketnik commented 2 years ago

The following code crashes when typing ivar in {% call convertType ivar %} - of course I was not finished typing. I wanted to type {% call convertType ivar.typeName %}, but code generation crashed as soon as ivar was entered - it is of the wrong type of course.

{% macro convertType typ %}{% typed typ as TypeName %}{% if typ.isDictionary %}IMap<{% call convertType typ.dictionary.keyTypeName %},{% call convertType typ.dictionary.valueTypeName %}>{% else %}{{ typ }}{% endif %}{% endmacro %}

<<<<<<<
{% for type in types.all %}
class {{ type.name }} {
    {% for ivar in type.variables %}
    final {% call convertType ivar.typeName %} _{{ ivar.name }};
    {% endfor %}
    {{ type.name }}({% for ivar in type.variables %}this._{{ ivar.name }}{% if not forloop.last %}, {% endif %}{% endfor %});
    {{ type.name }} copy({ {% for ivar in type.variables %}{% call convertType ivar %}{{ ivar.name }}{% if not forloop.last %}, {% endif %}{% endfor %}});
}
{% endfor %}
|||||||
class Article {
  final String _title;
  final IMap<String,Section> _sections;
  Article(this._title, this._sections);
  Article copy({String? title, IMap<String, Section>? sections}) => new Article(title ?? this._title, sections ?? this._sections);
  @override String toString() => "Article(title=$_title, sections=$_sections)";

  // Technique: Lenses for accessing/updating relevant properties
  static final title = lensS<Article, String>((article) => article._title, (article, title) => article.copy(title: title));
  static final sections = lensS<Article, IMap<String, Section>>((article) => article._sections, (article, sections) => article.copy(sections: sections));
  static final section = (String id) => sections.andThenE<Section, String>(imapLensE(id, () => "No section '$id'"));
}
>>>>>>>
rocketnik commented 2 years ago

Here is another crash in line 21 in typeName.:

{% macro customTypes %}

class Timestamp {

}

class Category {

}

class ClosedRange<Range> {

}

{% endmacro %}

{% macro qualifiedName name %}{% typed name as String %}{{ name | split:"."| join:"" }}{% endmacro %}

{% macro convertPrimitiveTypeName typeName %}{% typed typeName as TypeName %}{% if typeName.name == "Bool" %}bool{% elif typeName.name == "Int"%}int{% elif typeName.name == "Float"%}double{% else %}{% call qualifiedName typeName.name %}{% endif %}{% endmacro %}

{% macro convertTypeName typeName %}{% typed typeName as TypeName %}{% if typeName.isOptional %}Option<{% call convertTypeName typeName. %}>{% elif typeName.isDictionary %}IMap<{% call convertTypeName typeName.dictionary.keyTypeName %}, {% call convertTypeName typeName.dictionary.valueTypeName %}>{% elif typeName.isArray %}List<{% call convertTypeName typeName.array.elementTypeName %}>{% else %}{% call convertPrimitiveTypeName typeName %}{% endif %}{% endmacro %}

{% macro convertType type %}{% typed type as Enum %}{# Enum seems to by subclass of Type and we don't seem to need functionality of Class/Struct subclass #}
{% if type.kind == "struct" %}
class {% call qualifiedName type.name %} {
    {% for ivar in type.variables | !computed %}
    final {% call convertTypeName ivar.typeName %} _{{ ivar.name }};
    {% endfor %}
    {% call qualifiedName type.name %}({% for ivar in type.variables | !computed %}this._{{ ivar.name }}{% if not forloop.last %}, {% endif %}{% endfor %});
    {% call qualifiedName type.name %} copy({ {% for ivar in type.variables | !computed %}{% call convertTypeName ivar.typeName %}? {{ ivar.name }}{% if not forloop.last %}, {% endif %}{% endfor %}}) => {% call qualifiedName type.name %}({% for ivar in type.variables | !computed %}{{ ivar.name }} ?? _{{ ivar.name }}{% if not forloop.last %}, {% endif %}{% endfor %});
    @override String toString() => "{% call qualifiedName type.name %}({% for ivar in type.variables | !computed %}{{ ivar.name }} = $_{{ ivar.name }}{% if not forloop.last %}, {% endif %}{% endfor %})";
}

{% elif type.kind == "enum" %}
{% if type.cases.count != 0 %}{# don't generate empty enums #}
enum {% call qualifiedName type.name %} {
    {% for case in type.cases %}
    {{ case.name }}{% if not forloop.last %},{% endif %}
    {% endfor %}
}

{% endif %}
{% elif type.kind == "extension" %}
// extension {% call qualifiedName type.name %} {
{% for var in type.variables %}
//     var {{ var.name }}: {{ var.typeName }}
{% endfor %}
// }

{% else %}
// todo: unknown type {% call qualifiedName type.name %}: {{ type.kind }} with content {{ type }}

{% endif %}
{% endmacro %}
// Generated {% now %}

import 'package:dartz/dartz.dart';

{% call customTypes %}

{% for type in types.all where type.name != "Category" %}
{% call convertType type %}
{% endfor %}

//    static final title = lensS<Article, String>((article) => article._title, (article, title) => article.copy(title: title));
//    static final sections = lensS<Article, IMap<String, Section>>((article) => article._sections, (article, sections) => article.copy(sections: sections));
//    static final section = (String id) => sections.andThenE<Section, String>(imapLensE(id, () => "No section '$id'"));
{% newline %}