index.d.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import * as recast_lib_printer from 'recast/lib/printer';
  2. import * as t from '@babel/types';
  3. import * as generate from '@babel/generator';
  4. import { ComponentAnnotations, StoryAnnotations, IndexedCSFFile, IndexInput } from '@storybook/types';
  5. import * as recast from 'recast';
  6. import { Options } from 'recast';
  7. import * as babelParser from '@babel/parser';
  8. import { ParserOptions } from '@babel/parser';
  9. interface CsfOptions {
  10. fileName?: string;
  11. makeTitle: (userTitle: string) => string;
  12. }
  13. declare class NoMetaError extends Error {
  14. constructor(message: string, ast: t.Node, fileName?: string);
  15. }
  16. interface StaticMeta extends Pick<ComponentAnnotations, 'id' | 'title' | 'includeStories' | 'excludeStories' | 'tags'> {
  17. component?: string;
  18. }
  19. interface StaticStory extends Pick<StoryAnnotations, 'name' | 'parameters' | 'tags'> {
  20. id: string;
  21. }
  22. declare class CsfFile {
  23. _ast: t.File;
  24. _fileName: string;
  25. _makeTitle: (title: string) => string;
  26. _meta?: StaticMeta;
  27. _stories: Record<string, StaticStory>;
  28. _metaAnnotations: Record<string, t.Node>;
  29. _storyExports: Record<string, t.VariableDeclarator | t.FunctionDeclaration>;
  30. _metaStatement: t.Statement | undefined;
  31. _metaNode: t.Expression | undefined;
  32. _storyStatements: Record<string, t.ExportNamedDeclaration>;
  33. _storyAnnotations: Record<string, Record<string, t.Node>>;
  34. _templates: Record<string, t.Expression>;
  35. _namedExportsOrder?: string[];
  36. imports: string[];
  37. constructor(ast: t.File, { fileName, makeTitle }: CsfOptions);
  38. _parseTitle(value: t.Node): string;
  39. _parseMeta(declaration: t.ObjectExpression, program: t.Program): void;
  40. getStoryExport(key: string): t.Node;
  41. parse(): CsfFile & IndexedCSFFile;
  42. get meta(): StaticMeta | undefined;
  43. get stories(): StaticStory[];
  44. get indexInputs(): IndexInput[];
  45. }
  46. declare const loadCsf: (code: string, options: CsfOptions) => CsfFile;
  47. interface FormatOptions {
  48. sourceMaps?: boolean;
  49. preserveStyle?: boolean;
  50. }
  51. declare const formatCsf: (csf: CsfFile, options?: FormatOptions) => string | generate.GeneratorResult;
  52. /**
  53. * Use this function, if you want to preserve styles. Uses recast under the hood.
  54. */
  55. declare const printCsf: (csf: CsfFile, options?: Options) => recast_lib_printer.PrintResultType;
  56. declare const readCsf: (fileName: string, options: CsfOptions) => Promise<CsfFile>;
  57. declare const writeCsf: (csf: CsfFile, fileName?: string) => Promise<void>;
  58. declare class ConfigFile {
  59. _ast: t.File;
  60. _code: string;
  61. _exports: Record<string, t.Expression>;
  62. _exportDecls: Record<string, t.VariableDeclarator>;
  63. _exportsObject: t.ObjectExpression | undefined;
  64. _quotes: 'single' | 'double' | undefined;
  65. fileName?: string;
  66. hasDefaultExport: boolean;
  67. constructor(ast: t.File, code: string, fileName?: string);
  68. parse(): this;
  69. getFieldNode(path: string[]): t.Node | undefined;
  70. getFieldProperties(path: string[]): t.ObjectProperty[] | undefined;
  71. getFieldValue(path: string[]): any;
  72. getSafeFieldValue(path: string[]): any;
  73. setFieldNode(path: string[], expr: t.Expression): void;
  74. /**
  75. * Returns the name of a node in a given path, supporting the following formats:
  76. * 1. { framework: 'value' }
  77. * 2. { framework: { name: 'value', options: {} } }
  78. */
  79. /**
  80. * Returns the name of a node in a given path, supporting the following formats:
  81. * @example
  82. * // 1. { framework: 'framework-name' }
  83. * // 2. { framework: { name: 'framework-name', options: {} }
  84. * getNameFromPath(['framework']) // => 'framework-name'
  85. */
  86. getNameFromPath(path: string[]): string | undefined;
  87. /**
  88. * Returns an array of names of a node in a given path, supporting the following formats:
  89. * @example
  90. * const config = {
  91. * addons: [
  92. * 'first-addon',
  93. * { name: 'second-addon', options: {} }
  94. * ]
  95. * }
  96. * // => ['first-addon', 'second-addon']
  97. * getNamesFromPath(['addons'])
  98. *
  99. */
  100. getNamesFromPath(path: string[]): string[] | undefined;
  101. /**
  102. * Given a node and a fallback property, returns a **non-evaluated** string value of the node.
  103. * 1. { node: 'value' }
  104. * 2. { node: { fallbackProperty: 'value' } }
  105. */
  106. _getPresetValue(node: t.Node, fallbackProperty: string): string;
  107. removeField(path: string[]): void;
  108. appendValueToArray(path: string[], value: any): void;
  109. appendNodeToArray(path: string[], node: t.Expression): void;
  110. _inferQuotes(): "single" | "double";
  111. valueToNode(value: any): t.Expression | undefined;
  112. setFieldValue(path: string[], value: any): void;
  113. getBodyDeclarations(): t.Statement[];
  114. setBodyDeclaration(declaration: t.Declaration): void;
  115. /**
  116. * Import specifiers for a specific require import
  117. * @param importSpecifiers - The import specifiers to set. If a string is passed in, a default import will be set. Otherwise, an array of named imports will be set
  118. * @param fromImport - The module to import from
  119. * @example
  120. * // const { foo } = require('bar');
  121. * setRequireImport(['foo'], 'bar');
  122. *
  123. * // const foo = require('bar');
  124. * setRequireImport('foo', 'bar');
  125. *
  126. */
  127. setRequireImport(importSpecifier: string[] | string, fromImport: string): void;
  128. /**
  129. * Set import specifiers for a given import statement.
  130. * @description Does not support setting type imports (yet)
  131. * @param importSpecifiers - The import specifiers to set. If a string is passed in, a default import will be set. Otherwise, an array of named imports will be set
  132. * @param fromImport - The module to import from
  133. * @example
  134. * // import { foo } from 'bar';
  135. * setImport(['foo'], 'bar');
  136. *
  137. * // import foo from 'bar';
  138. * setImport('foo', 'bar');
  139. *
  140. */
  141. setImport(importSpecifier: string[] | string, fromImport: string): void;
  142. }
  143. declare const loadConfig: (code: string, fileName?: string) => ConfigFile;
  144. declare const formatConfig: (config: ConfigFile) => string;
  145. declare const printConfig: (config: ConfigFile, options?: Options) => recast_lib_printer.PrintResultType;
  146. declare const readConfig: (fileName: string) => Promise<ConfigFile>;
  147. declare const writeConfig: (config: ConfigFile, fileName?: string) => Promise<void>;
  148. declare const getStorySortParameter: (previewCode: string) => any;
  149. interface EnrichCsfOptions {
  150. disableSource?: boolean;
  151. disableDescription?: boolean;
  152. }
  153. declare const enrichCsfStory: (csf: CsfFile, csfSource: CsfFile, key: string, options?: EnrichCsfOptions) => void;
  154. declare const enrichCsfMeta: (csf: CsfFile, csfSource: CsfFile, options?: EnrichCsfOptions) => void;
  155. declare const enrichCsf: (csf: CsfFile, csfSource: CsfFile, options?: EnrichCsfOptions) => void;
  156. declare const extractSource: (node: t.Node) => string;
  157. declare const extractDescription: (node?: t.Node) => string;
  158. declare const parserOptions: ParserOptions;
  159. declare const babelParse: (code: string) => any;
  160. declare const babelPrint: (ast: recast.types.ASTNode) => string;
  161. declare const babelParseExpression: (code: string) => babelParser.ParseResult<t.Expression>;
  162. export { ConfigFile, CsfFile, CsfOptions, EnrichCsfOptions, NoMetaError, StaticMeta, StaticStory, babelParse, babelParseExpression, babelPrint, enrichCsf, enrichCsfMeta, enrichCsfStory, extractDescription, extractSource, formatConfig, formatCsf, getStorySortParameter, loadConfig, loadCsf, parserOptions, printConfig, printCsf, readConfig, readCsf, writeConfig, writeCsf };