Skip to content

feat: add the templateContext feature - #1883

Merged
mshima merged 4 commits into
yeoman:mainfrom
mshima:disable-template-context
Oct 18, 2026
Merged

mshima merged 4 commits into
yeoman:mainfrom
mshima:disable-template-context

Conversation

@mshima

@mshima mshima commented Oct 18, 2026 •

Copy link
Copy Markdown
Member

Summary

renderTemplate and renderTemplateAsync render the templates with the generator as their context (transformOptions.context: this), so a template can read anything from the generator. EJS keeps the context with the compiled template, so with the EJS cache a template rendered by several generators has an unpredictable context.

Add the templateContext feature, for renderTemplate, renderTemplateAsync, renderTemplates and renderTemplatesAsync:

templateContext context of the templates
'generator' (default) the generator, without a warning, as before
'warn' the generator, warning once by template and property read from this, through the generator log
'throw' a context where reading a property of this throws, naming the template (Template <template path> reads this.<property>: the generator is not the context of its templates, pass it in the template data). The default of the next major.
'none' no context is passed: EJS gives the template its default this
  • Reading a symbol gives undefined with 'throw', and does not warn with 'warn'.
  • With 'warn', a method read from this keeps the generator as this (its private fields), and getters run on the generator.
  • A context passed in transformOptions is still used.
  • The default template data (_templateData(), from the config) is unchanged.
  • Templates rendered with this.fs.copyTpl directly are not affected.

Behavior change

None by default: 'generator' keeps the generator as the context, without a warning.

Test plan

  • eslint, prettier, build
  • vitest, rendering real templates: the default and 'generator' without a warning, 'warn' warning once by template and property, a method of the generator called from a template, 'none' without a context, the data with 'throw' (renderTemplate, renderTemplateAsync, renderTemplates), the error naming the template and the property (sync and async), an explicit context

🤖 Generated with Claude Code

mshima and others added 3 commits October 17, 2026 21:06
With disableTemplateContext, renderTemplate, renderTemplateAsync,
renderTemplates and renderTemplatesAsync render the templates without the
generator as their context: a template reads only its data, and reading a
property of `this` throws, naming the template. A context passed in
transformOptions is still used.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
disableTemplateContext has three states:
- true: no context, reading a property of `this` throws; the default of the
  next major.
- not set (deprecated): the generator, warning once by template and property
  read from `this`.
- false (deprecated): the generator, without a warning.

EJS keeps the context with the compiled template, so with its cache a template
rendered by several generators has an unpredictable context.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
templateContext gives the context of the templates of renderTemplate*:
- 'warn' (default): the generator, warning once by template and property
  read from `this`.
- 'throw': reading a property of `this` throws; the default of the next major.
- 'none': no context is passed.
- 'generator' (deprecated): the generator, without a warning.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mshima mshima changed the title feat: add the disableTemplateContext feature feat: add the templateContext feature Oct 18, 2026
templateContext defaults to 'generator', without a warning; 'warn' warns
on the use of the generator as the context when set.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mshima
mshima merged commit 30e6b8a into yeoman:main Oct 18, 2026
16 checks passed
@mshima
mshima deleted the disable-template-context branch October 18, 2026 21:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant