Skip to content

Commit

Permalink
Merge pull request #1568 from glimmerjs/ast-refactor
Browse files Browse the repository at this point in the history
Cleanup v1 AST
  • Loading branch information
wycats authored Mar 7, 2024
2 parents 17ca94f + e63f159 commit 38366b3
Show file tree
Hide file tree
Showing 24 changed files with 2,167 additions and 1,326 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
node_modules/
**/node_modules/
**/.turbo
**/*.tsbuildinfo
tmp/
/ts-dist/
**/ts-dist
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,34 @@ class SyntaxErrors extends RenderTest {
);
}

@test
'Block params in HTML syntax - requires a space between as and pipes'() {
this.assert.throws(
() => {
preprocess('<x-bar as|foo|>foo</x-bar>', { meta: { moduleName: 'test-module' } });
},
syntaxErrorFor(
'Invalid block parameters syntax: expecting at least one space character between "as" and "|"',
'as|',
'test-module',
1,
7
)
);
}

@test
'Block params in HTML syntax - Throws exception if given zero parameters'() {
this.assert.throws(
() => {
preprocess('<x-bar as ||>foo</x-bar>', { meta: { moduleName: 'test-module' } });
},
syntaxErrorFor(
'Cannot use zero block parameters',
'<x-bar as ||>foo</x-bar>',
'Invalid block parameters syntax: empty parameters list, expecting at least one identifier',
'as ||',
'test-module',
1,
0
7
)
);

Expand All @@ -71,11 +87,108 @@ class SyntaxErrors extends RenderTest {
preprocess('<x-bar as | |>foo</x-bar>', { meta: { moduleName: 'test-module' } });
},
syntaxErrorFor(
'Cannot use zero block parameters',
'<x-bar as | |>foo</x-bar>',
'Invalid block parameters syntax: empty parameters list, expecting at least one identifier',
'as | |',
'test-module',
1,
0
7
)
);
}

@test
'Block params in HTML syntax - invalid mustaches in block params list'() {
this.assert.throws(
() => {
preprocess('<x-bar as |{{foo}}|>foo</x-bar>', { meta: { moduleName: 'test-module' } });
},
syntaxErrorFor(
'Invalid block parameters syntax: mustaches cannot be used inside parameters list',
'{{foo}}',
'test-module',
1,
11
)
);

this.assert.throws(
() => {
preprocess('<x-bar as |foo{{bar}}|>foo</x-bar>', { meta: { moduleName: 'test-module' } });
},
syntaxErrorFor(
'Invalid block parameters syntax: mustaches cannot be used inside parameters list',
'{{bar}}',
'test-module',
1,
14
)
);

this.assert.throws(
() => {
preprocess('<x-bar as |foo {{bar}}|>foo</x-bar>', { meta: { moduleName: 'test-module' } });
},
syntaxErrorFor(
'Invalid block parameters syntax: mustaches cannot be used inside parameters list',
'{{bar}}',
'test-module',
1,
15
)
);

this.assert.throws(
() => {
preprocess('<x-bar as |foo| {{bar}}>foo</x-bar>', { meta: { moduleName: 'test-module' } });
},
syntaxErrorFor(
'Invalid block parameters syntax: modifiers cannot follow parameters list',
'{{bar}}',
'test-module',
1,
16
)
);
}

@test
'Block params in HTML syntax - EOF in block params list'() {
this.assert.throws(
() => {
preprocess('<x-bar as |', { meta: { moduleName: 'test-module' } });
},
syntaxErrorFor(
'Invalid block parameters syntax: expecting the tag to be closed with ">" or "/>" after parameters list',
'as |',
'test-module',
1,
7
)
);

this.assert.throws(
() => {
preprocess('<x-bar as |foo', { meta: { moduleName: 'test-module' } });
},
syntaxErrorFor(
'Invalid block parameters syntax: expecting the tag to be closed with ">" or "/>" after parameters list',
'as |foo',
'test-module',
1,
7
)
);

this.assert.throws(
() => {
preprocess('<x-bar as |foo|', { meta: { moduleName: 'test-module' } });
},
syntaxErrorFor(
'Invalid block parameters syntax: expecting the tag to be closed with ">" or "/>" after parameters list',
'as |foo|',
'test-module',
1,
7
)
);
}
Expand All @@ -87,24 +200,26 @@ class SyntaxErrors extends RenderTest {
preprocess('<x-bar as |x y>{{x}},{{y}}</x-bar>', { meta: { moduleName: 'test-module' } });
},
syntaxErrorFor(
"Invalid block parameters syntax, 'as |x y'",
'<x-bar as |x y>{{x}},{{y}}</x-bar>',
'Invalid block parameters syntax: expecting "|" but the tag was closed prematurely',
'as |x y>',
'test-module',
1,
0
7
)
);

this.assert.throws(
() => {
preprocess('<x-bar as |x| y>{{x}},{{y}}</x-bar>', { meta: { moduleName: 'test-module' } });
preprocess('<x-bar as |x| wat>{{x}},{{y}}</x-bar>', {
meta: { moduleName: 'test-module' },
});
},
syntaxErrorFor(
"Invalid block parameters syntax, 'as |x| y'",
'<x-bar as |x| y>{{x}},{{y}}</x-bar>',
'Invalid block parameters syntax: expecting the tag to be closed with ">" or "/>" after parameters list',
'wat',
'test-module',
1,
0
14
)
);

