-
-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathtemplate-missing-invokable.js
More file actions
162 lines (149 loc) · 4.95 KB
/
template-missing-invokable.js
File metadata and controls
162 lines (149 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
'use strict';
const path = require('node:path');
const fs = require('node:fs');
// Packages that ship with Ember/Glimmer are always available to auto-fix.
function isBuiltinPackage(moduleName) {
return moduleName.startsWith('@ember/') || moduleName.startsWith('@glimmer/');
}
// Returns the root package name from a module specifier, e.g.
// 'ember-truth-helpers' -> 'ember-truth-helpers'
// 'ember-truth-helpers/helpers' -> 'ember-truth-helpers'
// '@scope/pkg/deep' -> '@scope/pkg'
function rootPackageName(moduleName) {
if (moduleName.startsWith('@')) {
const parts = moduleName.split('/');
return parts.slice(0, 2).join('/');
}
return moduleName.split('/')[0];
}
// Walk up the directory tree from startDir to find the nearest package.json.
function findNearestPackageJson(startDir) {
let dir = startDir;
let parent = path.dirname(dir);
while (dir !== parent) {
const candidate = path.join(dir, 'package.json');
if (fs.existsSync(candidate)) {
return candidate;
}
dir = parent;
parent = path.dirname(dir);
}
return null;
}
// Cache: `${pkg}\0${fileDir}` -> boolean. Survives the lifetime of the process
// (one ESLint run), avoiding repeated FS reads for the same file directory.
const packageInDepsCache = new Map();
function isPackageInProjectDeps(moduleName, fileDir) {
const pkg = rootPackageName(moduleName);
const cacheKey = `${pkg}::${fileDir}`;
if (packageInDepsCache.has(cacheKey)) {
return packageInDepsCache.get(cacheKey);
}
let result = false;
try {
const pkgPath = findNearestPackageJson(fileDir);
if (pkgPath) {
const packageJson = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
result = Boolean(
(packageJson.dependencies && pkg in packageJson.dependencies) ||
(packageJson.devDependencies && pkg in packageJson.devDependencies) ||
(packageJson.peerDependencies && pkg in packageJson.peerDependencies)
);
}
} catch {
result = false;
}
packageInDepsCache.set(cacheKey, result);
return result;
}
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'problem',
docs: {
description:
'disallow missing helpers, modifiers, or components in \\<template\\> with auto-fix to import them',
category: 'Ember Octane',
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-missing-invokable.md',
},
fixable: 'code',
schema: [
{
type: 'object',
properties: {
invokables: {
type: 'object',
additionalProperties: {
type: 'array',
prefixItems: [
{ type: 'string', description: 'The name to import from the module' },
{ type: 'string', description: 'The module to import from' },
],
},
},
},
},
],
messages: {
'missing-invokable':
'Not in scope. Did you forget to import this? Auto-fix may be configured.',
},
},
create: (context) => {
const sourceCode = context.sourceCode;
const fileDir = path.dirname(
path.resolve(context.getPhysicalFilename?.() ?? context.getFilename())
);
// takes a node with a `.path` property
function checkInvokable(node) {
if (node.path.type === 'GlimmerPathExpression' && node.path.tail.length === 0) {
if (!isBound(node.path.head, sourceCode.getScope(node.path))) {
const matched = context.options[0]?.invokables?.[node.path.head.name];
if (matched) {
const [name, moduleName] = matched;
const canAutoFix =
isBuiltinPackage(moduleName) ||
isPackageInProjectDeps(moduleName, fileDir);
const importStatement = buildImportStatement(node.path.head.name, name, moduleName);
context.report({
node: node.path,
messageId: 'missing-invokable',
fix: canAutoFix
? function (fixer) {
return fixer.insertTextBeforeRange([0, 0], `${importStatement};\n`);
}
: null,
});
}
}
}
}
return {
GlimmerSubExpression(node) {
return checkInvokable(node);
},
GlimmerElementModifierStatement(node) {
return checkInvokable(node);
},
GlimmerMustacheStatement(node) {
return checkInvokable(node);
},
};
},
};
function isBound(node, scope) {
const ref = scope.references.find((v) => v.identifier === node);
if (!ref) {
return false;
}
return Boolean(ref.resolved);
}
function buildImportStatement(consumedName, exportedName, module) {
if (exportedName === 'default') {
return `import ${consumedName} from '${module}'`;
} else {
return consumedName === exportedName
? `import { ${consumedName} } from '${module}'`
: `import { ${exportedName} as ${consumedName} } from '${module}'`;
}
}