All files / lib/core/tooling eslintComponentTagNaming.js

87.85% Statements 94/107
66.66% Branches 4/6
100% Functions 2/2
87.85% Lines 94/107

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 1075x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x                           7x 7x 7x 7x 7x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 7x 7x 5x 5x
import {
  findInvalidComponentTags,
  findProjectRoot,
  findRegisteredComponents,
  resolveComponentsDir,
} from './componentTagNaming.js';
 
export const componentTagNamingRule = {
  meta: {
    type: 'problem',
 
    docs: {
      description:
        'Require registered Avenx component tags to use PascalCase.',
    },
 
    schema: [
      {
        type: 'object',
        properties: {
          componentsDir: {
            type: 'string',
          },
 
          componentNames: {
            type: 'array',
            items: {
              type: 'string',
            },
          },
        },
        additionalProperties: false,
      },
    ],
 
    messages: {
      invalidName:
        'Avenx component <{{tagName}}> must use PascalCase: <{{expectedName}}>.',
    },
  },
 
  create(context) {
    const physicalFilename = context.physicalFilename;
    const options = context.options[0] || {};
    const sourceCode = context.sourceCode;
 
    return {
      Program() {
        const source = sourceCode.getText();
 
        let registeredComponents;
 
        if (Array.isArray(options.componentNames)) {
          registeredComponents = new Set(options.componentNames);
        } else {
          const projectRoot = findProjectRoot(
            physicalFilename,
            context.cwd,
          );

          registeredComponents = findRegisteredComponents(
            projectRoot,
            resolveComponentsDir(
              projectRoot,
              options.componentsDir,
            ),
          );
        }
 
        for (const issue of findInvalidComponentTags(
          source,
          registeredComponents,
        )) {
          const lineStart =
            source.lastIndexOf('\n', issue.index - 1) + 1;
 
          const line =
            source.slice(0, issue.index)
              .split(/\r\n|\r|\n/)
              .length;
 
          const column = issue.index - lineStart;
 
          context.report({
            loc: {
              start: {
                line,
                column,
              },
              end: {
                line,
                column: column + issue.tagName.length,
              },
            },
 
            messageId: 'invalidName',
 
            data: {
              tagName: issue.tagName,
              expectedName: issue.expectedName,
            },
          });
        }
      },
    };
  },
};