From f8333008fd3d71baccb6dc2897e3c81d471df3bb Mon Sep 17 00:00:00 2001 From: Lars Berger Date: Mon, 5 Feb 2024 06:19:25 +0800 Subject: [PATCH] feat: improve templating error messages --- .../template-engine/token-parsing/parse-tokens.ts | 12 +++++++----- .../template-engine/tokenizing/tokenize-template.ts | 12 ++++++++++-- packages/desktop/src/providers/weather/provider.rs | 6 ++---- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/packages/client-api/src/template-engine/token-parsing/parse-tokens.ts b/packages/client-api/src/template-engine/token-parsing/parse-tokens.ts index ff1c5d52..42904f5a 100644 --- a/packages/client-api/src/template-engine/token-parsing/parse-tokens.ts +++ b/packages/client-api/src/template-engine/token-parsing/parse-tokens.ts @@ -19,9 +19,10 @@ import type { TextNode } from './text-node.model'; export function parseTokens(tokens: Token[]) { let cursor = 0; const nodes: TemplateNode[] = []; + console.log('tokens', tokens); while (cursor < tokens.length) { - const node = parseStandaloneToken(tokens[cursor]); + const node = parseStandaloneToken(tokens[cursor]!); nodes.push(node); cursor += 1; } @@ -40,22 +41,22 @@ export function parseTokens(tokens: Token[]) { return parseSwitchStatement(token); case TokenType.SWITCH_CASE_STATEMENT: throw new TemplateError( - 'Cannot use a switch case statement without a switch statement.', + 'Cannot use @case without a @switch statement.', token.startIndex, ); case TokenType.SWITCH_DEFAULT_STATEMENT: throw new TemplateError( - 'Cannot use a switch default statement without a switch statement.', + 'Cannot use @default without a @switch statement.', token.startIndex, ); case TokenType.ELSE_IF_STATEMENT: throw new TemplateError( - 'Cannot use an else if statement without an if statement.', + 'Cannot use @elseif without an @if statement.', token.startIndex, ); case TokenType.ELSE_STATEMENT: throw new TemplateError( - 'Cannot use an else statement without an if statement.', + 'Cannot use @else without an @if statement.', token.startIndex, ); default: @@ -71,6 +72,7 @@ export function parseTokens(tokens: Token[]) { let next = tokens[cursor + 1]; while ( + // TODO: Add null check here for `next`. next.type === TokenType.TEXT || next.type === TokenType.OPEN_INTERPOLATION || next.type === TokenType.IF_STATEMENT || diff --git a/packages/client-api/src/template-engine/tokenizing/tokenize-template.ts b/packages/client-api/src/template-engine/tokenizing/tokenize-template.ts index 91970d7f..707fc76e 100644 --- a/packages/client-api/src/template-engine/tokenizing/tokenize-template.ts +++ b/packages/client-api/src/template-engine/tokenizing/tokenize-template.ts @@ -141,8 +141,7 @@ export function tokenizeTemplate(template: string): Token[] { function tokenizeStatementArgs() { if (scanner.scan(/\)?\s+/)) { - // Ignore whitespace within args, and closing parenthesis after - // statement args. + // Ignore the closing parenthesis and any following whitespace. } else if (scanner.scan(/\(/)) { pushState({ type: TokenizeStateType.IN_EXPRESSION, @@ -269,5 +268,14 @@ export function tokenizeTemplate(template: string): Token[] { return matched === inverse ? null : current; } + // If state stack includes more than just the default state, then a + // statement or expression has no closing tag. + if (stateStack.length > 1) { + throw new TemplateError( + 'Missing close symbol after statement or expression.', + scanner.cursor, + ); + } + return tokens; } diff --git a/packages/desktop/src/providers/weather/provider.rs b/packages/desktop/src/providers/weather/provider.rs index a0396e0a..f8662213 100644 --- a/packages/desktop/src/providers/weather/provider.rs +++ b/packages/desktop/src/providers/weather/provider.rs @@ -115,7 +115,7 @@ impl IntervalProvider for WeatherProvider { let current_weather = res.current_weather; let is_daytime = current_weather.is_day == 1; - let xxx = ProviderVariables::Weather(WeatherVariables { + Ok(ProviderVariables::Weather(WeatherVariables { is_daytime, status: Self::get_weather_status( current_weather.weather_code, @@ -126,8 +126,6 @@ impl IntervalProvider for WeatherProvider { current_weather.temperature, ), wind_speed: current_weather.wind_speed, - }); - println!("xxx {:?}", xxx); - Ok(xxx) + })) } }