Skip to content

Commit 91b2816

Browse files
authored
fix: Temporarily disable save from UI feature (#285)
1 parent 59c0609 commit 91b2816

File tree

7 files changed

+88
-27
lines changed

7 files changed

+88
-27
lines changed

src/compiler/post-transform/define-meta/index.test.ts

+62-1
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import MagicString from 'magic-string';
77
import { parseAst } from 'rollup/parseAst';
88
import { describe, it } from 'vitest';
99

10-
import { transformDefineMeta } from './index.js';
10+
import { createMetaVariableDeclaration, transformDefineMeta } from './index.js';
1111

1212
import { getSvelteAST } from '$lib/parser/ast.js';
1313
import { extractSvelteASTNodes } from '$lib/parser/extract/svelte/nodes.js';
1414
import { extractCompiledASTNodes } from '$lib/parser/extract/compiled/nodes.js';
15+
import { insertDefineMetaParameters } from './insert-parameters.js';
16+
import { replaceDefineMetaArgument } from './replace-argument.js';
1517

1618
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
1719

@@ -56,3 +58,62 @@ describe(transformDefineMeta.name, () => {
5658
);
5759
});
5860
});
61+
62+
describe(createMetaVariableDeclaration.name, () => {
63+
it('parameters are transformed correctly', async ({ expect }) => {
64+
const filename = path.resolve(__dirname, '../../../../tests/stories/Example.stories.svelte');
65+
const originalCode = fs.readFileSync(filename).toString();
66+
const compiledPreTransformCode = fs
67+
.readFileSync(
68+
path.resolve(
69+
__dirname,
70+
'../../../../tests/__compiled__/pre-transform/Example.stories.dev.js'
71+
)
72+
)
73+
.toString();
74+
const svelteAST = getSvelteAST({ code: originalCode, filename });
75+
const svelteASTNodes = await extractSvelteASTNodes({
76+
ast: svelteAST,
77+
filename,
78+
});
79+
const compiledASTNodes = await extractCompiledASTNodes({
80+
ast: parseAst(compiledPreTransformCode),
81+
filename,
82+
});
83+
insertDefineMetaParameters({
84+
nodes: {
85+
svelte: svelteASTNodes,
86+
compiled: compiledASTNodes,
87+
},
88+
filename,
89+
});
90+
91+
const metaObjectExpression = replaceDefineMetaArgument({
92+
nodes: {
93+
svelte: svelteASTNodes,
94+
compiled: compiledASTNodes,
95+
},
96+
});
97+
const metaVariableDeclaration = createMetaVariableDeclaration({ init: metaObjectExpression });
98+
99+
expect(print(metaVariableDeclaration).code).toMatchInlineSnapshot(`
100+
"const meta = {
101+
title: 'Example',
102+
component: Example,
103+
tags: ['autodocs'],
104+
args: {
105+
onclick: action('onclick'),
106+
onmouseenter: action('onmouseenter'),
107+
onmouseleave: action('onmouseleave')
108+
},
109+
parameters: {
110+
docs: {
111+
description: {
112+
component: "Description set explicitly in the comment above \`defineMeta\`.\\n\\nMultiline supported. And also Markdown syntax:\\n\\n* **Bold**,\\n* _Italic_,\\n* \`Code\`."
113+
}
114+
}
115+
}
116+
};"
117+
`);
118+
});
119+
});

src/compiler/post-transform/define-meta/index.ts

+4-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { print } from 'esrap';
22
import type MagicString from 'magic-string';
33

44
import { replaceDefineMetaArgument } from './replace-argument.js';
5-
import { insertDefineMetaJSDocCommentAsDescription } from './insert-description.js';
5+
import { insertDefineMetaParameters } from './insert-parameters.js';
66

