Skip to content

Commit

Permalink
fix(vue-script-setup-converter): Components converter fails if dynami…
Browse files Browse the repository at this point in the history
…c imports contain newlines (#60)

* fix typo

* test: Conversion fails if dynamic imports contain newlines

* fix: Conversion fails if dynamic imports contain newlines

* Revert "fix: Conversion fails if dynamic imports contain newlines"

This reverts commit 8bdd5b4.

* fix: Conversion fails if dynamic imports contain newlines

* test: Add test case which dynamic imports contain newlines

* The insertion of defineAsyncComponent should be done before definePageMeta because defineAsyncComponent is similar to an import declaration
  • Loading branch information
inouetakuya authored May 21, 2024
1 parent 323b7f1 commit 071c5a2
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packages/vue-script-setup-converter/src/lib/convertSrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ export const convertSrc = (input: string) => {
})
);

statements.addStatements(components);

if (isDefineNuxtComponent(callexpression)) {
statements.addStatements(pageMeta);
}

statements.addStatements(components);

statements.addStatements(props);
statements.addStatements(emits);
statements.addStatements(statement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ test("should be converted to defineAsyncComponent", () => {
components: {
HelloWorld,
MyComp: () => import('./MyComp.vue'),
Foo: () => import('./Foo.vue'),
Foo: () =>
import('./Foo.vue'),
}
})
`;
Expand All @@ -44,7 +45,8 @@ test("should be converted to defineAsyncComponent", () => {
expect(output).toMatchInlineSnapshot(
`
"const MyComp = defineAsyncComponent(() => import('./MyComp.vue'))
const Foo = defineAsyncComponent(() => import('./Foo.vue'))"
const Foo = defineAsyncComponent(() =>
import('./Foo.vue'))"
`
);
});
Expand All @@ -58,14 +60,16 @@ test("should be output as is", () => {
components: {
HelloWorld,
MyComp: defineAsyncComponent(() => import('./MyComp.vue')),
Foo: defineAsyncComponent(() => import('./Foo.vue')),
Foo: defineAsyncComponent(() =>
import('./Foo.vue')),
}
})
`;
const output = parseScript(source);

expect(output).toMatchInlineSnapshot(`
"const MyComp = defineAsyncComponent(() => import('./MyComp.vue'))
const Foo = defineAsyncComponent(() => import('./Foo.vue'))"
const Foo = defineAsyncComponent(() =>
import('./Foo.vue'))"
`);
});
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ const convertToDefineAsyncComponent = (node: PropertyAssignment) => {

const properties = child.getProperties();

const filterdProperties = properties.filter(filterDynamicImport);
const filteredProperties = properties.filter(filterDynamicImport);

if (filterdProperties.length === 0) return "";
if (filteredProperties.length === 0) return "";

const value = filterdProperties
const value = filteredProperties
.map((x) => {
const propertyName = x.getName();
const initializer = x.getInitializer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ export const filterDynamicImport = (
};

export const hasDynamicImport = (node: ObjectLiteralElementLike) => {
return node.getText().includes("() => import(");
return node.getText().includes("import(");
};

0 comments on commit 071c5a2

Please sign in to comment.