Skip to content

Commit b6bbc3e

Browse files
committed
default export support
1 parent 5a6438a commit b6bbc3e

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

lib/rules/template-missing-invokable.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,12 @@ module.exports = {
4242
const matched = context.options[0]?.invokables?.[node.path.head.name];
4343
if (matched) {
4444
const [name, module] = matched;
45+
const importStatement = buildImportStatement(node.path.head.name, name, module);
4546
context.report({
4647
node: node.path,
4748
messageId: 'missing-invokable',
4849
fix(fixer) {
49-
return fixer.insertTextBeforeRange(
50-
[0, 0],
51-
`import { ${name} } from '${module}';\n`
52-
);
50+
return fixer.insertTextBeforeRange([0, 0], `${importStatement};\n`);
5351
},
5452
});
5553
}
@@ -78,3 +76,13 @@ function isBound(node, scope) {
7876
}
7977
return Boolean(ref.resolved);
8078
}
79+
80+
function buildImportStatement(consumedName, exportedName, module) {
81+
if (exportedName === 'default') {
82+
return `import ${consumedName} from '${module}'`;
83+
} else {
84+
return consumedName === exportedName
85+
? `import { ${consumedName} } from '${module}'`
86+
: `import { ${exportedName} as ${consumedName} } from '${module}'`;
87+
}
88+
}

tests/lib/rules/template-missing-invokable.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ ruleTester.run('template-missing-invokable', rule, {
166166
],
167167
errors: [{ type: 'GlimmerPathExpression', message: rule.meta.messages['missing-invokable'] }],
168168
},
169+
169170
// Multiple copies of a fixable invocation
170171
{
171172
code: `
@@ -214,5 +215,33 @@ ruleTester.run('template-missing-invokable', rule, {
214215
{ type: 'GlimmerPathExpression', message: rule.meta.messages['missing-invokable'] },
215216
],
216217
},
218+
219+
// Auto-fix with a default export
220+
{
221+
code: `
222+
<template>
223+
{{#if (eq 1 1)}}
224+
They're equal
225+
{{/if}}
226+
</template>
227+
`,
228+
output: `import eq from 'ember-truth-helpers/helpers/equal';
229+
230+
<template>
231+
{{#if (eq 1 1)}}
232+
They're equal
233+
{{/if}}
234+
</template>
235+
`,
236+
options: [
237+
{
238+
invokables: {
239+
eq: ['default', 'ember-truth-helpers/helpers/equal'],
240+
},
241+
},
242+
],
243+
244+
errors: [{ type: 'GlimmerPathExpression', message: rule.meta.messages['missing-invokable'] }],
245+
},
217246
],
218247
});

0 commit comments

Comments
 (0)