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: Attribute closes the quotations prematurely #665

Merged
merged 3 commits into from
Oct 30, 2021
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
18 changes: 18 additions & 0 deletions src/parse-result.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1224,6 +1224,24 @@ describe('ember-template-recast', function () {
`);
});

test('mutations in MustacheStatements retain whitespace in AttrNode', function () {
let template = stripIndent`
<div
class="
block
{{if this.foo "bar"}}
"
>
hello
</div>
`;

let ast = parse(template) as any;
ast.body[0].attributes[0].value.parts[1].params[1].value = 'bar';

expect(print(ast)).toEqual(template);
});

test('quotes are preserved when updated a TextNode value (double quote)', function () {
let template = `<div class="lol"></div>`;

Expand Down
8 changes: 5 additions & 3 deletions src/parse-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ function fixASTIssues(sourceLines: any, ast: any) {
(node as any).quoteType = quote ? quote : null;
},
TextNode(node, path) {
const source = sourceForLoc(sourceLines, node.loc);
if (path.parentNode === null) {
throw new Error(
'ember-template-recast: Error while sanitizing input AST: found TextNode with no parentNode'
Expand All @@ -65,6 +64,7 @@ function fixASTIssues(sourceLines: any, ast: any) {

switch (path.parentNode.type) {
case 'AttrNode': {
const source = sourceForLoc(sourceLines, node.loc);
if (
node.chars.length > 0 &&
((source.startsWith(`'`) && source.endsWith(`'`)) ||
Expand All @@ -76,13 +76,15 @@ function fixASTIssues(sourceLines: any, ast: any) {
break;
}
case 'ConcatStatement': {
// TODO: manually working around https://github.com/glimmerjs/glimmer-vm/pull/954
const parent = path.parentNode as AST.ConcatStatement;
const isFirstPart = parent.parts.indexOf(node) === 0;

const { start, end } = node.loc;
if (isFirstPart && node.loc.start.column > path.parentNode.loc.start.column + 1) {
const { start, end } = node.loc;
// TODO: manually working around https://github.com/glimmerjs/glimmer-vm/pull/954
node.loc = builders.loc(start.line, start.column - 1, end.line, end.column);
} else if (isFirstPart && node.chars.charAt(0) === '\n') {
node.loc = builders.loc(start.line, start.column + 1, end.line, end.column);
}
}
}
Expand Down