ignore-lint-errors 0.1.0 → 0.2.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.
- package/dist/bin/ignore-lint-errors.js +1 -1
- package/dist/src/steps/create-options.js +15 -0
- package/dist/src/steps/ignore-errors/eslint.js +4 -6
- package/dist/src/steps/ignore-errors/index.js +1 -0
- package/dist/src/steps/ignore-errors/typescript.js +42 -0
- package/dist/src/steps/ignore-errors.js +11 -3
- package/dist/src/steps/lint-files/eslint.js +7 -1
- package/dist/src/steps/lint-files/index.js +1 -0
- package/dist/src/steps/lint-files/typescript.js +14 -0
- package/dist/src/steps/lint-files.js +13 -6
- package/dist/src/utils/linters/eslint.js +0 -6
- package/dist/src/utils/linters/typescript.js +49 -0
- package/package.json +3 -1
|
@@ -1,6 +1,21 @@
|
|
|
1
|
+
import { readPackageJson } from '@codemod-utils/package-json';
|
|
2
|
+
function findDependencies(projectRoot) {
|
|
3
|
+
const packageJson = readPackageJson({ projectRoot });
|
|
4
|
+
const projectDependencies = new Set([
|
|
5
|
+
...Object.keys(packageJson['dependencies'] ?? {}),
|
|
6
|
+
...Object.keys(packageJson['devDependencies'] ?? {}),
|
|
7
|
+
]);
|
|
8
|
+
return {
|
|
9
|
+
eslint: projectDependencies.has('eslint'),
|
|
10
|
+
glint: projectDependencies.has('@glint/core') ||
|
|
11
|
+
projectDependencies.has('@glint/ember-tsc'),
|
|
12
|
+
typescript: projectDependencies.has('typescript'),
|
|
13
|
+
};
|
|
14
|
+
}
|
|
1
15
|
export function createOptions(codemodOptions) {
|
|
2
16
|
const { linter, projectRoot } = codemodOptions;
|
|
3
17
|
return {
|
|
18
|
+
dependencies: findDependencies(projectRoot),
|
|
4
19
|
linter,
|
|
5
20
|
projectRoot,
|
|
6
21
|
};
|
|
@@ -1,22 +1,20 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { readFileSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { EOL } from 'node:os';
|
|
2
3
|
import { join } from 'node:path';
|
|
3
4
|
import { removeFiles } from '@codemod-utils/files';
|
|
4
5
|
import { outputFilePath, parseOutputFile } from '../../utils/linters/eslint.js';
|
|
5
6
|
export function ignoreErrorsFromEslint(options) {
|
|
6
7
|
const { projectRoot } = options;
|
|
7
|
-
if (!existsSync(join(projectRoot, outputFilePath))) {
|
|
8
|
-
return;
|
|
9
|
-
}
|
|
10
8
|
const outputFile = readFileSync(join(projectRoot, outputFilePath), 'utf8');
|
|
11
9
|
const filesWithErrors = parseOutputFile(outputFile);
|
|
12
10
|
filesWithErrors.forEach(({ absoluteFilePath, lintErrors }) => {
|
|
13
11
|
const file = readFileSync(absoluteFilePath, 'utf8');
|
|
14
|
-
const lines = file.split(
|
|
12
|
+
const lines = file.split(EOL);
|
|
15
13
|
lintErrors.forEach(({ line, message }) => {
|
|
16
14
|
const ignoreDirective = `// @ts-expect-error: ${message}`;
|
|
17
15
|
lines.splice(line - 1, 0, ignoreDirective);
|
|
18
16
|
});
|
|
19
|
-
writeFileSync(absoluteFilePath, lines.join(
|
|
17
|
+
writeFileSync(absoluteFilePath, lines.join(EOL), 'utf8');
|
|
20
18
|
});
|
|
21
19
|
removeFiles([outputFilePath], { projectRoot });
|
|
22
20
|
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { readFileSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { EOL } from 'node:os';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { findTemplateTags } from '@codemod-utils/ast-template-tag';
|
|
5
|
+
import { removeFiles } from '@codemod-utils/files';
|
|
6
|
+
import { outputFilePath, parseOutputFile, } from '../../utils/linters/typescript.js';
|
|
7
|
+
function findLinesWithTemplate(file) {
|
|
8
|
+
function getLOC(file) {
|
|
9
|
+
const matches = file.match(/\r?\n/g);
|
|
10
|
+
return (matches ?? []).length;
|
|
11
|
+
}
|
|
12
|
+
const templateTags = findTemplateTags(file);
|
|
13
|
+
const linesWithTemplate = [];
|
|
14
|
+
templateTags.forEach((templateTag) => {
|
|
15
|
+
const { range } = templateTag;
|
|
16
|
+
const lineStart = getLOC(file.substring(0, range.startChar)) + 1;
|
|
17
|
+
const lineEnd = getLOC(file.substring(0, range.endChar)) + 1;
|
|
18
|
+
linesWithTemplate.push([lineStart, lineEnd]);
|
|
19
|
+
});
|
|
20
|
+
return linesWithTemplate;
|
|
21
|
+
}
|
|
22
|
+
export function ignoreErrorsFromTypescript(options) {
|
|
23
|
+
const { projectRoot } = options;
|
|
24
|
+
const outputFile = readFileSync(join(projectRoot, outputFilePath), 'utf8');
|
|
25
|
+
const filesWithErrors = parseOutputFile(outputFile);
|
|
26
|
+
filesWithErrors.forEach(({ filePath, lintErrors }) => {
|
|
27
|
+
const file = readFileSync(join(projectRoot, filePath), 'utf8');
|
|
28
|
+
const lines = file.split(EOL);
|
|
29
|
+
const linesWithTemplate = findLinesWithTemplate(file);
|
|
30
|
+
lintErrors.forEach(({ line, message }) => {
|
|
31
|
+
const erroredInTemplate = linesWithTemplate.some(([lineStart, lineEnd]) => {
|
|
32
|
+
return lineStart <= line && line <= lineEnd;
|
|
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');
|
|
40
|
+
});
|
|
41
|
+
removeFiles([outputFilePath], { projectRoot });
|
|
42
|
+
}
|
|
@@ -1,9 +1,17 @@
|
|
|
1
|
-
import { ignoreErrorsFromEslint } from './ignore-errors/index.js';
|
|
1
|
+
import { ignoreErrorsFromEslint, ignoreErrorsFromTypescript, } from './ignore-errors/index.js';
|
|
2
2
|
export function ignoreErrors(options) {
|
|
3
|
-
const { linter } = options;
|
|
3
|
+
const { dependencies, linter } = options;
|
|
4
4
|
switch (linter) {
|
|
5
5
|
case 'eslint': {
|
|
6
|
-
|
|
6
|
+
if (dependencies.eslint) {
|
|
7
|
+
ignoreErrorsFromEslint(options);
|
|
8
|
+
}
|
|
9
|
+
break;
|
|
10
|
+
}
|
|
11
|
+
case 'typescript': {
|
|
12
|
+
if (dependencies.typescript) {
|
|
13
|
+
ignoreErrorsFromTypescript(options);
|
|
14
|
+
}
|
|
7
15
|
break;
|
|
8
16
|
}
|
|
9
17
|
}
|
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import { execSync } from 'node:child_process';
|
|
2
|
-
import {
|
|
2
|
+
import { outputFilePath } from '../../utils/linters/eslint.js';
|
|
3
3
|
export function runEslint(options) {
|
|
4
4
|
const { projectRoot } = options;
|
|
5
|
+
const command = [
|
|
6
|
+
'./node_modules/.bin/eslint',
|
|
7
|
+
'--format json',
|
|
8
|
+
`--output-file ${outputFilePath}`,
|
|
9
|
+
'--quiet',
|
|
10
|
+
].join(' ');
|
|
5
11
|
try {
|
|
6
12
|
execSync(command, { cwd: projectRoot });
|
|
7
13
|
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { execSync } from 'node:child_process';
|
|
2
|
+
import { outputFilePath } from '../../utils/linters/typescript.js';
|
|
3
|
+
export function runTypescript(options) {
|
|
4
|
+
const { dependencies, projectRoot } = options;
|
|
5
|
+
const command = dependencies.glint
|
|
6
|
+
? `./node_modules/.bin/ember-tsc > ${outputFilePath}`
|
|
7
|
+
: `./node_modules/.bin/tsc > ${outputFilePath}`;
|
|
8
|
+
try {
|
|
9
|
+
execSync(command, { cwd: projectRoot });
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
// Do nothing
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -1,15 +1,22 @@
|
|
|
1
1
|
import { existsSync, mkdirSync } from 'node:fs';
|
|
2
2
|
import { join } from 'node:path';
|
|
3
|
-
import { runEslint } from './lint-files/index.js';
|
|
3
|
+
import { runEslint, runTypescript } from './lint-files/index.js';
|
|
4
4
|
export function lintFiles(options) {
|
|
5
|
-
const { linter, projectRoot } = options;
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
mkdirSync(join(projectRoot, tempDir));
|
|
5
|
+
const { dependencies, linter, projectRoot } = options;
|
|
6
|
+
if (!existsSync(join(projectRoot, '.ignore-lint-errors'))) {
|
|
7
|
+
mkdirSync(join(projectRoot, '.ignore-lint-errors'));
|
|
9
8
|
}
|
|
10
9
|
switch (linter) {
|
|
11
10
|
case 'eslint': {
|
|
12
|
-
|
|
11
|
+
if (dependencies.eslint) {
|
|
12
|
+
runEslint(options);
|
|
13
|
+
}
|
|
14
|
+
break;
|
|
15
|
+
}
|
|
16
|
+
case 'typescript': {
|
|
17
|
+
if (dependencies.typescript) {
|
|
18
|
+
runTypescript(options);
|
|
19
|
+
}
|
|
13
20
|
break;
|
|
14
21
|
}
|
|
15
22
|
}
|
|
@@ -1,10 +1,4 @@
|
|
|
1
1
|
export const outputFilePath = '.ignore-lint-errors/eslint.txt';
|
|
2
|
-
export const command = [
|
|
3
|
-
'./node_modules/.bin/eslint',
|
|
4
|
-
'--format json',
|
|
5
|
-
`--output-file ${outputFilePath}`,
|
|
6
|
-
'--quiet',
|
|
7
|
-
].join(' ');
|
|
8
2
|
export function parseOutputFile(file) {
|
|
9
3
|
const filePathToData = new Map();
|
|
10
4
|
const records = JSON.parse(file);
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { EOL } from 'node:os';
|
|
2
|
+
export const outputFilePath = '.ignore-lint-errors/typescript.txt';
|
|
3
|
+
export function parseOutputFile(file) {
|
|
4
|
+
const filePathToData = new Map();
|
|
5
|
+
file.split(EOL).forEach((str) => {
|
|
6
|
+
const matches = str.match(/^(.+)\((\d+),\d+\): error TS\d+: (.+)\.$/);
|
|
7
|
+
if (matches === null) {
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
const filePath = matches[1];
|
|
11
|
+
const line = Number.parseInt(matches[2]);
|
|
12
|
+
const message = matches[3];
|
|
13
|
+
if (filePathToData.has(filePath)) {
|
|
14
|
+
if (filePathToData.get(filePath).has(line)) {
|
|
15
|
+
filePathToData.get(filePath).get(line).push(message);
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
filePathToData.get(filePath).set(line, [message]);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
filePathToData.set(filePath, new Map([[line, [message]]]));
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
const filesWithErrors = [];
|
|
26
|
+
filePathToData.forEach((data, filePath) => {
|
|
27
|
+
const lintErrors = [];
|
|
28
|
+
data.forEach((_allMessages, line) => {
|
|
29
|
+
lintErrors.push({
|
|
30
|
+
line,
|
|
31
|
+
message: 'Incorrect type',
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
lintErrors.sort((a, b) => {
|
|
35
|
+
if (a.line < b.line) {
|
|
36
|
+
return 1;
|
|
37
|
+
}
|
|
38
|
+
if (b.line < a.line) {
|
|
39
|
+
return -1;
|
|
40
|
+
}
|
|
41
|
+
return 0;
|
|
42
|
+
});
|
|
43
|
+
filesWithErrors.push({
|
|
44
|
+
filePath,
|
|
45
|
+
lintErrors,
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
return filesWithErrors;
|
|
49
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ignore-lint-errors",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Codemod to add ignore directives",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"codemod",
|
|
@@ -17,7 +17,9 @@
|
|
|
17
17
|
"dist"
|
|
18
18
|
],
|
|
19
19
|
"dependencies": {
|
|
20
|
+
"@codemod-utils/ast-template-tag": "^2.1.0",
|
|
20
21
|
"@codemod-utils/files": "^4.0.1",
|
|
22
|
+
"@codemod-utils/package-json": "^4.0.1",
|
|
21
23
|
"yargs": "^18.0.0"
|
|
22
24
|
},
|
|
23
25
|
"devDependencies": {
|