Fix: Type checker doesn't detect usage of optional properties #11 - #238
Fix: Type checker doesn't detect usage of optional properties #11#238Marvinrose wants to merge 1 commit into
Conversation
…roject#11 Signed-off-by: Chibuezem Marvinrose <rozzeymarvin32@gmail.com>
✅ Deploy Preview for ap-template-playground ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
mttrbrts
left a comment
There was a problem hiding this comment.
Thanks for your contribution @Marvinrose .
Unfortunately, this PR doesn't fix the root cause of #11
This fix is likely to need a change in accordproject/template-engine to improve type-checking for all examples of the same issue. Instead your change removes the error by changing the template.
The value of the template-engine is to catch error at compile time, rather than at run-time. This gives us confidence that templates won't fail unexpectedly at runtime.
|
Hi @mttrbrts, Thank you for your review and feedback on my previous attempt. I’ve reworked this PR to directly address the root cause of Issue #11 by enhancing type checking within here accordproject/template-engine#36 kindly review please. |
This PR addresses a runtime error encountered when an optional variable is used in a template (including outside of TypeScript expressions) but is missing from the provided data. This issue can lead to a "No values found" error during template rendering.
The template engine currently throws a runtime error when an optional variable is referenced in a template and is not present in the data. This behaviour is observed even when the variable is not used within a TypeScript expression, but its presence in the template context (especially alongside other TypeScript expressions) seems to trigger the error.
Steps to Reproduce:
Define the following Model (e.g.,
hello@1.0.0.cto):cto
namespace hello@1.0.0
@template
concept HelloWorld {
o String name
o String last optional
}
Define the following Template (e.g., grammar.tem.md):
Markdown
Hello {{name}}!
Today is {{% return now.toISOString() %}}.
{{last}}
Provide Data that is missing the optional last field:
JSON
{
"$class": "hello@1.0.0.HelloWorld",
"name": "John Doe"
}
Attempt to render the template using the TemplateMarkInterpreter.
Actual Behaviour: A runtime error "Error: No values found for path '$['last']' in data [object Object]." occurs.
Solution/Workaround Implemented in Code:
A workaround is to use a TypeScript if statement within the template to conditionally render content based on the presence of the optional variable in the data.
Example:
Markdown
Hello {{name}}!
Today is {{% return now.toISOString() %}}.
{{% if (data.last) { return data.last; } else { return ''; } %}}
Possible Fix:
To address this issue within the template engine itself, the TemplateMarkInterpreter should be modified to check for the existence of optional variables in the data before attempting to access their values. This could involve returning a default value (like undefined or an empty string) or simply skipping the rendering of the missing optional variable without throwing a fatal error.
My Environment:
Version used - ESLint Version: v9.21.0
Environment name and version (e.g. Chrome 39, node.js 5.4) - NPM Version: v10.8.2, Node.js Version: v18.20.7
Operating System and version (desktop or mobile) - Operating System: Windows 11 Home Single Language (Build 22631)
Link to your project - Template Playground: https://github.com/accordproject/template-playground