Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions packages/astro/src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export class AstroTransformer extends Transformer {

initMixedVisitor = (): MixedVisitorAstro =>
new MixedVisitor({
mstr: this.mstr,
mod: this.mod,
vars: this.vars,
getRange: this.getRange,
isText: node => node.type === 'text',
Expand All @@ -145,9 +145,9 @@ export class AstroTransformer extends Transformer {
fullHeuristicDetails: this.fullHeuristicDetails,
checkHeuristic: this.getHeuristicMessageType,
index: this.index,
wrapNested: (msgInfo, hasExprs, nestedRanges, lastChildEnd) => {
wrapNested: (s, inCompoundTxt, msgInfo, hasExprs, nestedRanges, lastChildEnd) => {
let begin = `{${rtRenderFunc}({\nx: `
if (this.inCompoundText) {
if (inCompoundTxt) {
begin += `${this.vars().nestCtx},\nn: true`
} else {
const index = this.index.get(getKey(msgInfo.msgStr, msgInfo.context))
Expand All @@ -161,7 +161,7 @@ export class AstroTransformer extends Transformer {
} else {
toAppend = ', '
}
this.mstr.appendRight(childStart, `${toAppend}${haveCtx ? this.vars().nestCtx : '()'} => `)
s.appendRight(childStart, `${toAppend}${haveCtx ? this.vars().nestCtx : '()'} => `)
}
begin = `]`
}
Expand All @@ -170,17 +170,17 @@ export class AstroTransformer extends Transformer {
begin += ',\na: ['
end = `]${end}`
}
this.mstr.appendLeft(lastChildEnd, begin)
this.mstr.appendRight(lastChildEnd, end)
s.appendLeft(lastChildEnd, begin)
s.appendRight(lastChildEnd, end)
},
})

_parseAndVisitExpr = (expr: string, startOffset: number, asScript = false): Message[] => {
const [ast, comments] = (asScript ? parseScript : parseExpr)(expr)
this.comments = comments
this.mstr.offset = startOffset
this.mod.offset = startOffset
const msgs = this.visit(ast)
this.mstr.offset = 0 // restore
this.mod.offset = 0 // restore
return msgs
}

Expand Down Expand Up @@ -255,7 +255,7 @@ export class AstroTransformer extends Transformer {
if (!pass) {
return []
}
this.mstr.update(start, start + node.value.length + 2, `{${this.literalRepl(msgInfo)}}`)
this.mod.msg(msgInfo, s => s.update(start, start + node.value.length + 2, `{${this.literalRepl(msgInfo)}}`))
return [msgInfo]
}
if (node.kind === 'expression') {
Expand All @@ -281,7 +281,7 @@ export class AstroTransformer extends Transformer {
return []
}
const { start, end } = this.getRange(node)
this.mstr.update(start + startWh, end - endWh, `{${this.literalRepl(msgInfo)}}`)
this.mod.msg(msgInfo, s => s.update(start + startWh, end - endWh, `{${this.literalRepl(msgInfo)}}`))
return [msgInfo]
}

Expand All @@ -304,8 +304,10 @@ export class AstroTransformer extends Transformer {
const { ast } = await parse(this.content)
const msgs = this.visitAs(ast)
if (this.frontMatterStart == null) {
this.mstr.appendLeft(0, '---\n')
this.mstr.appendRight(0, '---\n')
this.mod.gen(s => {
s.appendLeft(0, '---\n')
s.appendRight(0, '---\n')
})
}
const header = [`import ${rtRenderFunc} from "@wuchale/astro/runtime.js"`, this.initRuntime()].join('\n')
return this.finalize(msgs, this.frontMatterStart ?? 0, header)
Expand Down
18 changes: 9 additions & 9 deletions packages/jsx/src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class JSXTransformer extends Transformer {

initMixedVisitor = (): MixedVisitorJSX =>
new MixedVisitor({
mstr: this.mstr,
mod: this.mod,
vars: this.vars,
getRange: node => ({
start: node.start,
Expand All @@ -83,7 +83,7 @@ export class JSXTransformer extends Transformer {
fullHeuristicDetails: this.fullHeuristicDetails,
checkHeuristic: this.getHeuristicMessageType,
index: this.index,
wrapNested: (msgInfo, hasExprs, nestedRanges, lastChildEnd) => {
wrapNested: (s, inCompoundText, msgInfo, hasExprs, nestedRanges, lastChildEnd) => {
let begin = `<${rtComponent}`
if (nestedRanges.length > 0) {
for (const [i, [childStart, _, haveCtx]] of nestedRanges.entries()) {
Expand All @@ -93,12 +93,12 @@ export class JSXTransformer extends Transformer {
} else {
toAppend = ', '
}
this.mstr.appendRight(childStart, `${toAppend}${haveCtx ? this.vars().nestCtx : '()'} => `)
s.appendRight(childStart, `${toAppend}${haveCtx ? this.vars().nestCtx : '()'} => `)
}
begin = `]}`
}
begin += ' x='
if (this.inCompoundText) {
if (inCompoundText) {
begin += `{${this.vars().nestCtx}} n`
} else {
const index = this.index.get(getKey(msgInfo.msgStr, msgInfo.context))
Expand All @@ -109,8 +109,8 @@ export class JSXTransformer extends Transformer {
begin += ' a={['
end = `]}${end}`
}
this.mstr.appendLeft(lastChildEnd, begin)
this.mstr.appendRight(lastChildEnd, end)
s.appendLeft(lastChildEnd, begin)
s.appendRight(lastChildEnd, end)
},
})

Expand Down Expand Up @@ -154,7 +154,7 @@ export class JSXTransformer extends Transformer {
attr => attr.type === 'JSXAttribute' && attr.name.name === 'key',
)
if (!key) {
this.mstr.appendLeft(node.openingElement.name.end, ` key="_${this.currentJsxKey}"`)
this.mod.group(msgs, s => s.appendLeft(node.openingElement.name.end, ` key="_${this.currentJsxKey}"`))
this.currentJsxKey++
}
}
Expand All @@ -171,7 +171,7 @@ export class JSXTransformer extends Transformer {
if (!pass) {
return []
}
this.mstr.update(node.start + startWh, node.end - endWh, `{${this.literalRepl(msgInfo)}}`)
this.mod.msg(msgInfo, s => s.update(node.start + startWh, node.end - endWh, `{${this.literalRepl(msgInfo)}}`))
return [msgInfo]
}

Expand Down Expand Up @@ -224,7 +224,7 @@ export class JSXTransformer extends Transformer {
if (!pass) {
return []
}
this.mstr.update(value.start, value.end, `{${this.literalRepl(msgInfo)}}`)
this.mod.msg(msgInfo, s => s.update(value.start, value.end, `{${this.literalRepl(msgInfo)}}`))
return [msgInfo]
}

Expand Down
Loading
Loading