Lint and format src/compiler/parse/index.js

pull/8569/head
Simon Holthausen 3 years ago
parent f49a6b6f39
commit 4b3cacf3b9

@ -8,7 +8,6 @@ import parser_errors from './errors';
const regex_position_indicator = / \(\d+:\d+\)$/; const regex_position_indicator = / \(\d+:\d+\)$/;
/** */ /** */
export class Parser { export class Parser {
/** /**
* @readonly * @readonly
* @type {string} * @type {string}
@ -89,10 +88,13 @@ export class Parser {
const current = this.current(); const current = this.current();
const type = current.type === 'Element' ? `<${current.name}>` : 'Block'; const type = current.type === 'Element' ? `<${current.name}>` : 'Block';
const slug = current.type === 'Element' ? 'element' : 'block'; const slug = current.type === 'Element' ? 'element' : 'block';
this.error({ this.error(
{
code: `unclosed-${slug}`, code: `unclosed-${slug}`,
message: `${type} was left open` message: `${type} was left open`
}, current.start); },
current.start
);
} }
if (state !== fragment) { if (state !== fragment) {
this.error({ this.error({
@ -102,15 +104,12 @@ export class Parser {
} }
if (this.html.children.length) { if (this.html.children.length) {
let start = this.html.children[0].start; let start = this.html.children[0].start;
while (regex_whitespace.test(template[start])) while (regex_whitespace.test(template[start])) start += 1;
start += 1;
let end = this.html.children[this.html.children.length - 1].end; let end = this.html.children[this.html.children.length - 1].end;
while (regex_whitespace.test(template[end - 1])) while (regex_whitespace.test(template[end - 1])) end -= 1;
end -= 1;
this.html.start = start; this.html.start = start;
this.html.end = end; this.html.end = end;
} } else {
else {
this.html.start = this.html.end = null; this.html.start = this.html.end = null;
} }
} }
@ -121,10 +120,13 @@ export class Parser {
/** /**
* @param {any} err */ * @param {any} err */
acorn_error(err) { acorn_error(err) {
this.error({ this.error(
{
code: 'parse-error', code: 'parse-error',
message: err.message.replace(regex_position_indicator, '') message: err.message.replace(regex_position_indicator, '')
}, err.pos); },
err.pos
);
} }
/** /**
@ -150,10 +152,12 @@ export class Parser {
return true; return true;
} }
if (required) { if (required) {
this.error(error || this.error(
error ||
(this.index === this.template.length (this.index === this.template.length
? parser_errors.unexpected_eof_token(str) ? parser_errors.unexpected_eof_token(str)
: parser_errors.unexpected_token(str))); : parser_errors.unexpected_token(str))
);
} }
return false; return false;
} }
@ -169,8 +173,7 @@ export class Parser {
*/ */
match_regex(pattern) { match_regex(pattern) {
const match = pattern.exec(this.template.slice(this.index)); const match = pattern.exec(this.template.slice(this.index));
if (!match || match.index !== 0) if (!match || match.index !== 0) return null;
return null;
return match[0]; return match[0];
} }
allow_whitespace() { allow_whitespace() {
@ -184,29 +187,29 @@ export class Parser {
*/ */
read(pattern) { read(pattern) {
const result = this.match_regex(pattern); const result = this.match_regex(pattern);
if (result) if (result) this.index += result.length;
this.index += result.length;
return result; return result;
} }
read_identifier(allow_reserved = false) { read_identifier(allow_reserved = false) {
const start = this.index; const start = this.index;
let i = this.index; let i = this.index;
const code = full_char_code_at(this.template, i); const code = full_char_code_at(this.template, i);
if (!isIdentifierStart(code, true)) if (!isIdentifierStart(code, true)) return null;
return null;
i += code <= 0xffff ? 1 : 2; i += code <= 0xffff ? 1 : 2;
while (i < this.template.length) { while (i < this.template.length) {
const code = full_char_code_at(this.template, i); const code = full_char_code_at(this.template, i);
if (!isIdentifierChar(code, true)) if (!isIdentifierChar(code, true)) break;
break;
i += code <= 0xffff ? 1 : 2; i += code <= 0xffff ? 1 : 2;
} }
const identifier = this.template.slice(this.index, (this.index = i)); const identifier = this.template.slice(this.index, (this.index = i));
if (!allow_reserved && reserved.has(identifier)) { if (!allow_reserved && reserved.has(identifier)) {
this.error({ this.error(
{
code: 'unexpected-reserved-word', code: 'unexpected-reserved-word',
message: `'${identifier}' is a reserved word in JavaScript and cannot be used here` message: `'${identifier}' is a reserved word in JavaScript and cannot be used here`
}, start); },
start
);
} }
return identifier; return identifier;
} }
@ -217,10 +220,12 @@ export class Parser {
*/ */
read_until(pattern, error_message) { read_until(pattern, error_message) {
if (this.index >= this.template.length) { if (this.index >= this.template.length) {
this.error(error_message || { this.error(
error_message || {
code: 'unexpected-eof', code: 'unexpected-eof',
message: 'Unexpected end of input' message: 'Unexpected end of input'
}); }
);
} }
const start = this.index; const start = this.index;
const match = pattern.exec(this.template.slice(start)); const match = pattern.exec(this.template.slice(start));
@ -270,7 +275,6 @@ export default function parse(template, options = {}) {
}; };
} }
/** @typedef {(parser: Parser) => ParserState | void} ParserState */ /** @typedef {(parser: Parser) => ParserState | void} ParserState */
/** @typedef {Object} LastAutoClosedTag /** @typedef {Object} LastAutoClosedTag

Loading…
Cancel
Save