Repository navigation
feat(craft): add the engine core with the text format - #23
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces the core Dart implementation of the Craft template engine (a2ui_craft), which is a framework-independent fork of Remote Flutter Widgets (RFW) 2.0.0. It adds specification blueprints, a canonical JSON serializer for conformance testing, and shared test cases. The feedback highlights a potential runtime type error in canonical_json.dart when pattern matching maps, and notes that the newly introduced reference types (ThemeReference, MediaReference, and SymbolReference) lack unit tests in model_test.dart to verify their behavior.
| Map<String, Object?>() => <String, Object?>{ | ||
| for (final MapEntry<String, Object?> entry in node.entries) | ||
| entry.key: canonicalValue(entry.value), | ||
| }, |
There was a problem hiding this comment.
Matching Map<String, Object?>() using pattern matching will fail at runtime if the map's key type is not exactly String (for example, Map<dynamic, dynamic> or Map<Object, Object?>), even if all keys in the map are actually strings. This is because Map key types are not covariant with String in Dart.\n\nUsing Map() instead of Map<String, Object?>() is more robust and prevents unexpected runtime crashes when processing maps with different runtime type arguments.
Map() => <String, Object?>{\n for (final MapEntry<Object?, Object?> entry in node.entries)\n entry.key as String: canonicalValue(entry.value),\n },There was a problem hiding this comment.
Not needed: Map<String, dynamic> matches Map<String, Object?> at runtime, and the parser and decoder only produce Map<String, Object?>. A map with non-string keys isn't a library value and is meant to hit the ArgumentError at the end; an as String cast would hide that.
| }); | ||
| } |
There was a problem hiding this comment.
According to the repository style guide (Best practices #4: 'If there are code changes, code should have tests'), the newly introduced reference types (ThemeReference, MediaReference, and SymbolReference) should have unit tests in model_test.dart to verify their behavior (such as toString(), equality, and constructReference).
});\n\n test('ThemeReference, MediaReference, SymbolReference', () async {\n const themeRef = ThemeReference(<Object>['color', 'primary']);\n expect('$themeRef', 'theme.color.primary');\n expect(themeRef.constructReference(<Object>['accent']).parts, const <Object>['color', 'primary', 'accent']);\n\n const mediaRef = MediaReference(<Object>['width']);\n expect('$mediaRef', 'media.width');\n expect(mediaRef.constructReference(<Object>['height']).parts, const <Object>['width', 'height']);\n\n const symbolRef = SymbolReference('Location', <Object>['args', 'latLong']);\n expect('$symbolRef', 'Location.args.latLong');\n expect(symbolRef.constructReference(<Object>['alt']).parts, const <Object>['args', 'latLong', 'alt']);\n expect(symbolRef == const SymbolReference('Location', <Object>['args', 'latLong']), isTrue);\n });\n}References
- Best practices Bump joi from 17.13.7 to 17.13.9 in /typescript-starter #4: If there are code changes, code should have tests. (link)
There was a problem hiding this comment.
Fair point for toString and SymbolReference equality; added a test in the latest push. Parsing and the canonical form of these references are covered by the conformance cases. constructReference on them isn't used by the engine, so I didn't add tests for it.
2152ff8 to
5ac782f
Compare
Starts craft/packages/a2ui_craft, the framework-independent core of the Craft engine, with its first feature: the text format. - lib/src/rfw/model.dart and text.dart are Yegor's fork of RFW's library model and text parser (yjbanov/a2ui_craft at aece53f), formatted with the repo's formatter. Compared with RFW 2.0.0 they add three references: theme.<parts>, media.<parts> and symbol references (Name.<parts>); this PR also rejects keywords such as `default` as symbol names. craft/tool/diff_rfw.sh shows the exact diff, ignoring formatting. They keep RFW's BSD license (the package's LICENSE file says which files); the license script skips them. - RFW 2.0.0's own text and model tests pass, adapted only to run outside Flutter (imports, and testWidgets replaced by test). - craft/blueprints/ holds the engine's module blueprint and the craft_text_format feature blueprint, in A2UI's format, checked by craft/blueprints/validate.py (A2UI's validator). - craft/conformance/craft/craft_text_format.yaml holds 28 shared cases, stated in the canonical JSON form of a library, which test/conformance/ runs. - The shared cases follow A2UI's conformance conventions, in the layout they will have in the A2UI repository (craft/conformance/README.md): every case names an `action` (parse_library and parse_data so far) and the `features` it exercises. craft/conformance/conformance_schema.json defines the fields of each action, and craft/conformance/tests/test_conformance_yaml.py validates every suite against it. craft/conformance/features.yaml lists the feature IDs, one per blueprint requirement (F-CRAFT-TEXT-01 to 05), each with the anchor of its requirement, validated by features.schema.json (A2UI's, with Craft's ID pattern and harnesses). - The Dart harness (test/conformance/conformance_harness.dart) runs each case with the handler of its action, fails a case with an unknown action or key, and writes A2UI's executed-case manifest; suite_registry.dart names the test file of each suite. - Feature names carry a craft_ prefix and feature IDs an F-CRAFT- prefix, and paths in blueprints and suites are relative to craft/, so that the move to the A2UI repository is mechanical; craft/README.md maps each path and says how the codebase blueprints' commits get pinned. - A Presubmit Craft workflow validates the blueprints and the suites, analyzes the Craft packages, checks the feature registry with A2UI's check_feature_coverage.py, and checks with A2UI's check_executed_cases.py that the Dart harness ran every case. Co-authored-by: Yegor <yegor.jbanov@gmail.com>
5ac782f to
5dc010b
Compare
ditman
left a comment
There was a problem hiding this comment.
🤖 Really solid foundation — the pure-Dart split, minimal delta against unmodified RFW 2.0.0 (diff_rfw.sh), web/Wasm compilation check in CI, and direct reuse of A2UI's blueprint/conformance validators make this very easy to review and graduate into a2ui-project/a2ui later.
Approving this so we can keep the stack moving — please address the concerns below (and the inline comments) in a follow-up PR down the line:
- Remove wildcard exports from the main barrel (
lib/a2ui_craft.dart): Please replace the bareexport 'src/...';directives with explicitshowlists so we can track public API changes clearly across PRs, and keep conformance-only helpers (canonicalLibrary/canonicalValue) and internal RFW runtime types (Bound*Reference,Any*, etc.) off the main package barrel. SymbolReferenceequality vs.ThemeReference/MediaReference+ unusedconstructReference(model.dart):SymbolReferenceoverrides==/hashCodewhile the other reference nodes don't, which leaks intoswitchduplicate-case parsing (rejecting duplicateSymbolReferencekeys while accepting duplicateThemeReference/MediaReferencekeys) without being specified in the blueprint. Included a reproducer test inline. AlsoconstructReferenceon all three new reference classes is currently unused and untested.null.xconsistency in map vs. list values (text.dart):Box(a: [null.x])reportsnull cannot be used as a symbol name, whereasBox(a: null.x)takes thenullOkbranch first and fails later expecting)(and shifts to the next line if.xwraps). Included a reproducer test inline.- Minor harness/canonical-JSON/doc nits: copying
partsin_reference, adding direct unit tests forcanonicalValue(missing)/ArgumentError(and noting thedart2js1.0 is intcaveat), tighteningparse_cases.darterror position assertions, plus a couple of small schema/README nits.
(Also noted that CR/LF handling is already queued up in #38 on top of the stack, so I left line endings out of this review.)
- Theme, media and symbol references all compare by identity, like RFW's references: SymbolReference no longer overrides ==/hashCode, so a switch keyed twice by the same reference parses for every kind. The blueprint states the rule; the unused constructReference methods of the three classes are gone. - `null.x` as an argument or map value is rejected with "null cannot be used as a symbol name", as in a list; data files parse as in RFW. - canonicalValue copies reference parts and notes that under dart2js a whole-number double gets the integer form; canonical_json_test covers null, missing and the ArgumentError fallback. - The parse harness checks that every rejection reports a line >= 1 and a column >= 0 and that `expect` has exactly one key. - New conformance cases: duplicate reference switch keys, references rejected in data files, media(...) in a value position, theme and media without a part, and null.x as an argument (also wrapped). - README: diff_rfw.sh diffs lib/src/rfw/ only.
|
Thanks for the |
|
Thanks for creating the stack! I'll ask the agent to publish a bunch of review comments at once (looking at them myself to make sure they make sense :P) |
# Description Adds the **libraries and names** feature to the Craft engine core: how a name in a template denotes exactly one widget or function. Stacked on #23. Same three parts as every engine PR: a blueprint, conformance cases for it, and the code. - **Blueprint**: `craft/blueprints/features/craft_libraries_and_names.blueprint.md`. - **Text format** (`lib/src/rfw/text.dart`, `model.dart`): - qualified call names `lib::Name` and `a.b::Name`; the qualified string is the call's name, so the binary format doesn't change; - `export widget X = ...;`, which marks a declaration as exported. The canonical JSON form carries `"exported": true`. No source that RFW accepts changes meaning; RFW's own tests still pass. - **Name resolution** (`lib/src/name_resolution.dart`): - a library's own declarations come first, but a declaration never resolves to itself (`widget Button = Button(...)` wraps core's `Button`); - then `core`, when imported; - names from any other import must be qualified, so adding a name to a library can never change or break an existing template; - imports are not transitive, and other libraries' private (not exported) declarations are invisible; - only exported declarations of a catalog library are agent-facing components; - function names follow the same rules. - **Tests**: 45 conformance cases in `craft/conformance/craft/craft_libraries_and_names.yaml` (parse cases and resolution queries; failures are pinned by reason, not message). Every rejection case has a control case that differs only in the name. Unit tests are in `test/name_resolution_test.dart`. An independent review checked the blueprint against the tests and the code, including code mutations. Next: the binary format (versioning, extension tags, the exported flag). ## Pre-launch Checklist One time: - [x] I signed the [CLA]. - [x] I read the [Contributors Guide]. For this PR: - [x] I updated/added relevant documentation. - [x] My code changes (if any) have tests. - [x] License checks pass (`python3 ./scripts/fix_licenses.py --check`). - [x] Format checks pass (`./scripts/fix_format.sh --check`). <!-- Links --> [CLA]: https://cla.developers.google.com/ [Contributors Guide]: https://github.com/a2ui-project/a2ui-labs/blob/main/CONTRIBUTING.md
Description
Starts
craft/packages/a2ui_craft, the framework-independent core of the Craft engine, with its first feature: the text format. Stacked on #22.Every engine PR in this series has the same three parts: a feature blueprint (in
craft/blueprints/, A2UI's blueprint format), conformance cases for that blueprint (incraft/conformance/, language-agnostic, so any engine can run them), and the code.What's in it
Blueprints:
craft/blueprints/modules/craft_engine.blueprint.md(the engine's principles and layers) andcraft/blueprints/features/craft_text_format.blueprint.md.craft/blueprints/validate.pyruns A2UI's blueprint validator on them.Code:
lib/src/rfw/model.dartandtext.dartare Yegor's fork of RFW's library model and text parser, from yjbanov/a2ui_craft ataece53f. Compared with RFW 2.0.0 (feat(craft): add Remote Flutter Widgets 2.0.0, unmodified #22) they add three kinds of reference:theme.<parts>(yjbanov/a2ui_craft@c81f4dd),media.<parts>(yjbanov/a2ui_craft@b6ef483),Name.<parts>, for host metadata (yjbanov/a2ui_craft@038f4d9).This PR also rejects keywords such as
defaultas symbol names. To see exactly what changed relative to RFW, ignoring formatting, runcraft/tool/diff_rfw.sh(about 130 changed lines). The files keep RFW's BSD license (see the package'sLICENSE);scripts/fix_licenses.pyskips them.lib/src/canonical_json.dart: the canonical JSON form of a library, which the conformance cases use.Tests: RFW 2.0.0's own text and model tests (38) pass, adapted only to run outside Flutter (imports;
testWidgets→test). Also 28 conformance cases incraft/conformance/craft/craft_text_format.yaml, one or more per requirement.CI: a new
Presubmit Craftworkflow validates the blueprints and runsdart analyze --fatal-infoson Craft packages. The tests run in the existing presubmit.Aligned with the A2UI main repo (Craft will graduate into it): the blueprints use A2UI's format and validator, and the conformance suite follows A2UI's case format. Every case has an
actionandfeaturestags; feature IDs (F-CRAFT-…) are registered incraft/conformance/features.yaml; suites are validated againstcraft/conformance/conformance_schema.json; and A2UI's owncheck_feature_coverage.pyandcheck_executed_cases.pyrun in CI.craft/README.mdmaps every file to its place in the main repo. This PR adds that shared setup; each later PR adds only its own actions, feature IDs and cases.An independent review checked the blueprint against the tests and the code. It mutated expectations and the parser to confirm the tests catch regressions, re-derived expected outputs by hand, and checked licenses and claims.
Next: names and libraries (qualified names, imports,
export).Pre-launch Checklist
One time:
For this PR:
python3 ./scripts/fix_licenses.py --check)../scripts/fix_format.sh --check).