simplify detection

pull/4136/head
Conduitry 6 years ago
parent dd4084a69c
commit fb1961065e

@ -82,15 +82,6 @@ export class Parser {
return this.stack[this.stack.length - 1]; return this.stack[this.stack.length - 1];
} }
find_in_stack(fn) {
for (let i=this.stack.length -1; i>=0; i--) {
if (fn(this.stack[i])) {
return true;
}
}
return false;
}
acorn_error(err: any) { acorn_error(err: any) {
this.error({ this.error({
code: `parse-error`, code: `parse-error`,

@ -108,7 +108,7 @@ export default function mustache(parser: Parser) {
if (parser.eat('if')) { if (parser.eat('if')) {
const block = parser.current(); const block = parser.current();
if (block.type !== 'IfBlock') { if (block.type !== 'IfBlock') {
if (parser.find_in_stack(block => block.type === 'IfBlock')) { if (parser.stack.some(block => block.type === 'IfBlock')) {
parser.error({ parser.error({
code: 'unclosed-open-tag', code: 'unclosed-open-tag',
message: `Expect to close ${to_string(block)} before {:else if ...} block` message: `Expect to close ${to_string(block)} before {:else if ...} block`
@ -151,7 +151,7 @@ export default function mustache(parser: Parser) {
else { else {
const block = parser.current(); const block = parser.current();
if (block.type !== 'IfBlock' && block.type !== 'EachBlock') { if (block.type !== 'IfBlock' && block.type !== 'EachBlock') {
if (parser.find_in_stack(block => block.type === 'IfBlock' || block.type === 'EachBlock')) { if (parser.stack.some(block => block.type === 'IfBlock' || block.type === 'EachBlock')) {
parser.error({ parser.error({
code: 'unclosed-open-tag', code: 'unclosed-open-tag',
message: `Expect to close ${to_string(block)} before {:else} block` message: `Expect to close ${to_string(block)} before {:else} block`
@ -182,7 +182,7 @@ export default function mustache(parser: Parser) {
if (is_then) { if (is_then) {
if (block.type !== 'PendingBlock') { if (block.type !== 'PendingBlock') {
if (parser.find_in_stack(block => block.type === 'PendingBlock')) { if (parser.stack.some(block => block.type === 'PendingBlock')) {
parser.error({ parser.error({
code: 'unclosed-open-tag', code: 'unclosed-open-tag',
message: `Expect to close ${to_string(block)} before {:then} block` message: `Expect to close ${to_string(block)} before {:then} block`
@ -195,7 +195,7 @@ export default function mustache(parser: Parser) {
} }
} else { } else {
if (block.type !== 'ThenBlock' && block.type !== 'PendingBlock') { if (block.type !== 'ThenBlock' && block.type !== 'PendingBlock') {
if (parser.find_in_stack(block => block.type === 'ThenBlock' || block.type === 'PendingBlock')) { if (parser.stack.some(block => block.type === 'ThenBlock' || block.type === 'PendingBlock')) {
parser.error({ parser.error({
code: 'unclosed-open-tag', code: 'unclosed-open-tag',
message: `Expect to close ${to_string(block)} before {:catch} block` message: `Expect to close ${to_string(block)} before {:catch} block`

Loading…
Cancel
Save