Fix: Type checker doesn't detect usage of optional properties #11 #238
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.,
[email protected]):cto
namespace [email protected]
@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": "[email protected]",
"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