-
Notifications
You must be signed in to change notification settings - Fork 105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: use get-tsconfig to handle tsconfig file resolution #247
base: master
Are you sure you want to change the base?
Changes from 1 commit
834b4b5
525ffa2
ec0cda5
5d96305
bdec663
5d2f1c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"compilerOptions": { | ||
"paths": { | ||
"@": [] | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "./tsconfig.preset.json" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "./tsconfig.base.json" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ | |
"node": ">=6" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^27.0.3", | ||
"@types/jest": "^29.5.0", | ||
"@types/minimist": "^1.2.2", | ||
"@types/node": "^6.0.54", | ||
"@types/strip-bom": "^3.0.0", | ||
|
@@ -32,18 +32,17 @@ | |
"eslint-plugin-import": "^2.26.0", | ||
"eslint-plugin-jsdoc": "^39.2.9", | ||
"husky": "^4.2.5", | ||
"jest": "^27.3.1", | ||
"jest": "^29.5.0", | ||
"lint-staged": "^10.2.11", | ||
"prettier": "^2.0.5", | ||
"rimraf": "^2.6.2", | ||
"ts-jest": "^27.0.7", | ||
"ts-jest": "^29.1.0", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updating |
||
"ts-node": "^10.7.0", | ||
"typescript": "^4.5.2" | ||
"typescript": "^5.0.3" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need to update |
||
}, | ||
"dependencies": { | ||
"json5": "^2.2.2", | ||
"minimist": "^1.2.6", | ||
"strip-bom": "^3.0.0" | ||
"get-tsconfig": "^4.5.0", | ||
"minimist": "^1.2.6" | ||
}, | ||
"scripts": { | ||
"start": "cd src && ts-node index.ts", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,5 @@ | ||
import { | ||
loadTsconfig, | ||
tsConfigLoader, | ||
walkForTsConfig, | ||
} from "../tsconfig-loader"; | ||
import { join } from "path"; | ||
import { tsConfigLoader, walkForTsConfig } from "../tsconfig-loader"; | ||
import { join, resolve } from "path"; | ||
|
||
describe("tsconfig-loader", () => { | ||
it("should find tsconfig in cwd", () => { | ||
|
@@ -167,262 +163,18 @@ describe("walkForTsConfig", () => { | |
}); | ||
}); | ||
|
||
describe("loadConfig", () => { | ||
it("should load a config", () => { | ||
const config = { compilerOptions: { baseUrl: "hej" } }; | ||
const res = loadTsconfig( | ||
"/root/dir1/tsconfig.json", | ||
(path) => path === "/root/dir1/tsconfig.json", | ||
(_) => JSON.stringify(config) | ||
); | ||
expect(res).toStrictEqual(config); | ||
}); | ||
|
||
it("should load a config with comments", () => { | ||
const config = { compilerOptions: { baseUrl: "hej" } }; | ||
const res = loadTsconfig( | ||
"/root/dir1/tsconfig.json", | ||
(path) => path === "/root/dir1/tsconfig.json", | ||
(_) => `{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since the API from |
||
// my comment | ||
"compilerOptions": { | ||
"baseUrl": "hej" | ||
} | ||
}` | ||
); | ||
expect(res).toStrictEqual(config); | ||
}); | ||
|
||
it("should load a config with trailing commas", () => { | ||
const config = { compilerOptions: { baseUrl: "hej" } }; | ||
const res = loadTsconfig( | ||
"/root/dir1/tsconfig.json", | ||
(path) => path === "/root/dir1/tsconfig.json", | ||
(_) => `{ | ||
"compilerOptions": { | ||
"baseUrl": "hej", | ||
}, | ||
}` | ||
); | ||
expect(res).toStrictEqual(config); | ||
}); | ||
|
||
it("should throw an error including the file path when encountering invalid JSON5", () => { | ||
expect(() => | ||
loadTsconfig( | ||
"/root/dir1/tsconfig.json", | ||
(path) => path === "/root/dir1/tsconfig.json", | ||
(_) => `{ | ||
"compilerOptions": { | ||
}` | ||
) | ||
).toThrowError( | ||
"/root/dir1/tsconfig.json is malformed JSON5: invalid end of input at 3:12" | ||
); | ||
}); | ||
|
||
it("should load a config with string extends and overwrite all options", () => { | ||
const firstConfig = { | ||
extends: "../base-config.json", | ||
compilerOptions: { baseUrl: "kalle", paths: { foo: ["bar2"] } }, | ||
}; | ||
const firstConfigPath = join("/root", "dir1", "tsconfig.json"); | ||
const baseConfig = { | ||
compilerOptions: { | ||
baseUrl: "olle", | ||
paths: { foo: ["bar1"] }, | ||
strict: true, | ||
}, | ||
}; | ||
const baseConfigPath = join("/root", "base-config.json"); | ||
const res = loadTsconfig( | ||
join("/root", "dir1", "tsconfig.json"), | ||
(path) => path === firstConfigPath || path === baseConfigPath, | ||
(path) => { | ||
if (path === firstConfigPath) { | ||
return JSON.stringify(firstConfig); | ||
} | ||
if (path === baseConfigPath) { | ||
return JSON.stringify(baseConfig); | ||
} | ||
return ""; | ||
} | ||
); | ||
|
||
expect(res).toEqual({ | ||
extends: "../base-config.json", | ||
compilerOptions: { | ||
baseUrl: "kalle", | ||
paths: { foo: ["bar2"] }, | ||
strict: true, | ||
}, | ||
}); | ||
}); | ||
|
||
it("should load a config with string extends from node_modules and overwrite all options", () => { | ||
const firstConfig = { | ||
extends: "my-package/base-config.json", | ||
compilerOptions: { baseUrl: "kalle", paths: { foo: ["bar2"] } }, | ||
}; | ||
const firstConfigPath = join("/root", "dir1", "tsconfig.json"); | ||
const baseConfig = { | ||
compilerOptions: { | ||
baseUrl: "olle", | ||
paths: { foo: ["bar1"] }, | ||
strict: true, | ||
}, | ||
}; | ||
const baseConfigPath = join( | ||
"/root", | ||
"dir1", | ||
"node_modules", | ||
"my-package", | ||
"base-config.json" | ||
); | ||
const res = loadTsconfig( | ||
join("/root", "dir1", "tsconfig.json"), | ||
(path) => path === firstConfigPath || path === baseConfigPath, | ||
(path) => { | ||
if (path === firstConfigPath) { | ||
return JSON.stringify(firstConfig); | ||
} | ||
if (path === baseConfigPath) { | ||
return JSON.stringify(baseConfig); | ||
} | ||
return ""; | ||
} | ||
); | ||
|
||
expect(res).toEqual({ | ||
extends: "my-package/base-config.json", | ||
compilerOptions: { | ||
baseUrl: "kalle", | ||
paths: { foo: ["bar2"] }, | ||
strict: true, | ||
}, | ||
}); | ||
}); | ||
|
||
it("should use baseUrl relative to location of extended tsconfig", () => { | ||
const firstConfig = { compilerOptions: { baseUrl: "." } }; | ||
const firstConfigPath = join("/root", "first-config.json"); | ||
const secondConfig = { extends: "../first-config.json" }; | ||
const secondConfigPath = join("/root", "dir1", "second-config.json"); | ||
const thirdConfig = { extends: "../second-config.json" }; | ||
const thirdConfigPath = join("/root", "dir1", "dir2", "third-config.json"); | ||
const res = loadTsconfig( | ||
join("/root", "dir1", "dir2", "third-config.json"), | ||
(path) => | ||
path === firstConfigPath || | ||
path === secondConfigPath || | ||
path === thirdConfigPath, | ||
(path) => { | ||
if (path === firstConfigPath) { | ||
return JSON.stringify(firstConfig); | ||
} | ||
if (path === secondConfigPath) { | ||
return JSON.stringify(secondConfig); | ||
} | ||
if (path === thirdConfigPath) { | ||
return JSON.stringify(thirdConfig); | ||
} | ||
return ""; | ||
} | ||
); | ||
|
||
expect(res).toEqual({ | ||
extends: "../second-config.json", | ||
compilerOptions: { baseUrl: join("..", "..") }, | ||
}); | ||
}); | ||
|
||
it("should load a config with array extends and overwrite all options", () => { | ||
const baseConfig1 = { | ||
compilerOptions: { baseUrl: ".", paths: { foo: ["bar"] } }, | ||
}; | ||
const baseConfig1Path = join("/root", "base-config-1.json"); | ||
const baseConfig2 = { compilerOptions: { baseUrl: "." } }; | ||
const baseConfig2Path = join("/root", "dir1", "base-config-2.json"); | ||
const baseConfig3 = { | ||
compilerOptions: { baseUrl: ".", paths: { foo: ["bar2"] } }, | ||
}; | ||
const baseConfig3Path = join("/root", "dir1", "dir2", "base-config-3.json"); | ||
const actualConfig = { | ||
extends: [ | ||
"./base-config-1.json", | ||
"./dir1/base-config-2.json", | ||
"./dir1/dir2/base-config-3.json", | ||
], | ||
}; | ||
const actualConfigPath = join("/root", "tsconfig.json"); | ||
|
||
const res = loadTsconfig( | ||
join("/root", "tsconfig.json"), | ||
(path) => | ||
[ | ||
baseConfig1Path, | ||
baseConfig2Path, | ||
baseConfig3Path, | ||
actualConfigPath, | ||
].indexOf(path) >= 0, | ||
(path) => { | ||
if (path === baseConfig1Path) { | ||
return JSON.stringify(baseConfig1); | ||
} | ||
if (path === baseConfig2Path) { | ||
return JSON.stringify(baseConfig2); | ||
} | ||
if (path === baseConfig3Path) { | ||
return JSON.stringify(baseConfig3); | ||
} | ||
if (path === actualConfigPath) { | ||
return JSON.stringify(actualConfig); | ||
} | ||
return ""; | ||
} | ||
); | ||
|
||
expect(res).toEqual({ | ||
extends: [ | ||
"./base-config-1.json", | ||
"./dir1/base-config-2.json", | ||
"./dir1/dir2/base-config-3.json", | ||
], | ||
compilerOptions: { | ||
baseUrl: join("dir1", "dir2"), | ||
paths: { foo: ["bar2"] }, | ||
}, | ||
describe("loadSyncDefault", () => { | ||
it("should result multiple levels of tsconfig extension", () => { | ||
const cwd = resolve(__dirname, "../../example/inherited"); | ||
const result = tsConfigLoader({ | ||
cwd, | ||
getEnv: (_: string) => undefined, | ||
}); | ||
}); | ||
|
||
it("should load a config with array extends without .json extension", () => { | ||
const baseConfig = { | ||
compilerOptions: { baseUrl: ".", paths: { foo: ["bar"] } }, | ||
}; | ||
const baseConfigPath = join("/root", "base-config-1.json"); | ||
const actualConfig = { extends: ["./base-config-1"] }; | ||
const actualConfigPath = join("/root", "tsconfig.json"); | ||
|
||
const res = loadTsconfig( | ||
join("/root", "tsconfig.json"), | ||
(path) => [baseConfigPath, actualConfigPath].indexOf(path) >= 0, | ||
(path) => { | ||
if (path === baseConfigPath) { | ||
return JSON.stringify(baseConfig); | ||
} | ||
if (path === actualConfigPath) { | ||
return JSON.stringify(actualConfig); | ||
} | ||
return ""; | ||
} | ||
); | ||
|
||
expect(res).toEqual({ | ||
extends: ["./base-config-1"], | ||
compilerOptions: { | ||
baseUrl: ".", | ||
paths: { foo: ["bar"] }, | ||
}, | ||
expect(result).toEqual({ | ||
baseUrl: undefined, | ||
paths: { "@": [] }, | ||
tsConfigPath: resolve(cwd, "tsconfig.json"), | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updating
jest
for compatibility withts-jest