Skip to content

Commit

Permalink
Replace estree-util-to-js with esrap
Browse files Browse the repository at this point in the history
  • Loading branch information
xeho91 committed Jun 19, 2024
1 parent 3152066 commit 4c21eb4
Show file tree
Hide file tree
Showing 14 changed files with 202 additions and 265 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
},
"dependencies": {
"dedent": "^1.5.3",
"estree-util-to-js": "^2.0.0",
"esrap": "^1.2.2",
"magic-string": "^0.30.10",
"zimmerframe": "^1.1.2"
},
Expand Down
34 changes: 3 additions & 31 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions src/compiler/transform/appendix/create-export-default.test.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import type { Program } from 'estree';
import { toJs } from 'estree-util-to-js';
import { print } from 'esrap';
import { describe, it } from 'vitest';

import { createExportDefaultMeta } from './create-export-default';

describe(createExportDefaultMeta.name, () => {
it('creates a new export default correctly', ({ expect }) => {
const stringified = toJs(
const stringified = print(
createExportDefaultMeta({
metaIdentifier: {
type: 'Identifier',
name: 'meta',
},
}) as unknown as Program
).value;
).code;

expect(stringified).toMatchInlineSnapshot(`"export default meta;"`);
});

it("works when 'meta' identifier was destructured manually and renamed by user", ({ expect }) => {
const stringified = toJs(
const stringified = print(
createExportDefaultMeta({
metaIdentifier: {
type: 'Identifier',
name: '__renamed_meta',
},
}) as unknown as Program
).value;
).code;

expect(stringified).toMatchInlineSnapshot(`"export default __renamed_meta;"`);
});
Expand Down
17 changes: 11 additions & 6 deletions src/compiler/transform/appendix/create-export-order.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
import type { Program } from 'estree';
import { toJs } from 'estree-util-to-js';
import { print } from 'esrap';
import { describe, it } from 'vitest';

import { createExportOrderVariable } from './create-export-order';

describe(createExportOrderVariable.name, () => {
it('correctly creates a variable with named exports order', ({ expect }) => {
const stringified = toJs(
const stringified = print(
createExportOrderVariable({
storyIdentifiers: [
{ exportName: 'Default', name: 'Default' },
{ exportName: 'SomeComponent', name: 'Some Component' },
{ exportName: 'ThisNameIsWeird', name: 'This-Name-Is-Weird' },
],
}) as unknown as Program
).value;
})
).code;

expect(stringified).toMatchInlineSnapshot(
`"export const __namedExportsOrder = ["Default", "SomeComponent", "ThisNameIsWeird"];"`
`
"export const __namedExportsOrder = [
"Default",
"SomeComponent",
"ThisNameIsWeird"
];"
`
);
});
});
7 changes: 3 additions & 4 deletions src/compiler/transform/appendix/create-import.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import type { Program } from 'estree';
import { toJs } from 'estree-util-to-js';
import { print } from 'esrap';
import { describe, it } from 'vitest';

import { createRuntimeStoriesImport } from './create-import';

describe(createRuntimeStoriesImport.name, () => {
it('creates import correctly', ({ expect }) => {
const stringified = toJs(createRuntimeStoriesImport() as unknown as Program).value;
const stringified = print(createRuntimeStoriesImport()).code;

expect(stringified).toMatchInlineSnapshot(
`"import {createRuntimeStories} from \"@storybook/addon-svelte-csf/internal/create-runtime-stories\";"`
`"import { createRuntimeStories } from "@storybook/addon-svelte-csf/internal/create-runtime-stories";"`
);
});
});
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { Program } from 'estree';
import { toJs } from 'estree-util-to-js';
import { print } from 'esrap';
import { describe, it } from 'vitest';

import { createNamedExportStory } from './create-named-export-story';
import { createVariableFromRuntimeStoriesCall } from './create-variable-from-runtime-stories-call';

