diff --git a/packages/compiler-core/src/assert.ts b/packages/compiler-core/src/assert.ts new file mode 100644 index 00000000..2f349812 --- /dev/null +++ b/packages/compiler-core/src/assert.ts @@ -0,0 +1,5 @@ +export function assert(condition: boolean, msg?: string) { + if (!condition) { + throw new Error(msg || `unexpected parser condition`) + } +} diff --git a/packages/compiler-core/src/parser.ts b/packages/compiler-core/src/parser.ts index abb7fa81..fd9dd434 100644 --- a/packages/compiler-core/src/parser.ts +++ b/packages/compiler-core/src/parser.ts @@ -1,4 +1,4 @@ -import assert from 'assert' +import { assert } from './assert' import { ParserErrorTypes } from './errorTypes' import { Node, @@ -69,7 +69,7 @@ function startsWith(source: string, searchString: string): boolean { } function advanceBy(context: ParserContext, numberOfCharacters: number): void { - assert(numberOfCharacters <= context.source.length) + __DEV__ && assert(numberOfCharacters <= context.source.length) const { column, source } = context const str = source.slice(0, numberOfCharacters) @@ -175,7 +175,7 @@ function parseChildren( const nodes: RootNode['children'] = [] while (!isEnd(context, mode, ancestors)) { - assert(context.source.length > 0) + __DEV__ && assert(context.source.length > 0) const s = context.source let node: any = null @@ -332,15 +332,16 @@ function parseCDATA( context: ParserContext, ancestors: ElementNode[] ): RootNode['children'] { - assert(last(ancestors) == null || last(ancestors)!.ns !== Namespaces.HTML) - assert(startsWith(context.source, '')) + __DEV__ && assert(startsWith(context.source, ']]>')) advanceBy(context, 3) } @@ -348,7 +349,7 @@ function parseCDATA( } function parseComment(context: ParserContext): CommentNode { - assert(startsWith(context.source, '