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 4, 2023
1 parent 208e6b6 commit c889e14
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion lib/preprocessors/glimmer.js
Expand Up @@ -8,6 +8,7 @@ const util = require('ember-template-imports/src/util');

const TRANSFORM_CACHE = new Map();
const TEXT_CACHE = new Map();
const RULE_CACHE = new Map();

function arrayEq(arr1, arr2) {
return arr1.length === arr2.length && arr1.every((val, idx) => val === arr2[idx]);
Expand Down Expand Up @@ -86,10 +87,33 @@ function gjs(text, filename) {
//
// We need to disable all of eslint around this "scope" return
// value and allow-list the rules that check for undefined identifiers
const groups = [];
const transformed = preprocessed.output;
let isInsideTemplate = false;
for (const [index, line] of transformed.split('\n').entries()) {
const lineHasOpeningTag = openingTemplateTagRegex.test(line);
const lineHasClosingTag = closingTemplateTagRegex.test(line);

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

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

TRANSFORM_CACHE.set(filename, transformed);
TEXT_CACHE.set(filename, text);
RULE_CACHE.set(filename, groups);

return [
{
Expand All @@ -108,6 +132,7 @@ function gjs(text, filename) {
function mapRange(messages, filename) {
const transformed = TRANSFORM_CACHE.get(filename);
const original = TEXT_CACHE.get(filename);
const groups = RULE_CACHE.get(filename);
const flattened = messages.flat();

if (!transformed) {
Expand Down Expand Up @@ -135,7 +160,29 @@ function mapRange(messages, filename) {
);
}

return flattened.map((message) => {
const filtered = flattened.filter(
(it) =>
it.ruleId === 'no-undef' ||
groups.some(
(
g // include if message wraps group
) =>
g.startLine >= it.line &&
g.startColumn >= it.column &&
g.endLine <= it.endLine &&
g.endColumn <= it.endColumn
) ||
!groups.some(
(
g // exclude if message is within group (also when it ends on next line, column 1)
) =>
g.startLine <= it.line &&
g.startColumn <= it.column &&
(g.endLine >= it.endLine || (it.endLine === g.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

0 comments on commit c889e14

Please sign in to comment.