77
import { createASTIdentifier, type ESTreeAST } from '$lib/parser/ast.js';
88
import type { CompiledASTNodes } from '$lib/parser/extract/compiled/nodes.js';
@@ -24,17 +24,9 @@ interface Params {
2424
export function transformDefineMeta(params: Params): void {
2525
const { code, nodes, filename } = params;
2626

27-
insertDefineMetaJSDocCommentAsDescription({
28-
nodes,
29-
filename,
30-
});
31-
const metaObjectExpression = replaceDefineMetaArgument({
32-
nodes,
33-
filename,
34-
});
35-
const metaVariableDeclaration = createMetaVariableDeclaration({
36-
init: metaObjectExpression,
37-
});
27+
insertDefineMetaParameters({ nodes, filename });
28+
const metaObjectExpression = replaceDefineMetaArgument({ nodes, filename });
29+
const metaVariableDeclaration = createMetaVariableDeclaration({ init: metaObjectExpression });
3830

3931
const { compiled } = nodes;
4032
const { defineMetaVariableDeclaration } = compiled;
@@ -49,7 +41,6 @@ export function createMetaVariableDeclaration({
4941
}: {
5042
init: ESTreeAST.ObjectExpression;
5143
}): ESTreeAST.VariableDeclaration {
52-
//
5344
return {
5445
type: 'VariableDeclaration',
5546
kind: 'const',

src/compiler/post-transform/define-meta/insert-description.ts src/compiler/post-transform/define-meta/insert-parameters.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
getDescriptionPropertyValue,
77
getDocsPropertyValue,
88
getParametersPropertyValue,
9-
} from '$lib/compiler/post-transform/shared/description.js';
9+
} from '$lib/compiler/post-transform/shared/parameters.js';
1010
import { createASTObjectExpression, createASTProperty, type ESTreeAST } from '$lib/parser/ast.js';
1111
import type { SvelteASTNodes } from '$lib/parser/extract/svelte/nodes.js';
1212
import type { CompiledASTNodes } from '$lib/parser/extract/compiled/nodes.js';
@@ -21,6 +21,7 @@ interface Params {
2121
}
2222

2323
/**
24+
* This function inserts parameters to `defineMeta()`.
2425
* Attempt to insert JSDoc comment above the `defineMeta()` call.
2526
*
2627
* Before:
@@ -42,18 +43,15 @@ interface Params {
4243
* });
4344
* ```
4445
*/
45-
export function insertDefineMetaJSDocCommentAsDescription(params: Params): void {
46+
export function insertDefineMetaParameters(params: Params): void {
4647
const { nodes, filename } = params;
47-
const { compiled, svelte } = nodes;
48-
const { defineMetaVariableDeclaration } = svelte;
49-
const { leadingComments } = defineMetaVariableDeclaration;
5048

51-
if (!leadingComments) {
49+
if (!nodes.svelte.defineMetaVariableDeclaration.leadingComments) {
5250
return;
5351
}
5452

5553
const defineMetaFirstArgumentObjectExpression = getDefineMetaFirstArgumentObjectExpression({
56-
nodes: compiled,
54+
nodes: nodes.compiled,
5755
filename,
5856
});
5957

@@ -117,7 +115,7 @@ export function insertDefineMetaJSDocCommentAsDescription(params: Params): void
117115
}).properties.push(
118116
createASTProperty('component', {
119117
type: 'Literal',
120-
value: extractDescription(leadingComments),
118+
value: extractDescription(nodes.svelte.defineMetaVariableDeclaration.leadingComments),
121119
})
122120
);
123121
}

src/compiler/post-transform/shared/description.ts src/compiler/post-transform/shared/parameters.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import dedent from 'dedent';
22

3-
import type { ESTreeAST, SvelteAST } from '$lib/parser/ast.js';
4-
5-
import { createASTObjectExpression } from '$lib/parser/ast.js';
3+
import { createASTObjectExpression, type ESTreeAST, type SvelteAST } from '$lib/parser/ast.js';
64

75
interface FindPropertyOptions {
86
name: string;

src/compiler/post-transform/story/insert-description.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
getParametersPropertyValue,
99
getDocsPropertyValue,
1010
getDescriptionPropertyValue,
11-
} from '$lib/compiler/post-transform/shared/description.js';
11+
} from '$lib/compiler/post-transform/shared/parameters.js';
1212
import { createASTObjectExpression, createASTProperty } from '$lib/parser/ast.js';
1313

1414
import type { ESTreeAST } from '$lib/parser/ast.js';

src/compiler/post-transform/story/insert-svelte-csf.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
findPropertyParametersIndex,
33
getParametersPropertyValue,
4-
} from '$lib/compiler/post-transform/shared/description.js';
4+
} from '$lib/compiler/post-transform/shared/parameters.js';
55

66
import type { extractStoriesNodesFromExportDefaultFn } from '$lib/parser/extract/compiled/stories.js';
77
import { getStoryPropsObjectExpression } from '$lib/parser/extract/compiled/story.js';

src/runtime/create-runtime-stories.ts

+13
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,18 @@ export const createRuntimeStories = (Stories: Component, meta: Meta<Cmp>) => {
8383
stories[exportName] = storyObj;
8484
}
8585

86+
if (!meta.parameters) {
87+
meta.parameters = {};
88+
}
89+
90+
if (!meta.parameters.controls) {
91+
meta.parameters.controls = {};
92+
}
93+
94+
// Inserts https://storybook.js.org/docs/essentials/controls#disablesavefromui
95+
// Ref: https://github.com/storybookjs/addon-svelte-csf/issues/240
96+
// TODO: Restore this feature
97+
meta.parameters.controls.disableSaveFromUI = true;
98+
8699
return stories;
87100
};

0 commit comments

Comments
 (0)