Skip to content

Commit

Permalink
Revert making Template.locals readonly
Browse files Browse the repository at this point in the history
It turns out to be important that AST plugins can mutate the list
of local variables in the root `Template` node. Instead, use *that*
list of local variables as the ultimate source of truth after AST
plugin ran.
  • Loading branch information
chancancode committed Mar 5, 2024
1 parent 68509ac commit 31cc612
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -762,10 +762,6 @@ export function preprocess(
options.locals ?? []
);

if (options.strictMode && options.locals?.length) {
template = b.template({ ...template, locals: options.locals });
}

if (options?.plugins?.ast) {
for (const transform of options.plugins.ast) {
let env: ASTPluginEnvironment = assign({}, options, { syntax }, { plugins: undefined });
Expand Down
15 changes: 8 additions & 7 deletions packages/@glimmer/syntax/lib/v1/legacy-interop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,24 @@ export function buildLegacyTemplate({ body, locals, loc }: TemplateParams): ASTv
const node = {
type: 'Template',
body,
locals,
loc,
};

Object.defineProperty(node, 'locals', {
enumerable: true,
writable: false,
value: Object.freeze([...locals]),
});

Object.defineProperty(node, 'blockParams', {
enumerable: false,
get(): readonly string[] {
get(): string[] {
deprecate(
`Template nodes can never have block params, for in-scope variables, use locals instead`
);
return this.locals;
},
set(value: string[]) {
deprecate(
`Template nodes can never have block params, for in-scope variables, use locals instead`
);
this.locals = value;
},
});

return node as ASTv1.Template;
Expand Down
4 changes: 2 additions & 2 deletions packages/@glimmer/syntax/lib/v1/nodes-v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ export type EntityEncodingState = 'transformed' | 'raw';

export interface Template extends CommonProgram {
type: 'Template';
readonly locals: readonly string[];
locals: string[];

/**
* @deprecated use locals instead
*/
readonly blockParams: readonly string[];
blockParams: string[];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/@glimmer/syntax/lib/v2/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export function normalize(

let normalizeOptions = {
strictMode: false,
locals: [],
...options,
locals: ast.locals,
};

let top = SymbolTable.top(
Expand Down

0 comments on commit 31cc612

Please sign in to comment.