Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix vue 2.7 support #173

Merged
merged 3 commits into from
Jun 25, 2024
Merged
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
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;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it appropriate to always rethrow the error? In what cases do we not want to log the warning but still rethrow?

Copy link
Owner Author

Choose a reason for hiding this comment

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

The warning is an attempt to help guide the user towards a solution. It detects when there is a MODULE_NOT_FOUND, and takes a guess that it's probably a missing dependency, so it gives that tip.

I think it was just a mistake that it swallowed the error in the past, rather than re-throwing it.

}
}

Expand Down
Loading