|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +import { types as t } from 'storybook/internal/babel'; |
| 4 | +import { generate, parser } from 'storybook/internal/babel'; |
| 5 | + |
| 6 | +import { |
| 7 | + cleanupTypeImports, |
| 8 | + getConfigProperties, |
| 9 | + removeExportDeclarations, |
| 10 | +} from './csf-factories-utils'; |
| 11 | + |
| 12 | +expect.addSnapshotSerializer({ |
| 13 | + serialize: (val: any) => { |
| 14 | + if (typeof val === 'string') { |
| 15 | + return val; |
| 16 | + } |
| 17 | + if (typeof val === 'object' && val !== null) { |
| 18 | + return JSON.stringify(val, null, 2); |
| 19 | + } |
| 20 | + return String(val); |
| 21 | + }, |
| 22 | + test: (_val) => true, |
| 23 | +}); |
| 24 | + |
| 25 | +function parseCodeToProgramNode(code: string): t.Program { |
| 26 | + return parser.parse(code, { sourceType: 'module', plugins: ['typescript'] }).program; |
| 27 | +} |
| 28 | + |
| 29 | +function generateCodeFromAST(node: t.Program) { |
| 30 | + return generate(node).code; |
| 31 | +} |
| 32 | + |
| 33 | +describe('cleanupTypeImports', () => { |
| 34 | + it('removes disallowed imports from @storybook/*', () => { |
| 35 | + const code = ` |
| 36 | + import { Story, SomethingElse } from '@storybook/react'; |
| 37 | + import { Other } from 'some-other-package'; |
| 38 | + `; |
| 39 | + |
| 40 | + const programNode = parseCodeToProgramNode(code); |
| 41 | + const cleanedNodes = cleanupTypeImports(programNode, ['Story']); |
| 42 | + |
| 43 | + expect(generateCodeFromAST({ ...programNode, body: cleanedNodes })).toMatchInlineSnapshot(` |
| 44 | + import { SomethingElse } from '@storybook/react'; |
| 45 | + import { Other } from 'some-other-package'; |
| 46 | + `); |
| 47 | + }); |
| 48 | + |
| 49 | + it('removes entire import if all specifiers are removed', () => { |
| 50 | + const code = ` |
| 51 | + import { Story, Meta } from '@storybook/react'; |
| 52 | + `; |
| 53 | + |
| 54 | + const programNode = parseCodeToProgramNode(code); |
| 55 | + const cleanedNodes = cleanupTypeImports(programNode, ['Story', 'Meta']); |
| 56 | + |
| 57 | + expect(generateCodeFromAST({ ...programNode, body: cleanedNodes })).toMatchInlineSnapshot(``); |
| 58 | + }); |
| 59 | + |
| 60 | + it('retains non storybook imports', () => { |
| 61 | + const code = ` |
| 62 | + import { Preview } from 'internal-types'; |
| 63 | + `; |
| 64 | + |
| 65 | + const programNode = parseCodeToProgramNode(code); |
| 66 | + const cleanedNodes = cleanupTypeImports(programNode, ['Preview']); |
| 67 | + |
| 68 | + expect(generateCodeFromAST({ ...programNode, body: cleanedNodes })).toMatchInlineSnapshot( |
| 69 | + `import { Preview } from 'internal-types';` |
| 70 | + ); |
| 71 | + }); |
| 72 | + |
| 73 | + it('retains namespace imports', () => { |
| 74 | + const code = ` |
| 75 | + import * as Storybook from '@storybook/react'; |
| 76 | + `; |
| 77 | + |
| 78 | + const programNode = parseCodeToProgramNode(code); |
| 79 | + const cleanedNodes = cleanupTypeImports(programNode, ['Preview']); |
| 80 | + |
| 81 | + expect(generateCodeFromAST({ ...programNode, body: cleanedNodes })).toMatchInlineSnapshot( |
| 82 | + `import * as Storybook from '@storybook/react';` |
| 83 | + ); |
| 84 | + }); |
| 85 | + |
| 86 | + it('retains imports if they are used', () => { |
| 87 | + const code = ` |
| 88 | + import { Type1, type Type2 } from '@storybook/react'; |
| 89 | + import type { Type3, ShouldBeRemoved, Type4 } from '@storybook/react'; |
| 90 | +
|
| 91 | + const example: Type1 = {}; |
| 92 | + const example2 = {} as Type2; |
| 93 | + const example3 = {} satisfies Type3; |
| 94 | + const example4 = { |
| 95 | + render: (args: Type4['args']) => {} |
| 96 | + }; |
| 97 | + `; |
| 98 | + |
| 99 | + const programNode = parseCodeToProgramNode(code); |
| 100 | + const cleanedNodes = cleanupTypeImports(programNode, [ |
| 101 | + 'Type1', |
| 102 | + 'Type2', |
| 103 | + 'Type3', |
| 104 | + 'Type4', |
| 105 | + 'ShouldBeRemoved', |
| 106 | + ]); |
| 107 | + |
| 108 | + const result = generateCodeFromAST({ ...programNode, body: cleanedNodes }); |
| 109 | + |
| 110 | + expect(result).toMatchInlineSnapshot(` |
| 111 | + import { Type1, type Type2 } from '@storybook/react'; |
| 112 | + import type { Type3, Type4 } from '@storybook/react'; |
| 113 | + const example: Type1 = {}; |
| 114 | + const example2 = {} as Type2; |
| 115 | + const example3 = {} satisfies Type3; |
| 116 | + const example4 = { |
| 117 | + render: (args: Type4['args']) => {} |
| 118 | + }; |
| 119 | + `); |
| 120 | + |
| 121 | + expect(result).not.toContain('ShouldBeRemoved'); |
| 122 | + }); |
| 123 | +}); |
| 124 | + |
| 125 | +describe('removeExportDeclarations', () => { |
| 126 | + it('removes specified variable export declarations', () => { |
| 127 | + const code = ` |
| 128 | + export const foo = 'foo'; |
| 129 | + export const bar = 'bar'; |
| 130 | + export const baz = 'baz'; |
| 131 | + `; |
| 132 | + |
| 133 | + const programNode = parseCodeToProgramNode(code); |
| 134 | + const exportDecls = { |
| 135 | + foo: t.variableDeclarator(t.identifier('foo')), |
| 136 | + baz: t.variableDeclarator(t.identifier('baz')), |
| 137 | + }; |
| 138 | + |
| 139 | + const cleanedNodes = removeExportDeclarations(programNode, exportDecls); |
| 140 | + const cleanedCode = generateCodeFromAST({ ...programNode, body: cleanedNodes }); |
| 141 | + |
| 142 | + expect(cleanedCode).toMatchInlineSnapshot(`export const bar = 'bar';`); |
| 143 | + }); |
| 144 | + |
| 145 | + it('removes specified function export declarations', () => { |
| 146 | + const code = ` |
| 147 | + export function foo() { return 'foo'; } |
| 148 | + export function bar() { return 'bar'; } |
| 149 | + `; |
| 150 | + |
| 151 | + const programNode = parseCodeToProgramNode(code); |
| 152 | + const exportDecls = { |
| 153 | + foo: t.functionDeclaration(t.identifier('foo'), [], t.blockStatement([])), |
| 154 | + }; |
| 155 | + |
| 156 | + const cleanedNodes = removeExportDeclarations(programNode, exportDecls); |
| 157 | + const cleanedCode = generateCodeFromAST({ ...programNode, body: cleanedNodes }); |
| 158 | + |
| 159 | + expect(cleanedCode).toMatchInlineSnapshot(` |
| 160 | + export function bar() { |
| 161 | + return 'bar'; |
| 162 | + } |
| 163 | + `); |
| 164 | + }); |
| 165 | + |
| 166 | + it('retains exports not in the disallow list', () => { |
| 167 | + const code = ` |
| 168 | + export const foo = 'foo'; |
| 169 | + export const bar = 'bar'; |
| 170 | + `; |
| 171 | + |
| 172 | + const programNode = parseCodeToProgramNode(code); |
| 173 | + const exportDecls = { |
| 174 | + nonExistent: t.variableDeclarator(t.identifier('nonExistent')), |
| 175 | + }; |
| 176 | + |
| 177 | + const cleanedNodes = removeExportDeclarations(programNode, exportDecls); |
| 178 | + const cleanedCode = generateCodeFromAST({ ...programNode, body: cleanedNodes }); |
| 179 | + |
| 180 | + expect(cleanedCode).toMatchInlineSnapshot(` |
| 181 | + export const foo = 'foo'; |
| 182 | + export const bar = 'bar'; |
| 183 | + `); |
| 184 | + }); |
| 185 | +}); |
| 186 | + |
| 187 | +describe('getConfigProperties', () => { |
| 188 | + it('returns object properties from variable declarations', () => { |
| 189 | + const exportDecls = { |
| 190 | + foo: t.variableDeclarator(t.identifier('foo'), t.stringLiteral('fooValue')), |
| 191 | + bar: t.variableDeclarator(t.identifier('bar'), t.numericLiteral(42)), |
| 192 | + }; |
| 193 | + |
| 194 | + const properties = getConfigProperties(exportDecls); |
| 195 | + |
| 196 | + expect(properties).toHaveLength(2); |
| 197 | + expect(properties[0].key.name).toBe('foo'); |
| 198 | + expect(properties[0].value.value).toBe('fooValue'); |
| 199 | + expect(properties[1].key.name).toBe('bar'); |
| 200 | + expect(properties[1].value.value).toBe(42); |
| 201 | + }); |
| 202 | + |
| 203 | + it('returns object properties from function declarations', () => { |
| 204 | + const exportDecls = { |
| 205 | + foo: t.functionDeclaration(t.identifier('foo'), [], t.blockStatement([])), |
| 206 | + }; |
| 207 | + |
| 208 | + const properties = getConfigProperties(exportDecls); |
| 209 | + |
| 210 | + expect(properties).toHaveLength(1); |
| 211 | + expect(properties[0].key.name).toBe('foo'); |
| 212 | + expect(properties[0].value.type).toBe('ArrowFunctionExpression'); |
| 213 | + }); |
| 214 | +}); |
0 commit comments