Skip to content

Commit

Permalink
drive-by: small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
xeho91 committed Sep 6, 2024
1 parent 09e6509 commit 164e6e6
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions src/compiler/pre-transform/codemods/legacy-story.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { camelCase } from 'es-toolkit/string';

import {
createASTArrayExpression,
createASTAttribute,
createASTExpressionTag,
createASTObjectExpression,
Expand All @@ -18,7 +19,6 @@ interface Params {
export function transformLegacyStory(params: Params): SvelteAST.Component {
const { node, filename } = params;
let { attributes, fragment, ...rest } = node;

let newAttributes: SvelteAST.Component['attributes'] = [];
let autodocs: SvelteAST.Attribute | undefined;
let source: SvelteAST.Attribute | undefined;
Expand All @@ -32,6 +32,7 @@ export function transformLegacyStory(params: Params): SvelteAST.Component {
letDirectiveArgs = attribute;
continue;
}

if (attribute.type === 'LetDirective' && attribute.name === 'context') {
letDirectiveContext = attribute;
continue;
Expand All @@ -43,24 +44,16 @@ export function transformLegacyStory(params: Params): SvelteAST.Component {
}

if (attribute.type === 'Attribute' && attribute.name === 'source') {
const { value } = attribute;

if (value === true) continue;
source = attribute;
continue;
}

if (attribute.type === 'Attribute' && attribute.name === 'parameters') {
const { value } = attribute;

if (value === true || (Array.isArray(value) && value[0].type === 'Text')) continue; // WARN: Invalid syntax (shorthand or text expression), but lets move on
parameters = attribute;
continue;
}

if (attribute.type === 'Attribute' && attribute.name === 'tags') {
const { value } = attribute;
if (value === true || (Array.isArray(value) && value[0].type === 'Text')) continue; // WARN: Invalid syntax (shorthand or text expression), but lets move on
tags = attribute;
continue;
}
Expand Down Expand Up @@ -96,6 +89,10 @@ export function transformLegacyStory(params: Params): SvelteAST.Component {
});
}

if (tags) {
newAttributes.push(tags);
}

return {
...rest,
attributes: newAttributes,
Expand All @@ -116,21 +113,15 @@ function transformAutodocs(params: InsertAutodocsParams): void {
}

if (!tags) {
tags = createASTAttribute(
'tags',
createASTExpressionTag({
type: 'ArrayExpression',
elements: [],
})
);
tags = createASTAttribute('tags', createASTExpressionTag(createASTArrayExpression()));
}

const autodocsLiteral = {
type: 'Literal',
value: 'autodocs',
} satisfies ESTreeAST.Literal;

((tags?.value as SvelteAST.ExpressionTag).expression as ESTreeAST.ArrayExpression).elements.push(
((tags.value as SvelteAST.ExpressionTag).expression as ESTreeAST.ArrayExpression).elements.push(
autodocsLiteral
);

Expand Down

0 comments on commit 164e6e6

Please sign in to comment.