Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
58 changes: 50 additions & 8 deletions lib/rules/template-no-at-ember-render-modifiers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
// Map of `@ember/render-modifiers` sub-path → canonical kebab-case modifier name.
// Default-imported names are user-chosen, so we track local names to canonical.
const RENDER_MODIFIER_IMPORT_PATHS = {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and also: @ember/render-modifiers -- all 3 can be imported from there

'@ember/render-modifiers/modifiers/did-insert': 'did-insert',
'@ember/render-modifiers/modifiers/did-update': 'did-update',
'@ember/render-modifiers/modifiers/will-destroy': 'will-destroy',
};

const KEBAB_NAMES = new Set(['did-insert', 'did-update', 'will-destroy']);

/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
Expand All @@ -23,24 +33,56 @@ module.exports = {
},

create(context) {
const filename = context.filename;
const isStrictMode = filename.endsWith('.gjs') || filename.endsWith('.gts');

// local import name → canonical kebab name. Only populated in GJS/GTS.
const importedModifiers = new Map();

function isRenderModifier(name) {
if (isStrictMode) {
return importedModifiers.has(name);
}
// HBS: resolver-resolved by canonical kebab name
return KEBAB_NAMES.has(name);
}

function canonicalName(name) {
return importedModifiers.get(name) || name;
}

return {
ImportDeclaration(node) {
if (!isStrictMode) {
return;
}
const canonical = RENDER_MODIFIER_IMPORT_PATHS[node.source.value];
if (!canonical) {
return;
}
// Default import (`import didInsert from '...'`) is the supported form
for (const specifier of node.specifiers) {
if (specifier.type === 'ImportDefaultSpecifier') {
importedModifiers.set(specifier.local.name, canonical);
}
}
},

GlimmerElementNode(node) {
if (!node.modifiers) {
return;
}

for (const modifier of node.modifiers) {
if (
modifier.path &&
modifier.path.type === 'GlimmerPathExpression' &&
(modifier.path.original === 'did-insert' ||
modifier.path.original === 'did-update' ||
modifier.path.original === 'will-destroy')
) {
if (modifier.path?.type !== 'GlimmerPathExpression') {
continue;
}
const name = modifier.path.original;
if (isRenderModifier(name)) {
context.report({
node: modifier,
messageId: 'noRenderModifier',
data: { modifier: modifier.path.original },
data: { modifier: canonicalName(name) },
});
}
}
Expand Down
44 changes: 44 additions & 0 deletions tests/lib/rules/template-no-at-ember-render-modifiers.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ ruleTester.run('template-no-at-ember-render-modifiers', rule, {
'<template>{{did-insert}}</template>',
'<template>{{did-update}}</template>',
'<template>{{will-destroy}}</template>',

// In GJS/GTS, kebab identifiers cannot be imports; these are bare paths
// that happen to share the canonical name but are not the render modifier.
{
filename: 'test.gjs',
code: '<template><div {{did-insert this.setup}}></div></template>',
},
// Unrelated imports with matching local names should not match
{
filename: 'test.gjs',
code: `import didInsert from './my-lib';
<template><div {{didInsert this.setup}}></div></template>`,
},
],

invalid: [
Expand Down Expand Up @@ -87,6 +100,37 @@ ruleTester.run('template-no-at-ember-render-modifiers', rule, {
output: null,
errors: [{ messageId: 'noRenderModifier' }],
},

// GJS/GTS import-based forms — local name is user-chosen
{
filename: 'test.gjs',
code: `import didInsert from '@ember/render-modifiers/modifiers/did-insert';
<template><div {{didInsert this.setup}}></div></template>`,
output: null,
errors: [{ messageId: 'noRenderModifier' }],
},
{
filename: 'test.gjs',
code: `import didUpdate from '@ember/render-modifiers/modifiers/did-update';
<template><div {{didUpdate this.update}}></div></template>`,
output: null,
errors: [{ messageId: 'noRenderModifier' }],
},
{
filename: 'test.gjs',
code: `import willDestroy from '@ember/render-modifiers/modifiers/will-destroy';
<template><div {{willDestroy this.cleanup}}></div></template>`,
output: null,
errors: [{ messageId: 'noRenderModifier' }],
},
// Renamed default import still flags
{
filename: 'test.gjs',
code: `import myInsert from '@ember/render-modifiers/modifiers/did-insert';
<template><div {{myInsert this.setup}}></div></template>`,
output: null,
errors: [{ messageId: 'noRenderModifier' }],
},
],
});

Expand Down
Loading