From f5dfa5fffbb5f7d963b12a83637d9d0e85ba81ff Mon Sep 17 00:00:00 2001 From: Ian VanSchooten Date: Tue, 12 Mar 2024 10:05:09 -0400 Subject: [PATCH] Support vue 2.7 (#156) Pulls in https://github.com/trivago/prettier-plugin-sort-imports/pull/215 from upstream, to add support for Vue 2.7, which as far as I can tell still has ~1 million weekly downloads. Co-authored-by: Aleksei Nagovitsyn --- package.json | 2 +- src/preprocessors/vue.ts | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 0e5acf6..50189f4 100644 --- a/package.json +++ b/package.json @@ -76,7 +76,7 @@ "vite": "^4.0.0" }, "peerDependencies": { - "@vue/compiler-sfc": ">=3.0.0", + "@vue/compiler-sfc": "2.7.x || 3.x", "prettier": "2 || 3" }, "peerDependenciesMeta": { diff --git a/src/preprocessors/vue.ts b/src/preprocessors/vue.ts index 23e8348..62435f2 100644 --- a/src/preprocessors/vue.ts +++ b/src/preprocessors/vue.ts @@ -1,4 +1,4 @@ -import type { parse as Parse } from '@vue/compiler-sfc'; +import type { parse as Parse, SFCDescriptor } from '@vue/compiler-sfc'; import { ImportOrderParserPlugin } from '../../types'; import { PrettierOptions } from '../types'; @@ -7,8 +7,12 @@ import { preprocessor } from './preprocessor'; export function vuePreprocessor(code: string, options: PrettierOptions) { try { - const { parse }: { parse: typeof Parse } = require('@vue/compiler-sfc'); - const { descriptor } = parse(code); + const { parse, transformRef } = require('@vue/compiler-sfc'); + const descriptor: SFCDescriptor = transformRef + ? // @vue/compiler-sfc 3.x + parse(code).descriptor + : // @vue/compiler-sfc 2.7.x + parse({ source: code }); // 1. Filter valid blocks. const blocks = [descriptor.script, descriptor.scriptSetup].filter(