Skip to content

The open alternative to Salesforce, designed for AI.

2.0 KB
Raw Blame
import { defineRule } from '@oxlint/plugins';

export const RULE_NAME = 'no-runtime-import-from-upgrade-command';

const UPGRADE_COMMAND_IMPORT_PREFIXES = [
  'src/database/commands/upgrade-version-command/',
  'src/database/commands/agent-history/',
];

const ALLOWED_IMPORT_REGEX = /-upgrade-command-name\.constants?$/;

const EXEMPT_FILE_REGEXES = [
  /\/src\/database\/commands\//,
  /\/src\/engine\/core-modules\/upgrade\//,
  /\/test\//,
  /\/__tests__\//,
  /\.spec\.ts$/,
  /\.integration-spec\.ts$/,
];

const isExemptFile = (filename: string): boolean =>
  EXEMPT_FILE_REGEXES.some((regex) => regex.test(filename));

const isForbiddenImportSource = (source: string): boolean =>
  UPGRADE_COMMAND_IMPORT_PREFIXES.some((prefix) => source.startsWith(prefix)) &&
  !ALLOWED_IMPORT_REGEX.test(source);

export const rule = defineRule({
  meta: {
    type: 'problem',
    docs: {
      description:
        'Forbid runtime code from importing upgrade command or agent-history migration code, which is frozen once released',
    },
    schema: [],
    messages: {
      noRuntimeImportFromUpgradeCommand:
        "Runtime code must not import '{{ source }}'. Keep migration-only logic, constants and legacy formats with the migration code, and move shared primitives out of it instead.",
    },
  },
  create: (context) => {
    if (isExemptFile(context.filename)) {
      return {};
    }

    const checkSource = (sourceNode: any) => {
      const source = sourceNode?.value;

      if (typeof source !== 'string' || !isForbiddenImportSource(source)) {
        return;
      }

      context.report({
        node: sourceNode,
        messageId: 'noRuntimeImportFromUpgradeCommand',
        data: { source },
      });
    };

    return {
      ImportDeclaration: (node: any) => checkSource(node.source),
      ExportNamedDeclaration: (node: any) => checkSource(node.source),
      ExportAllDeclaration: (node: any) => checkSource(node.source),
      ImportExpression: (node: any) => checkSource(node.source),
    };
  },
});