ignore-lint-errors 0.1.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.
@@ -8,7 +8,7 @@ process.title = 'ignore-lint-errors';
8
8
  // Set codemod options
9
9
  const argv = yargs(hideBin(process.argv))
10
10
  .option('linter', {
11
- choices: ['eslint'],
11
+ choices: ['eslint', 'typescript'],
12
12
  describe: 'Linter to run',
13
13
  type: 'string',
14
14
  })
@@ -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,24 @@
1
- import { existsSync, readFileSync, writeFileSync } from 'node:fs';
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';
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
+ }
5
14
  export function ignoreErrorsFromEslint(options) {
6
15
  const { projectRoot } = options;
7
- if (!existsSync(join(projectRoot, outputFilePath))) {
8
- return;
9
- }
10
16
  const outputFile = readFileSync(join(projectRoot, outputFilePath), 'utf8');
11
- const filesWithErrors = parseOutputFile(outputFile);
12
- filesWithErrors.forEach(({ absoluteFilePath, lintErrors }) => {
13
- const file = readFileSync(absoluteFilePath, 'utf8');
14
- const lines = file.split('\n');
15
- lintErrors.forEach(({ line, message }) => {
16
- const ignoreDirective = `// @ts-expect-error: ${message}`;
17
- lines.splice(line - 1, 0, ignoreDirective);
18
- });
19
- writeFileSync(absoluteFilePath, lines.join('\n'), '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');
20
22
  });
21
23
  removeFiles([outputFilePath], { projectRoot });
22
24
  }
@@ -1 +1,2 @@
1
1
  export * from './eslint.js';
2
+ export * from './typescript.js';
@@ -0,0 +1,60 @@
1
+ import { readFileSync, writeFileSync } from 'node:fs';
2
+ import { EOL } from 'node:os';
3
+ import { join } from 'node:path';
4
+ import { updateTemplates } from '@codemod-utils/ast-template-tag';
5
+ import { removeFiles } from '@codemod-utils/files';
6
+ import { findLinesWithTemplate, isParseable, } from '../../utils/ignore-errors/typescript.js';
7
+ import { outputFilePath, parseOutputFile, } from '../../utils/linters/typescript.js';
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);
37
+ });
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;
46
+ }
47
+ export function ignoreErrorsFromTypescript(options) {
48
+ const { projectRoot } = options;
49
+ const outputFile = readFileSync(join(projectRoot, outputFilePath), 'utf8');
50
+ const filesWithErrors = parseOutputFile(outputFile);
51
+ filesWithErrors.forEach(({ filePath, lintErrors }) => {
52
+ const file = readFileSync(join(projectRoot, filePath), 'utf8');
53
+ let newFile = ignoreErrors(file, lintErrors);
54
+ if (!isParseable(newFile)) {
55
+ newFile = ignoreErrorsFallback(file, lintErrors);
56
+ }
57
+ writeFileSync(join(projectRoot, filePath), newFile, 'utf8');
58
+ });
59
+ removeFiles([outputFilePath], { projectRoot });
60
+ }
@@ -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
- ignoreErrorsFromEslint(options);
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 { command } from '../../utils/linters/eslint.js';
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
  }
@@ -1 +1,2 @@
1
1
  export * from './eslint.js';
2
+ export * from './typescript.js';
@@ -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
- const tempDir = '.ignore-lint-errors';
7
- if (!existsSync(join(projectRoot, tempDir))) {
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
- runEslint(options);
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
  }
@@ -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,11 +1,6 @@
1
+ import { relative, sep } from 'node:path';
1
2
  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
- export function parseOutputFile(file) {
3
+ export function parseOutputFile(file, projectRoot) {
9
4
  const filePathToData = new Map();
10
5
  const records = JSON.parse(file);
11
6
  records.forEach((record) => {
@@ -19,10 +14,11 @@ export function parseOutputFile(file) {
19
14
  data.set(line, [ruleId]);
20
15
  }
21
16
  });
22
- filePathToData.set(absoluteFilePath, data);
17
+ const filePath = relative(projectRoot, absoluteFilePath).replaceAll(sep, '/');
18
+ filePathToData.set(filePath, data);
23
19
  });
24
20
  const filesWithErrors = [];
25
- filePathToData.forEach((data, absoluteFilePath) => {
21
+ filePathToData.forEach((data, filePath) => {
26
22
  const lintErrors = [];
27
23
  data.forEach((allMessages, line) => {
28
24
  const ruleIds = Array.from(new Set(allMessages.sort()));
@@ -41,7 +37,7 @@ export function parseOutputFile(file) {
41
37
  return 0;
42
38
  });
43
39
  filesWithErrors.push({
44
- absoluteFilePath,
40
+ filePath,
45
41
  lintErrors,
46
42
  });
47
43
  });
@@ -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.1.0",
3
+ "version": "0.3.0",
4
4
  "description": "Codemod to add ignore directives",
5
5
  "keywords": [
6
6
  "codemod",
@@ -17,7 +17,10 @@
17
17
  "dist"
18
18
  ],
19
19
  "dependencies": {
20
+ "@codemod-utils/ast-template": "^3.0.1",
21
+ "@codemod-utils/ast-template-tag": "^2.1.0",
20
22
  "@codemod-utils/files": "^4.0.1",
23
+ "@codemod-utils/package-json": "^4.0.1",
21
24
  "yargs": "^18.0.0"
22
25
  },
23
26
  "devDependencies": {