UNPKG

@formatjs/ts-transformer

Version:

TS Compiler transformer for formatjs

107 lines (106 loc) 3.47 kB
import { SourceFile, TransformerFactory } from "typescript"; import { TsCompilerInstance } from "ts-jest"; //#region packages/ts-transformer/types.d.ts interface MessageDescriptor { id: string; description?: string | object; defaultMessage?: string; file?: string; start?: number; end?: number; } //#endregion //#region packages/ts-transformer/transform.d.ts type Extractor = (filePath: string, msgs: MessageDescriptor[]) => void; type MetaExtractor = (filePath: string, meta: Record<string, string>) => void; type InterpolateNameFn = (id?: MessageDescriptor["id"], defaultMessage?: MessageDescriptor["defaultMessage"], description?: MessageDescriptor["description"], filePath?: string) => string; interface Opts { /** * Parse specific additional custom pragma. * This allows you to tag certain file with metadata such as `project`. * For example with this file: * ```tsx * // @intl-meta project:my-custom-project * import {FormattedMessage} from 'react-intl'; * <FormattedMessage defaultMessage="foo" id="bar" />; * ``` * and with option `{pragma: "@intl-meta"}`, * we'll parse out `// @intl-meta project:my-custom-project` * into `{project: 'my-custom-project'}` in the result file. */ pragma?: string; /** * Whether the metadata about the location of the message in the source file * should be extracted. If `true`, then `file`, `start`, and `end` * fields will exist for each extracted message descriptors. * Defaults to `false`. */ extractSourceLocation?: boolean; /** * Remove `defaultMessage` field in generated js after extraction. */ removeDefaultMessage?: boolean; /** * Additional component names to extract messages from, * e.g: `['FormattedFooBarMessage']`. */ additionalComponentNames?: string[]; /** * Additional function names to extract messages from, * e.g: `['formatMessage', '$t']` * Default to `['formatMessage']` */ additionalFunctionNames?: string[]; /** * Callback function that gets called everytime we encountered something * that looks like a MessageDescriptor * * @type {Extractor} * @memberof Opts */ onMsgExtracted?: Extractor; /** * Callback function that gets called when we successfully parsed meta * declared in pragma */ onMetaExtracted?: MetaExtractor; /** * webpack-style name interpolation. * Can also be a string like '[sha512:contenthash:hex:6]' * * @type {(InterpolateNameFn | string)} * @memberof Opts */ overrideIdFn?: InterpolateNameFn | string; /** * Whether to compile `defaultMessage` to AST. * This is no-op if `removeDefaultMessage` is `true` */ ast?: boolean; /** * Whether to preserve whitespace and newlines. */ preserveWhitespace?: boolean; /** * Whether to hoist selectors & flatten sentences */ flatten?: boolean; /** * Whether to throw on errors extracting messages. * When false, invalid messages are skipped with a warning. * Defaults to true. */ throws?: boolean; /** * Callback for reporting errors when throws is false. */ onMsgError?: (filePath: string, error: Error) => void; } //#endregion //#region packages/ts-transformer/ts-jest-integration.d.ts declare const name = "@formatjs/ts-transformer"; declare const version = "2.10.1"; declare function factory(compilerInstance: TsCompilerInstance, opts: Opts): TransformerFactory<SourceFile>; //#endregion export { factory, name, version }; //# sourceMappingURL=ts-jest-integration.d.ts.map