Repository navigation
Give each template a function scope - #261
Merged
NullVoxPopuli merged 1 commit intoOct 9, 2026
Merged
Conversation
ESLint's no-use-before-define reports a component that renders itself:
const Foo = <template><Foo /></template>;
class Bar extends Component { <template><Bar /></template> }
It does so even with `{ functions: false, classes: false, variables:
false }` (eslint-config-standard), because it only exempts references from
a separate execution context, and a template's block scope shares its
upper scope's. A template compiles to a closure (`scope: () => ({ Foo })`),
so give it a function scope instead.
On the TypeScript path, FunctionScope declares an implicit `arguments` and
scans the body for a "use strict" directive, which throws on a template's
body when the upper scope isn't strict (a script). A TemplateScope
subclass removes `arguments` and passes `isMethodDefinition` to skip the
scan; the eslint-scope path uses the base Scope, which declares nothing.
NullVoxPopuli
approved these changes
Oct 9, 2026
Contributor
🏎️ Benchmark ComparisonParse
Full mitata output
Full mitata output |
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Follow-up to #256. ESLint's own
no-use-before-definereports a component that renders itself:It does this on every release, for
.gjsand.gts, and even with eslint-config-standard's{ functions: false, classes: false, variables: false }. The only way out is turning the rule off. The rule exempts a reference only when it runs in a separate execution context (variable.scope.variableScope !== reference.from.variableScope), and the block scope #256 gives each template shares its upper scope'svariableScope.Fix
A template compiles to a closure (
template(…, { scope: () => ({ Tree }) })), so each template now gets a function scope instead of a block scope. Block params keep their block scopes inside it.TemplateScopesubclass of scope-manager'sFunctionScope.isMethodDefinitionso the scope is strict without the "use strict" directive scan, which throws on a template'sbodywhen the enclosing scope isn't strict (a script, which is also ESLint's default withoutsourceType).argumentsthatFunctionScopedeclares, so{{arguments}}stays undefined.Scopewith typefunction, which declares nothing, with the same strictness argument.Behaviour changes
no-use-before-defineno longer reports a template's reference to its own component (const, class, or static class field), with default or standard options.variables: false, both core and typescript-eslint'sno-use-before-definenow exempt a template reading a variable declared later, as they do for an arrow function. With default options it's still reported.no-varkeeps reporting a loopvarthat a template reads, but no longer autofixes it, as for a closure.require-atomic-updatestreats a variable a template reads as escaping its function, as for a closure.Tests
template scopeintests/parser.test.js, each for.gjs,.gtsand.gjswithuseBabel:no-use-before-defineover const, class and static-field self-references, with default and standard options;{{arguments}}is still reported byno-undef;functiontype, its ownvariableScope, strict, no variables, block-param scopes inside it.test-projects/gts/src/use-before-define.gts(from Stop typescript-eslint's no-use-before-define reporting references in a template assigned to a const #256) now also runs coreno-use-before-define, which Stop typescript-eslint's no-use-before-define reporting references in a template assigned to a const #256 had turned off for it, and gains a class component that renders itself. Onmaincore reports bothTreeandBranch.Also run: the full suite, lint, the test projects, eslint-plugin-ember's suite (
scripts/eslint-plugin-ember-test.mjs), the CI matrix's^6/^7overrides, and a lint comparison against #256's block scope over the repo's 48.gjs/.gtsfiles plus self-reference samples (TS, TS script mode, Babel) with 14 scope-sensitive rules, which differs only in the intendedno-use-before-definereports.