Skip to content

Commit 00e4fb9

Browse files
authored
Merge pull request #42 from vitonsky/37-comments-in-tsconfig-break-the-linter
feat: parse json configs even with comments
2 parents 7f6f4b5 + 08dfc02 commit 00e4fb9

File tree

4 files changed

+53
-3
lines changed

4 files changed

+53
-3
lines changed

package-lock.json

Lines changed: 45 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,8 @@
4646
"ts-jest": "^29.1.2",
4747
"tslib": "^2.6.2",
4848
"typescript": "^5.4.3"
49+
},
50+
"dependencies": {
51+
"comment-json": "^4.2.3"
4952
}
5053
}

src/rules/alias.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
import { parse as parseJsonWithComments } from 'comment-json';
34
import { Rule } from 'eslint';
45
import fs from 'fs';
56
import path from 'path';
@@ -34,11 +35,12 @@ function findAlias(
3435
: null;
3536

3637
if (configFile) {
37-
const tsconfig = JSON.parse(
38+
const tsconfig = parseJsonWithComments(
3839
fs.readFileSync(path.join(baseDir, configFile)).toString('utf8'),
3940
);
4041

41-
const paths: Record<string, string[]> = tsconfig?.compilerOptions?.paths ?? {};
42+
const paths: Record<string, string[]> =
43+
(tsconfig as any)?.compilerOptions?.paths ?? {};
4244
for (const [alias, aliasPaths] of Object.entries(paths)) {
4345
// TODO: support full featured glob patterns instead of trivial cases like `@utils/*` and `src/utils/*`
4446
const matchedPath = aliasPaths.find((dirPath) => {

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"allowJs": true,
3030
"resolveJsonModule": true,
3131
"skipLibCheck": true,
32+
// Test comment, that must not break a parsing of config file
3233
"baseUrl": ".",
3334
"paths": {
3435
"@foo/*": ["src/foo/*"],

0 commit comments

Comments
 (0)