describe(createNamedExportStory.name, () => {
it('correctly creates a variable with named exports order', ({ expect }) => {
const stringified = toJs(
const stringified = print(
createNamedExportStory({
exportName: 'Default',
node: createVariableFromRuntimeStoriesCall({
Expand All @@ -29,7 +29,7 @@ describe(createNamedExportStory.name, () => {
},
}),
}) as unknown as Program
).value;
).code;

expect(stringified).toMatchInlineSnapshot(`"export const Default = __stories["Default"];"`);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import type { Program } from 'estree';
import { toJs } from 'estree-util-to-js';
import { print } from 'esrap';
import { describe, it } from 'vitest';

import { createVariableFromRuntimeStoriesCall } from './create-variable-from-runtime-stories-call';

describe(createVariableFromRuntimeStoriesCall.name, () => {
it('creates a variable correctly', ({ expect }) => {
const stringified = toJs(
const stringified = print(
createVariableFromRuntimeStoriesCall({
metaIdentifier: {
type: 'Identifier',
Expand All @@ -24,8 +23,8 @@ describe(createVariableFromRuntimeStoriesCall.name, () => {
},
params: [],
},
}) as unknown as Program
).value;
})
).code;

expect(stringified).toMatchInlineSnapshot(
`"const __stories = createRuntimeStories(Example_stories, meta);"`
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/transform/create-appendix.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { print } from 'esrap';
import MagicString from 'magic-string';
import { toJs } from 'estree-util-to-js';

import { createExportDefaultMeta } from './appendix/create-export-default';
import { createExportOrderVariable } from './appendix/create-export-order';
Expand Down Expand Up @@ -47,7 +47,7 @@ export async function createAppendix(params: Params) {
})
);

const appendix = toJs({
const appendix = print({
type: 'Program',
sourceType: 'module',
body: [
Expand All @@ -59,5 +59,5 @@ export async function createAppendix(params: Params) {
],
});

code.append('\n' + appendix.value);
code.append('\n' + appendix.code);
}
41 changes: 20 additions & 21 deletions src/compiler/transform/define-meta/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import fs from 'node:fs';
import path from 'node:path';
import url from 'node:url';

import type { Program } from 'estree';
import { toJs } from 'estree-util-to-js';
import { print } from 'esrap';
import MagicString from 'magic-string';
import { parseAst } from 'rollup/parseAst';
import { describe, it } from 'vitest';
Expand Down Expand Up @@ -52,24 +51,24 @@ describe(transformDefineMeta.name, () => {
ast: parseAst(code.toString()),
});

expect(toJs(defineMetaVariableDeclaration as unknown as Program).value).toMatchInlineSnapshot(`
"const {Story, meta} = defineMeta({
title: 'Example',
component: Example,
tags: ['autodocs'],
args: {
onclick: action('onclick'),
onmouseenter: action('onmouseenter'),
onmouseleave: action('onmouseleave')
},
parameters: {
docs: {
description: {
component: "Description set explicitly in the comment above \`defineMeta\`.\\n\\nMultiline supported. And also Markdown syntax:\\n\\n* **Bold**,\\n* _Italic_,\\n* \`Code\`."
}
}
}
});"
`);
expect(print(defineMetaVariableDeclaration).code).toMatchInlineSnapshot(`
"const { Story, meta } = defineMeta({
title: 'Example',
component: Example,
tags: ['autodocs'],
args: {
onclick: action('onclick'),
onmouseenter: action('onmouseenter'),
onmouseleave: action('onmouseleave')
},
parameters: {
docs: {
description: {
component: "Description set explicitly in the comment above \`defineMeta\`.\\n\\nMultiline supported. And also Markdown syntax:\\n\\n* **Bold**,\\n* _Italic_,\\n* \`Code\`."
}
}
}
});"
`);
});
});
9 changes: 2 additions & 7 deletions src/compiler/transform/define-meta/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Program } from 'estree';
import { toJs } from 'estree-util-to-js';
import { print } from 'esrap';
import type MagicString from 'magic-string';

import { destructureMetaFromDefineMeta } from './destructure-meta';
Expand Down Expand Up @@ -37,9 +36,5 @@ export function transformDefineMeta(params: Params): void {
const { defineMetaVariableDeclaration } = compiled;
const { start, end } = defineMetaVariableDeclaration;

code.update(
start as number,
end as number,
toJs(defineMetaVariableDeclaration as unknown as Program).value
);
code.update(start as number, end as number, print(defineMetaVariableDeclaration).code);
}
Loading

0 comments on commit 4c21eb4

Please sign in to comment.