[feat] add loose parsing mode

Related to #4818
pull/6728/head
Simon Holthausen 5 years ago
parent c040f130b7
commit c8c47c179e

@ -116,6 +116,7 @@ export interface Ast {
css: Style; css: Style;
instance: Script; instance: Script;
module: Script; module: Script;
firstError?: any;
} }
export interface Warning { export interface Warning {
@ -170,6 +171,7 @@ export interface CompileOptions {
export interface ParserOptions { export interface ParserOptions {
filename?: string; filename?: string;
customElement?: boolean; customElement?: boolean;
errorMode?: 'throw' | 'warn' | 'loose';
} }
export interface Visitor { export interface Visitor {

@ -19,6 +19,7 @@ export class Parser {
readonly template: string; readonly template: string;
readonly filename?: string; readonly filename?: string;
readonly customElement: boolean; readonly customElement: boolean;
private readonly isLooseParsing: boolean;
index = 0; index = 0;
stack: TemplateNode[] = []; stack: TemplateNode[] = [];
@ -27,6 +28,7 @@ export class Parser {
css: Style[] = []; css: Style[] = [];
js: Script[] = []; js: Script[] = [];
meta_tags = {}; meta_tags = {};
firstError?: any;
last_auto_closed_tag?: LastAutoClosedTag; last_auto_closed_tag?: LastAutoClosedTag;
constructor(template: string, options: ParserOptions) { constructor(template: string, options: ParserOptions) {
@ -37,6 +39,7 @@ export class Parser {
this.template = template.replace(/\s+$/, ''); this.template = template.replace(/\s+$/, '');
this.filename = options.filename; this.filename = options.filename;
this.customElement = options.customElement; this.customElement = options.customElement;
this.isLooseParsing = options.errorMode === 'loose';
this.html = { this.html = {
start: null, start: null,
@ -98,13 +101,18 @@ export class Parser {
} }
error({ code, message }: { code: string; message: string }, index = this.index) { error({ code, message }: { code: string; message: string }, index = this.index) {
error(message, { const errorProps = {
name: 'ParseError', name: 'ParseError',
code, code,
source: this.template, source: this.template,
start: index, start: index,
filename: this.filename 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 }) { eat(str: string, required?: boolean, error?: { code: string, message: string }) {
@ -238,6 +246,7 @@ export default function parse(
html: parser.html, html: parser.html,
css: parser.css[0], css: parser.css[0],
instance: instance_scripts[0], instance: instance_scripts[0],
module: module_scripts[0] module: module_scripts[0],
firstError: parser.firstError
}; };
} }

Loading…
Cancel
Save