]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
feat(compiler-sfc): support passing template parsing options when parsing sfc
authorEvan You <yyx990803@gmail.com>
Sat, 30 Dec 2023 00:42:43 +0000 (08:42 +0800)
committerEvan You <yyx990803@gmail.com>
Sat, 30 Dec 2023 00:44:39 +0000 (08:44 +0800)
- This is released in a patch because it is an relatively internal API
  but required to properly fix https://github.com/vitejs/vite-plugin-vue/issues/322

- `parseExpressions` is now deprecated because it can be passed using
  original template parsing options (`prefixIdentifiers`)

packages/compiler-sfc/__tests__/parse.spec.ts
packages/compiler-sfc/src/parse.ts

index e650a850abd803b8b05942577e41a8c335eb3fda..3d5ec8604b9e886d0e361af8cfac03dab6f2a254 100644 (file)
@@ -1,5 +1,10 @@
 import { parse } from '../src'
-import { baseCompile, createRoot } from '@vue/compiler-core'
+import {
+  ElementTypes,
+  NodeTypes,
+  baseCompile,
+  createRoot,
+} from '@vue/compiler-core'
 import { SourceMapConsumer } from 'source-map-js'
 
 describe('compiler:sfc', () => {
@@ -350,6 +355,20 @@ h1 { color: red }
     expect(descriptor.customBlocks[0].content).toBe(` <-& `)
   })
 
+  test('should accept parser options', () => {
+    const { errors, descriptor } = parse(`<template><hello/></template>`, {
+      templateParseOptions: {
+        isCustomElement: t => t === 'hello',
+      },
+    })
+    expect(errors.length).toBe(0)
+    expect(descriptor.template!.ast!.children[0]).toMatchObject({
+      type: NodeTypes.ELEMENT,
+      tag: 'hello',
+      tagType: ElementTypes.ELEMENT,
+    })
+  })
+
   describe('warnings', () => {
     function assertWarning(errors: Error[], msg: string) {
       expect(errors.some(e => e.message.match(msg))).toBe(true)
index 0c891e8215909e7975e508946f97fe1d21a7dd08..0acceb1fabaa836c7602db9f6dfc564e8c186629 100644 (file)
@@ -3,6 +3,7 @@ import {
   type CompilerError,
   type ElementNode,
   NodeTypes,
+  type ParserOptions,
   type RootNode,
   type SourceLocation,
   createRoot,
@@ -24,6 +25,11 @@ export interface SFCParseOptions {
   pad?: boolean | 'line' | 'space'
   ignoreEmpty?: boolean
   compiler?: TemplateCompiler
+  templateParseOptions?: ParserOptions
+  /**
+   * TODO remove in 3.5
+   * @deprecated use `templateParseOptions: { prefixIdentifiers: false }` instead
+   */
   parseExpressions?: boolean
 }
 
@@ -106,6 +112,7 @@ export function parse(
     pad = false,
     ignoreEmpty = true,
     compiler = CompilerDOM,
+    templateParseOptions = {},
     parseExpressions = true,
   }: SFCParseOptions = {},
 ): SFCParseResult {
@@ -133,6 +140,7 @@ export function parse(
   const ast = compiler.parse(source, {
     parseMode: 'sfc',
     prefixIdentifiers: parseExpressions,
+    ...templateParseOptions,
     onError: e => {
       errors.push(e)
     },