Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add element parts, startTag & endTag #1553

Merged
merged 2 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion packages/@glimmer/syntax/lib/parser/tokenizer-event-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import * as src from '../source/api';
import { generateSyntaxError } from '../syntax-error';
import traverse from '../traversal/traverse';
import Walker from '../traversal/walker';
import { appendChild, parseElementBlockParams } from '../utils';
import { appendChild, parseElementBlockParams, parseElementPartLocs } from '../utils';
import b from '../v1/parser-builders';
import publicBuilder from '../v1/public-builders';
import { HandlebarsNodeVisitors } from './handlebars-node-visitors';
Expand Down Expand Up @@ -135,6 +135,21 @@ export class TokenizerEventHandlers extends HandlebarsNodeVisitors {
blockParams: [],
loc,
});
element.startTag = {
type: 'ElementStartNode',
value: name,
loc: loc,
};
element.nameNode = {
type: 'ElementNameNode',
value: name,
loc: loc
.withStart(this.source.offsetFor(loc.startPosition.line, loc.startPosition.column + 1))
.withEnd(
this.source.offsetFor(loc.startPosition.line, loc.startPosition.column + 1 + name.length)
),
};
parseElementPartLocs(this.source, element);
this.elementStack.push(element);
}

Expand All @@ -143,6 +158,12 @@ export class TokenizerEventHandlers extends HandlebarsNodeVisitors {

let element = this.elementStack.pop() as ASTv1.ElementNode;

element.endTag = {
type: 'ElementEndNode',
loc: tag.loc,
value: element.selfClosing ? '' : tag.name,
};

this.validateEndTag(tag, element, isVoid);
let parent = this.currentElement();

Expand Down
21 changes: 21 additions & 0 deletions packages/@glimmer/syntax/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Nullable } from '@glimmer/interfaces';
import { expect, unwrap } from '@glimmer/util';

import type { src } from '..';
import type * as ASTv1 from './v1/api';
import type * as HBS from './v1/handlebars-ast';

Expand All @@ -20,6 +21,26 @@ export function parseElementBlockParams(element: ASTv1.ElementNode): void {
if (params) element.blockParams = params;
}

export function parseElementPartLocs(code: src.Source, element: ASTv1.ElementNode) {
const elementRange = [element.loc.getStart().offset!, element.loc.getEnd().offset!] as [
number,
number,
];
let start = elementRange[0];
let codeSlice = code.slice(...elementRange);
for (const part of element.parts) {
const idx = codeSlice.indexOf(part.value);
const range = [start + idx, 0] as [number, number];
range[1] = range[0] + part.value.length;
codeSlice = code.slice(range[1], elementRange[1]);
start = range[1];
part.loc = code.spanFor({
start: code.hbsPosFor(range[0])!,
end: code.hbsPosFor(range[1])!,
});
}
}

