Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 3x 3x 8x 8x 8x 8x 8x 8x 7x 7x 7x 7x 7x 8x 8x 8x 8x 8x 8x 8x 12x 12x 12x 12x 12x 12x 12x 12x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 2x 2x 5x 5x 5x 5x 12x 12x 12x 16x 16x 16x 7x 16x 9x 9x 16x 5x 5x 5x 5x 5x 5x 7x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 4x 4x 4x 8x 8x 8x 8x 8x 8x 8x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 144x 144x 24x 24x 24x 8x 8x 8x 8x 8x 8x 8x 8x 15x 15x 15x 15x 15x 15x 15x 27x 27x 27x 4x 4x 23x 23x 23x 23x 23x 23x 23x 23x 27x 8x 8x 8x 8x 8x 8x 27x 15x 15x 15x 8x 8x 8x 8x 8x 8x 8x 8x | import fs from 'fs';
import path from 'path';
// How long a cached registry is trusted before the next call rescans it.
// The components directory's own mtime catches components added or removed
// directly inside it right away, but not a file appearing in a nested folder
// (src/components/<name>/<name>.component.js), so this bounds that staleness
// without registering a watcher in someone else's lint process.
const DEFAULT_REGISTRY_MAX_AGE_MS = 2000;
/** @type {Map<string, { names: Set<string>, mtimeMs: number, scannedAt: number }>} */
const registryCache = new Map();
/**
* Clears the cached component registries. Mostly useful in tests, since the
* cache is module state shared across the whole process.
*/
export function clearRegistryCache() {
registryCache.clear();
}
/**
* Returns the mtime of a directory, or -1 when it does not exist.
* @param {string} directory
* @returns {number}
*/
function directoryMtimeMs(directory) {
try {
return fs.statSync(directory).mtimeMs;
} catch {
return -1;
}
}
/**
* Converts an Avenx component filename into its canonical PascalCase name.
* @param {string} fileName
* @returns {string}
*/
export function componentNameFromFile(fileName) {
const baseName = String(fileName)
.replace(/^.*[/\\]/, '')
.replace(/\.component\.js$/i, '');
return baseName
.split(/[-_]/)
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
.join('');
}
/**
* Finds components registered by the Avenx compiler.
* Avenx registers components by scanning src/components for
* .component.js files and normalizing their filenames.
*
* Results are cached per directory. The cache is rescanned when the
* components directory's mtime changes (a component added or removed directly
* inside it) or when the entry is older than `maxAgeMs`. A component added in
* a nested folder that already existed only changes that folder's mtime, so it
* shows up once the entry expires, at most `maxAgeMs` later.
* @param {string} projectRoot
* @param {string} [componentsDir]
* @param {{ maxAgeMs?: number }} [options]
* @returns {Set<string>}
*/
export function findRegisteredComponents(
projectRoot,
componentsDir = 'src/components',
{ maxAgeMs = DEFAULT_REGISTRY_MAX_AGE_MS } = {},
) {
const root = path.resolve(projectRoot);
const directory = path.resolve(root, componentsDir);
const cacheKey = directory;
const mtimeMs = directoryMtimeMs(directory);
const now = Date.now();
const cached = registryCache.get(cacheKey);
if (cached && cached.mtimeMs === mtimeMs && now - cached.scannedAt < maxAgeMs) {
return new Set(cached.names);
}
const names = new Set();
const visit = (currentDir) => {
if (!fs.existsSync(currentDir) || !fs.statSync(currentDir).isDirectory()) {
return;
}
for (const entry of fs.readdirSync(currentDir, { withFileTypes: true })) {
const fullPath = path.join(currentDir, entry.name);
if (entry.isDirectory()) {
visit(fullPath);
} else if (entry.isFile() && entry.name.endsWith('.component.js')) {
names.add(componentNameFromFile(entry.name));
}
}
};
visit(directory);
registryCache.set(cacheKey, { names: new Set(names), mtimeMs, scannedAt: now });
return new Set(names);
}
/**
* Resolves the configured Avenx components directory.
* @param {string} projectRoot
* @param {string} [componentsDir]
* @returns {string}
*/
export function resolveComponentsDir(projectRoot, componentsDir) {
if (componentsDir) {
return componentsDir;
}
const configPath = path.join(path.resolve(projectRoot), 'avenx.config.json');
if (!fs.existsSync(configPath)) {
return 'src/components';
}
try {
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
const srcDir =
typeof config.srcDir === 'string' && config.srcDir.trim() !== ''
? config.srcDir.trim()
: 'src';
return path.join(srcDir, 'components');
} catch {
return 'src/components';
}
}
/**
* Masks text while preserving line positions.
* @param {string} value
* @returns {string}
*/
function mask(value) {
return value.replace(/[^\r\n]/g, ' ');
}
/**
* Removes Avenx metadata blocks that are not part of the template.
* @param {string} source
* @returns {string}
*/
export function extractLintableTemplate(source) {
let template = source;
const patterns = [
/<!--[\s\S]*?-->/g,
/<state\b[\s\S]*?\/>/gi,
/<computed\b[\s\S]*?\/>/gi,
/<action\b[\s\S]*?<\/action>/gi,
/<resource\b[\s\S]*?<\/resource>/gi,
/<resource\b[\s\S]*?\/>/gi,
];
for (const pattern of patterns) {
template = template.replace(pattern, mask);
}
return template;
}
/**
* Finds registered component tags that are not written in PascalCase.
* @param {string} source
* @param {Set<string>} registeredComponents
* @returns {Array<{tagName: string, expectedName: string, index: number}>}
*/
export function findInvalidComponentTags(source, registeredComponents) {
const template = extractLintableTemplate(source);
const invalidTags = [];
const tagRegex = /<([A-Za-z][A-Za-z0-9:_-]*)\b/g;
let match;
while ((match = tagRegex.exec(template)) !== null) {
const tagName = match[1];
if (registeredComponents.has(tagName)) {
continue;
}
const comparableTag = tagName.replace(/[-_]/g, '').toLowerCase();
const normalized = [...registeredComponents].find(
(componentName) =>
componentName.replace(/[-_]/g, '').toLowerCase() === comparableTag,
);
if (normalized && normalized !== tagName) {
invalidTags.push({
tagName,
expectedName: normalized,
index: match.index + 1,
});
}
}
return invalidTags;
}
/**
* Finds the nearest package root.
* @param {string} filePath
* @param {string} fallbackRoot
* @returns {string}
*/
export function findProjectRoot(filePath, fallbackRoot) {
let currentDir = path.dirname(path.resolve(filePath));
const fallback = path.resolve(fallbackRoot);
while (true) {
if (fs.existsSync(path.join(currentDir, 'package.json'))) {
return currentDir;
}
const parent = path.dirname(currentDir);
const relativeToFallback = path.relative(fallback, parent);
if (
parent === currentDir ||
relativeToFallback.startsWith('..') ||
path.isAbsolute(relativeToFallback)
) {
break;
}
currentDir = parent;
}
return fallback;
} |