Skip to content

Commit 717e10c

Browse files
author
Simon Holthausen
committed
(feat) add loose parsing mode
Related to #4818
1 parent c040f13 commit 717e10c

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/compiler/interfaces.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ export interface Ast {
116116
css: Style;
117117
instance: Script;
118118
module: Script;
119+
firstError?: any;
119120
}
120121

121122
export interface Warning {
@@ -170,6 +171,7 @@ export interface CompileOptions {
170171
export interface ParserOptions {
171172
filename?: string;
172173
customElement?: boolean;
174+
errorMode?: 'throw' | 'warn' | 'loose';
173175
}
174176

175177
export interface Visitor {

src/compiler/parse/index.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export class Parser {
1919
readonly template: string;
2020
readonly filename?: string;
2121
readonly customElement: boolean;
22+
private readonly isLooseParsing: boolean;
2223

2324
index = 0;
2425
stack: TemplateNode[] = [];
@@ -27,6 +28,7 @@ export class Parser {
2728
css: Style[] = [];
2829
js: Script[] = [];
2930
meta_tags = {};
31+
firstError?: any;
3032
last_auto_closed_tag?: LastAutoClosedTag;
3133

3234
constructor(template: string, options: ParserOptions) {
@@ -37,6 +39,7 @@ export class Parser {
3739
this.template = template.replace(/\s+$/, '');
3840
this.filename = options.filename;
3941
this.customElement = options.customElement;
42+
this.isLooseParsing = options.errorMode === 'loose';
4043

4144
this.html = {
4245
start: null,
@@ -98,13 +101,18 @@ export class Parser {
98101
}
99102

100103
error({ code, message }: { code: string; message: string }, index = this.index) {
101-
error(message, {
104+
const errorProps = {
102105
name: 'ParseError',
103106
code,
104107
source: this.template,
105108
start: index,
106109
filename: this.filename
107-
});
110+
};
111+
if (this.isLooseParsing) {
112+
this.firstError = this.firstError = errorProps;
113+
} else {
114+
error(message, errorProps);
115+
}
108116
}
109117

110118
eat(str: string, required?: boolean, error?: { code: string, message: string }) {
@@ -238,6 +246,7 @@ export default function parse(
238246
html: parser.html,
239247
css: parser.css[0],
240248
instance: instance_scripts[0],
241-
module: module_scripts[0]
249+
module: module_scripts[0],
250+
firstError: parser.firstError
242251
};
243252
}

0 commit comments

Comments
 (0)