Skip to content

Commit

Permalink
Fix vue 2.7 support (#173)
Browse files Browse the repository at this point in the history
Fixes #162

Older versions (not sure which ones exactly) of vue/compiler-sfc do not
include a `loc` property on each block, but rather give the offsets
directly on the block itself. This accounts for those differences, and
causes the reproduction provided in the issue to succeed.
  • Loading branch information
IanVS authored Jun 25, 2024
1 parent 920639a commit 9484685
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/preprocessors/vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,16 @@ export function vuePreprocessor(code: string, options: PrettierOptions) {
// https://github.com/vuejs/core/blob/b8fc18c0b23be9a77b05dc41ed452a87a0becf82/packages/compiler-core/src/ast.ts#L74-L80
// The node's range. The `start` is inclusive and `end` is exclusive.
// [start, end)
const { start, end } = block.loc;

// @ts-expect-error Some vue versions have a `block.loc`, others have start and end directly on the block
let { start, end } = block;
if ('loc' in block) {
start = block.loc.start.offset;
end = block.loc.end.offset;
}
const preprocessedBlockCode = sortScript(block, options);
result += code.slice(offset, start.offset) + preprocessedBlockCode;
offset = end.offset;
result += code.slice(offset, start) + preprocessedBlockCode;
offset = end;
}

// 4. Append the rest.
Expand All @@ -48,8 +54,8 @@ export function vuePreprocessor(code: string, options: PrettierOptions) {
console.warn(
'[@ianvs/prettier-plugin-sort-imports]: Could not process .vue file. Please be sure that "@vue/compiler-sfc" is installed in your project.',
);
throw err;
}
throw err;
}
}

Expand Down

0 comments on commit 9484685

Please sign in to comment.