From 5dde25d6b1e395f2daeedede4b04836ffb2caa85 Mon Sep 17 00:00:00 2001 From: Ivan Hofer Date: Mon, 8 Aug 2022 07:46:48 +0200 Subject: [PATCH] optimize `parser.*` calls --- src/compiler/parse/read/style.ts | 6 ++++-- src/compiler/parse/state/mustache.ts | 24 +++++++++++++----------- src/compiler/parse/state/tag.ts | 23 +++++++++++++++-------- 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/src/compiler/parse/read/style.ts b/src/compiler/parse/read/style.ts index e4a885a8cc..1e21dddc67 100644 --- a/src/compiler/parse/read/style.ts +++ b/src/compiler/parse/read/style.ts @@ -5,10 +5,12 @@ import { Node } from 'estree'; import { Style } from '../../interfaces'; import parser_errors from '../errors'; +const regex_closing_style_tag = /<\/style\s*>/; + export default function read_style(parser: Parser, start: number, attributes: Node[]): Style { const content_start = parser.index; - const styles = parser.read_until(/<\/style\s*>/, parser_errors.unclosed_style); + const styles = parser.read_until(regex_closing_style_tag, parser_errors.unclosed_style); if (parser.index >= parser.template.length) { parser.error(parser_errors.unclosed_style); @@ -67,7 +69,7 @@ export default function read_style(parser: Parser, start: number, attributes: No } }); - parser.read(/<\/style\s*>/); + parser.read(regex_closing_style_tag); const end = parser.index; return { diff --git a/src/compiler/parse/state/mustache.ts b/src/compiler/parse/state/mustache.ts index da9016a9df..f0df3c21af 100644 --- a/src/compiler/parse/state/mustache.ts +++ b/src/compiler/parse/state/mustache.ts @@ -33,6 +33,8 @@ function trim_whitespace(block: TemplateNode, trim_before: boolean, trim_after: } } +const regex_whitespace_with_closing_curly_brace = /\s*}/; + export default function mustache(parser: Parser) { const start = parser.index; parser.index += 1; @@ -106,8 +108,8 @@ export default function mustache(parser: Parser) { const block = parser.current(); if (block.type !== 'IfBlock') { parser.error( - parser.stack.some(block => block.type === 'IfBlock') - ? parser_errors.invalid_elseif_placement_unclosed_block(to_string(block)) + parser.stack.some(block => block.type === 'IfBlock') + ? parser_errors.invalid_elseif_placement_unclosed_block(to_string(block)) : parser_errors.invalid_elseif_placement_outside_if ); } @@ -141,8 +143,8 @@ export default function mustache(parser: Parser) { const block = parser.current(); if (block.type !== 'IfBlock' && block.type !== 'EachBlock') { parser.error( - parser.stack.some(block => block.type === 'IfBlock' || block.type === 'EachBlock') - ? parser_errors.invalid_else_placement_unclosed_block(to_string(block)) + parser.stack.some(block => block.type === 'IfBlock' || block.type === 'EachBlock') + ? parser_errors.invalid_else_placement_unclosed_block(to_string(block)) : parser_errors.invalid_else_placement_outside_if ); } @@ -167,15 +169,15 @@ export default function mustache(parser: Parser) { if (block.type !== 'PendingBlock') { parser.error( parser.stack.some(block => block.type === 'PendingBlock') - ? parser_errors.invalid_then_placement_unclosed_block(to_string(block)) - : parser_errors.invalid_then_placement_without_await + ? parser_errors.invalid_then_placement_unclosed_block(to_string(block)) + : parser_errors.invalid_then_placement_without_await ); } } else { if (block.type !== 'ThenBlock' && block.type !== 'PendingBlock') { parser.error(parser.stack.some(block => block.type === 'ThenBlock' || block.type === 'PendingBlock') - ? parser_errors.invalid_catch_placement_unclosed_block(to_string(block)) - : parser_errors.invalid_catch_placement_without_await + ? parser_errors.invalid_catch_placement_unclosed_block(to_string(block)) + : parser_errors.invalid_catch_placement_without_await ); } } @@ -290,7 +292,7 @@ export default function mustache(parser: Parser) { const await_block_shorthand = type === 'AwaitBlock' && parser.eat('then'); if (await_block_shorthand) { - if (parser.match_regex(/\s*}/)) { + if (parser.match_regex(regex_whitespace_with_closing_curly_brace)) { parser.allow_whitespace(); } else { parser.require_whitespace(); @@ -301,7 +303,7 @@ export default function mustache(parser: Parser) { const await_block_catch_shorthand = !await_block_shorthand && type === 'AwaitBlock' && parser.eat('catch'); if (await_block_catch_shorthand) { - if (parser.match_regex(/\s*}/)) { + if (parser.match_regex(regex_whitespace_with_closing_curly_brace)) { parser.allow_whitespace(); } else { parser.require_whitespace(); @@ -350,7 +352,7 @@ export default function mustache(parser: Parser) { let identifiers; // Implies {@debug} which indicates "debug all" - if (parser.read(/\s*}/)) { + if (parser.read(regex_whitespace_with_closing_curly_brace)) { identifiers = []; } else { const expression = read_expression(parser); diff --git a/src/compiler/parse/state/tag.ts b/src/compiler/parse/state/tag.ts index 4be47f25b0..19d0e79da6 100644 --- a/src/compiler/parse/state/tag.ts +++ b/src/compiler/parse/state/tag.ts @@ -53,13 +53,16 @@ function parent_is_head(stack) { return false; } +const regex_closing_textarea_tag = /^<\/textarea(\s[^>]*)?>/i; +const regex_closing_comment = /-->/; + export default function tag(parser: Parser) { const start = parser.index++; let parent = parser.current(); if (parser.eat('!--')) { - const data = parser.read_until(/-->/); + const data = parser.read_until(regex_closing_comment); parser.eat('-->', true, parser_errors.unclosed_comment); parser.current().children.push({ @@ -218,11 +221,10 @@ export default function tag(parser: Parser) { // special case element.children = read_sequence( parser, - () => - /^<\/textarea(\s[^>]*)?>/i.test(parser.template.slice(parser.index)), + () => regex_closing_textarea_tag.test(parser.template.slice(parser.index)), 'inside