/** * Detection of Intlayer nodes used where a string is expected. * * Every framework adapter wraps a string leaf of a dictionary in an * `IntlayerNode`. With the editor disabled the node type is `T & { value: T }`, * so `alt={content.title}` type-checks. With `editor.enabled: true` it becomes * `ReactNode & { value: T }` (or the framework's equivalent: `JSX.Element`, * `VNode`, `VNodeChild`, a Svelte component, a renderer function), which is no * longer assignable to `string` nor carries string methods. `.value` holds the * primitive in both modes, so reading it is always correct. * * Template literals, `+` concatenation and `String()` accept any type, so they * type-check in both modes and are not reported. * * The helpers below are AST-agnostic: they run on ESTree (ESLint, oxlint, * Prettier's TypeScript/Acorn/Meriyah parsers), Babel (Prettier's Babel parser), * and the template ASTs of `vue-eslint-parser` / `svelte-eslint-parser`. Callers * provide the ancestor chain, so no parent pointers are required. */ /** Any AST node, whatever the parser. */ export type IntlayerNodeAstNode = { type: string; [field: string]: unknown; }; /** Where an Intlayer node is consumed as a string. */ export type IntlayerNodeStringContext = 'string-method' | 'attribute'; /** A field read that must go through `.value`. */ export type IntlayerNodeStringUsage = { /** Outermost node of the field chain; `.value` is appended after it. */ chainEnd: IntlayerNodeAstNode; /** How the node is consumed. */ context: IntlayerNodeStringContext; }; /** A `.value` read rendering the primitive where the node itself renders. */ export type IntlayerNodeRedundantValue = { /** The field chain; the fix replaces `valueAccess` by it. */ chainEnd: IntlayerNodeAstNode; /** The `chainEnd.value` member expression. */ valueAccess: IntlayerNodeAstNode; }; /** Variables bound from an Intlayer getter call. */ export type IntlayerNodeBindingNames = { /** Bound to the whole dictionary (`const content = useIntlayer('x')`). */ rootNames: string[]; /** Destructured fields, possibly leaves (`const { title } = …`). */ leafNames: string[]; }; /** * Packages whose getters wrap string leaves in an `IntlayerNode`. Subpaths * (`next-intlayer/server`, `react-intlayer/server`…) are included. * * Absent on purpose: `intlayer` / `@intlayer/core` and the server adapters * (express, fastify…), whose getters return primitives, and `remix-intlayer`, * which keeps plain strings as primitives. */ export const INTLAYER_NODE_PACKAGES = [ 'react-intlayer', 'next-intlayer', 'preact-intlayer', 'solid-intlayer', 'react-native-intlayer', 'vue-intlayer', 'nuxt-intlayer', 'svelte-intlayer', 'angular-intlayer', 'lit-intlayer', 'vanilla-intlayer', 'astro-intlayer', ] as const; /** * Packages whose nodes render as JSX children: `

{content.title}

` * renders the node (selectable by the visual editor), where * `content.title.value` renders a bare string. */ export const INTLAYER_JSX_NODE_PACKAGES = [ 'react-intlayer', 'next-intlayer', 'preact-intlayer', 'solid-intlayer', 'react-native-intlayer', ] as const; /** Getters returning dictionary content built from Intlayer nodes. */ export const INTLAYER_NODE_GETTERS = [ 'useIntlayer', 'getIntlayer', 'useDictionary', 'getDictionary', 'useDictionaryAsync', 'useDictionaryDynamic', ] as const; /** * Members of the node itself rather than dictionary fields. A chain reaching * one of them already reads the primitive (or deliberately the node). */ const INTLAYER_NODE_MEMBERS = new Set([ 'value', 'raw', 'render', 'use', 'toString', 'valueOf', ]); /** * `String.prototype` methods absent from `Array.prototype`, so calling one on a * field proves the field is read as a string. */ const STRING_ONLY_METHODS = new Set([ 'charAt', 'charCodeAt', 'codePointAt', 'endsWith', 'localeCompare', 'match', 'matchAll', 'normalize', 'padEnd', 'padStart', 'repeat', 'replace', 'replaceAll', 'search', 'split', 'startsWith', 'substring', 'toLocaleLowerCase', 'toLocaleUpperCase', 'toLowerCase', 'toUpperCase', 'trim', 'trimEnd', 'trimStart', ]); /** Attributes of a native element that accept something other than a string. */ const NON_STRING_ATTRIBUTES = new Set([ 'children', 'class', 'dangerouslySetInnerHTML', 'ref', 'style', ]); /** Expression wrappers that do not change what the value is used for. */ const TRANSPARENT_WRAPPERS = new Set([ 'ChainExpression', 'ParenthesizedExpression', 'TSNonNullExpression', ]); /** Expression wrappers around a getter call (`await`, TypeScript casts). */ const CALL_WRAPPERS = new Set([ 'AwaitExpression', 'ParenthesizedExpression', 'TSAsExpression', 'TSNonNullExpression', 'TSSatisfiesExpression', ]); /** Identifier-like nodes across ESTree, JSX, Vue and Svelte ASTs. */ const NAME_NODE_TYPES = new Set([ 'Identifier', 'JSXIdentifier', 'SvelteName', 'VIdentifier', ]); const MEMBER_TYPES = new Set(['MemberExpression', 'OptionalMemberExpression']); const CALL_TYPES = new Set(['CallExpression', 'OptionalCallExpression']); const isNode = (value: unknown): value is IntlayerNodeAstNode => typeof value === 'object' && value !== null && typeof (value as IntlayerNodeAstNode).type === 'string'; /** * True for an import of a package that produces Intlayer nodes. * * @param source - Module specifier, e.g. `'next-intlayer/server'`. */ export const isIntlayerNodePackage = (source: string): boolean => INTLAYER_NODE_PACKAGES.some( (packageName) => source === packageName || source.startsWith(`${packageName}/`) ); /** * True for an import of a package whose nodes render as JSX children. * * @param source - Module specifier, e.g. `'next-intlayer/server'`. */ export const isIntlayerJsxNodePackage = (source: string): boolean => INTLAYER_JSX_NODE_PACKAGES.some( (packageName) => source === packageName || source.startsWith(`${packageName}/`) ); /** Name of an identifier or string-literal node. */ const readName = (node: unknown): string | null => { if (!isNode(node)) return null; if (NAME_NODE_TYPES.has(node.type)) { return typeof node.name === 'string' ? node.name : null; } if ( (node.type === 'Literal' || node.type === 'StringLiteral') && typeof node.value === 'string' ) { return node.value; } return null; }; /** * Local names of the Intlayer getters a module imports, aliases included * (`import { useIntlayer as useContent } from 'react-intlayer'`). * * @param program - Program node (ESTree or Babel). * @param isPackageIncluded - Which import sources count; every node package * by default, {@link isIntlayerJsxNodePackage} for JSX-renderable nodes. */ export const getIntlayerNodeGetterNames = ( program: IntlayerNodeAstNode, isPackageIncluded: (source: string) => boolean = isIntlayerNodePackage ): Set => { const getterNames = new Set(); const getters = new Set(INTLAYER_NODE_GETTERS); const body = Array.isArray(program.body) ? program.body : []; // Svelte nests the module statements inside `