Skip to content

Commit

Permalink
only show no-undef errors for templates in gts files
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklx committed May 10, 2023
1 parent 208e6b6 commit 909d1bc
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
46 changes: 45 additions & 1 deletion lib/preprocessors/glimmer.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,30 @@ function mapRange(messages, filename) {

const lines = transformed.split('\n');
const originalLines = original.split('\n');

Check failure on line 125 in lib/preprocessors/glimmer.js

View workflow job for this annotation

GitHub Actions / build (ubuntu, 14.x)

Delete `··`

Check failure on line 125 in lib/preprocessors/glimmer.js

View workflow job for this annotation

GitHub Actions / build (ubuntu, 16.x)

Delete `··`

Check failure on line 125 in lib/preprocessors/glimmer.js

View workflow job for this annotation

GitHub Actions / build (ubuntu, 18.x)

Delete `··`
// get template ranges to remove eslint messages that are inside the templates
const templateRanges = [];
let isInsideTemplate = false;
for (const [index, line] of lines.entries()) {
const lineHasOpeningTag = openingTemplateTagRegex.test(line);
const lineHasClosingTag = closingTemplateTagRegex.test(line);

Check failure on line 132 in lib/preprocessors/glimmer.js

View workflow job for this annotation

GitHub Actions / build (ubuntu, 14.x)

Delete `··`

Check failure on line 132 in lib/preprocessors/glimmer.js

View workflow job for this annotation

GitHub Actions / build (ubuntu, 16.x)

Delete `··`

Check failure on line 132 in lib/preprocessors/glimmer.js

View workflow job for this annotation

GitHub Actions / build (ubuntu, 18.x)

Delete `··`
if (lineHasOpeningTag) {
isInsideTemplate = true;
templateRanges.push({
startLine: index + 1,
endLine: index + 1,
startColumn: line.search(openingTemplateTagRegex) + 1,
});
}

Check failure on line 141 in lib/preprocessors/glimmer.js

View workflow job for this annotation

GitHub Actions / build (ubuntu, 14.x)

Delete `··`

Check failure on line 141 in lib/preprocessors/glimmer.js

View workflow job for this annotation

GitHub Actions / build (ubuntu, 16.x)

Delete `··`

Check failure on line 141 in lib/preprocessors/glimmer.js

View workflow job for this annotation

GitHub Actions / build (ubuntu, 18.x)

Delete `··`
if (lineHasClosingTag && isInsideTemplate) {
isInsideTemplate = false;
const match = line.match(closingTemplateTagRegex);
templateRanges.slice(-1)[0].endLine = index + 1;
templateRanges.slice(-1)[0].endColumn = match.index + match[0].length + 1;
}
}

// this function assumes the original output and transformed output
// are identical, minus the changes to transform `<template>` into the
Expand All @@ -135,7 +159,27 @@ function mapRange(messages, filename) {
);
}

return flattened.map((message) => {
const filtered = flattened.filter(
(it) =>
it.ruleId === 'no-undef' ||
// include if message wraps a template
templateRanges.some(
(tpl) =>
tpl.startLine >= it.line &&
tpl.startColumn >= it.column &&
tpl.endLine <= it.endLine &&
tpl.endColumn <= it.endColumn
) ||
// exclude if message is within template (also when it ends on next line, column 1)
!templateRanges.some(
(tpl) =>
tpl.startLine <= it.line &&
tpl.startColumn <= it.column &&
(tpl.endLine >= it.endLine || (it.endLine === tpl.endLine + 1 && it.endColumn === 1))
)
);

return filtered.map((message) => {
// 1. handle eslint diagnostics on single lines.
if (message.line === message.endLine) {
const originalLine = originalLines[message.line - 1];
Expand Down
3 changes: 2 additions & 1 deletion 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: {
'object-curly-spacing': ['error', 'always'],
'lines-between-class-members': 'error',
'no-undef': 'error',
'no-unused-vars': 'error',
Expand Down Expand Up @@ -388,7 +389,7 @@ describe('multiple tokens in same file', () => {
it('handles duplicate template tokens', async () => {
const eslint = initESLint();
const code = `
// comment Bad
// comment Bad
const tmpl = <template><Bad /></template>
`;
Expand Down

0 comments on commit 909d1bc

Please sign in to comment.