index.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getAllNamedExports = exports.isValidStoryExport = exports.getDescriptor = exports.getMetaObjectExpression = exports.docsUrl = void 0;
  4. /* eslint-disable no-fallthrough */
  5. const csf_1 = require("@storybook/csf");
  6. const utils_1 = require("@typescript-eslint/utils");
  7. const ast_1 = require("./ast");
  8. const docsUrl = (ruleName) => `https://github.com/storybookjs/eslint-plugin-storybook/blob/main/docs/rules/${ruleName}.md`;
  9. exports.docsUrl = docsUrl;
  10. const getMetaObjectExpression = (node, context) => {
  11. let meta = node.declaration;
  12. if ((0, ast_1.isIdentifier)(meta)) {
  13. const variable = utils_1.ASTUtils.findVariable(context.getScope(), meta.name);
  14. const decl = variable && variable.defs.find((def) => (0, ast_1.isVariableDeclarator)(def.node));
  15. if (decl && (0, ast_1.isVariableDeclarator)(decl.node)) {
  16. meta = decl.node.init;
  17. }
  18. }
  19. if ((0, ast_1.isTSAsExpression)(meta) || (0, ast_1.isTSSatisfiesExpression)(meta)) {
  20. meta = meta.expression;
  21. }
  22. return (0, ast_1.isObjectExpression)(meta) ? meta : null;
  23. };
  24. exports.getMetaObjectExpression = getMetaObjectExpression;
  25. const getDescriptor = (metaDeclaration, propertyName) => {
  26. const property = metaDeclaration &&
  27. metaDeclaration.properties.find((p) => 'key' in p && 'name' in p.key && p.key.name === propertyName);
  28. if (!property || (0, ast_1.isSpreadElement)(property)) {
  29. return undefined;
  30. }
  31. const { type } = property.value;
  32. switch (type) {
  33. case 'ArrayExpression':
  34. return property.value.elements.map((t) => {
  35. if (!['StringLiteral', 'Literal'].includes(t.type)) {
  36. throw new Error(`Unexpected descriptor element: ${t.type}`);
  37. }
  38. // @ts-expect-error TODO: t should be only StringLiteral or Literal, and the type is not resolving correctly
  39. return t.value;
  40. });
  41. case 'Literal':
  42. // @ts-expect-error TODO: Investigation needed. Type systems says, that "RegExpLiteral" does not exist
  43. case 'RegExpLiteral':
  44. // @ts-expect-error TODO: investigation needed
  45. return property.value.value;
  46. default:
  47. throw new Error(`Unexpected descriptor: ${type}`);
  48. }
  49. };
  50. exports.getDescriptor = getDescriptor;
  51. const isValidStoryExport = (node, nonStoryExportsConfig) => (0, csf_1.isExportStory)(node.name, nonStoryExportsConfig) && node.name !== '__namedExportsOrder';
  52. exports.isValidStoryExport = isValidStoryExport;
  53. const getAllNamedExports = (node) => {
  54. // e.g. `export { MyStory }`
  55. if (!node.declaration && node.specifiers) {
  56. return node.specifiers.reduce((acc, specifier) => {
  57. if ((0, ast_1.isIdentifier)(specifier.exported)) {
  58. acc.push(specifier.exported);
  59. }
  60. return acc;
  61. }, []);
  62. }
  63. const decl = node.declaration;
  64. if ((0, ast_1.isVariableDeclaration)(decl)) {
  65. const declaration = decl.declarations[0];
  66. if (declaration) {
  67. const { id } = declaration;
  68. // e.g. `export const MyStory`
  69. if ((0, ast_1.isIdentifier)(id)) {
  70. return [id];
  71. }
  72. }
  73. }
  74. if ((0, ast_1.isFunctionDeclaration)(decl)) {
  75. // e.g. `export function MyStory() {}`
  76. if ((0, ast_1.isIdentifier)(decl.id)) {
  77. return [decl.id];
  78. }
  79. }
  80. return [];
  81. };
  82. exports.getAllNamedExports = getAllNamedExports;