|
1 |
| -import * as Utils from "../../_namespaces/Utils"; |
| 1 | +import * as ts from "../../_namespaces/ts"; |
| 2 | +import { |
| 3 | + dedent, |
| 4 | +} from "../../_namespaces/Utils"; |
| 5 | +import { |
| 6 | + libContent, |
| 7 | +} from "../helpers/contents"; |
2 | 8 | import {
|
3 | 9 | getFsConentsForNode10ResultAtTypesPackageJson,
|
4 | 10 | getFsContentsForNode10Result,
|
5 | 11 | getFsContentsForNode10ResultDts,
|
6 | 12 | getFsContentsForNode10ResultPackageJson,
|
7 | 13 | } from "../helpers/node10Result";
|
| 14 | +import { |
| 15 | + solutionBuildWithBaseline, |
| 16 | +} from "../helpers/solutionBuilder"; |
8 | 17 | import {
|
9 | 18 | baselineTsserverLogs,
|
10 | 19 | createLoggerWithInMemoryLogs,
|
11 | 20 | createSession,
|
12 | 21 | openFilesForSession,
|
| 22 | + protocolTextSpanFromSubstring, |
13 | 23 | verifyGetErrRequest,
|
14 | 24 | } from "../helpers/tsserver";
|
15 | 25 | import {
|
@@ -38,14 +48,14 @@ describe("unittests:: tsserver:: moduleResolution", () => {
|
38 | 48 | };
|
39 | 49 | const fileA: File = {
|
40 | 50 | path: `/user/username/projects/myproject/src/fileA.ts`,
|
41 |
| - content: Utils.dedent` |
| 51 | + content: dedent` |
42 | 52 | import { foo } from "./fileB.mjs";
|
43 | 53 | foo();
|
44 | 54 | `,
|
45 | 55 | };
|
46 | 56 | const fileB: File = {
|
47 | 57 | path: `/user/username/projects/myproject/src/fileB.mts`,
|
48 |
| - content: Utils.dedent` |
| 58 | + content: dedent` |
49 | 59 | export function foo() {
|
50 | 60 | }
|
51 | 61 | `,
|
@@ -195,4 +205,116 @@ describe("unittests:: tsserver:: moduleResolution", () => {
|
195 | 205 | });
|
196 | 206 | }
|
197 | 207 | });
|
| 208 | + |
| 209 | + describe("using referenced project", () => { |
| 210 | + it("not built", () => { |
| 211 | + verify(); |
| 212 | + }); |
| 213 | + it("built", () => { |
| 214 | + verify(/*built*/ true); |
| 215 | + }); |
| 216 | + function verify(built?: boolean) { |
| 217 | + const indexContent = dedent` |
| 218 | + import { FOO } from "package-a"; |
| 219 | + console.log(FOO); |
| 220 | + `; |
| 221 | + const host = createServerHost({ |
| 222 | + "/home/src/projects/project/packages/package-a/package.json": getPackageJson("package-a"), |
| 223 | + "/home/src/projects/project/packages/package-a/tsconfig.json": getTsConfig(), |
| 224 | + "/home/src/projects/project/packages/package-a/src/index.ts": `export * from "./subfolder";`, |
| 225 | + "/home/src/projects/project/packages/package-a/src/subfolder/index.ts": `export const FOO = "bar";`, |
| 226 | + "/home/src/projects/project/packages/package-b/package.json": getPackageJson("package-b"), |
| 227 | + "/home/src/projects/project/packages/package-b/tsconfig.json": getTsConfig([{ path: "../package-a" }]), |
| 228 | + "/home/src/projects/project/packages/package-b/src/index.ts": indexContent, |
| 229 | + "/home/src/projects/project/node_modules/package-a": { symLink: "/home/src/projects/project/packages/package-a" }, |
| 230 | + "/home/src/projects/project/node_modules/package-b": { symLink: "/home/src/projects/project/packages/package-b" }, |
| 231 | + "/a/lib/lib.es2021.d.ts": libContent, |
| 232 | + }, { currentDirectory: "/home/src/projects/project" }); |
| 233 | + if (built) { |
| 234 | + solutionBuildWithBaseline(host, ["packages/package-b"]); |
| 235 | + host.clearOutput(); |
| 236 | + } |
| 237 | + const session = createSession(host, { logger: createLoggerWithInMemoryLogs(host), canUseEvents: true }); |
| 238 | + openFilesForSession(["/home/src/projects/project/packages/package-b/src/index.ts"], session); |
| 239 | + verifyGetErrRequest({ |
| 240 | + session, |
| 241 | + files: ["/home/src/projects/project/packages/package-b/src/index.ts"], |
| 242 | + }); |
| 243 | + const { end } = protocolTextSpanFromSubstring(indexContent, "package-a"); |
| 244 | + session.executeCommandSeq<ts.server.protocol.UpdateOpenRequest>({ |
| 245 | + command: ts.server.protocol.CommandTypes.UpdateOpen, |
| 246 | + arguments: { |
| 247 | + changedFiles: [{ |
| 248 | + fileName: "/home/src/projects/project/packages/package-b/src/index.ts", |
| 249 | + textChanges: [{ |
| 250 | + start: end, |
| 251 | + end, |
| 252 | + newText: "X", |
| 253 | + }], |
| 254 | + }], |
| 255 | + }, |
| 256 | + }); |
| 257 | + verifyGetErrRequest({ |
| 258 | + session, |
| 259 | + files: ["/home/src/projects/project/packages/package-b/src/index.ts"], |
| 260 | + }); |
| 261 | + session.executeCommandSeq<ts.server.protocol.UpdateOpenRequest>({ |
| 262 | + command: ts.server.protocol.CommandTypes.UpdateOpen, |
| 263 | + arguments: { |
| 264 | + changedFiles: [{ |
| 265 | + fileName: "/home/src/projects/project/packages/package-b/src/index.ts", |
| 266 | + textChanges: [{ |
| 267 | + start: end, |
| 268 | + end: { ...end, offset: end.offset + 1 }, |
| 269 | + newText: "", |
| 270 | + }], |
| 271 | + }], |
| 272 | + }, |
| 273 | + }); |
| 274 | + verifyGetErrRequest({ |
| 275 | + session, |
| 276 | + files: ["/home/src/projects/project/packages/package-b/src/index.ts"], |
| 277 | + }); |
| 278 | + baselineTsserverLogs("moduleResolution", `using referenced project${built ? " built" : ""}`, session); |
| 279 | + } |
| 280 | + function getPackageJson(packageName: string) { |
| 281 | + return JSON.stringify( |
| 282 | + { |
| 283 | + name: packageName, |
| 284 | + version: "1.0.0", |
| 285 | + type: "module", |
| 286 | + main: "build/index.js", |
| 287 | + exports: { |
| 288 | + ".": "./build/index.js", |
| 289 | + "./package.json": "./package.json", |
| 290 | + "./*": ["./build/*/index.js", "./build/*.js"], |
| 291 | + }, |
| 292 | + }, |
| 293 | + undefined, |
| 294 | + " ", |
| 295 | + ); |
| 296 | + } |
| 297 | + |
| 298 | + function getTsConfig(references?: object[]) { |
| 299 | + return JSON.stringify({ |
| 300 | + compilerOptions: { |
| 301 | + allowSyntheticDefaultImports: true, |
| 302 | + baseUrl: "./", |
| 303 | + composite: true, |
| 304 | + declarationMap: true, |
| 305 | + esModuleInterop: true, |
| 306 | + lib: ["es2021"], |
| 307 | + module: "esnext", |
| 308 | + moduleResolution: "bundler", |
| 309 | + outDir: "build", |
| 310 | + rootDir: "./src", |
| 311 | + target: "ES2021", |
| 312 | + traceResolution: true, |
| 313 | + tsBuildInfoFile: "./build/tsconfig.tsbuildinfo", |
| 314 | + }, |
| 315 | + include: ["./src/**/*.ts"], |
| 316 | + references, |
| 317 | + }); |
| 318 | + } |
| 319 | + }); |
198 | 320 | });
|
0 commit comments