Skip to content

Commit

Permalink
fix locations after template
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklx committed Nov 9, 2023
1 parent d545258 commit 80d13e2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
15 changes: 11 additions & 4 deletions lib/parsers/gjs-gts-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,20 +450,27 @@ function transformForLint(code) {
*/
const result = processor.parse(code);
for (const tplInfo of result.reverse()) {
const lineBreaks = [...tplInfo.contents].reduce((prev, curr) => prev + (DocumentLines.isLineBreak(curr.codePointAt(0)) ? 1 : 0), 0);
if (tplInfo.type === 'class-member') {
const tplLength = tplInfo.range.end - tplInfo.range.start;
const spaces = tplLength - 'static{`'.length - '`}'.length;
const total = ' '.repeat(spaces);
const spaces = tplLength - 'static{`'.length - '`}'.length - lineBreaks;
const total = ' '.repeat(spaces) + '\n'.repeat(lineBreaks);
const replacementCode = `static{\`${total}\`}`;
jsCode = replaceRange(jsCode, tplInfo.range.start, tplInfo.range.end, replacementCode);
} else {
const tplLength = tplInfo.range.end - tplInfo.range.start;
const spaces = tplLength - '`'.length - '`'.length;
const total = ' '.repeat(spaces);
const spaces = tplLength - '`'.length - '`'.length - lineBreaks;
const total = ' '.repeat(spaces) + '\n'.repeat(lineBreaks);
const replacementCode = `\`${total}\``;
jsCode = replaceRange(jsCode, tplInfo.range.start, tplInfo.range.end, replacementCode);
}
}
if (jsCode.length !== code.length) {
throw new Error('bad transform');
}
if (jsCode.split('\n').length !== code.split('\n').length) {
throw new Error('bad transform');
}
return {
templateInfos: result,
output: jsCode,
Expand Down
1 change: 1 addition & 0 deletions lib/utils/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,4 @@ function isLineBreak(ch) {
}

module.exports = DocumentLines;
module.exports.isLineBreak = isLineBreak;
22 changes: 12 additions & 10 deletions tests/lib/rules-preprocessor/gjs-gts-parser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,9 @@ const valid = [
filename: 'my-component.gjs',
code: `
const Foo = <template>hi</template>;
const Bar = 'x';
<template>
<Foo as |Abc Xyz|>
<Abc.x />
{{Xyz.x}}
</Foo>
<Bar.x />
<Foo />
</template>
`,
},
Expand Down Expand Up @@ -692,7 +688,8 @@ describe('multiple tokens in same file', () => {
severity: 2,
});
});
it('correctly maps duplicate <template> tokens to the correct lines', async () => {

it('correctly maps tokens after handlebars', async () => {
const eslint = initESLint();
const code = `
import Component from '@glimmer/component';
Expand All @@ -706,7 +703,8 @@ describe('multiple tokens in same file', () => {
super(...arguments);
}
foo = 'bar';
foo = bar;
<template>
<div>
some totally random, non-meaningful text {{bar}}
Expand All @@ -717,11 +715,15 @@ describe('multiple tokens in same file', () => {
const results = await eslint.lintText(code, { filePath: 'my-component.gjs' });

const resultErrors = results.flatMap((result) => result.messages);
expect(resultErrors).toHaveLength(2);
expect(resultErrors).toHaveLength(3);
expect(resultErrors[0].message).toBe("'foo' is not defined.");
expect(resultErrors[0].line).toBe(5);

expect(resultErrors[1].message).toBe("'bar' is not defined.");
expect(resultErrors[1].line).toBe(16);
expect(resultErrors[1].endLine).toBe(13);
expect(resultErrors[1].line).toBe(13);

expect(resultErrors[2].message).toBe("'bar' is not defined.");
expect(resultErrors[2].line).toBe(17);
});
});

0 comments on commit 80d13e2

Please sign in to comment.