Expand All @@ -113,11 +228,11 @@ class SyntaxErrors extends RenderTest {
preprocess('<x-bar as |x| y|>{{x}},{{y}}</x-bar>', { meta: { moduleName: 'test-module' } });
},
syntaxErrorFor(
"Invalid block parameters syntax, 'as |x| y|'",
'<x-bar as |x| y|>{{x}},{{y}}</x-bar>',
'Invalid block parameters syntax: expecting the tag to be closed with ">" or "/>" after parameters list',
'y|',
'test-module',
1,
0
14
)
);
}
Expand All @@ -129,31 +244,37 @@ class SyntaxErrors extends RenderTest {
preprocess('<x-bar as |x foo.bar|></x-bar>', { meta: { moduleName: 'test-module' } });
},
syntaxErrorFor(
"Invalid identifier for block parameters, 'foo.bar'",
'<x-bar as |x foo.bar|></x-bar>',
'Invalid block parameters syntax: invalid identifier name `foo.bar`',
'foo.bar',
'test-module',
1,
0
13
)
);

this.assert.throws(
() => {
preprocess('<x-bar as |x "foo"|></x-bar>', { meta: { moduleName: 'test-module' } });
},
syntaxErrorFor('" is not a valid character within attribute names', '', 'test-module', 1, 17)
syntaxErrorFor(
'Invalid block parameters syntax: invalid identifier name `"foo"`',
'"foo"',
'test-module',
1,
13
)
);

this.assert.throws(
() => {
preprocess('<x-bar as |foo[bar]|></x-bar>', { meta: { moduleName: 'test-module' } });
},
syntaxErrorFor(
"Invalid identifier for block parameters, 'foo[bar]'",
'<x-bar as |foo[bar]|></x-bar>',
'Invalid block parameters syntax: invalid identifier name `foo[bar]`',
'foo[bar]',
'test-module',
1,
0
11
)
);
}
Expand All @@ -165,11 +286,11 @@ class SyntaxErrors extends RenderTest {
preprocess('<x-bar |x|></x-bar>', { meta: { moduleName: 'test-module' } });
},
syntaxErrorFor(
'Block parameters must be preceded by the `as` keyword, detected block parameters without `as`',
'<x-bar |x|></x-bar>',
'Invalid block parameters syntax: block parameters must be preceded by the `as` keyword',
'|x|',
'test-module',
1,
0
7
)
);

Expand All @@ -180,11 +301,11 @@ class SyntaxErrors extends RenderTest {
});
},
syntaxErrorFor(
'Block parameters must be preceded by the `as` keyword, detected block parameters without `as`',
'<:baz |x|></:baz>',
'Invalid block parameters syntax: block parameters must be preceded by the `as` keyword',
'|x|',
'test-module',
1,
7
13
)
);
}
Expand Down
1 change: 0 additions & 1 deletion packages/@glimmer-workspace/tsconfig.tsbuildinfo

This file was deleted.

30 changes: 9 additions & 21 deletions packages/@glimmer/syntax/lib/generation/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ export default class Printer {
switch (node.type) {
case 'MustacheStatement':
case 'BlockStatement':
case 'PartialStatement':
case 'MustacheCommentStatement':
case 'CommentStatement':
case 'TextNode':
Expand All @@ -113,8 +112,6 @@ export default class Printer {
case 'PathExpression':
case 'SubExpression':
return this.Expression(node);
case 'Program':
return this.Block(node);
case 'ConcatStatement':
// should have an AttrNode parent
return this.ConcatStatement(node);
Expand Down Expand Up @@ -163,8 +160,6 @@ export default class Printer {
return this.MustacheStatement(statement);
case 'BlockStatement':
return this.BlockStatement(statement);
case 'PartialStatement':
return this.PartialStatement(statement);
case 'MustacheCommentStatement':
return this.MustacheCommentStatement(statement);
case 'CommentStatement':
Expand All @@ -174,15 +169,20 @@ export default class Printer {
case 'ElementNode':
return this.ElementNode(statement);
case 'Block':
case 'Template':
return this.Block(statement);
case 'Template':
return this.Template(statement);
case 'AttrNode':
// should have element
return this.AttrNode(statement);
}
}

Block(block: ASTv1.Block | ASTv1.Program | ASTv1.Template): void {
Template(template: ASTv1.Template): void {
this.TopLevelStatements(template.body);
}

Block(block: ASTv1.Block): void {
/*
When processing a template like:
Expand Down Expand Up @@ -320,7 +320,7 @@ export default class Printer {
return;
}

this.buffer += mustache.escaped ? '{{' : '{{{';
this.buffer += mustache.trusting ? '{{{' : '{{';

if (mustache.strip.open) {
this.buffer += '~';
Expand All @@ -334,7 +334,7 @@ export default class Printer {
this.buffer += '~';
}

this.buffer += mustache.escaped ? '}}' : '}}}';
this.buffer += mustache.trusting ? '}}}' : '}}';
}

BlockStatement(block: ASTv1.BlockStatement): void {
Expand Down Expand Up @@ -385,18 +385,6 @@ export default class Printer {
this.buffer += ` as |${blockParams.join(' ')}|`;
}

PartialStatement(partial: ASTv1.PartialStatement): void {
if (this.handledByOverride(partial)) {
return;
}

this.buffer += '{{>';
this.Expression(partial.name);
this.Params(partial.params);
this.Hash(partial.hash);
this.buffer += '}}';
}

ConcatStatement(concat: ASTv1.ConcatStatement): void {
if (this.handledByOverride(concat)) {
return;
Expand Down
Loading

0 comments on commit 38366b3

Please sign in to comment.