Skip to content

Commit 8ec4864

Browse files
wagnermacielmmalerba
authored andcommitted
fix(material/schematics): add attr for multi-line start tags (#24586)
* add logic for handling multi-line start tags in #addAttribute * unit tested * delete unused template-migration.spec.ts file
1 parent a8f28e9 commit 8ec4864

File tree

4 files changed

+73
-28
lines changed

4 files changed

+73
-28
lines changed

src/material/schematics/ng-generate/mdc-migration/rules/components/card/card-template.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,24 @@ describe('card template migrator', () => {
9090
`,
9191
);
9292
});
93+
94+
it('should match indentation', async () => {
95+
await runMigrationTest(
96+
`
97+
<mat-card
98+
class="some card"
99+
aria-label="some card"
100+
aria-describedby="some card"
101+
></mat-card>
102+
`,
103+
`
104+
<mat-card
105+
appearance="outlined"
106+
class="some card"
107+
aria-label="some card"
108+
aria-describedby="some card"
109+
></mat-card>
110+
`,
111+
);
112+
});
93113
});

src/material/schematics/ng-generate/mdc-migration/rules/template-migration.spec.ts

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/material/schematics/ng-generate/mdc-migration/rules/tree-traversal.spec.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,47 @@ describe('#visitElements', () => {
119119
expect(html).toBe('<a attr2="val2" attr1="val1"></a>');
120120
});
121121
});
122+
123+
it('should match indentation', async () => {
124+
runAddAttributeTest(
125+
`
126+
<a
127+
class="a"
128+
aria-label="a"
129+
aria-describedby="a"
130+
></a>
131+
`,
132+
`
133+
<a
134+
attr="val"
135+
class="a"
136+
aria-label="a"
137+
aria-describedby="a"
138+
></a>
139+
`,
140+
);
141+
});
142+
143+
it('should match indentation w/ multiple attrs', async () => {
144+
let html = `
145+
<a
146+
class="a"
147+
aria-label="a"
148+
aria-describedby="a"
149+
></a>
150+
`;
151+
visitElements(parseTemplate(html).nodes, undefined, node => {
152+
html = addAttribute(html, node, 'attr1', 'val1');
153+
html = addAttribute(html, node, 'attr2', 'val2');
154+
});
155+
expect(html).toBe(`
156+
<a
157+
attr2="val2"
158+
attr1="val1"
159+
class="a"
160+
aria-label="a"
161+
aria-describedby="a"
162+
></a>
163+
`);
164+
});
122165
});

src/material/schematics/ng-generate/mdc-migration/rules/tree-traversal.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,16 @@ export function addAttribute(
9898
const index = node.startSourceSpan.start.offset + node.name.length + 1;
9999
const prefix = html.slice(0, index);
100100
const suffix = html.slice(index);
101-
return prefix + ` ${name}="${value}"` + suffix;
101+
102+
if (node.startSourceSpan.start.line === node.startSourceSpan.end.line) {
103+
return prefix + ` ${name}="${value}"` + suffix;
104+
}
105+
106+
const attr = node.attributes[0];
107+
const ctx = attr.sourceSpan.start.getContext(attr.sourceSpan.start.col + 1, 1)!;
108+
const indentation = ctx.before;
109+
110+
return prefix + indentation + `${name}="${value}"` + suffix;
102111
}
103112

104113
/**

0 commit comments

Comments
 (0)