Skip to content

Commit 7f5299b

Browse files
committed
Test
1 parent 97dc5f0 commit 7f5299b

13 files changed

+887
-0
lines changed

src/testRunner/unittests/tsc/projectReferences.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,47 @@ describe("unittests:: tsc:: projectReferences::", () => {
7676
}),
7777
commandLineArgs: ["--p", "src/project"],
7878
});
79+
80+
verifyTsc({
81+
scenario: "projectReferences",
82+
subScenario: "referencing ambient const enum as value from referenced project with preserveConstEnums",
83+
fs: () =>
84+
loadProjectFromFiles({
85+
"/src/utils/index.ts": "export const enum E { A = 1 }",
86+
"/src/utils/index.d.ts": "export declare const enum E { A = 1 }",
87+
"/src/utils/tsconfig.json": jsonToReadableText({
88+
compilerOptions: {
89+
composite: true,
90+
declaration: true,
91+
preserveConstEnums: true,
92+
},
93+
}),
94+
"/src/utilsNonPreserved/index.ts": "export const enum E2 { A = 1 }",
95+
"/src/utilsNonPreserved/index.d.ts": "export declare const enum E2 { A = 1 }",
96+
"/src/utilsNonPreserved/tsconfig.json": jsonToReadableText({
97+
compilerOptions: {
98+
composite: true,
99+
declaration: true,
100+
preserveConstEnums: false,
101+
},
102+
}),
103+
"/src/project/index.ts": `
104+
import { E } from "../utils";
105+
import { E2 } from "../utilsNonPreserved";
106+
107+
E; declare const x: E; E[x];
108+
109+
E2; declare const y: E2; E2[y];
110+
`,
111+
"/src/project/tsconfig.json": jsonToReadableText({
112+
compilerOptions: {
113+
isolatedModules: true,
114+
},
115+
references: [
116+
{ path: "../utils" },
117+
],
118+
}),
119+
}),
120+
commandLineArgs: ["--p", "src/project"],
121+
});
79122
});

src/testRunner/unittests/tsserver/projectReferences.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,54 @@ function foo() {
376376
baselineTsserverLogs("projectReferences", `referencing const enum from referenced project with preserveConstEnums`, session);
377377
});
378378

