|
| 1 | +import { fileURLToPath } from "node:url"; |
| 2 | +import ts from "typescript"; |
| 3 | +import { it } from "vitest"; |
| 4 | + |
| 5 | +const source = ` |
| 6 | +import { defineConfig, defineContainer, defineWorker } from "../definition"; |
| 7 | +
|
| 8 | +defineConfig({ |
| 9 | + worker: { |
| 10 | + name: "inline-worker", |
| 11 | + compatibilityDate: "2026-09-25", |
| 12 | + }, |
| 13 | + containers: [ |
| 14 | + { |
| 15 | + name: "inline-container", |
| 16 | + image: { dockerfile: "./inline.Dockerfile" }, |
| 17 | + }, |
| 18 | + ], |
| 19 | +}); |
| 20 | +
|
| 21 | +defineWorker({ |
| 22 | + name: "standalone-worker", |
| 23 | + compatibilityDate: "2026-09-25", |
| 24 | +}); |
| 25 | +
|
| 26 | +defineContainer({ |
| 27 | + name: "standalone-container", |
| 28 | + image: { dockerfile: "./Dockerfile" }, |
| 29 | +}); |
| 30 | +`; |
| 31 | +const testFilePath = fileURLToPath( |
| 32 | + new URL("inline-worker-jsdoc.ts", import.meta.url) |
| 33 | +); |
| 34 | +const canonicalize = (fileName: string) => |
| 35 | + ts.sys.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase(); |
| 36 | +const canonicalTestFilePath = canonicalize(ts.sys.resolvePath(testFilePath)); |
| 37 | + |
| 38 | +it("preserves JSDoc for define helper fields", ({ expect }) => { |
| 39 | + const host: ts.LanguageServiceHost = { |
| 40 | + ...ts.sys, |
| 41 | + getCompilationSettings: () => ({ |
| 42 | + module: ts.ModuleKind.ESNext, |
| 43 | + moduleResolution: ts.ModuleResolutionKind.Bundler, |
| 44 | + strict: true, |
| 45 | + target: ts.ScriptTarget.ESNext, |
| 46 | + }), |
| 47 | + getDefaultLibFileName: ts.getDefaultLibFilePath, |
| 48 | + getScriptFileNames: () => [testFilePath], |
| 49 | + getScriptSnapshot: (fileName) => { |
| 50 | + const contents = |
| 51 | + canonicalize(ts.sys.resolvePath(fileName)) === canonicalTestFilePath |
| 52 | + ? source |
| 53 | + : ts.sys.readFile(fileName); |
| 54 | + return contents === undefined |
| 55 | + ? undefined |
| 56 | + : ts.ScriptSnapshot.fromString(contents); |
| 57 | + }, |
| 58 | + getScriptVersion: () => "0", |
| 59 | + useCaseSensitiveFileNames: () => ts.sys.useCaseSensitiveFileNames, |
| 60 | + }; |
| 61 | + const languageService = ts.createLanguageService(host); |
| 62 | + |
| 63 | + try { |
| 64 | + expect(languageService.getSemanticDiagnostics(testFilePath)).toEqual([]); |
| 65 | + |
| 66 | + for (const [marker, expectedDocumentation] of [ |
| 67 | + ['name: "inline-worker"', "The name of your Worker."], |
| 68 | + [ |
| 69 | + 'compatibilityDate: "2026-09-25"', |
| 70 | + "which version of the Workers runtime is used.", |
| 71 | + ], |
| 72 | + ['name: "inline-container"', "Name of the application."], |
| 73 | + [ |
| 74 | + 'image: { dockerfile: "./inline.Dockerfile" }', |
| 75 | + "The image to build or deploy.", |
| 76 | + ], |
| 77 | + ['dockerfile: "./inline.Dockerfile"', "The path to a Dockerfile."], |
| 78 | + ['name: "standalone-worker"', "The name of your Worker."], |
| 79 | + ['name: "standalone-container"', "Name of the application."], |
| 80 | + [ |
| 81 | + 'image: { dockerfile: "./Dockerfile" }', |
| 82 | + "The image to build or deploy.", |
| 83 | + ], |
| 84 | + ['dockerfile: "./Dockerfile"', "The path to a Dockerfile."], |
| 85 | + ] as const) { |
| 86 | + const info = languageService.getQuickInfoAtPosition( |
| 87 | + testFilePath, |
| 88 | + source.indexOf(marker) |
| 89 | + ); |
| 90 | + expect(ts.displayPartsToString(info?.documentation), marker).toContain( |
| 91 | + expectedDocumentation |
| 92 | + ); |
| 93 | + } |
| 94 | + } finally { |
| 95 | + languageService.dispose(); |
| 96 | + } |
| 97 | +}); |
0 commit comments