Skip to content

Commit 3ec4b26

Browse files
Merge pull request #1994 from patricklx/fix-locations
fix locations for ast after templates
2 parents 59ea472 + 05bf9bb commit 3ec4b26

3 files changed

Lines changed: 24 additions & 14 deletions

File tree

lib/parsers/gjs-gts-parser.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -489,20 +489,27 @@ function transformForLint(code) {
489489
*/
490490
const result = processor.parse(code);
491491
for (const tplInfo of result.reverse()) {
492+
const lineBreaks = [...tplInfo.contents].reduce(
493+
(prev, curr) => prev + (DocumentLines.isLineBreak(curr.codePointAt(0)) ? 1 : 0),
494+
0
495+
);
492496
if (tplInfo.type === 'class-member') {
493497
const tplLength = tplInfo.range.end - tplInfo.range.start;
494-
const spaces = tplLength - 'static{`'.length - '`}'.length;
495-
const total = ' '.repeat(spaces);
498+
const spaces = tplLength - 'static{`'.length - '`}'.length - lineBreaks;
499+
const total = ' '.repeat(spaces) + '\n'.repeat(lineBreaks);
496500
const replacementCode = `static{\`${total}\`}`;
497501
jsCode = replaceRange(jsCode, tplInfo.range.start, tplInfo.range.end, replacementCode);
498502
} else {
499503
const tplLength = tplInfo.range.end - tplInfo.range.start;
500-
const spaces = tplLength - '`'.length - '`'.length;
501-
const total = ' '.repeat(spaces);
504+
const spaces = tplLength - '`'.length - '`'.length - lineBreaks;
505+
const total = ' '.repeat(spaces) + '\n'.repeat(lineBreaks);
502506
const replacementCode = `\`${total}\``;
503507
jsCode = replaceRange(jsCode, tplInfo.range.start, tplInfo.range.end, replacementCode);
504508
}
505509
}
510+
if (jsCode.length !== code.length) {
511+
throw new Error('bad transform');
512+
}
506513
return {
507514
templateInfos: result,
508515
output: jsCode,

lib/utils/document.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,4 @@ function isLineBreak(ch) {
9898
}
9999

100100
module.exports = DocumentLines;
101+
module.exports.isLineBreak = isLineBreak;

tests/lib/rules-preprocessor/gjs-gts-parser-test.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,9 @@ const valid = [
159159
filename: 'my-component.gjs',
160160
code: `
161161
const Foo = <template>hi</template>;
162-
const Bar = 'x';
162+
163163
<template>
164-
<Foo as |Abc Xyz|>
165-
<Abc.x />
166-
{{Xyz.x}}
167-
</Foo>
168-
<Bar.x />
164+
<Foo />
169165
</template>
170166
`,
171167
},
@@ -727,7 +723,8 @@ describe('multiple tokens in same file', () => {
727723
severity: 2,
728724
});
729725
});
730-
it('correctly maps duplicate <template> tokens to the correct lines', async () => {
726+
727+
it('correctly maps tokens after handlebars', async () => {
731728
const eslint = initESLint();
732729
const code = `
733730
import Component from '@glimmer/component';
@@ -741,7 +738,8 @@ describe('multiple tokens in same file', () => {
741738
super(...arguments);
742739
}
743740
744-
foo = 'bar';
741+
foo = bar;
742+
745743
<template>
746744
<div>
747745
some totally random, non-meaningful text {{bar}}
@@ -752,11 +750,15 @@ describe('multiple tokens in same file', () => {
752750
const results = await eslint.lintText(code, { filePath: 'my-component.gjs' });
753751

754752
const resultErrors = results.flatMap((result) => result.messages);
755-
expect(resultErrors).toHaveLength(2);
753+
expect(resultErrors).toHaveLength(3);
756754
expect(resultErrors[0].message).toBe("'foo' is not defined.");
757755
expect(resultErrors[0].line).toBe(5);
758756

759757
expect(resultErrors[1].message).toBe("'bar' is not defined.");
760-
expect(resultErrors[1].line).toBe(16);
758+
expect(resultErrors[1].endLine).toBe(13);
759+
expect(resultErrors[1].line).toBe(13);
760+
761+
expect(resultErrors[2].message).toBe("'bar' is not defined.");
762+
expect(resultErrors[2].line).toBe(17);
761763
});
762764
});

0 commit comments

Comments
 (0)