function parseBlockParams(element: ASTv1.ElementNode): Nullable<string[]> {
let l = element.attributes.length;
let attrNames = [];
Expand Down
37 changes: 37 additions & 0 deletions packages/@glimmer/syntax/lib/v1/nodes-v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,42 @@ export interface ElementName {
loc: src.SourceLocation;
}

export interface ElementStartNode extends BaseNode {
type: 'ElementStartNode';
value: string;
}

export interface ElementNameNode extends BaseNode {
type: 'ElementNameNode';
value: string;
}

export interface ElementEndNode extends BaseNode {
type: 'ElementEndNode';
value: string;
}

export interface ElementPartNode extends BaseNode {
type: 'ElementPartNode';
value: string;
}

/*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

beautiful, thank you!

<Foo.bar.x attr='2'></Foo.bar.x>
^-- ElementPartNode
^-- ElementPartNode
^- ElementPartNode
^-------- ElementNameNode
^------------------ ElementStartNode
^----------- ElementEndNode
*/
export interface ElementNode extends BaseNode {
type: 'ElementNode';
tag: string;
nameNode: ElementNameNode;
startTag: ElementStartNode;
endTag: ElementEndNode;
parts: ElementPartNode[];
selfClosing: boolean;
attributes: AttrNode[];
blockParams: string[];
Expand Down Expand Up @@ -315,6 +348,10 @@ export type SharedNodes = {
};

export type Nodes = SharedNodes & {
ElementEndNode: ElementEndNode;
ElementStartNode: ElementStartNode;
ElementPartNode: ElementPartNode;
ElementNameNode: ElementNameNode;
Program: Program;
Template: Template;
Block: Block;
Expand Down
15 changes: 15 additions & 0 deletions packages/@glimmer/syntax/lib/v1/parser-builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,21 @@ class Builders {
return {
type: 'ElementNode',
tag,
nameNode: {
type: 'ElementNameNode',
value: tag,
} as ASTv1.ElementNameNode,
startTag: {
type: 'ElementStartNode',
value: tag,
} as ASTv1.ElementStartNode,
endTag: {
type: 'ElementEndNode',
value: selfClosing ? '' : tag,
} as ASTv1.ElementEndNode,
parts: tag
.split('.')
.map((t) => ({ type: 'ElementPartNode', value: t }) as ASTv1.ElementPartNode),
selfClosing: selfClosing,
attributes: attrs || [],
blockParams: blockParams || [],
Expand Down
15 changes: 15 additions & 0 deletions packages/@glimmer/syntax/lib/v1/public-builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,21 @@ function buildElement(tag: TagDescriptor, options: BuildElementOptions = {}): AS
return {
type: 'ElementNode',
tag: tagName,
nameNode: {
type: 'ElementNameNode',
value: tag,
} as ASTv1.ElementNameNode,
startTag: {
type: 'ElementStartNode',
value: tag,
} as ASTv1.ElementStartNode,
endTag: {
type: 'ElementEndNode',
value: selfClosing ? '' : tag,
} as ASTv1.ElementEndNode,
parts: tagName
.split('.')
.map((t) => ({ type: 'ElementPartNode', value: t }) as ASTv1.ElementPartNode),
selfClosing: selfClosing,
attributes: attrs || [],
blockParams: blockParams || [],
Expand Down
4 changes: 4 additions & 0 deletions packages/@glimmer/syntax/lib/v1/visitor-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const visitorKeys = {
CommentStatement: [],
MustacheCommentStatement: [],
ElementNode: ['attributes', 'modifiers', 'children', 'comments'],
ElementStartNode: [],
ElementPartNode: [],
ElementEndNode: [],
ElementNameNode: [],
AttrNode: ['value'],
TextNode: [],

Expand Down
23 changes: 23 additions & 0 deletions packages/@glimmer/syntax/test/loc-node-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,29 @@ test('html elements', () => {
}
});

test('html elements with paths', () => {
let ast = parse(`
<Foo as |bar|>
<bar.x.y class='bar'/>
<bar.x.y class='bar'></bar.x.y>
</Foo>
`);

let [, foo] = ast.body;
locEqual(foo, 2, 4, 5, 10, 'Foo element');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this range right? 4 to 10 is 6, but the element is <Foo as |bar|> ~ 14

Copy link
Contributor Author

@patricklx patricklx Jan 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the end is at </Foo>, so this is correct. the element is the whole thing, from <Foo to </Foo>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the range of 6 though? feels ... arbitrary? I'm probably missing something though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

start: line 2, column 4
end: line 5, column 10

different line

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol oops. thanks

if (assertNodeType(foo, 'ElementNode')) {
locEqual(foo.startTag, 2, 4, 2, 18, 'Foo start tag');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

18-4 = 14 nice

locEqual(foo.nameNode, 2, 5, 2, 8, 'Foo name node');
locEqual(foo.endTag, 5, 4, 5, 10, 'Foo end tag');
let [, barSelfClosed] = foo.children;
if (assertNodeType(barSelfClosed, 'ElementNode')) {
locEqual(barSelfClosed.parts[0], 3, 7, 3, 10, 'bar.x.y bar part');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do each of these have their part name attached? if so, it would be good to assert that as well.

locEqual(barSelfClosed.parts[1], 3, 11, 3, 12, 'bar.x.y x part');
locEqual(barSelfClosed.parts[2], 3, 13, 3, 14, 'bar.x.y y part');
}
}
});

test('html elements with nested blocks', (assert) => {
let ast = parse(`
<div>
Expand Down
28 changes: 27 additions & 1 deletion packages/@glimmer/syntax/test/parser-node-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,17 @@ test('Element modifiers', () => {
);
});

test('Element paths', (assert) => {
let t = "<bar.x.y class='bar'></bar.x.y>";
const elem = element('bar.x.y', ['attrs', ['class', 'bar']]);
astEqual(t, b.program([elem]));
assert.strictEqual(elem.parts.length, 3);
assert.deepEqual(
elem.parts.map((p) => p.value),
['bar', 'x', 'y']
);
});

test('Tokenizer: MustacheStatement encountered in beforeAttributeName state', () => {
let t = '<input {{bar}}>';
astEqual(t, b.program([element('input', ['modifiers', 'bar'])]));
Expand Down Expand Up @@ -891,7 +902,22 @@ export function element(tag: TagDescriptor, ...options: ElementParts[]): ASTv1.E

return {
type: 'ElementNode',
tag: tag || '',
tag: tag,
nameNode: {
type: 'ElementNameNode',
value: tag,
} as ASTv1.ElementNameNode,
startTag: {
type: 'ElementStartNode',
value: tag,
} as ASTv1.ElementStartNode,
endTag: {
type: 'ElementEndNode',
value: selfClosing ? '' : tag,
} as ASTv1.ElementEndNode,
parts: tag
.split('.')
.map((t) => ({ type: 'ElementPartNode', value: t }) as ASTv1.ElementPartNode),
selfClosing: selfClosing,
attributes: attrs || [],
blockParams: blockParams || [],
Expand Down
Loading