Skip to content

Commit 5a6438a

Browse files
committed
real configuration
1 parent 2ef166a commit 5a6438a

2 files changed

Lines changed: 59 additions & 14 deletions

File tree

lib/rules/template-missing-invokable.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
11
/** @type {import('eslint').Rule.RuleModule} */
22
module.exports = {
33
meta: {
4-
type: 'suggestion',
4+
type: 'problem',
55
docs: {
6-
description: 'disallow referencing let variables in \\<template\\>',
6+
description:
7+
'disallow missing helpers, modifiers, or components in \\<template\\> with auto-fix to import them',
78
category: 'Ember Octane',
8-
recommendedGjs: false,
9-
recommendedGts: false,
109
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-missing-invokable.md',
1110
},
1211
fixable: 'code',
13-
schema: [],
12+
schema: [
13+
{
14+
type: 'object',
15+
properties: {
16+
invokables: {
17+
type: 'object',
18+
additionalProperties: {
19+
type: 'array',
20+
prefixItems: [
21+
{ type: 'string', description: 'The name to import from the module' },
22+
{ type: 'string', description: 'The module to import from' },
23+
],
24+
},
25+
},
26+
},
27+
},
28+
],
1429
messages: {
1530
'missing-invokable':
1631
'Not in scope. Did you forget to import this? Auto-fix may be configured.',
@@ -20,25 +35,20 @@ module.exports = {
2035
create: (context) => {
2136
const sourceCode = context.sourceCode;
2237

23-
// TODO make real config
24-
const config = {
25-
eq: { name: 'eq', module: 'ember-truth-helpers' },
26-
on: { name: 'on', module: '@ember/modifier' },
27-
};
28-
2938
// takes a node with a `.path` property
3039
function checkInvokable(node) {
3140
if (node.path.type === 'GlimmerPathExpression' && node.path.tail.length === 0) {
3241
if (!isBound(node.path.head, sourceCode.getScope(node.path))) {
33-
const matched = config[node.path.head.name];
42+
const matched = context.options[0]?.invokables?.[node.path.head.name];
3443
if (matched) {
44+
const [name, module] = matched;
3545
context.report({
3646
node: node.path,
3747
messageId: 'missing-invokable',
3848
fix(fixer) {
3949
return fixer.insertTextBeforeRange(
4050
[0, 0],
41-
`import { ${matched.name} } from '${matched.module}';\n`
51+
`import { ${name} } from '${module}';\n`
4252
);
4353
},
4454
});
@@ -64,7 +74,6 @@ module.exports = {
6474
function isBound(node, scope) {
6575
const ref = scope.references.find((v) => v.identifier === node);
6676
if (!ref) {
67-
// TODO: can we make a test case for this?
6877
return false;
6978
}
7079
return Boolean(ref.resolved);

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ ruleTester.run('template-missing-invokable', rule, {
8585
{{/if}}
8686
</template>
8787
`,
88+
options: [
89+
{
90+
invokables: {
91+
eq: ['eq', 'ember-truth-helpers'],
92+
},
93+
},
94+
],
95+
8896
errors: [{ type: 'GlimmerPathExpression', message: rule.meta.messages['missing-invokable'] }],
8997
},
9098

@@ -101,6 +109,13 @@ ruleTester.run('template-missing-invokable', rule, {
101109
{{eq 1 1}}
102110
</template>
103111
`,
112+
options: [
113+
{
114+
invokables: {
115+
eq: ['eq', 'ember-truth-helpers'],
116+
},
117+
},
118+
],
104119
errors: [{ type: 'GlimmerPathExpression', message: rule.meta.messages['missing-invokable'] }],
105120
},
106121
{
@@ -117,6 +132,13 @@ ruleTester.run('template-missing-invokable', rule, {
117132
<MyComponent @flag={{eq 1 1}} />
118133
</template>
119134
`,
135+
options: [
136+
{
137+
invokables: {
138+
eq: ['eq', 'ember-truth-helpers'],
139+
},
140+
},
141+
],
120142
errors: [{ type: 'GlimmerPathExpression', message: rule.meta.messages['missing-invokable'] }],
121143
},
122144

@@ -135,6 +157,13 @@ ruleTester.run('template-missing-invokable', rule, {
135157
<button {{on "click" doSomething}}>Go</button>
136158
</template>
137159
`,
160+
options: [
161+
{
162+
invokables: {
163+
on: ['on', '@ember/modifier'],
164+
},
165+
},
166+
],
138167
errors: [{ type: 'GlimmerPathExpression', message: rule.meta.messages['missing-invokable'] }],
139168
},
140169
// Multiple copies of a fixable invocation
@@ -172,6 +201,13 @@ ruleTester.run('template-missing-invokable', rule, {
172201
{{/if}}
173202
</template>
174203
`,
204+
options: [
205+
{
206+
invokables: {
207+
eq: ['eq', 'ember-truth-helpers'],
208+
},
209+
},
210+
],
175211
errors: [
176212
{ type: 'GlimmerPathExpression', message: rule.meta.messages['missing-invokable'] },
177213
{ type: 'GlimmerPathExpression', message: rule.meta.messages['missing-invokable'] },

0 commit comments

Comments
 (0)