379+
it("referencing const enum as value from referenced project with preserveConstEnums", () => {
380+
const projectLocation = `/user/username/projects/project`;
381+
const utilsIndex: File = {
382+
path: `${projectLocation}/src/utils/index.ts`,
383+
content: "export const enum E { A = 1 }",
384+
};
385+
const utilsDeclaration: File = {
386+
path: `${projectLocation}/src/utils/index.d.ts`,
387+
content: "export declare const enum E { A = 1 }",
388+
};
389+
const utilsConfig: File = {
390+
path: `${projectLocation}/src/utils/tsconfig.json`,
391+
content: jsonToReadableText({ compilerOptions: { composite: true, declaration: true, preserveConstEnums: true } }),
392+
};
393+
const utilsNonPreservedIndex: File = {
394+
path: `${projectLocation}/src/utilsNonPreserved/index.ts`,
395+
content: "export const enum E2 { A = 1 }",
396+
};
397+
const utilsNonPreservedDeclaration: File = {
398+
path: `${projectLocation}/src/utilsNonPreserved/index.d.ts`,
399+
content: "export declare const enum E2 { A = 1 }",
400+
};
401+
const utilsNonPreservedConfig: File = {
402+
path: `${projectLocation}/src/utilsNonPreserved/tsconfig.json`,
403+
content: jsonToReadableText({ compilerOptions: { composite: true, declaration: true, preserveConstEnums: false } }),
404+
};
405+
const projectIndex: File = {
406+
path: `${projectLocation}/src/project/index.ts`,
407+
content: `
408+
import { E } from "../utils";
409+
import { E2 } from "../utilsNonPreserved";
410+
411+
E; declare const x: E; E[x];
412+
413+
E2; declare const y: E2; E2[y];
414+
`,
415+
};
416+
const projectConfig: File = {
417+
path: `${projectLocation}/src/project/tsconfig.json`,
418+
content: jsonToReadableText({ compilerOptions: { isolatedModules: true }, references: [{ path: "../utils" }, { path: "../utilsNoPreserved" }] }),
419+
};
420+
const host = createServerHost([libFile, utilsIndex, utilsDeclaration, utilsConfig, utilsNonPreservedIndex, utilsNonPreservedDeclaration, utilsNonPreservedConfig, projectIndex, projectConfig]);
421+
const session = new TestSession(host);
422+
openFilesForSession([projectIndex], session);
423+
verifyGetErrRequest({ session, files: [projectIndex] });
424+
baselineTsserverLogs("projectReferences", `referencing const enum as value from referenced project with preserveConstEnums`, session);
425+
});
426+
379427
describe("when references are monorepo like with symlinks", () => {
380428
interface Packages {
381429
bPackageJson: File;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
constEnumUsedAsValue.ts(9,1): error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.
2+
constEnumUsedAsValue.ts(10,3): error TS2476: A const enum member can only be accessed using a string literal.
3+
4+
5+
==== constEnumUsedAsValue.ts (2 errors) ====
6+
const enum E {
7+
A,
8+
B,
9+
C,
10+
}
11+
12+
declare const x: E;
13+
14+
E;
15+
~
16+
!!! error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.
17+
E[x];
18+
~
19+
!!! error TS2476: A const enum member can only be accessed using a string literal.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//// [tests/cases/compiler/constEnumUsedAsValue.ts] ////
2+
3+
//// [constEnumUsedAsValue.ts]
4+
const enum E {
5+
A,
6+
B,
7+
C,
8+
}
9+
10+
declare const x: E;
11+
12+
E;
13+
E[x];
14+
15+
//// [constEnumUsedAsValue.js]
16+
"use strict";
17+
E;
18+
E[x];
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//// [tests/cases/compiler/constEnumUsedAsValue.ts] ////
2+
3+
=== constEnumUsedAsValue.ts ===
4+
const enum E {
5+
>E : Symbol(E, Decl(constEnumUsedAsValue.ts, 0, 0))
6+
7+
A,
8+
>A : Symbol(E.A, Decl(constEnumUsedAsValue.ts, 0, 14))
9+
10+
B,
11+
>B : Symbol(E.B, Decl(constEnumUsedAsValue.ts, 1, 6))
12+
13+
C,
14+
>C : Symbol(E.C, Decl(constEnumUsedAsValue.ts, 2, 6))
15+
}
16+
17+
declare const x: E;
18+
>x : Symbol(x, Decl(constEnumUsedAsValue.ts, 6, 13))
19+
>E : Symbol(E, Decl(constEnumUsedAsValue.ts, 0, 0))
20+
21+
E;
22+
>E : Symbol(E, Decl(constEnumUsedAsValue.ts, 0, 0))
23+
24+
E[x];
25+
>E : Symbol(E, Decl(constEnumUsedAsValue.ts, 0, 0))
26+
>x : Symbol(x, Decl(constEnumUsedAsValue.ts, 6, 13))
27+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//// [tests/cases/compiler/constEnumUsedAsValue.ts] ////
2+
3+
=== constEnumUsedAsValue.ts ===
4+
const enum E {
5+
>E : E
6+
> : ^
7+
8+
A,
9+
>A : E.A
10+
> : ^^^
11+
12+
B,
13+
>B : E.B
14+
> : ^^^
15+
16+
C,
17+
>C : E.C
18+
> : ^^^
19+
}
20+
21+
declare const x: E;
22+
>x : E
23+
> : ^
24+
25+
E;
26+
>E : typeof E
27+
> : ^^^^^^^^
28+
29+
E[x];
30+
>E[x] : any
31+
> : ^^^
32+
>E : typeof E
33+
> : ^^^^^^^^
34+
>x : E
35+
> : ^
36+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
constEnumUsedAsValue.ts(9,1): error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.
2+
constEnumUsedAsValue.ts(10,3): error TS2476: A const enum member can only be accessed using a string literal.
3+
4+
5+
==== constEnumUsedAsValue.ts (2 errors) ====
6+
const enum E {
7+
A,
8+
B,
9+
C,
10+
}
11+
12+
declare const x: E;
13+
14+
E;
15+
~
16+
!!! error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment or type query.
17+
E[x];
18+
~
19+
!!! error TS2476: A const enum member can only be accessed using a string literal.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//// [tests/cases/compiler/constEnumUsedAsValue.ts] ////
2+
3+
//// [constEnumUsedAsValue.ts]
4+
const enum E {
5+
A,
6+
B,
7+
C,
8+
}
9+
10+
declare const x: E;
11+
12+
E;
13+
E[x];
14+
15+
//// [constEnumUsedAsValue.js]
16+
"use strict";
17+
var E;
18+
(function (E) {
19+
E[E["A"] = 0] = "A";
20+
E[E["B"] = 1] = "B";
21+
E[E["C"] = 2] = "C";
22+
})(E || (E = {}));
23+
E;
24+
E[x];
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//// [tests/cases/compiler/constEnumUsedAsValue.ts] ////
2+
3+
=== constEnumUsedAsValue.ts ===
4+
const enum E {
5+
>E : Symbol(E, Decl(constEnumUsedAsValue.ts, 0, 0))
6+
7+
A,
8+
>A : Symbol(E.A, Decl(constEnumUsedAsValue.ts, 0, 14))
9+
10+
B,
11+
>B : Symbol(E.B, Decl(constEnumUsedAsValue.ts, 1, 6))
12+
13+
C,
14+
>C : Symbol(E.C, Decl(constEnumUsedAsValue.ts, 2, 6))
15+
}
16+
17+
declare const x: E;
18+
>x : Symbol(x, Decl(constEnumUsedAsValue.ts, 6, 13))
19+
>E : Symbol(E, Decl(constEnumUsedAsValue.ts, 0, 0))
20+
21+
E;
22+
>E : Symbol(E, Decl(constEnumUsedAsValue.ts, 0, 0))
23+
24+
E[x];
25+
>E : Symbol(E, Decl(constEnumUsedAsValue.ts, 0, 0))
26+
>x : Symbol(x, Decl(constEnumUsedAsValue.ts, 6, 13))
27+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//// [tests/cases/compiler/constEnumUsedAsValue.ts] ////
2+
3+
=== constEnumUsedAsValue.ts ===
4+
const enum E {
5+
>E : E
6+
> : ^
7+
8+
A,
9+
>A : E.A
10+
> : ^^^
11+
12+
B,
13+
>B : E.B
14+
> : ^^^
15+
16+
C,
17+
>C : E.C
18+
> : ^^^
19+
}
20+
21+
declare const x: E;
22+
>x : E
23+
> : ^
24+
25+
E;
26+
>E : typeof E
27+
> : ^^^^^^^^
28+
29+
E[x];
30+
>E[x] : any
31+
> : ^^^
32+
>E : typeof E
33+
> : ^^^^^^^^
34+
>x : E
35+
> : ^
36+

0 commit comments

Comments
 (0)