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

Maintain relative order of attributes and comments within an ElementNode #1266

Merged
merged 3 commits into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
34 changes: 16 additions & 18 deletions packages/@glimmer/syntax/lib/generation/printer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as ASTv1 from '../v1/api';
import { escapeAttrValue, escapeText } from './util';
import { escapeAttrValue, escapeText, sortByLoc } from './util';

export const voidMap: {
[tagName: string]: boolean;
Expand Down Expand Up @@ -219,23 +219,21 @@ export default class Printer {

OpenElementNode(el: ASTv1.ElementNode): void {
this.buffer += `<${el.tag}`;
if (el.attributes.length) {
el.attributes.forEach((attr) => {
this.buffer += ' ';
this.AttrNode(attr);
});
}
if (el.modifiers.length) {
el.modifiers.forEach((mod) => {
this.buffer += ' ';
this.ElementModifierStatement(mod);
});
}
if (el.comments.length) {
el.comments.forEach((comment) => {
this.buffer += ' ';
this.MustacheCommentStatement(comment);
});
const parts = [...el.attributes, ...el.modifiers, ...el.comments].sort(sortByLoc);

for (const part of parts) {
this.buffer += ' ';
switch (part.type) {
case 'AttrNode':
this.AttrNode(part);
break;
case 'ElementModifierStatement':
this.ElementModifierStatement(part);
break;
case 'MustacheCommentStatement':
this.MustacheCommentStatement(part);
break;
}
}
if (el.blockParams.length) {
this.BlockParams(el.blockParams);
Expand Down
29 changes: 29 additions & 0 deletions packages/@glimmer/syntax/lib/generation/util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as ASTv1 from '../v1/api';

const enum Char {
NBSP = 0xa0,
QUOT = 0x22,
Expand Down Expand Up @@ -53,3 +55,30 @@ export function escapeText(text: string): string {
}
return text;
}

export function sortByLoc(a: ASTv1.Node, b: ASTv1.Node): -1 | 0 | 1 {
Copy link
Member

Choose a reason for hiding this comment

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

Can you re-export this from the top level @glimmer/syntax index file? That will ultimately allow me to remove the original code this is copied from (over in ember-template-recast).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done! Worth noting, the detection of whether a Node is invisible or not is slightly different from the original in ember-template-recast, not sure if that will impact your ability to re-use it there.

Copy link
Member

Choose a reason for hiding this comment

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

Ya, it definitely will but ember-template-recast has not yet updated to the latest @glimmer/syntax yet. When it does, it'll need to deal with this new loc format as well, so it should be good to go.

Copy link
Contributor

@dcyriller dcyriller Oct 27, 2021

Choose a reason for hiding this comment

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

// If either is invisible, don't try to order them
if (a.loc.isInvisible || b.loc.isInvisible) {
return 0;
}

if (a.loc.startPosition.line < b.loc.startPosition.line) {
return -1;
}

if (
a.loc.startPosition.line === b.loc.startPosition.line &&
a.loc.startPosition.column < b.loc.startPosition.column
) {
return -1;
}

if (
a.loc.startPosition.line === b.loc.startPosition.line &&
a.loc.startPosition.column === b.loc.startPosition.column
) {
return 0;
}

return 1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ export abstract class HandlebarsNodeVisitors extends Parser {

switch (tokenizer.state) {
case TokenizerState.beforeAttributeName:
case TokenizerState.afterAttributeName:
this.currentStartTag.comments.push(comment);
break;

Expand Down
3 changes: 3 additions & 0 deletions packages/@glimmer/syntax/test/generation/print-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ let templates = [

// unescaped
'{{{unescaped}}}',

// Comment in Angle Bracket component
'<Foo {{!-- This is a comment --}} attribute></Foo>',
];

QUnit.module('[glimmer-syntax] Code generation', function () {
Expand Down
10 changes: 10 additions & 0 deletions packages/@glimmer/syntax/test/parser-node-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,16 @@ test('a Handlebars comment in proper element space', function () {
);
});

test('a Handlebars comment after a valueless attribute', function () {
let t = '<input foo {{! comment }}>';
astEqual(
t,
b.program([
element('input', ['attrs', ['foo', '']], ['comments', b.mustacheComment(' comment ')]),
])
);
});

test('a Handlebars comment in invalid element space', function (assert) {
assert.throws(() => {
parse('\nbefore <div \n a{{! some comment }} data-foo="bar"></div> after', {
Expand Down