optimize `parser.*` calls

pull/7716/head
Ivan Hofer 4 years ago committed by tanhauhau
parent 4e9f1a6e7d
commit 5dde25d6b1

@ -5,10 +5,12 @@ import { Node } from 'estree';
import { Style } from '../../interfaces'; import { Style } from '../../interfaces';
import parser_errors from '../errors'; import parser_errors from '../errors';
const regex_closing_style_tag = /<\/style\s*>/;
export default function read_style(parser: Parser, start: number, attributes: Node[]): Style { export default function read_style(parser: Parser, start: number, attributes: Node[]): Style {
const content_start = parser.index; 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) { if (parser.index >= parser.template.length) {
parser.error(parser_errors.unclosed_style); 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; const end = parser.index;
return { return {

@ -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) { export default function mustache(parser: Parser) {
const start = parser.index; const start = parser.index;
parser.index += 1; parser.index += 1;
@ -174,8 +176,8 @@ export default function mustache(parser: Parser) {
} else { } else {
if (block.type !== 'ThenBlock' && block.type !== 'PendingBlock') { if (block.type !== 'ThenBlock' && block.type !== 'PendingBlock') {
parser.error(parser.stack.some(block => 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_unclosed_block(to_string(block))
: parser_errors.invalid_catch_placement_without_await : 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'); const await_block_shorthand = type === 'AwaitBlock' && parser.eat('then');
if (await_block_shorthand) { if (await_block_shorthand) {
if (parser.match_regex(/\s*}/)) { if (parser.match_regex(regex_whitespace_with_closing_curly_brace)) {
parser.allow_whitespace(); parser.allow_whitespace();
} else { } else {
parser.require_whitespace(); 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'); const await_block_catch_shorthand = !await_block_shorthand && type === 'AwaitBlock' && parser.eat('catch');
if (await_block_catch_shorthand) { if (await_block_catch_shorthand) {
if (parser.match_regex(/\s*}/)) { if (parser.match_regex(regex_whitespace_with_closing_curly_brace)) {
parser.allow_whitespace(); parser.allow_whitespace();
} else { } else {
parser.require_whitespace(); parser.require_whitespace();
@ -350,7 +352,7 @@ export default function mustache(parser: Parser) {
let identifiers; let identifiers;
// Implies {@debug} which indicates "debug all" // Implies {@debug} which indicates "debug all"
if (parser.read(/\s*}/)) { if (parser.read(regex_whitespace_with_closing_curly_brace)) {
identifiers = []; identifiers = [];
} else { } else {
const expression = read_expression(parser); const expression = read_expression(parser);

@ -53,13 +53,16 @@ function parent_is_head(stack) {
return false; return false;
} }
const regex_closing_textarea_tag = /^<\/textarea(\s[^>]*)?>/i;
const regex_closing_comment = /-->/;
export default function tag(parser: Parser) { export default function tag(parser: Parser) {
const start = parser.index++; const start = parser.index++;
let parent = parser.current(); let parent = parser.current();
if (parser.eat('!--')) { if (parser.eat('!--')) {
const data = parser.read_until(/-->/); const data = parser.read_until(regex_closing_comment);
parser.eat('-->', true, parser_errors.unclosed_comment); parser.eat('-->', true, parser_errors.unclosed_comment);
parser.current().children.push({ parser.current().children.push({
@ -218,11 +221,10 @@ export default function tag(parser: Parser) {
// special case // special case
element.children = read_sequence( element.children = read_sequence(
parser, parser,
() => () => regex_closing_textarea_tag.test(parser.template.slice(parser.index)),
/^<\/textarea(\s[^>]*)?>/i.test(parser.template.slice(parser.index)),
'inside <textarea>' 'inside <textarea>'
); );
parser.read(/^<\/textarea(\s[^>]*)?>/i); parser.read(regex_closing_textarea_tag);
element.end = parser.index; element.end = parser.index;
} else if (name === 'script' || name === 'style') { } else if (name === 'script' || name === 'style') {
// special case // special case
@ -237,6 +239,8 @@ export default function tag(parser: Parser) {
} }
} }
const regex_whitespace_or_slash_or_closing_tag = /(\s|\/|>)/;
function read_tag_name(parser: Parser) { function read_tag_name(parser: Parser) {
const start = parser.index; const start = parser.index;
@ -266,7 +270,7 @@ function read_tag_name(parser: Parser) {
if (parser.read(SLOT)) return 'svelte:fragment'; if (parser.read(SLOT)) return 'svelte:fragment';
const name = parser.read_until(/(\s|\/|>)/); const name = parser.read_until(regex_whitespace_or_slash_or_closing_tag);
if (meta_tags.has(name)) return name; if (meta_tags.has(name)) return name;
@ -286,6 +290,10 @@ function read_tag_name(parser: Parser) {
return name; return name;
} }
// eslint-disable-next-line no-useless-escape
const regex_token_ending_character = /[\s=\/>"']/;
const regex_quote_characters = /["']/;
function read_attribute(parser: Parser, unique_names: Set<string>) { function read_attribute(parser: Parser, unique_names: Set<string>) {
const start = parser.index; const start = parser.index;
@ -344,8 +352,7 @@ function read_attribute(parser: Parser, unique_names: Set<string>) {
} }
} }
// eslint-disable-next-line no-useless-escape const name = parser.read_until(regex_token_ending_character);
const name = parser.read_until(/[\s=\/>"']/);
if (!name) return null; if (!name) return null;
let end = parser.index; let end = parser.index;
@ -360,7 +367,7 @@ function read_attribute(parser: Parser, unique_names: Set<string>) {
parser.allow_whitespace(); parser.allow_whitespace();
value = read_attribute_value(parser); value = read_attribute_value(parser);
end = parser.index; end = parser.index;
} else if (parser.match_regex(/["']/)) { } else if (parser.match_regex(regex_quote_characters)) {
parser.error(parser_errors.unexpected_token('='), parser.index); parser.error(parser_errors.unexpected_token('='), parser.index);
} }

Loading…
Cancel
Save