Skip to content

Commit

Permalink
[gjs] Fix bug with regex issues when parsing GLIMMER_TEMPLATE (#1793)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
hmajoros and Henry Majoros committed Mar 1, 2023
1 parent 97069dd commit cc33ece
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/preprocessors/glimmer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ const util = require('ember-template-imports/src/util');
const TRANSFORM_CACHE = new Map();
const TEXT_CACHE = new Map();

// source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
function escapeRegExp(string) {
return string.replace(/[$()*+.?[\\\]^{|}]/g, '\\$&'); // $& means the whole matched string
}

/**
* This function is responsible for running the embedded templates transform
* from ember-template-imports.
Expand Down Expand Up @@ -122,7 +127,7 @@ function mapRange(messages, filename) {
let originalColumnNumber = 0;

for (const [index, line] of originalLines.entries()) {
const column = line.search(new RegExp(`\\b${token}\\b`));
const column = line.search(new RegExp(`\\b${escapeRegExp(token)}\\b`));
if (column > -1) {
originalLineNumber = index + 1;
originalColumnNumber = column + 1;
Expand Down
28 changes: 28 additions & 0 deletions tests/lib/rules-preprocessor/gjs-gts-processor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function initESLint(options) {
plugins: ['ember'],
extends: ['plugin:ember/recommended'],
rules: {
'lines-between-class-members': 'error',
'no-undef': 'error',
'ember/no-get': 'off',
'ember/no-array-prototype-extensions': 'error',
Expand Down Expand Up @@ -254,3 +255,30 @@ describe('line/col numbers should be correct', () => {
});
});
});

describe('lint errors on the exact line as the <template> tag', () => {
it('correctly outputs the lint error', async () => {
const eslint = initESLint();
const code = `
import Component from '@glimmer/component';
export default class MyComponent extends Component {
constructor() {
super(...arguments);
}
foo = 'bar';
<template>
<div>
some totally random, non-meaningful text
</div>
</template>
}
`;
const results = await eslint.lintText(code, { filePath: 'my-component.gjs' });

const resultErrors = results.flatMap((result) => result.messages);
expect(resultErrors).toHaveLength(1);
expect(resultErrors[0].message).toBe('Expected blank line between class members.');
});
});

0 comments on commit cc33ece

Please sign in to comment.