Skip to content

Commit

Permalink
Host uses getDirectories from System (#3165)
Browse files Browse the repository at this point in the history
  • Loading branch information
edemaine committed Jul 8, 2024
1 parent b5d65fb commit 441338c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/many-avocados-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@typescript/vfs": patch
---

Use System's getDirectories if it's provided when constructing Host
13 changes: 9 additions & 4 deletions packages/ts-twoslasher/test/fixtures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ expect.extend({ toMatchFile })
// To add a test, create a file in the fixtures folder and it will will run through
// as though it was the codeblock.

const defaultCompilerOptions = {
// avoid extra annotations from @types/node
types: []
}

describe("with fixtures", () => {
// Add all codefixes
const fixturesFolder = join(__dirname, "fixtures")
Expand All @@ -27,7 +32,7 @@ describe("with fixtures", () => {

const file = readFileSync(fixture, "utf8")

const fourslashed = twoslasher(file, extname(fixtureName).substr(1), { customTags: ["annotate"] })
const fourslashed = twoslasher(file, extname(fixtureName).substr(1), { customTags: ["annotate"], defaultCompilerOptions })
const jsonString = format(JSON.stringify(cleanFixture(fourslashed)), { parser: "json" })
expect(jsonString).toMatchFile(result)
})
Expand All @@ -46,7 +51,7 @@ describe("with fixtures", () => {

const file = readFileSync(fixture, "utf8")

const fourslashed = twoslasher(file, extname(fixtureName).substr(1))
const fourslashed = twoslasher(file, extname(fixtureName).substr(1), { defaultCompilerOptions })
const jsonString = format(JSON.stringify(cleanFixture(fourslashed)), { parser: "json" })
expect(jsonString).toMatchFile(result)
})
Expand All @@ -65,7 +70,7 @@ describe("with fixtures", () => {

const file = readFileSync(fixture, "utf8")

const fourslashed = twoslasher(file, extname(fixtureName).substr(1))
const fourslashed = twoslasher(file, extname(fixtureName).substr(1), { defaultCompilerOptions })
const jsonString = format(JSON.stringify(cleanFixture(fourslashed)), { parser: "json" })
expect(jsonString).toMatchFile(result)
})
Expand All @@ -87,7 +92,7 @@ describe("with fixtures", () => {

let thrown = false
try {
twoslasher(file, extname(fixtureName).substr(1))
twoslasher(file, extname(fixtureName).substr(1), { defaultCompilerOptions })
} catch (err) {
thrown = true
if (err instanceof Error) expect(err.message).toMatchFile(result)
Expand Down
1 change: 0 additions & 1 deletion packages/typescript-vfs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,6 @@ export function createVirtualCompilerHost(sys: System, compilerOptions: Compiler
getCanonicalFileName: fileName => fileName,
getDefaultLibFileName: () => "/" + ts.getDefaultLibFileName(compilerOptions), // '/lib.d.ts',
// getDefaultLibLocation: () => '/',
getDirectories: () => [],
getNewLine: () => sys.newLine,
getSourceFile: (fileName, languageVersionOrOptions) => {
return (
Expand Down
14 changes: 14 additions & 0 deletions packages/typescript-vfs/test/fsbacked.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,17 @@ it("can import files in the virtual fs", () => {

expect(errs.map(e => e.messageText)).toEqual([])
})

it("searches node_modules/@types", () => {
const compilerOpts: ts.CompilerOptions = { target: ts.ScriptTarget.ES2016, esModuleInterop: true }
const monorepoRoot = __dirname

const fsMap = new Map<string, string>()
fsMap.set("index.ts", "it('found @types/jest', () => undefined)")

const system = createFSBackedSystem(fsMap, monorepoRoot, ts)
const env = createVirtualTypeScriptEnvironment(system, ["index.ts"], ts, compilerOpts)

const semDiags = env.languageService.getSemanticDiagnostics("index.ts")
expect(semDiags.length).toBe(0)
})

0 comments on commit 441338c

Please sign in to comment.