Skip to content

Commit cc33ece

Browse files
hmajorosHenry Majoros
andauthored
[gjs] Fix bug with regex issues when parsing GLIMMER_TEMPLATE (#1793)
* failing test case for regex issues when parsing GLIMMER_TEMPLATE * update test assertion to be correct once fix is applied * fix test by properly escaping text before constructing regex * rename test --------- Co-authored-by: Henry Majoros <hmajoros@linkedin.com>
1 parent 97069dd commit cc33ece

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

lib/preprocessors/glimmer.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ const util = require('ember-template-imports/src/util');
99
const TRANSFORM_CACHE = new Map();
1010
const TEXT_CACHE = new Map();
1111

12+
// source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
13+
function escapeRegExp(string) {
14+
return string.replace(/[$()*+.?[\\\]^{|}]/g, '\\$&'); // $& means the whole matched string
15+
}
16+
1217
/**
1318
* This function is responsible for running the embedded templates transform
1419
* from ember-template-imports.
@@ -122,7 +127,7 @@ function mapRange(messages, filename) {
122127
let originalColumnNumber = 0;
123128

124129
for (const [index, line] of originalLines.entries()) {
125-
const column = line.search(new RegExp(`\\b${token}\\b`));
130+
const column = line.search(new RegExp(`\\b${escapeRegExp(token)}\\b`));
126131
if (column > -1) {
127132
originalLineNumber = index + 1;
128133
originalColumnNumber = column + 1;

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ function initESLint(options) {
3535
plugins: ['ember'],
3636
extends: ['plugin:ember/recommended'],
3737
rules: {
38+
'lines-between-class-members': 'error',
3839
'no-undef': 'error',
3940
'ember/no-get': 'off',
4041
'ember/no-array-prototype-extensions': 'error',
@@ -254,3 +255,30 @@ describe('line/col numbers should be correct', () => {
254255
});
255256
});
256257
});
258+
259+
describe('lint errors on the exact line as the <template> tag', () => {
260+
it('correctly outputs the lint error', async () => {
261+
const eslint = initESLint();
262+
const code = `
263+
import Component from '@glimmer/component';
264+
265+
export default class MyComponent extends Component {
266+
constructor() {
267+
super(...arguments);
268+
}
269+
270+
foo = 'bar';
271+
<template>
272+
<div>
273+
some totally random, non-meaningful text
274+
</div>
275+
</template>
276+
}
277+
`;
278+
const results = await eslint.lintText(code, { filePath: 'my-component.gjs' });
279+
280+
const resultErrors = results.flatMap((result) => result.messages);
281+
expect(resultErrors).toHaveLength(1);
282+
expect(resultErrors[0].message).toBe('Expected blank line between class members.');
283+
});
284+
});

0 commit comments

Comments
 (0)