Skip to content

Commit

Permalink
fix element block params loc
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklx committed Jan 26, 2024
1 parent a88263f commit 8a0e979
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
17 changes: 16 additions & 1 deletion packages/@glimmer/syntax/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,25 @@ function parseBlockParams(element: ASTv1.ElementNode): Nullable<ASTv1.BlockParam
element.loc
);
}
let loc = element.attributes[i]!.loc;
if (attrNames[i]!.startsWith('|')) {
loc = loc.slice({ skipStart: 1 });
}
if (attrNames[i]!.endsWith('|')) {
loc = loc.slice({ skipEnd: 1 });
}

// fix hbs parser bug, the range contains the whitespace between attributes...
if (loc.endPosition.column - loc.startPosition.column > param.length) {
loc = loc.slice({
skipEnd: loc.endPosition.column - loc.startPosition.column - param.length,
});
}

params.push({
type: 'BlockParam',
value: param,
loc: element.attributes[i]!.loc,
loc,
});
}
}
Expand Down
8 changes: 5 additions & 3 deletions packages/@glimmer/syntax/test/loc-node-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,14 +320,16 @@ test('element block params', () => {
let [Foo] = ast.body;
if (assertNodeType(Foo, 'ElementNode')) {
let [ab, cd, efg] = guardArray({ blockParamNodes: Foo.blockParamNodes }, { min: 3 });
locEqual(ab, 1, 8, 1, 12);
locEqual(cd, 1, 12, 1, 15);
locEqual(efg, 1, 15, 1, 19);
locEqual(ab, 1, 9, 1, 11);
locEqual(cd, 1, 12, 1, 14);
locEqual(efg, 1, 15, 1, 18);
}
});

test('mustache block params', () => {
let ast = parse(`{{#Foo as |ab cd efg|}}{{/Foo}}`);
`
{{#Foo as |ab cd efg|}}{{/Foo}}`;

let [Foo] = ast.body;
if (assertNodeType(Foo, 'BlockStatement')) {
Expand Down

0 comments on commit 8a0e979

Please sign in to comment.