mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-17 05:20:55 +00:00
* feat: Reject Module-Load Use of Other Packages in the Chat Package App specs mock whole packages (@librechat/client, librechat-data-provider), so a package module that calls into one or reads a member of one while it loads throws before any test runs. chat/boundary now reports a call, new, tagged template or member read through an external import at module scope in packages/chat/src outside specs; React and Jotai are exempt through a loadSafe option. * fix: Count Immediately Invoked Functions and Computed Field Keys as Module Load * fix: See Through Erased TypeScript Wrappers in the Module-Load Check * fix: Count Destructuring, Spreads and Wrapped IIFEs as Module-Load Reads * refactor: Judge Every Load-Time Reference to an External Import The check now walks each external import's references once and reports any that runs on load outside an inert position, instead of listing the node types that read an import. Class heritage, in, instanceof and iteration are reported with no new visitor. * fix: Follow Branches to Their Consumer and Skip Erased Heritage Types * fix: Count Constructed Classes and Destructuring Assignments as Module Load * fix: Count Constructors of Constructed Classes and Branch-Fed IIFEs as Module Load * fix: Follow Const Aliases of External Imports in the Module-Load Check * fix: Find Aliases Behind Wrappers and Branches * fix: Track require Bindings, Skip Writes and Defer Generator Bodies in the Module-Load Check * fix: Follow Assignment Aliases of External Imports * fix: Collect require Bindings From Exported Declarations * refactor: Cover Module-Load Failures With a Mocked-Dependency Load Spec A packages/chat spec now loads the index, react and components entries with every package the sources import (React and Jotai aside) mocked to a module whose exports are missing, the way an app spec's factory mock leaves it, and asserts nothing throws. That covers aliases, require, invoked functions and the rest at runtime, so chat/boundary keeps only the plain case: a call, new, tagged template or member read through a direct import binding at module scope. * chore: Retest Against Current Dev * fix: Exempt Spec Support Files and Node Built-ins From the Module-Load Check
218 lines
8 KiB
JavaScript
218 lines
8 KiB
JavaScript
import path from 'node:path';
|
|
import { describe, it } from 'node:test';
|
|
import tsParser from '@typescript-eslint/parser';
|
|
import { RuleTester } from 'eslint';
|
|
import chat from './chat.mjs';
|
|
|
|
RuleTester.describe = describe;
|
|
RuleTester.it = it;
|
|
|
|
const root = path.resolve(import.meta.dirname, '../..');
|
|
const options = [{ sourceRoot: 'packages/chat/src', packageName: '@librechat/chat' }];
|
|
const at = (file) => path.join(root, 'packages/chat/src', file);
|
|
|
|
const tester = new RuleTester({
|
|
languageOptions: { parser: tsParser, parserOptions: { ecmaFeatures: { jsx: true } } },
|
|
});
|
|
|
|
/** Every spelling a source can use to load one target, in each syntactic form the rule reads. */
|
|
const forms = (specifier) => [
|
|
`import x from '${specifier}';`,
|
|
`import type { X } from '${specifier}';`,
|
|
`export * from '${specifier}';`,
|
|
`export { x } from '${specifier}';`,
|
|
`export const load = () => import('${specifier}');`,
|
|
`export const load = () => import(\`${specifier}\`);`,
|
|
`export const load = () => require('${specifier}');`,
|
|
`import x = require('${specifier}');`,
|
|
`export type X = typeof import('${specifier}');`,
|
|
];
|
|
|
|
/** Ways of naming the `/react` and `/components` entries from `src/core/streaming/`. */
|
|
const entrySpellings = ['react', 'components'].flatMap((entry) => [
|
|
`../../${entry}`,
|
|
`../../${entry}/`,
|
|
`../../${entry}.ts`,
|
|
`../../${entry}.tsx`,
|
|
`../../${entry}.js`,
|
|
`../../${entry}.mjs`,
|
|
`../../${entry}.cjs`,
|
|
`../../${entry}/index`,
|
|
`../../${entry}/index.js`,
|
|
`../../${entry}/Part.tsx`,
|
|
`../.././${entry}`,
|
|
`../../core/../${entry}`,
|
|
]);
|
|
|
|
const uiSpellings = [
|
|
'react',
|
|
'react/jsx-runtime',
|
|
'react-dom',
|
|
'react-dom/client',
|
|
'jotai',
|
|
'jotai/utils',
|
|
'@tanstack/react-query',
|
|
'@librechat/client',
|
|
'@librechat/client/theme.css',
|
|
];
|
|
|
|
const appSpellings = [
|
|
'client/src/hooks',
|
|
'src/client/src',
|
|
'~/store',
|
|
'~',
|
|
'@librechat/frontend',
|
|
'@librechat/frontend/src/store/filesDialog',
|
|
'../../../../client/src/hooks',
|
|
'../../../client/src',
|
|
'../../package.json',
|
|
];
|
|
|
|
const selfSpellings = ['@librechat/chat', '@librechat/chat/react', '@librechat/chat/components'];
|
|
|
|
const cases = (file, specifiers, messageId) =>
|
|
specifiers.flatMap((specifier) =>
|
|
forms(specifier).map((code) => ({
|
|
code,
|
|
filename: at(file),
|
|
options,
|
|
errors: [{ messageId }],
|
|
})),
|
|
);
|
|
|
|
const allowed = (file, specifiers) =>
|
|
specifiers.flatMap((specifier) =>
|
|
forms(specifier).map((code) => ({ code, filename: at(file), options })),
|
|
);
|
|
|
|
tester.run('chat/boundary', chat.rules.boundary, {
|
|
valid: [
|
|
...allowed('core/streaming/probe.ts', [
|
|
'librechat-data-provider',
|
|
'./types',
|
|
'../types.js',
|
|
'../../core/types',
|
|
'./react-utils',
|
|
'../reactive.js',
|
|
'../../index',
|
|
]),
|
|
...allowed('index.ts', ['./core', './core/index.ts']),
|
|
...allowed('react/probe.tsx', [
|
|
'react',
|
|
'jotai',
|
|
'@tanstack/react-query',
|
|
'../core',
|
|
'../components',
|
|
]),
|
|
...allowed('components/Probe.tsx', ['react', '@librechat/client', '../react']),
|
|
...allowed('__tests__/core.spec.ts', ['react']),
|
|
{
|
|
code: "const name = 'react'; export const load = () => import(name);",
|
|
filename: at('react/probe.ts'),
|
|
options,
|
|
},
|
|
{ code: "import x from '~/store';", filename: path.join(root, 'client/src/x.ts'), options },
|
|
...[
|
|
"import { cn } from '@librechat/client'; export const C = () => cn('a');",
|
|
"import { cn } from '@librechat/client'; export function f() { return cn('a'); }",
|
|
"import { cn } from '@librechat/client'; export const C = memo(function C() { return cn('a'); });",
|
|
"import { cn } from '@librechat/client'; class A { value = cn('a'); }",
|
|
"import type { TShowToast } from '@librechat/client'; export const t = TShowToast;",
|
|
"import { cn } from './utils'; export const classes = cn('a');",
|
|
"import { cn } from '@librechat/client'; export const f = (cn: (v: string) => string) => cn('a');",
|
|
"import { memo, createContext } from 'react'; export const C = memo(() => null); export const Ctx = createContext(null);",
|
|
"import { atom } from 'jotai'; import { atomFamily } from 'jotai/utils'; export const a = atom(0); export const f = atomFamily(() => a);",
|
|
"import { FileSources } from 'librechat-data-provider'; export const icon = (s: string) => ({ [FileSources.local]: 'x' })[s];",
|
|
"import { Search } from 'lucide-react'; export const icons = { search: Search, list: [Search] };",
|
|
].map((code) => ({ code, filename: at('components/Probe.tsx'), options })),
|
|
{
|
|
code: "import { cn } from '@librechat/client'; const classes = cn('a');",
|
|
filename: at('components/__tests__/Probe.spec.tsx'),
|
|
options,
|
|
},
|
|
{
|
|
code: "import path from 'node:path'; export const dir = path.join('a', 'b');",
|
|
filename: at('__fixtures__/i18n.ts'),
|
|
options,
|
|
},
|
|
{
|
|
code: "import { cn } from '@librechat/client'; export const classes = cn('a');",
|
|
filename: at('components/__mocks__/Probe.tsx'),
|
|
options,
|
|
},
|
|
{
|
|
code: "import path from 'node:path'; export const dir = path.join('a', 'b');",
|
|
filename: at('react/probe.ts'),
|
|
options,
|
|
},
|
|
],
|
|
invalid: [
|
|
...cases('core/streaming/probe.ts', entrySpellings, 'entry'),
|
|
...cases('core/streaming/probe.ts', uiSpellings, 'ui'),
|
|
...cases('index.ts', ['react'], 'ui'),
|
|
...cases(
|
|
'core/probe.ts',
|
|
['components', 'components/Part', 'src/components', 'src/react', '@/react', '@/components/'],
|
|
'entry',
|
|
),
|
|
...cases('index.ts', ['./react', './components/'], 'entry'),
|
|
...cases('core/probe.ts', appSpellings.slice(0, 4), 'app'),
|
|
...cases('react/probe.ts', appSpellings, 'app'),
|
|
...cases('components/Probe.tsx', ['recoil', 'recoil/x'], 'recoil'),
|
|
...cases('react/probe.ts', selfSpellings, 'self'),
|
|
...cases('core/probe.ts', selfSpellings, 'self'),
|
|
{
|
|
// An absolute root holds when ESLint runs from another directory, as a workspace script does.
|
|
code: "import x from 'react';",
|
|
filename: at('core/probe.ts'),
|
|
options: [
|
|
{ sourceRoot: path.join(root, 'packages/chat/src'), packageName: '@librechat/chat' },
|
|
],
|
|
errors: [{ messageId: 'ui' }],
|
|
},
|
|
...[
|
|
["import { cn } from '@librechat/client'; export const classes = cn('a');", 'cn'],
|
|
["import { cn as join } from '@librechat/client'; export const classes = join('a');", 'join'],
|
|
[
|
|
"import * as Client from '@librechat/client'; export const c = Client['cn']('a');",
|
|
'Client',
|
|
],
|
|
["import { cva } from '@librechat/client'; export const variants = cva('a', {});", 'cva'],
|
|
["import { Store } from '@librechat/client'; export const store = new Store();", 'Store'],
|
|
["import { css } from '@librechat/client'; export const rule = css`color: red;`;", 'css'],
|
|
["import { cn } from '@librechat/client'; class A { static value = cn('a'); }", 'cn'],
|
|
["export const classes = cn('a'); import { cn } from '@librechat/client';", 'cn'],
|
|
[
|
|
"import { FileSources } from 'librechat-data-provider'; export const icons = { [FileSources.local]: 'x' };",
|
|
'FileSources',
|
|
'librechat-data-provider',
|
|
],
|
|
["import { v4 } from 'uuid'; export const id = v4();", 'v4', 'uuid'],
|
|
[
|
|
"import { atom } from 'jotai'; export const a = atom(0);",
|
|
'atom',
|
|
'jotai',
|
|
{ loadSafe: [] },
|
|
],
|
|
].flatMap(([code, name, source = '@librechat/client', extra = {}]) =>
|
|
['components/Probe.tsx', 'react/probe.ts'].map((file) => ({
|
|
code,
|
|
filename: at(file),
|
|
options: [{ ...options[0], ...extra }],
|
|
errors: [{ messageId: 'load', data: { name, source } }],
|
|
})),
|
|
),
|
|
{
|
|
code: 'export const load = (name: string) => import(name);',
|
|
filename: at('core/probe.ts'),
|
|
options,
|
|
errors: [{ messageId: 'opaque' }],
|
|
},
|
|
{
|
|
code: 'export const load = (name: string) => require(`./${name}`);',
|
|
filename: at('core/probe.ts'),
|
|
options,
|
|
errors: [{ messageId: 'opaque' }],
|
|
},
|
|
],
|
|
});
|