Skip to content

Commit

Permalink
fix(vue-extractor): don't crash when there is no <template> in an SFC (
Browse files Browse the repository at this point in the history
  • Loading branch information
Touffy authored Apr 27, 2023
1 parent 284255b commit 9e6ea70
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 deletions.
16 changes: 16 additions & 0 deletions packages/extractor-vue/src/__snapshots__/extractor.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`vue extractor should extract message from functional component 1`] = `
[
{
comment: undefined,
context: undefined,
id: Render function message,
message: undefined,
origin: [
functional.vue,
10,
33,
],
},
]
`;

exports[`vue extractor should extract message from vue file 1`] = `
[
{
Expand Down
22 changes: 22 additions & 0 deletions packages/extractor-vue/src/extractor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,26 @@ describe("vue extractor", () => {

expect(messages).toMatchSnapshot()
})

it("should extract message from functional component", async () => {
const filePath = path.resolve(__dirname, "fixtures/functional.vue")
const code = fs.readFileSync(filePath, "utf-8")

let messages: ExtractedMessage[] = []

await vueExtractor.extract(
"functional.vue",
code,
(res) => {
messages.push(res)
},
{
linguiConfig,
}
)

messages = normalizePath(messages)

expect(messages).toMatchSnapshot()
})
})
12 changes: 12 additions & 0 deletions packages/extractor-vue/src/fixtures/functional.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script lang="ts">
import { defineComponent } from "vue"
import { i18n } from "@lingui/core"
// @ts-ignore only used to check if extractor doesn't crash with Typescript
const foo: number = 5
export default defineComponent({
render(createElement) {
return createElement('div', [i18n._("Render function message")])
}})
</script>
14 changes: 8 additions & 6 deletions packages/extractor-vue/src/vue-extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ export const vueExtractor: ExtractorType = {
ignoreEmpty: true,
})

const compiledTemplate = compileTemplate({
source: descriptor.template.content,
filename,
inMap: descriptor.template.map,
id: filename,
})
const compiledTemplate =
descriptor.template &&
compileTemplate({
source: descriptor.template.content,
filename,
inMap: descriptor.template.map,
id: filename,
})

const isTsBlock = (block: SFCBlock) => block?.lang === "ts"

Expand Down

1 comment on commit 9e6ea70

@vercel
Copy link

@vercel vercel bot commented on 9e6ea70 Apr 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.