Skip to content

Commit

Permalink
feat: improve templating error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-berger committed Feb 4, 2024
1 parent d502678 commit f833300
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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:
Expand All @@ -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 ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
6 changes: 2 additions & 4 deletions packages/desktop/src/providers/weather/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -126,8 +126,6 @@ impl IntervalProvider for WeatherProvider {
current_weather.temperature,
),
wind_speed: current_weather.wind_speed,
});
println!("xxx {:?}", xxx);
Ok(xxx)
}))
}
}

0 comments on commit f833300

Please sign in to comment.