All files / lib/core/tooling componentTagNaming.js

60% Statements 117/195
100% Branches 13/13
57.14% Functions 4/7
60% Lines 117/195

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 1956x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 3x 3x 3x 3x 3x 3x 3x 3x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x                                                               6x 6x 6x 6x 6x 6x 6x 6x                                               6x 6x 6x 6x 6x 6x 4x 4x 4x 6x 6x 6x 6x 6x 6x 6x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 90x 90x 15x 15x 15x 6x 6x 6x 6x 6x 6x 6x 6x 14x 14x 14x 14x 14x 14x 14x 25x 25x 25x 4x 4x 21x 21x 21x 21x 21x 21x 21x 21x 25x 7x 7x 7x 7x 7x 7x 25x 14x 14x 14x 6x 6x 6x 6x 6x 6x 6x 6x                                                
import fs from 'fs';
import path from 'path';
 
const registryCache = new Map();
 
/**
 * 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.
 * @param {string} projectRoot
 * @param {string} [componentsDir]
 * @returns {Set<string>}
 */
export function findRegisteredComponents(projectRoot, componentsDir = 'src/components') {
  const root = path.resolve(projectRoot);
  const directory = path.resolve(root, componentsDir);
  const cacheKey = directory;

  if (registryCache.has(cacheKey)) {
    return new Set(registryCache.get(cacheKey));
  }

  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, new Set(names));
  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;
}