Просмотр исходного кода

fix: should be able to parse decorators in script lang="ts" & jsx (#2088)

* fix: should be able to parse decorators in script lang="ts"

* fix: should also support parsing jsx

Added to `compileScript` instead of `babelParserDefaultPlugins` because
it's not needed for template expression parsing
Haoqun Jiang 5 лет назад
Родитель
Сommit
273d19ad46

+ 16 - 0
packages/compiler-sfc/__tests__/compileScript.spec.ts

@@ -520,6 +520,22 @@ describe('SFC compile <script setup>', () => {
 })
 })
 
 
 describe('SFC analyze <script> bindings', () => {
 describe('SFC analyze <script> bindings', () => {
+  it('can parse decorators syntax in typescript block', () => {
+    const { scriptAst } = compile(`
+      <script lang="ts">
+        import { Options, Vue } from 'vue-class-component';
+        @Options({
+          components: {
+            HelloWorld,
+          },
+          props: ['foo', 'bar']
+        })
+        export default class Home extends Vue {}
+      </script>
+    `)
+
+    expect(scriptAst).toBeDefined()
+  })
   it('recognizes props array declaration', () => {
   it('recognizes props array declaration', () => {
     const { bindings } = compile(`
     const { bindings } = compile(`
       <script>
       <script>

+ 2 - 2
packages/compiler-sfc/src/compileScript.ts

@@ -60,9 +60,9 @@ export function compileScript(
   const scriptLang = script && script.lang
   const scriptLang = script && script.lang
   const scriptSetupLang = scriptSetup && scriptSetup.lang
   const scriptSetupLang = scriptSetup && scriptSetup.lang
   const isTS = scriptLang === 'ts' || scriptSetupLang === 'ts'
   const isTS = scriptLang === 'ts' || scriptSetupLang === 'ts'
-  const plugins: ParserPlugin[] = [...babelParserDefaultPlugins]
+  const plugins: ParserPlugin[] = [...babelParserDefaultPlugins, 'jsx']
   if (options.babelParserPlugins) plugins.push(...options.babelParserPlugins)
   if (options.babelParserPlugins) plugins.push(...options.babelParserPlugins)
-  if (isTS) plugins.push('typescript')
+  if (isTS) plugins.push('typescript', 'decorators-legacy')
 
 
   if (!scriptSetup) {
   if (!scriptSetup) {
     if (!script) {
     if (!script) {