Skip to content

Commit

Permalink
Support ${configDir} interpolation for all the fields
Browse files Browse the repository at this point in the history
  • Loading branch information
trikadin committed Sep 11, 2024
1 parent eba8538 commit 03f5234
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# JetBrains (WebStorm)
.idea

# Dependency directories
node_modules/

Expand Down
60 changes: 46 additions & 14 deletions src/parse-tsconfig/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const resolveExtends = (
const { compilerOptions } = extendsConfig;
if (compilerOptions) {
const { baseUrl } = compilerOptions;
if (baseUrl) {
if (baseUrl != null && !baseUrl.startsWith(configDirPlaceholder)) {

Check failure on line 40 in src/parse-tsconfig/index.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Use '===' to compare with null
compilerOptions.baseUrl = slash(
path.relative(
fromDirectoryPath,
Expand Down Expand Up @@ -166,7 +166,7 @@ const _parseTsconfig = (

for (const property of normalizedPaths) {
const unresolvedPath = compilerOptions[property];
if (unresolvedPath) {
if (unresolvedPath != null && !unresolvedPath.startsWith(configDirPlaceholder)) {

Check failure on line 169 in src/parse-tsconfig/index.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Use '===' to compare with null
const resolvedBaseUrl = path.resolve(directoryPath, unresolvedPath);
const relativeBaseUrl = normalizeRelativePath(path.relative(
directoryPath,
Expand Down Expand Up @@ -222,14 +222,26 @@ const _parseTsconfig = (
return config;
};

const interpolateConfigDir = (
filePath: string,
function interpolateConfigDir<T extends string | string[]> (

Check failure on line 225 in src/parse-tsconfig/index.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Unexpected space before function parentheses
filePaths: T,
configDir: string,
) => {
if (filePath.startsWith(configDirPlaceholder)) {
return slash(path.join(configDir, filePath.slice(configDirPlaceholder.length)));
postProcess?: (input: string) => string
): T extends string ? string : string[];
function interpolateConfigDir (

Check failure on line 230 in src/parse-tsconfig/index.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Prefer arrow function

Check failure on line 230 in src/parse-tsconfig/index.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Unexpected space before function parentheses
filePaths: string | string[],
configDir: string,
postProcess: (input: string) => string = (value) => value

Check failure on line 233 in src/parse-tsconfig/index.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Unexpected parentheses around single function argument having a body with no curly braces

Check failure on line 233 in src/parse-tsconfig/index.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Missing trailing comma
): string | string[] {
if (Array.isArray(filePaths)) {
return filePaths.map((filePath) => interpolateConfigDir(filePath, configDir, postProcess));

Check failure on line 236 in src/parse-tsconfig/index.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Unexpected parentheses around single function argument having a body with no curly braces
}

if (filePaths.startsWith(configDirPlaceholder)) {
return postProcess(slash(path.join(configDir, filePaths.slice(configDirPlaceholder.length))));
}
};

return filePaths;
}

export const parseTsconfig = (
tsconfigPath: string,
Expand All @@ -238,14 +250,34 @@ export const parseTsconfig = (
const resolvedTsconfigPath = path.resolve(tsconfigPath);
const config = _parseTsconfig(resolvedTsconfigPath, cache);

/**
* @see https://github.com/microsoft/TypeScript/issues/57485#issuecomment-2027787456
* exclude paths, as it requires custom processing
*/
const compilerFieldsWithConfigDir = [
'outDir',
'declarationDir',
'outFile',
'rootDir',
'baseUrl',
'tsBuildInfoFile',
'rootDirs',
'typeRoots',
] as const satisfies Array<keyof NonNullable<TsConfigJson['compilerOptions']>>;

const configDir = path.dirname(resolvedTsconfigPath);
if (config.compilerOptions) {
let { outDir } = config.compilerOptions;
if (outDir) {
const interpolated = interpolateConfigDir(outDir, configDir);
if (interpolated) {
outDir = normalizeRelativePath(path.relative(configDir, interpolated));
config.compilerOptions.outDir = outDir;
for (const field of compilerFieldsWithConfigDir) {
const value = config.compilerOptions[field];

if (value != null) {

Check failure on line 273 in src/parse-tsconfig/index.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Use '===' to compare with null
/**
* I used Object.assign instead of the direct assignment to work around TS bug (it fails to infer types correctly).

Check failure on line 275 in src/parse-tsconfig/index.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

This line has a length of 123. Maximum allowed is 100
* @see https://github.com/microsoft/TypeScript/issues/33912
*/
Object.assign(config.compilerOptions, {
[field]: interpolateConfigDir(value, configDir, (interpolated) => normalizeRelativePath(path.relative(configDir, interpolated)))
});
}
}

Expand Down

0 comments on commit 03f5234

Please sign in to comment.