Skip to content

Commit

Permalink
Additionally to continue parsing, add the ErrorNode in the result tem…
Browse files Browse the repository at this point in the history
…plate
  • Loading branch information
Marine Dunstetter committed Nov 21, 2024
1 parent 5c9a3f6 commit f0076f4
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
14 changes: 7 additions & 7 deletions packages/@glimmer/syntax/lib/parser/handlebars-node-visitors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,15 +355,15 @@ export abstract class HandlebarsNodeVisitors extends Parser {
return comment;
}

PartialStatement(partial: HBS.PartialStatement): unknown { // Error node type
PartialStatement(partial: HBS.PartialStatement): ASTv1.ErrorNode {
if(this.continueOnError) {
// return b.error({
// loc: partial.loc,
// error: `Handlebars partials are not supported`
// })
return null;
let error = b.error({
loc: this.source.spanFor(partial.loc),
message: `Handlebars partials are not supported`
});
appendChild(this.currentElement(), error);
return error;
}

throw generateSyntaxError(
`Handlebars partials are not supported`,
this.source.spanFor(partial.loc)
Expand Down
8 changes: 8 additions & 0 deletions packages/@glimmer/syntax/lib/v1/nodes-v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export type StatementName =
| 'BlockStatement'
| 'MustacheCommentStatement'
| 'TextNode'
| 'ErrorNode'
| 'ElementNode';

export interface AttrNode extends BaseNode {
Expand Down Expand Up @@ -288,6 +289,11 @@ export interface HashPair extends BaseNode {
value: Expression;
}

export interface ErrorNode extends BaseNode {
type: 'ErrorNode';
message: string;
}

export interface StripFlags {
open: boolean;
close: boolean;
Expand Down Expand Up @@ -318,6 +324,8 @@ export type Nodes = {

Hash: Hash;
HashPair: HashPair;

ErrorNode: ErrorNode;
};

export type NodeType = keyof Nodes;
Expand Down
8 changes: 8 additions & 0 deletions packages/@glimmer/syntax/lib/v1/parser-builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,14 @@ class Builders {
}): T {
return buildLegacyLiteral({ type, value, loc });
}

error({ message, loc }: { message: string; loc: SourceSpan }): ASTv1.ErrorNode {
return {
type: 'ErrorNode',
message,
loc,
};
}
}

const b = new Builders();
Expand Down
8 changes: 8 additions & 0 deletions packages/@glimmer/syntax/lib/v1/public-builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,13 @@ function buildLoc(
}
}

function buildError(message = '', loc?: SourceLocation): ASTv1.ErrorNode {
return b.error({
message,
loc: buildLoc(loc || null),
});
}

export default {
mustache: buildMustache,
block: buildBlock,
Expand All @@ -503,6 +510,7 @@ export default {
template: buildTemplate,
loc: buildLoc,
pos: buildPosition,
error: buildError,

path: buildPath,

Expand Down
2 changes: 1 addition & 1 deletion packages/@glimmer/syntax/test/parser-node-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ test('continue parsing after an error', () => {
let t = '<img id="one">{{> face}}<img id="two">';
astEqual(t, b.template([
element('img', ['attrs', ['id', 'one']]),
// errorNode('error message'),
b.error('Handlebars partials are not supported'),
element('img', ['attrs', ['id', 'two']])
]), undefined, { continueOnError: true });
});
Expand Down

0 comments on commit f0076f4

Please sign in to comment.