From c8c47c179ef25a15273929f9415cea93a3b7cef6 Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Tue, 14 Sep 2021 13:54:02 +0200 Subject: [PATCH] [feat] add loose parsing mode Related to #4818 --- src/compiler/interfaces.ts | 2 ++ src/compiler/parse/index.ts | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/compiler/interfaces.ts b/src/compiler/interfaces.ts index 7446c4c14b57..aeb58ef35870 100644 --- a/src/compiler/interfaces.ts +++ b/src/compiler/interfaces.ts @@ -116,6 +116,7 @@ export interface Ast { css: Style; instance: Script; module: Script; + firstError?: any; } export interface Warning { @@ -170,6 +171,7 @@ export interface CompileOptions { export interface ParserOptions { filename?: string; customElement?: boolean; + errorMode?: 'throw' | 'warn' | 'loose'; } export interface Visitor { diff --git a/src/compiler/parse/index.ts b/src/compiler/parse/index.ts index 836fb670cfd5..26e4583dc0de 100644 --- a/src/compiler/parse/index.ts +++ b/src/compiler/parse/index.ts @@ -19,6 +19,7 @@ export class Parser { readonly template: string; readonly filename?: string; readonly customElement: boolean; + private readonly isLooseParsing: boolean; index = 0; stack: TemplateNode[] = []; @@ -27,6 +28,7 @@ export class Parser { css: Style[] = []; js: Script[] = []; meta_tags = {}; + firstError?: any; last_auto_closed_tag?: LastAutoClosedTag; constructor(template: string, options: ParserOptions) { @@ -37,6 +39,7 @@ export class Parser { this.template = template.replace(/\s+$/, ''); this.filename = options.filename; this.customElement = options.customElement; + this.isLooseParsing = options.errorMode === 'loose'; this.html = { start: null, @@ -98,13 +101,18 @@ export class Parser { } error({ code, message }: { code: string; message: string }, index = this.index) { - error(message, { + const errorProps = { name: 'ParseError', code, source: this.template, start: index, filename: this.filename - }); + }; + if (this.isLooseParsing) { + this.firstError = this.firstError || errorProps; + } else { + error(message, errorProps); + } } eat(str: string, required?: boolean, error?: { code: string, message: string }) { @@ -238,6 +246,7 @@ export default function parse( html: parser.html, css: parser.css[0], instance: instance_scripts[0], - module: module_scripts[0] + module: module_scripts[0], + firstError: parser.firstError }; }