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

Fix infinite recursion when printing sub-nodes #671

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
31 changes: 10 additions & 21 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,22 @@ import type { ASTv1 as AST, NodeVisitor } from '@glimmer/syntax';
import ParseResult, { NodeInfo } from './parse-result';
import { builders } from './custom-nodes';

const PARSE_RESULT_FOR = new WeakMap<AST.Node, ParseResult>();
const NODE_INFO = new WeakMap<AST.Node, NodeInfo>();

export function parse(template: string): AST.Template {
const result = new ParseResult(template, NODE_INFO);

PARSE_RESULT_FOR.set(result.ast, result);

return result.ast;
return new ParseResult(template, NODE_INFO).ast;
}

export function print(ast: AST.Node): string {
const parseResult = PARSE_RESULT_FOR.get(ast);

// TODO: write a test for this case
if (parseResult === undefined) {
return glimmerPrint(ast, {
entityEncoding: 'raw',
override: (ast) => {
if (NODE_INFO.has(ast)) {
return print(ast);
}
},
});
}

return parseResult.print();
return glimmerPrint(ast, {
entityEncoding: 'raw',
override: (ast) => {
let info = NODE_INFO.get(ast);
if (info) {
return info.parse_result.print(ast);
}
},
});
}

export interface Syntax {
Expand Down
26 changes: 26 additions & 0 deletions src/parse-result.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,32 @@ describe('ember-template-recast', function () {
expect(print(ast)).toEqual(`<img src="{{this.something}}">`);
});

test('reusing another template part to build a new template', function () {
let template = `foo`;
let original = parse(template);
let text = original.body[0] as AST.TextNode;
let ast = builders.template([text]);

expect(print(ast)).toEqual(`foo`);
});

test('wrapping a parsed node (which uses custom formatting) with a raw node', function () {
// Ensuring fix for GH#586
// (infinite recursion when printing custom nodes containing parsed nodes)
// plays nicely with custom printing from GH#653
// (specifying quoteType on custom nodes, adds a printing override)
let template = `<Foo @class='a {{b}} c' />`;
let original = parse(template);

let raw_wrapping_ast = builders.template([
builders.element('div', {
children: original.body,
}),
]);

expect(print(raw_wrapping_ast)).toEqual(`<div><Foo @class='a {{b}} c' /></div>`);
});

test('changing an element to a void element does not print closing tag', function () {
let template = `<div data-foo="{{something}}"></div>`;

Expand Down
2 changes: 2 additions & 0 deletions src/parse-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export interface NodeInfo {
node: AST.Node;
original: AST.Node;
source: string;
parse_result: ParseResult;

hadHash?: boolean;
hadParams?: boolean;
Expand Down Expand Up @@ -156,6 +157,7 @@ export default class ParseResult {
node,
original: JSON.parse(JSON.stringify(node)),
source: this.sourceForLoc(node.loc),
parse_result: this,
};

this.nodeInfo.set(node, nodeInfo);
Expand Down