Skip to content

Commit dd77a09

Browse files
committed
Reuse code for finding syntax errors
1 parent 63245e7 commit dd77a09

File tree

4 files changed

+37
-42
lines changed

4 files changed

+37
-42
lines changed

src/loader-query.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ export function parseVueRequest(id: string) {
1717
ret.type = params.get('type') as VueQuery['type']
1818

1919
if (params.has('blockType'))
20-
ret.blockType = params.get('blockType')
20+
ret.blockType = params.get('blockType') ?? undefined
2121

2222
if (params.has('index'))
2323
ret.index = Number(params.get('index'))
2424

2525
if (params.has('locale'))
26-
ret.locale = params.get('locale')
26+
ret.locale = params.get('locale') ?? undefined
2727

2828
return {
2929
filename,

src/plugins/external-plugin.ts

+10-24
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import MagicString from 'magic-string'
88
import { createUnplugin } from 'unplugin'
99
import { isCustomBlock, parseVueRequest } from '../loader-query'
1010
import { getInjectFtl } from './ftl/inject'
11-
import { getSyntaxErrors } from './ftl/parse'
1211

1312
const isVue = createFilter(['**/*.vue'])
1413
const isFtl = createFilter(['**/*.ftl'])
@@ -42,6 +41,7 @@ function isFluentCustomBlock(id: string) {
4241
export const unplugin = createUnplugin((options: ExternalPluginOptions) => {
4342
const resolvedOptions = {
4443
checkSyntax: true,
44+
parseFtl: false,
4545
virtualModuleName: 'virtual:ftl-for-file',
4646
getFtlPath: undefined as ((locale: string, vuePath: string) => string) | undefined,
4747
...options,
@@ -132,46 +132,32 @@ export const unplugin = createUnplugin((options: ExternalPluginOptions) => {
132132
}
133133

134134
if (isFtl(id)) {
135-
if (resolvedOptions.checkSyntax) {
136-
const errorsText = getSyntaxErrors(source)
137-
if (errorsText)
138-
this.error(errorsText)
139-
}
140-
141135
const injectFtl = getInjectFtl(resolvedOptions)
142-
143136
const result = injectFtl`
144137
export default /*#__PURE__*/ new FluentResource(${source})
145138
`
146139

147-
return {
148-
code: result.toString(),
149-
map: result.generateMap(),
150-
}
140+
if (result.error)
141+
this.error(result.error)
142+
143+
return result.code
151144
}
152145

153146
const query = parseVueRequest(id).query
154147
if (isFluentCustomBlock(id)) {
155-
if (options.checkSyntax) {
156-
const errorsText = getSyntaxErrors(source)
157-
if (errorsText)
158-
this.error(errorsText)
159-
}
160-
161148
const injectFtl = getInjectFtl(resolvedOptions)
162-
163-
const magic = injectFtl`
149+
const result = injectFtl`
164150
export default function (Component) {
165151
const target = Component.options || Component
166152
target.fluent = target.fluent || {}
167153
target.fluent['${query.locale}'] = ${source}
168154
}
169155
`
170156

171-
return {
172-
code: magic.toString(),
173-
map: magic.generateMap(),
174-
}
157+
if (result.error)
158+
this.error(result.error)
159+
160+
return result.code
175161
}
176162

177163
return undefined

src/plugins/ftl/inject.ts

+20-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { FluentResource } from '@fluent/bundle'
2-
import MagicString from 'magic-string'
2+
import MagicString, { type SourceMap } from 'magic-string'
3+
import { getSyntaxErrors } from './parse'
34

4-
type InjectFtlFn = (template: TemplateStringsArray, locale?: string, source?: string) => MagicString
5+
type InjectFtlFn = (template: TemplateStringsArray, locale?: string, source?: string) => { code?: { code: string, map: SourceMap }, error?: string }
56

67
function normalize(str: string) {
78
return str.replace(/\r\n/g, '\n').trim()
89
}
910

10-
export function getInjectFtl(options: { parseFtl?: boolean }): InjectFtlFn {
11+
export function getInjectFtl(options: { checkSyntax: boolean, parseFtl: boolean }): InjectFtlFn {
1112
return (template, locale, source) => {
1213
if (source == null) {
1314
source = locale
@@ -17,6 +18,16 @@ export function getInjectFtl(options: { parseFtl?: boolean }): InjectFtlFn {
1718
if (source == null)
1819
throw new Error('Missing source')
1920

21+
if (options.checkSyntax) {
22+
const errorsText = getSyntaxErrors(source.replace(/\r\n/g, '\n').trim())
23+
24+
if (errorsText) {
25+
return {
26+
error: errorsText,
27+
}
28+
}
29+
}
30+
2031
const magic = new MagicString(source)
2132
const importString = options.parseFtl === true ? '' : '\nimport { FluentResource } from \'@fluent/bundle\'\n'
2233
const localeString = locale == null ? '' : locale
@@ -36,6 +47,11 @@ export function getInjectFtl(options: { parseFtl?: boolean }): InjectFtlFn {
3647
if (template[2] != null)
3748
magic.append(template[2])
3849

39-
return magic
50+
return {
51+
code: {
52+
code: magic.toString(),
53+
map: magic.generateMap(),
54+
},
55+
}
4056
}
4157
}

src/plugins/sfc-plugin.ts

+5-12
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import { createUnplugin } from 'unplugin'
55

66
import { isCustomBlock, parseVueRequest } from '../loader-query'
77
import { getInjectFtl } from './ftl/inject'
8-
import { getSyntaxErrors } from './ftl/parse'
98

109
export const unplugin = createUnplugin((options: SFCPluginOptions, meta) => {
1110
const resolvedOptions = {
1211
blockType: 'fluent',
1312
checkSyntax: true,
13+
parseFtl: false,
1414
...options,
1515
}
1616

@@ -37,14 +37,7 @@ export const unplugin = createUnplugin((options: SFCPluginOptions, meta) => {
3737
if (source.includes('FluentResource') || source.includes('unplugin-fluent-vue-sfc') || source.includes('target.fluent'))
3838
return undefined
3939

40-
if (resolvedOptions.checkSyntax) {
41-
const errorsText = getSyntaxErrors(source.replace(/\r\n/g, '\n').trim())
42-
if (errorsText)
43-
this.error(errorsText)
44-
}
45-
4640
const injectFtl = getInjectFtl(resolvedOptions)
47-
4841
const result = injectFtl`
4942
export default function (Component) {
5043
const target = Component.options || Component
@@ -53,10 +46,10 @@ export default function (Component) {
5346
}
5447
`
5548

56-
return {
57-
code: result.toString(),
58-
map: result.generateMap(),
59-
}
49+
if (result.error)
50+
this.error(result.error)
51+
52+
return result.code
6053
},
6154
}
6255
})

0 commit comments

Comments
 (0)