Skip to content

Commit

Permalink
Detect vue 2.7 / 3 more reliably (#160)
Browse files Browse the repository at this point in the history
Fixes #158

The `transformRef` method is not found on the most recent versions of
@vue/compiler-sfc (3.4.x), because it seems to have only existed for
some versions of 3.x (including the version we use in devDependencies,
which is why our tests passed). I couldn't find any mention of it in the
Vue changelog, though it seems like it should have been a breaking
change to remove the method.

So, now instead of relying on the presence of a method, we'll import the
package.json directly, look at the version field, and make our decision
based on that, which should be much safer.
  • Loading branch information
IanVS committed Mar 17, 2024
1 parent 0ffe11e commit d97c9e4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 56 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -66,7 +66,7 @@
"@types/node": "^18.15.13",
"@types/prettier": "^3.0.0",
"@types/semver": "^7.5.3",
"@vue/compiler-sfc": "3.3.4",
"@vue/compiler-sfc": "3.4.21",
"cross-env": "^7.0.3",
"prettier": "^3.0.3",
"typescript": "5.2.2",
Expand Down
13 changes: 6 additions & 7 deletions src/preprocessors/vue.ts
@@ -1,4 +1,4 @@
import type { parse as Parse, SFCDescriptor } from '@vue/compiler-sfc';
import type { SFCDescriptor } from '@vue/compiler-sfc';

import { ImportOrderParserPlugin } from '../../types';
import { PrettierOptions } from '../types';
Expand All @@ -7,12 +7,11 @@ import { preprocessor } from './preprocessor';

export function vuePreprocessor(code: string, options: PrettierOptions) {
try {
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 });
const { parse } = require('@vue/compiler-sfc');
const version =
require('@vue/compiler-sfc/package.json').version?.split('.')[0];
const descriptor: SFCDescriptor =
version === '2' ? parse({ source: code }) : parse(code).descriptor;

// 1. Filter valid blocks.
const blocks = [descriptor.script, descriptor.scriptSetup].filter(
Expand Down
90 changes: 42 additions & 48 deletions yarn.lock
Expand Up @@ -150,7 +150,7 @@
chalk "^2.4.2"
js-tokens "^4.0.0"

"@babel/parser@^7.20.15", "@babel/parser@^7.21.3", "@babel/parser@^7.24.0":
"@babel/parser@^7.23.9", "@babel/parser@^7.24.0":
version "7.24.0"
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.24.0.tgz#26a3d1ff49031c53a97d03b604375f028746a9ac"
integrity sha512-QuP/FxEAzMSjXygs8v4N9dvdXzEHN4W1oF3PxuWAtPo08UdM17u89RDMgjLn/mlc56iM0HlLmVkO/wgR+rDgHg==
Expand Down Expand Up @@ -438,63 +438,52 @@
loupe "^2.3.6"
pretty-format "^29.5.0"

"@vue/compiler-core@3.3.4":
version "3.3.4"
resolved "https://registry.yarnpkg.com/@vue/compiler-core/-/compiler-core-3.3.4.tgz#7fbf591c1c19e1acd28ffd284526e98b4f581128"
integrity sha512-cquyDNvZ6jTbf/+x+AgM2Arrp6G4Dzbb0R64jiG804HRMfRiFXWI6kqUVqZ6ZR0bQhIoQjB4+2bhNtVwndW15g==
"@vue/compiler-core@3.4.21":
version "3.4.21"
resolved "https://registry.yarnpkg.com/@vue/compiler-core/-/compiler-core-3.4.21.tgz#868b7085378fc24e58c9aed14c8d62110a62be1a"
integrity sha512-MjXawxZf2SbZszLPYxaFCjxfibYrzr3eYbKxwpLR9EQN+oaziSu3qKVbwBERj1IFIB8OLUewxB5m/BFzi613og==
dependencies:
"@babel/parser" "^7.21.3"
"@vue/shared" "3.3.4"
"@babel/parser" "^7.23.9"
"@vue/shared" "3.4.21"
entities "^4.5.0"
estree-walker "^2.0.2"
source-map-js "^1.0.2"

"@vue/compiler-dom@3.3.4":
version "3.3.4"
resolved "https://registry.yarnpkg.com/@vue/compiler-dom/-/compiler-dom-3.3.4.tgz#f56e09b5f4d7dc350f981784de9713d823341151"
integrity sha512-wyM+OjOVpuUukIq6p5+nwHYtj9cFroz9cwkfmP9O1nzH68BenTTv0u7/ndggT8cIQlnBeOo6sUT/gvHcIkLA5w==
"@vue/compiler-dom@3.4.21":
version "3.4.21"
resolved "https://registry.yarnpkg.com/@vue/compiler-dom/-/compiler-dom-3.4.21.tgz#0077c355e2008207283a5a87d510330d22546803"
integrity sha512-IZC6FKowtT1sl0CR5DpXSiEB5ayw75oT2bma1BEhV7RRR1+cfwLrxc2Z8Zq/RGFzJ8w5r9QtCOvTjQgdn0IKmA==
dependencies:
"@vue/compiler-core" "3.3.4"
"@vue/shared" "3.3.4"
"@vue/compiler-core" "3.4.21"
"@vue/shared" "3.4.21"

"@vue/compiler-sfc@3.3.4":
version "3.3.4"
resolved "https://registry.yarnpkg.com/@vue/compiler-sfc/-/compiler-sfc-3.3.4.tgz#b19d942c71938893535b46226d602720593001df"
integrity sha512-6y/d8uw+5TkCuzBkgLS0v3lSM3hJDntFEiUORM11pQ/hKvkhSKZrXW6i69UyXlJQisJxuUEJKAWEqWbWsLeNKQ==
"@vue/compiler-sfc@3.4.21":
version "3.4.21"
resolved "https://registry.yarnpkg.com/@vue/compiler-sfc/-/compiler-sfc-3.4.21.tgz#4af920dc31ab99e1ff5d152b5fe0ad12181145b2"
integrity sha512-me7epoTxYlY+2CUM7hy9PCDdpMPfIwrOvAXud2Upk10g4YLv9UBW7kL798TvMeDhPthkZ0CONNrK2GoeI1ODiQ==
dependencies:
"@babel/parser" "^7.20.15"
"@vue/compiler-core" "3.3.4"
"@vue/compiler-dom" "3.3.4"
"@vue/compiler-ssr" "3.3.4"
"@vue/reactivity-transform" "3.3.4"
"@vue/shared" "3.3.4"
"@babel/parser" "^7.23.9"
"@vue/compiler-core" "3.4.21"
"@vue/compiler-dom" "3.4.21"
"@vue/compiler-ssr" "3.4.21"
"@vue/shared" "3.4.21"
estree-walker "^2.0.2"
magic-string "^0.30.0"
postcss "^8.1.10"
magic-string "^0.30.7"
postcss "^8.4.35"
source-map-js "^1.0.2"

"@vue/compiler-ssr@3.3.4":
version "3.3.4"
resolved "https://registry.yarnpkg.com/@vue/compiler-ssr/-/compiler-ssr-3.3.4.tgz#9d1379abffa4f2b0cd844174ceec4a9721138777"
integrity sha512-m0v6oKpup2nMSehwA6Uuu+j+wEwcy7QmwMkVNVfrV9P2qE5KshC6RwOCq8fjGS/Eak/uNb8AaWekfiXxbBB6gQ==
"@vue/compiler-ssr@3.4.21":
version "3.4.21"
resolved "https://registry.yarnpkg.com/@vue/compiler-ssr/-/compiler-ssr-3.4.21.tgz#b84ae64fb9c265df21fc67f7624587673d324fef"
integrity sha512-M5+9nI2lPpAsgXOGQobnIueVqc9sisBFexh5yMIMRAPYLa7+5wEJs8iqOZc1WAa9WQbx9GR2twgznU8LTIiZ4Q==
dependencies:
"@vue/compiler-dom" "3.3.4"
"@vue/shared" "3.3.4"
"@vue/compiler-dom" "3.4.21"
"@vue/shared" "3.4.21"

"@vue/reactivity-transform@3.3.4":
version "3.3.4"
resolved "https://registry.yarnpkg.com/@vue/reactivity-transform/-/reactivity-transform-3.3.4.tgz#52908476e34d6a65c6c21cd2722d41ed8ae51929"
integrity sha512-MXgwjako4nu5WFLAjpBnCj/ieqcjE2aJBINUNQzkZQfzIZA4xn+0fV1tIYBJvvva3N3OvKGofRLvQIwEQPpaXw==
dependencies:
"@babel/parser" "^7.20.15"
"@vue/compiler-core" "3.3.4"
"@vue/shared" "3.3.4"
estree-walker "^2.0.2"
magic-string "^0.30.0"

"@vue/shared@3.3.4":
version "3.3.4"
resolved "https://registry.yarnpkg.com/@vue/shared/-/shared-3.3.4.tgz#06e83c5027f464eef861c329be81454bc8b70780"
integrity sha512-7OjdcV8vQ74eiz1TZLzZP4JwqM5fA94K6yntPS5Z25r9HDuGNzaGdgvwKYq6S+MxwF0TFRwe50fIR/MYnakdkQ==
"@vue/shared@3.4.21":
version "3.4.21"
resolved "https://registry.yarnpkg.com/@vue/shared/-/shared-3.4.21.tgz#de526a9059d0a599f0b429af7037cd0c3ed7d5a1"
integrity sha512-PuJe7vDIi6VYSinuEbUIQgMIRZGgM8e4R+G+/dQTk0X1NEdvgvvgv7m+rfmDH1gZzyA1OjjoWskvHlfRNfQf3g==

acorn-walk@^8.2.0:
version "8.3.2"
Expand Down Expand Up @@ -629,6 +618,11 @@ electron-to-chromium@^1.4.668:
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.699.tgz#dd53c939e13da64e94b341e563f0a3011b4ef0e9"
integrity sha512-I7q3BbQi6e4tJJN5CRcyvxhK0iJb34TV8eJQcgh+fR2fQ8miMgZcEInckCo1U9exDHbfz7DLDnFn8oqH/VcRKw==

entities@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/entities/-/entities-4.5.0.tgz#5d268ea5e7113ec74c4d033b79ea5a35a488fb48"
integrity sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw==

esbuild@^0.18.10:
version "0.18.20"
resolved "https://registry.yarnpkg.com/esbuild/-/esbuild-0.18.20.tgz#4709f5a34801b43b799ab7d6d82f7284a9b7a7a6"
Expand Down Expand Up @@ -748,7 +742,7 @@ lru-cache@^6.0.0:
dependencies:
yallist "^4.0.0"

magic-string@^0.30.0, magic-string@^0.30.1:
magic-string@^0.30.1, magic-string@^0.30.7:
version "0.30.8"
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.30.8.tgz#14e8624246d2bedba70d5462aa99ac9681844613"
integrity sha512-ISQTe55T2ao7XtlAStud6qwYPZjE4GK1S/BeVPus4jrq6JuOnQ00YKQC581RWhR122W7msZV263KzVeLoqidyQ==
Expand Down Expand Up @@ -816,7 +810,7 @@ pkg-types@^1.0.3:
mlly "^1.2.0"
pathe "^1.1.0"

postcss@^8.1.10, postcss@^8.4.27:
postcss@^8.4.27, postcss@^8.4.35:
version "8.4.35"
resolved "https://registry.yarnpkg.com/postcss/-/postcss-8.4.35.tgz#60997775689ce09011edf083a549cea44aabe2f7"
integrity sha512-u5U8qYpBCpN13BsiEB0CbR1Hhh4Gc0zLFuedrHJKMctHCHAGrMdG0PRM/KErzAL3CU6/eckEtmHNB3x6e3c0vA==
Expand Down

0 comments on commit d97c9e4

Please sign in to comment.