Skip to content

Commit

Permalink
Merge pull request #317 from benyap/312-the-extends-field-in-tsconfig…
Browse files Browse the repository at this point in the history
…-is-not-supported

fix: `extends` field in tsconfig not resolved correctly
  • Loading branch information
benyap authored Oct 21, 2023
2 parents be5beb3 + c2bd8ab commit e901ce5
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/steps/loadTSConfig.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { dirname } from "path";
import {
findConfigFile,
parseJsonConfigFileContent,
Expand All @@ -17,6 +18,7 @@ export function loadTSConfig(path: string): TSConfig {
const configFileName = findConfigFile(process.cwd(), sys.fileExists, path);
if (!configFileName) throw new FileNotFoundError(loadTSConfig.name, path);
const configFile = readConfigFile(configFileName, sys.readFile);
const options = parseJsonConfigFileContent(configFile.config, sys, ".");
const directory = dirname(configFileName);
const options = parseJsonConfigFileContent(configFile.config, sys, directory);
return options;
}
2 changes: 1 addition & 1 deletion test/fixtures/module/tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"outDir": "./test/fixtures/module/dist",
"outDir": "./dist",
"baseUrl": ".",
"paths": {
"~/*": ["./src/*"]
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/tsconfig/tsconfig.extends.jsonc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "./test/fixtures/tsconfig/nested/tsconfig.parent.jsonc",
"extends": "./nested/tsconfig.parent.jsonc",
/**
* This is a multiline comment
*/
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/tsconfig/tsconfig.extends.no-base-url.jsonc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "./test/fixtures/tsconfig/nested/tsconfig.parent.no-base-url.jsonc",
"extends": "./nested/tsconfig.parent.no-base-url.jsonc",
/**
* This is a multiline comment
*/
Expand Down
8 changes: 4 additions & 4 deletions test/module/resolveTsPaths.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ describe(resolveTsPaths.name, () => {
files.forEach((path) =>
copyFileSync(
`test/fixtures/module/src/${path}`,
`test/fixtures/module/dist/${path}`
)
`test/fixtures/module/dist/${path}`,
),
);
});

Expand All @@ -31,8 +31,8 @@ describe(resolveTsPaths.name, () => {
});
files.forEach((file) =>
expect(
readFileSync(`test/fixtures/module/dist/${file}`).toString()
).toMatchSnapshot()
readFileSync(`test/fixtures/module/dist/${file}`).toString(),
).toMatchSnapshot(),
);
});
});
23 changes: 20 additions & 3 deletions test/steps/loadTSConfig.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import { describe, expect, it } from "vitest";
import { resolve } from "path";

import { loadTSConfig } from "~/steps/loadTSConfig";

/**
* HACK: transforms any aboslute paths to relative paths to make testing portable
*/
function toRelativePath<T>(path: T): T {
if (typeof path === "string")
return path.replace(resolve("."), "(relative)") as T;
return path;
}

describe("steps/loadTSConfig", () => {
it("loads tsconfig (json) correctly", () => {
const config = loadTSConfig("test/fixtures/tsconfig/tsconfig.test.json");
config.options.pathsBasePath = toRelativePath(config.options.pathsBasePath);
expect(config.options).toMatchInlineSnapshot(`
{
"configFilePath": undefined,
Expand All @@ -29,8 +40,11 @@ describe("steps/loadTSConfig", () => {

it("loads tsconfig (jsonc) with extends correctly", () => {
const config = loadTSConfig(
"test/fixtures/tsconfig/tsconfig.extends.jsonc"
"test/fixtures/tsconfig/tsconfig.extends.jsonc",
);
config.options.baseUrl = toRelativePath(config.options.baseUrl);
config.options.outDir = toRelativePath(config.options.outDir);
config.options.pathsBasePath = toRelativePath(config.options.pathsBasePath);
expect(config.options).toMatchInlineSnapshot(`
{
"baseUrl": "test/fixtures/tsconfig/nested",
Expand All @@ -57,8 +71,10 @@ describe("steps/loadTSConfig", () => {

it("loads tsconfig (jsonc) with extends correctly (no baseUrl)", () => {
const config = loadTSConfig(
"test/fixtures/tsconfig/tsconfig.extends.no-base-url.jsonc"
"test/fixtures/tsconfig/tsconfig.extends.no-base-url.jsonc",
);
config.options.outDir = toRelativePath(config.options.outDir);
config.options.pathsBasePath = toRelativePath(config.options.pathsBasePath);
expect(config.options).toMatchInlineSnapshot(`
{
"configFilePath": undefined,
Expand Down Expand Up @@ -86,8 +102,9 @@ describe("steps/loadTSConfig", () => {
// Turns out the Typescript config loader does some crazy stuff to recover from syntax errors!
it("loads tsconfig with syntax errors (crazy stuff!)", () => {
const config = loadTSConfig(
"test/fixtures/tsconfig/tsconfig.syntax-error.json"
"test/fixtures/tsconfig/tsconfig.syntax-error.json",
);
config.options.pathsBasePath = toRelativePath(config.options.pathsBasePath);
expect(config.options).toMatchInlineSnapshot(`
{
"configFilePath": undefined,
Expand Down

0 comments on commit e901ce5

Please sign in to comment.