Skip to content

Commit

Permalink
[feat] add loose parsing mode
Browse files Browse the repository at this point in the history
Related to #4818
  • Loading branch information
Simon Holthausen committed Sep 14, 2021
1 parent c040f13 commit c8c47c1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/compiler/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export interface Ast {
css: Style;
instance: Script;
module: Script;
firstError?: any;
}

export interface Warning {
Expand Down Expand Up @@ -170,6 +171,7 @@ export interface CompileOptions {
export interface ParserOptions {
filename?: string;
customElement?: boolean;
errorMode?: 'throw' | 'warn' | 'loose';
}

export interface Visitor {
Expand Down
15 changes: 12 additions & 3 deletions src/compiler/parse/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class Parser {
readonly template: string;
readonly filename?: string;
readonly customElement: boolean;
private readonly isLooseParsing: boolean;

index = 0;
stack: TemplateNode[] = [];
Expand All @@ -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) {
Expand All @@ -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,
Expand Down Expand Up @@ -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 }) {
Expand Down Expand Up @@ -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
};
}

0 comments on commit c8c47c1

Please sign in to comment.