-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from vitonsky/26-add-option-to-provide-a-path-…
…to-tsconfigjsconfig feat: Add option to provide a path to tsconfig/jsconfig
Showing
7 changed files
with
159 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export type CompilerOptions = { | ||
baseUrl?: string; | ||
paths: Record<string, string[]>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { parse as parseJsonWithComments } from 'comment-json'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
import { CompilerOptions } from '../types'; | ||
|
||
export function getCompilerConfigFromFile( | ||
baseDir: string, | ||
configFilePath?: string, | ||
): CompilerOptions | null { | ||
if (!configFilePath) { | ||
// Looking for a config file | ||
for (const filename of ['tsconfig.json', 'jsconfig.json']) { | ||
const resolvedPath = path.resolve(path.join(baseDir, filename)); | ||
const isFileExists = fs.existsSync(resolvedPath); | ||
if (isFileExists) { | ||
configFilePath = resolvedPath; | ||
break; | ||
} | ||
} | ||
|
||
if (!configFilePath) return null; | ||
} | ||
|
||
const tsconfig = parseJsonWithComments( | ||
fs.readFileSync(path.resolve(configFilePath)).toString('utf8'), | ||
); | ||
|
||
// TODO: validate options | ||
const { baseUrl, paths = {} } = (tsconfig as any)?.compilerOptions ?? {}; | ||
|
||
return { | ||
baseUrl, | ||
paths, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"compilerOptions": { | ||
// Test comment, that must not break a parsing of config file | ||
// See more info in https://github.com/vitonsky/eslint-plugin-paths/issues/37#issuecomment-2052542343 | ||
"baseUrl": ".", | ||
"paths": { | ||
"@foo/*": ["src/foo/*"], | ||
"@qux/*": ["src/qux/*"] | ||
} | ||
} | ||
} |