Skip to content
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

Allow cross-project references to const enums in isolatedModules when referenced project has preserveConstEnums #57914

Merged
merged 5 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39705,7 +39705,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (getIsolatedModules(compilerOptions)) {
Debug.assert(!!(type.symbol.flags & SymbolFlags.ConstEnum));
const constEnumDeclaration = type.symbol.valueDeclaration as EnumDeclaration;
if (constEnumDeclaration.flags & NodeFlags.Ambient && !isValidTypeOnlyAliasUseSite(node)) {
const redirect = host.getResolvedProjectReferenceByOutputFile(getSourceFileOfNode(constEnumDeclaration).resolvedPath);
if (constEnumDeclaration.flags & NodeFlags.Ambient && !isValidTypeOnlyAliasUseSite(node) && (!redirect || !shouldPreserveConstEnums(redirect.commandLine.options))) {
andrewbranch marked this conversation as resolved.
Show resolved Hide resolved
error(node, Diagnostics.Cannot_access_ambient_const_enums_when_0_is_enabled, isolatedModulesLikeFlagName);
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1923,6 +1923,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
getResolvedProjectReferenceByPath,
forEachResolvedProjectReference,
isSourceOfProjectReferenceRedirect,
getResolvedProjectReferenceByOutputFile,
emitBuildInfo,
fileExists,
readFile,
Expand Down Expand Up @@ -3835,6 +3836,13 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
return projectReferenceRedirects.get(projectReferencePath) || undefined;
}

function getResolvedProjectReferenceByOutputFile(path: Path): ResolvedProjectReference | undefined {
andrewbranch marked this conversation as resolved.
Show resolved Hide resolved
const source = getSourceOfProjectReferenceRedirect(path);
if (typeof source === "string") {
return getResolvedProjectReferenceToRedirect(source);
}
}

function processReferencedFiles(file: SourceFile, isDefaultLib: boolean) {
forEach(file.referencedFiles, (ref, index) => {
processSourceFile(
Expand Down
8 changes: 7 additions & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4724,9 +4724,14 @@ export interface Program extends ScriptReferenceHost {
getProjectReferences(): readonly ProjectReference[] | undefined;
getResolvedProjectReferences(): readonly (ResolvedProjectReference | undefined)[] | undefined;
/** @internal */ getProjectReferenceRedirect(fileName: string): string | undefined;
/** @internal */ getResolvedProjectReferenceToRedirect(fileName: string): ResolvedProjectReference | undefined;
/**
* @internal
* Get the referenced project if the file is input file from that reference project
*/
getResolvedProjectReferenceToRedirect(fileName: string): ResolvedProjectReference | undefined;
/** @internal */ forEachResolvedProjectReference<T>(cb: (resolvedProjectReference: ResolvedProjectReference) => T | undefined): T | undefined;
/** @internal */ getResolvedProjectReferenceByPath(projectReferencePath: Path): ResolvedProjectReference | undefined;
/** @internal */ getResolvedProjectReferenceByOutputFile(path: Path): ResolvedProjectReference | undefined;
/** @internal */ isSourceOfProjectReferenceRedirect(fileName: string): boolean;
/** @internal */ getBuildInfo?(): BuildInfo;
/** @internal */ emitBuildInfo(writeFile?: WriteFileCallback, cancellationToken?: CancellationToken): EmitResult;
Expand Down Expand Up @@ -4843,6 +4848,7 @@ export interface TypeCheckerHost extends ModuleSpecifierResolutionHost {
getSourceFile(fileName: string): SourceFile | undefined;
getProjectReferenceRedirect(fileName: string): string | undefined;
isSourceOfProjectReferenceRedirect(fileName: string): boolean;
getResolvedProjectReferenceByOutputFile(path: Path): ResolvedProjectReference | undefined;
getModeForUsageLocation(file: SourceFile, usage: StringLiteralLike): ResolutionMode;

getResolvedModule(f: SourceFile, moduleName: string, mode: ResolutionMode): ResolvedModuleWithFailedLookupLocations | undefined;
Expand Down
27 changes: 27 additions & 0 deletions src/testRunner/unittests/tsc/projectReferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,31 @@ describe("unittests:: tsc:: projectReferences::", () => {
}),
commandLineArgs: ["--p", "src/project"],
});

verifyTsc({
scenario: "projectReferences",
subScenario: "referencing ambient const enum from referenced project with preserveConstEnums",
fs: () =>
loadProjectFromFiles({
"/src/utils/index.ts": "export const enum E { A = 1 }",
"/src/utils/index.d.ts": "export declare const enum E { A = 1 }",
"/src/utils/tsconfig.json": jsonToReadableText({
compilerOptions: {
composite: true,
declaration: true,
preserveConstEnums: true,
},
}),
"/src/project/index.ts": `import { E } from "../utils"; E.A;`,
"/src/project/tsconfig.json": jsonToReadableText({
compilerOptions: {
isolatedModules: true,
},
references: [
{ path: "../utils" },
],
}),
}),
commandLineArgs: ["--p", "src/project"],
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
currentDirectory:: / useCaseSensitiveFileNames: false
Input::
//// [/lib/lib.d.ts]
/// <reference no-default-lib="true"/>
interface Boolean {}
interface Function {}
interface CallableFunction {}
interface NewableFunction {}
interface IArguments {}
interface Number { toExponential: any; }
interface Object {}
interface RegExp {}
interface String { charAt: any; }
interface Array<T> { length: number; [n: number]: T; }
interface ReadonlyArray<T> {}
declare const console: { log(msg: any): void; };

//// [/src/project/index.ts]
import { E } from "../utils"; E.A;

//// [/src/project/tsconfig.json]
{
"compilerOptions": {
"isolatedModules": true
},
"references": [
{
"path": "../utils"
}
]
}

//// [/src/utils/index.d.ts]
export declare const enum E { A = 1 }

//// [/src/utils/index.ts]
export const enum E { A = 1 }

//// [/src/utils/tsconfig.json]
{
"compilerOptions": {
"composite": true,
"declaration": true,
"preserveConstEnums": true
}
}



Output::
/lib/tsc --p src/project
exitCode:: ExitStatus.Success


//// [/src/project/index.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var utils_1 = require("../utils");
utils_1.E.A;