Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes Ember keyword shadowing #20782

Merged
merged 2 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { assert, deprecate } from '@ember/debug';
import type { AST, ASTPlugin } from '@glimmer/syntax';
import calculateLocationDisplay from '../system/calculate-location-display';
import type { EmberASTPluginEnvironment } from '../types';
import { trackLocals } from './utils';

/**
@module ember
Expand All @@ -23,48 +24,16 @@ import type { EmberASTPluginEnvironment } from '../types';
export default function assertAgainstAttrs(env: EmberASTPluginEnvironment): ASTPlugin {
let { builders: b } = env.syntax;
let moduleName = env.meta?.moduleName;

let stack: string[][] = [[]];

function updateBlockParamsStack(blockParams: string[]) {
let parent = stack[stack.length - 1];
assert('has parent', parent);
stack.push(parent.concat(blockParams));
}
let { hasLocal, visitor } = trackLocals(env);

return {
name: 'assert-against-attrs',

visitor: {
Template: {
enter(node: AST.Template) {
updateBlockParamsStack(node.blockParams);
},
exit() {
stack.pop();
},
},

Block: {
enter(node: AST.Block) {
updateBlockParamsStack(node.blockParams);
},
exit() {
stack.pop();
},
},

ElementNode: {
enter(node: AST.ElementNode) {
updateBlockParamsStack(node.blockParams);
},
exit() {
stack.pop();
},
},
...visitor,

PathExpression(node: AST.PathExpression): AST.Node | void {
if (isAttrs(node, stack[stack.length - 1]!)) {
if (isAttrs(node, hasLocal)) {
assert(
`Using {{attrs}} to reference named arguments is not supported. {{${
node.original
Expand Down Expand Up @@ -106,12 +75,8 @@ export default function assertAgainstAttrs(env: EmberASTPluginEnvironment): ASTP
};
}

function isAttrs(node: AST.PathExpression, symbols: string[]) {
return (
node.head.type === 'VarHead' &&
node.head.name === 'attrs' &&
symbols.indexOf(node.head.name) === -1
);
function isAttrs(node: AST.PathExpression, hasLocal: (k: string) => boolean) {
return node.head.type === 'VarHead' && node.head.name === 'attrs' && !hasLocal(node.head.name);
}

function isThisDotAttrs(node: AST.PathExpression) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { assert } from '@ember/debug';
import type { AST, ASTPlugin } from '@glimmer/syntax';
import calculateLocationDisplay from '../system/calculate-location-display';
import type { EmberASTPluginEnvironment } from '../types';
import { trackLocals } from './utils';

/**
@module ember
Expand All @@ -15,16 +16,19 @@ import type { EmberASTPluginEnvironment } from '../types';
*/
export default function assertAgainstNamedOutlets(env: EmberASTPluginEnvironment): ASTPlugin {
let moduleName = env.meta?.moduleName;
let { hasLocal, visitor } = trackLocals(env);

return {
name: 'assert-against-named-outlets',

visitor: {
...visitor,
MustacheStatement(node: AST.MustacheStatement) {
if (
node.path.type === 'PathExpression' &&
node.path.original === 'outlet' &&
node.params[0]
node.params[0] &&
!hasLocal('outlet')
) {
let sourceInformation = calculateLocationDisplay(moduleName, node.loc);
assert(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ import { assert } from '@ember/debug';
import type { AST, ASTPlugin } from '@glimmer/syntax';
import calculateLocationDisplay from '../system/calculate-location-display';
import type { EmberASTPluginEnvironment } from '../types';
import { isPath } from './utils';
import { isPath, trackLocals } from './utils';

export default function errorOnInputWithContent(env: EmberASTPluginEnvironment): ASTPlugin {
let moduleName = env.meta?.moduleName;
let { hasLocal, visitor } = trackLocals(env);

return {
name: 'assert-input-helper-without-block',

visitor: {
...visitor,
BlockStatement(node: AST.BlockStatement) {
if (hasLocal('input')) return;

if (isPath(node.path) && node.path.original === 'input') {
assert(assertMessage(moduleName, node));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AST, ASTPlugin } from '@glimmer/syntax';
import type { Builders, EmberASTPluginEnvironment } from '../types';
import { isPath } from './utils';
import { isPath, trackLocals } from './utils';

/**
@module ember
Expand All @@ -27,36 +27,41 @@ import { isPath } from './utils';
@class TransformActionSyntax
*/

export default function transformActionSyntax({ syntax }: EmberASTPluginEnvironment): ASTPlugin {
let { builders: b } = syntax;
export default function transformActionSyntax(env: EmberASTPluginEnvironment): ASTPlugin {
let { builders: b } = env.syntax;
let { hasLocal, visitor } = trackLocals(env);

return {
name: 'transform-action-syntax',

visitor: {
...visitor,
ElementModifierStatement(node: AST.ElementModifierStatement) {
if (isAction(node)) {
if (isAction(node, hasLocal)) {
insertThisAsFirstParam(node, b);
}
},

MustacheStatement(node: AST.MustacheStatement) {
if (isAction(node)) {
if (isAction(node, hasLocal)) {
insertThisAsFirstParam(node, b);
}
},

SubExpression(node: AST.SubExpression) {
if (isAction(node)) {
if (isAction(node, hasLocal)) {
insertThisAsFirstParam(node, b);
}
},
},
};
}

function isAction(node: AST.ElementModifierStatement | AST.MustacheStatement | AST.SubExpression) {
return isPath(node.path) && node.path.original === 'action';
function isAction(
node: AST.ElementModifierStatement | AST.MustacheStatement | AST.SubExpression,
hasLocal: (k: string) => boolean
) {
return isPath(node.path) && node.path.original === 'action' && !hasLocal('action');
}

function insertThisAsFirstParam(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { AST, ASTPlugin } from '@glimmer/syntax';
import { assert } from '@ember/debug';
import type { EmberASTPluginEnvironment } from '../types';
import { isPath } from './utils';
import { isPath, trackLocals } from './utils';

/**
@module ember
Expand All @@ -25,13 +25,15 @@ import { isPath } from './utils';
*/
export default function transformEachTrackArray(env: EmberASTPluginEnvironment): ASTPlugin {
let { builders: b } = env.syntax;
let { hasLocal, visitor } = trackLocals(env);

return {
name: 'transform-each-track-array',

visitor: {
...visitor,
BlockStatement(node: AST.BlockStatement): AST.Node | void {
if (isPath(node.path) && node.path.original === 'each') {
if (isPath(node.path) && node.path.original === 'each' && !hasLocal('each')) {
let firstParam = node.params[0];
assert('has firstParam', firstParam);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const TARGETS = Object.freeze(['helper', 'modifier']);
export default function transformResolutions(env: EmberASTPluginEnvironment): ASTPlugin {
let { builders: b } = env.syntax;
let moduleName = env.meta?.moduleName;
let { hasLocal, node: tracker } = trackLocals();
let { hasLocal, node: tracker } = trackLocals(env);
let seen: Set<AST.Node> | undefined;

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,13 @@ import { isPath, trackLocals } from './utils';
export default function transformWrapMountAndOutlet(env: EmberASTPluginEnvironment): ASTPlugin {
let { builders: b } = env.syntax;

let { hasLocal, node } = trackLocals();
let { hasLocal, visitor } = trackLocals(env);

return {
name: 'transform-wrap-mount-and-outlet',

visitor: {
Template: node,
ElementNode: node,
Block: node,
...visitor,

MustacheStatement(node: AST.MustacheStatement): AST.Node | void {
if (
Expand Down
14 changes: 12 additions & 2 deletions packages/ember-template-compiler/lib/plugins/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { AST } from '@glimmer/syntax';
import type { EmberASTPluginEnvironment } from '../types';

export function isPath(node: AST.Node): node is AST.PathExpression {
return node.type === 'PathExpression';
Expand All @@ -20,7 +21,11 @@ function getLocalName(node: string | AST.VarHead): string {
}
}

export function trackLocals() {
export function inScope(env: EmberASTPluginEnvironment, name: string): boolean {
return Boolean(env.lexicalScope?.(name));
}

export function trackLocals(env: EmberASTPluginEnvironment) {
let locals = new Map();

let node = {
Expand Down Expand Up @@ -49,7 +54,12 @@ export function trackLocals() {
};

return {
hasLocal: (key: string) => locals.has(key),
hasLocal: (key: string) => locals.has(key) || inScope(env, key),
node,
visitor: {
Template: node,
ElementNode: node,
Block: node,
},
};
}
1 change: 1 addition & 0 deletions packages/ember-template-compiler/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface EmberPrecompileOptions extends PrecompileOptions {
isProduction?: boolean;
moduleName?: string;
plugins?: Plugins;
lexicalScope?: (name: string) => boolean;
}

export type EmberASTPluginEnvironment = ASTPluginEnvironment & EmberPrecompileOptions;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import TransformTestCase from '../utils/transform-test-case';
import { moduleFor, RenderingTestCase } from 'internal-test-helpers';
import { defineComponent, moduleFor, RenderingTestCase } from 'internal-test-helpers';

moduleFor(
'ember-template-compiler: assert against attrs',
Expand Down Expand Up @@ -64,6 +64,14 @@ moduleFor(
this.assertComponentElement(this.firstChild, { content: 'foo' });
}

["@test it doesn't assert lexical scope values"]() {
let component = defineComponent({ attrs: 'just a string' }, `It's {{attrs}}`);
this.registerComponent('root', { ComponentClass: component });
this.render('<Root />');
this.assertHTML("It's just a string");
this.assertStableRerender();
}

["@test it doesn't assert component block params"]() {
this.registerComponent('foo', {
template: '{{yield "foo"}}',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { defineComponent, moduleFor, RenderingTestCase } from 'internal-test-helpers';

moduleFor(
'ember-template-compiler: assert-array-test',
class extends RenderingTestCase {
['@test block param `array` is not transformed']() {
// Intentionally double all of the values to verify that this
// version of the `array` function is used.
function customArray(...values) {
return values.map((value) => value * 2);
}

let Root = defineComponent(
{ customArray },
`{{#let customArray as |array|}}<ul>{{#each (array 1 2 3) as |item|}}<li>{{item}}</li>{{/each}}</ul>{{/let}}`
);
this.registerComponent('root', { ComponentClass: Root });

this.render('<Root />');
this.assertHTML('<ul><li>2</li><li>4</li><li>6</li></ul>');
this.assertStableRerender();
}

['@test lexical scope `array` is not transformed']() {
// Intentionally double all of the values to verify that this
// version of the `array` function is used.
function array(...values) {
return values.map((value) => value * 2);
}

let Root = defineComponent(
{ array },
`<ul>{{#each (array 1 2 3) as |item|}}<li>{{item}}</li>{{/each}}</ul>`
);
this.registerComponent('root', { ComponentClass: Root });

this.render('<Root />');
this.assertHTML('<ul><li>2</li><li>4</li><li>6</li></ul>');
this.assertStableRerender();
}
}
);
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { defineComponent, moduleFor, RenderingTestCase } from 'internal-test-helpers';
import { compile } from '../../index';
import { moduleFor, AbstractTestCase } from 'internal-test-helpers';

moduleFor(
'ember-template-compiler: assert-input-helper-without-block',
class extends AbstractTestCase {
class extends RenderingTestCase {
['@test Using {{#input}}{{/input}} is not valid']() {
let expectedMessage = `The {{input}} helper cannot be used in block form. ('baz/foo-bar' @ L1:C0) `;

Expand All @@ -13,5 +13,30 @@ moduleFor(
});
}, expectedMessage);
}

['@test Block params are not asserted']() {
let shadowInput = defineComponent({}, `It's just {{yield}}`);

let Root = defineComponent(
{ shadowInput },
`{{#let shadowInput as |input|}}{{#input}}an input{{/input}}{{/let}}`
);
this.registerComponent('root', { ComponentClass: Root });

this.render('<Root />');
this.assertHTML("It's just an input");
this.assertStableRerender();
}

['@test Lexical scope values are not asserted']() {
let input = defineComponent({}, `It's just {{yield}}`);

let Root = defineComponent({ input }, `{{#input}}an input{{/input}}`);
this.registerComponent('root', { ComponentClass: Root });

this.render('<Root />');
this.assertHTML("It's just an input");
this.assertStableRerender();
}
}
);