ignore-lint-errors 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -3,18 +3,22 @@ import { EOL } from 'node:os';
|
|
|
3
3
|
import { join } from 'node:path';
|
|
4
4
|
import { removeFiles } from '@codemod-utils/files';
|
|
5
5
|
import { outputFilePath, parseOutputFile } from '../../utils/linters/eslint.js';
|
|
6
|
+
function ignoreErrors(file, lintErrors) {
|
|
7
|
+
const lines = file.split(EOL);
|
|
8
|
+
lintErrors.forEach(({ line, message }) => {
|
|
9
|
+
const ignoreDirective = `// eslint-disable-next-line ${message}`;
|
|
10
|
+
lines.splice(line - 1, 0, ignoreDirective);
|
|
11
|
+
});
|
|
12
|
+
return lines.join(EOL);
|
|
13
|
+
}
|
|
6
14
|
export function ignoreErrorsFromEslint(options) {
|
|
7
15
|
const { projectRoot } = options;
|
|
8
16
|
const outputFile = readFileSync(join(projectRoot, outputFilePath), 'utf8');
|
|
9
|
-
const filesWithErrors = parseOutputFile(outputFile);
|
|
10
|
-
filesWithErrors.forEach(({
|
|
11
|
-
const file = readFileSync(
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
const ignoreDirective = `// @ts-expect-error: ${message}`;
|
|
15
|
-
lines.splice(line - 1, 0, ignoreDirective);
|
|
16
|
-
});
|
|
17
|
-
writeFileSync(absoluteFilePath, lines.join(EOL), 'utf8');
|
|
17
|
+
const filesWithErrors = parseOutputFile(outputFile, projectRoot);
|
|
18
|
+
filesWithErrors.forEach(({ filePath, lintErrors }) => {
|
|
19
|
+
const file = readFileSync(join(projectRoot, filePath), 'utf8');
|
|
20
|
+
const newFile = ignoreErrors(file, lintErrors);
|
|
21
|
+
writeFileSync(join(projectRoot, filePath), newFile, 'utf8');
|
|
18
22
|
});
|
|
19
23
|
removeFiles([outputFilePath], { projectRoot });
|
|
20
24
|
}
|
|
@@ -1,23 +1,48 @@
|
|
|
1
1
|
import { readFileSync, writeFileSync } from 'node:fs';
|
|
2
2
|
import { EOL } from 'node:os';
|
|
3
3
|
import { join } from 'node:path';
|
|
4
|
-
import {
|
|
4
|
+
import { updateTemplates } from '@codemod-utils/ast-template-tag';
|
|
5
5
|
import { removeFiles } from '@codemod-utils/files';
|
|
6
|
+
import { findLinesWithTemplate, isParseable, } from '../../utils/ignore-errors/typescript.js';
|
|
6
7
|
import { outputFilePath, parseOutputFile, } from '../../utils/linters/typescript.js';
|
|
7
|
-
function
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
8
|
+
function ignoreErrors(file, lintErrors) {
|
|
9
|
+
const linesWithTemplate = findLinesWithTemplate(file);
|
|
10
|
+
const lines = file.split(EOL);
|
|
11
|
+
lintErrors.forEach(({ line, message }) => {
|
|
12
|
+
const erroredInTemplate = linesWithTemplate.some(([lineStart, lineEnd]) => {
|
|
13
|
+
return lineStart <= line && line <= lineEnd;
|
|
14
|
+
});
|
|
15
|
+
const ignoreDirective = erroredInTemplate
|
|
16
|
+
? `{{! @glint-expect-error: ${message} }}`
|
|
17
|
+
: `// @ts-expect-error: ${message}`;
|
|
18
|
+
lines.splice(line - 1, 0, ignoreDirective);
|
|
19
|
+
});
|
|
20
|
+
return lines.join(EOL);
|
|
21
|
+
}
|
|
22
|
+
// For fallback, ignore type checks in templates
|
|
23
|
+
function ignoreErrorsFallback(file, lintErrors) {
|
|
24
|
+
const linesWithTemplate = findLinesWithTemplate(file);
|
|
25
|
+
const lines = file.split(EOL);
|
|
26
|
+
let hasErrorInTemplate = false;
|
|
27
|
+
lintErrors.forEach(({ line, message }) => {
|
|
28
|
+
const erroredInTemplate = linesWithTemplate.some(([lineStart, lineEnd]) => {
|
|
29
|
+
return lineStart <= line && line <= lineEnd;
|
|
30
|
+
});
|
|
31
|
+
if (erroredInTemplate) {
|
|
32
|
+
hasErrorInTemplate = true;
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const ignoreDirective = `// @ts-expect-error: ${message}`;
|
|
36
|
+
lines.splice(line - 1, 0, ignoreDirective);
|
|
19
37
|
});
|
|
20
|
-
|
|
38
|
+
let newFile = lines.join(EOL);
|
|
39
|
+
if (hasErrorInTemplate) {
|
|
40
|
+
newFile = updateTemplates(newFile, (code) => {
|
|
41
|
+
const ignoreDirective = '{{! @glint-nocheck }}';
|
|
42
|
+
return [ignoreDirective, code].join(EOL);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
return newFile;
|
|
21
46
|
}
|
|
22
47
|
export function ignoreErrorsFromTypescript(options) {
|
|
23
48
|
const { projectRoot } = options;
|
|
@@ -25,18 +50,11 @@ export function ignoreErrorsFromTypescript(options) {
|
|
|
25
50
|
const filesWithErrors = parseOutputFile(outputFile);
|
|
26
51
|
filesWithErrors.forEach(({ filePath, lintErrors }) => {
|
|
27
52
|
const file = readFileSync(join(projectRoot, filePath), 'utf8');
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
});
|
|
34
|
-
const ignoreDirective = erroredInTemplate
|
|
35
|
-
? `{{! @glint-expect-error: ${message} }}`
|
|
36
|
-
: `// @ts-expect-error: ${message}`;
|
|
37
|
-
lines.splice(line - 1, 0, ignoreDirective);
|
|
38
|
-
});
|
|
39
|
-
writeFileSync(join(projectRoot, filePath), lines.join(EOL), 'utf8');
|
|
53
|
+
let newFile = ignoreErrors(file, lintErrors);
|
|
54
|
+
if (!isParseable(newFile)) {
|
|
55
|
+
newFile = ignoreErrorsFallback(file, lintErrors);
|
|
56
|
+
}
|
|
57
|
+
writeFileSync(join(projectRoot, filePath), newFile, 'utf8');
|
|
40
58
|
});
|
|
41
59
|
removeFiles([outputFilePath], { projectRoot });
|
|
42
60
|
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { AST } from '@codemod-utils/ast-template';
|
|
2
|
+
import { findTemplateTags } from '@codemod-utils/ast-template-tag';
|
|
3
|
+
export function findLinesWithTemplate(file) {
|
|
4
|
+
function getLOC(file) {
|
|
5
|
+
const matches = file.match(/\r?\n/g);
|
|
6
|
+
return (matches ?? []).length;
|
|
7
|
+
}
|
|
8
|
+
const templateTags = findTemplateTags(file);
|
|
9
|
+
const linesWithTemplate = [];
|
|
10
|
+
templateTags.forEach((templateTag) => {
|
|
11
|
+
const { range } = templateTag;
|
|
12
|
+
const lineStart = getLOC(file.substring(0, range.startChar)) + 1;
|
|
13
|
+
const lineEnd = getLOC(file.substring(0, range.endChar)) + 1;
|
|
14
|
+
linesWithTemplate.push([lineStart, lineEnd]);
|
|
15
|
+
});
|
|
16
|
+
return linesWithTemplate;
|
|
17
|
+
}
|
|
18
|
+
export function isParseable(file) {
|
|
19
|
+
const traverse = AST.traverse();
|
|
20
|
+
let isParseable = true;
|
|
21
|
+
function parseTemplate(template) {
|
|
22
|
+
try {
|
|
23
|
+
traverse(template);
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
isParseable = false;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const templateTags = findTemplateTags(file);
|
|
30
|
+
templateTags.forEach((templateTag) => {
|
|
31
|
+
parseTemplate(templateTag.contents);
|
|
32
|
+
});
|
|
33
|
+
return isParseable;
|
|
34
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { relative, sep } from 'node:path';
|
|
1
2
|
export const outputFilePath = '.ignore-lint-errors/eslint.txt';
|
|
2
|
-
export function parseOutputFile(file) {
|
|
3
|
+
export function parseOutputFile(file, projectRoot) {
|
|
3
4
|
const filePathToData = new Map();
|
|
4
5
|
const records = JSON.parse(file);
|
|
5
6
|
records.forEach((record) => {
|
|
@@ -13,10 +14,11 @@ export function parseOutputFile(file) {
|
|
|
13
14
|
data.set(line, [ruleId]);
|
|
14
15
|
}
|
|
15
16
|
});
|
|
16
|
-
|
|
17
|
+
const filePath = relative(projectRoot, absoluteFilePath).replaceAll(sep, '/');
|
|
18
|
+
filePathToData.set(filePath, data);
|
|
17
19
|
});
|
|
18
20
|
const filesWithErrors = [];
|
|
19
|
-
filePathToData.forEach((data,
|
|
21
|
+
filePathToData.forEach((data, filePath) => {
|
|
20
22
|
const lintErrors = [];
|
|
21
23
|
data.forEach((allMessages, line) => {
|
|
22
24
|
const ruleIds = Array.from(new Set(allMessages.sort()));
|
|
@@ -35,7 +37,7 @@ export function parseOutputFile(file) {
|
|
|
35
37
|
return 0;
|
|
36
38
|
});
|
|
37
39
|
filesWithErrors.push({
|
|
38
|
-
|
|
40
|
+
filePath,
|
|
39
41
|
lintErrors,
|
|
40
42
|
});
|
|
41
43
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ignore-lint-errors",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Codemod to add ignore directives",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"codemod",
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
"dist"
|
|
18
18
|
],
|
|
19
19
|
"dependencies": {
|
|
20
|
+
"@codemod-utils/ast-template": "^3.0.1",
|
|
20
21
|
"@codemod-utils/ast-template-tag": "^2.1.0",
|
|
21
22
|
"@codemod-utils/files": "^4.0.1",
|
|
22
23
|
"@codemod-utils/package-json": "^4.0.1",
|