Skip to content

Commit

Permalink
fix(directives): fix parsing positions, close #3795
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed May 12, 2024
1 parent 9597923 commit 84170e1
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions packages/transformer-directives/src/apply.ts
Expand Up @@ -22,6 +22,7 @@ export async function handleApply(ctx: TransformerDirectivesContext, node: Rule)

export async function parseApply({ code, uno, offset, applyVariable }: TransformerDirectivesContext, node: Rule, childNode: CssNode) {
const calcOffset = (pos: number) => offset ? pos + offset : pos
const original = code.original

let body: string | undefined
if (childNode.type === 'Atrule' && childNode.name === 'apply' && childNode.prelude && childNode.prelude.type === 'Raw') {
Expand All @@ -31,10 +32,10 @@ export async function parseApply({ code, uno, offset, applyVariable }: Transform
else if (childNode!.type === 'Declaration' && applyVariable.includes(childNode.property) && childNode.value.type === 'Value') {
// Get raw value of the declaration
// as csstree would try to parse the content with operators, but we don't need them.
let rawValue = code.original.slice(
let rawValue = original.slice(
calcOffset(childNode.value.loc!.start.offset),
calcOffset(childNode.value.loc!.end.offset),
)
).trim()
rawValue = removeQuotes(rawValue)
const items = rawValue
.split(/\s+/g)
Expand Down Expand Up @@ -69,8 +70,8 @@ export async function parseApply({ code, uno, offset, applyVariable }: Transform

if (!utils.length)
return
const simicolonOffset = code.toString()[childNode.loc!.end.offset] === ';' ? 1 : 0

const simicolonOffset = original[calcOffset(childNode.loc!.end.offset)] === ';' ? 1 : 0
for (const i of utils) {
const [, _selector, body, parent] = i
const selectorOrGroup = _selector?.replace(regexScopePlaceholder, ' ') || _selector
Expand Down Expand Up @@ -108,7 +109,7 @@ export async function parseApply({ code, uno, offset, applyVariable }: Transform
else {
// If nested css was scoped, put them last.
if (body.includes('@'))
code.appendRight(code.original.length + simicolonOffset, body)
code.appendRight(original.length + simicolonOffset, body)
else
code.appendRight(calcOffset(childNode!.loc!.end.offset + simicolonOffset), body)
}
Expand Down

0 comments on commit 84170e1

Please sign in to comment.