Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,34 @@ async function load(resolveFrom: string, options, logger): Promise<PathMapType>
return result;
}

async function loadConfig(options, resolveFrom) {
async function loadConfigByName(names, options, resolveFrom) {
let result = await loadConfigUtil(
options.inputFS,
resolveFrom,
['tsconfig.json', 'tsconfig.js'],
names,
options.projectRoot
);
if (!result?.config) {
throw new Error(`Missing or invalid tsconfig.json in project root (${options.projectRoot})`);
throw new Error(`Missing or invalid ${names.join(',')} in project root (${options.projectRoot})`);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add space after comma

}
return result.config;
}

async function loadCompilerOptions(options, resolveFrom) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To reduce references to compilerOptions let's keep loadConfig and have loadCompilerOptions call the former and do error checks

let baseConfig = await loadConfigByName(['tsconfig.json', 'tsconfig.js'], options, resolveFrom)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The naming of this variable is ambiguous. I suggest childConfig for this line and parentConfig in place of extendsConfig

if (!baseConfig.extends) {
return baseConfig.compilerOptions
}
let extendsConfig = await loadConfigByName([baseConfig.extends], options, resolveFrom)
return extendsConfig?.compilerOptions || baseConfig?.compilerOptions ? {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the project tsconfig extends another, this line drops the base config entirely. Unless items are undefined (I expect missing keys to just be missing rather than undefined, but I'll double check), it should just be:

return {
  ...extendsConfig.compilerOptions,
  ...baseConfig.compilerOptions,
};

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loadConfigByName checks for null results already, so qualifiers are not needed

...baseConfig?.compilerOptions,
...extendsConfig?.compilerOptions
} : null
}

/** Populate a map with any paths from tsconfig.json starting from baseUrl */
async function loadTsPaths(resolveFrom: string, options, logger): Promise<PathMapType> {
let config = await loadConfig(options, resolveFrom);
let compilerOptions = config?.['compilerOptions'];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to keep this line just to reduce code size

let compilerOptions = await loadCompilerOptions(options, resolveFrom);
if (!compilerOptions) {
throw new Error(`Couldn't find compilerOptions in tsconfig`);
}
Expand Down