Skip to content

Commit

Permalink
Enable --isolatedDeclarations on TS codebase (#59635)
Browse files Browse the repository at this point in the history
Co-authored-by: Sheetal Nandi <shkamat@microsoft.com>
Co-authored-by: Andrew Branch <andrew@wheream.io>
  • Loading branch information
3 people authored Sep 16, 2024
1 parent 89e004f commit 52eaa7b
Show file tree
Hide file tree
Showing 136 changed files with 1,924 additions and 1,591 deletions.
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export default tseslint.config(
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/class-literal-property-style": "off",
"@typescript-eslint/consistent-indexed-object-style": "off",
"@typescript-eslint/consistent-generic-constructors": "off",
"@typescript-eslint/no-duplicate-enum-values": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-namespace": "off",
Expand Down
3 changes: 2 additions & 1 deletion scripts/processDiagnosticMessages.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ function buildInfoFileOutput(messageTable, inputFilePathRel) {
" return { code, category, key, message, reportsUnnecessary, elidedInCompatabilityPyramid, reportsDeprecated };",
"}",
"",
"/* eslint-disable @typescript-eslint/no-unnecessary-type-assertion*/", // type assertions are needed for isolatedDeclarations
"/** @internal */",
"export const Diagnostics = {",
];
Expand All @@ -101,7 +102,7 @@ function buildInfoFileOutput(messageTable, inputFilePathRel) {
const argElidedInCompatabilityPyramid = elidedInCompatabilityPyramid ? `${!reportsUnnecessary ? ", /*reportsUnnecessary*/ undefined" : ""}, /*elidedInCompatabilityPyramid*/ ${elidedInCompatabilityPyramid}` : "";
const argReportsDeprecated = reportsDeprecated ? `${!argElidedInCompatabilityPyramid ? ", /*reportsUnnecessary*/ undefined, /*elidedInCompatabilityPyramid*/ undefined" : ""}, /*reportsDeprecated*/ ${reportsDeprecated}` : "";

result.push(` ${propName}: diag(${code}, DiagnosticCategory.${category}, "${createKey(propName, code)}", ${JSON.stringify(name)}${argReportsUnnecessary}${argElidedInCompatabilityPyramid}${argReportsDeprecated}),`);
result.push(` ${propName}: diag(${code}, DiagnosticCategory.${category}, "${createKey(propName, code)}", ${JSON.stringify(name)}${argReportsUnnecessary}${argElidedInCompatabilityPyramid}${argReportsDeprecated}) as DiagnosticMessage,`);
});

result.push("};");
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ export function createFlowNode(flags: FlowFlags, node: unknown, antecedent: Flow
const binder = /* @__PURE__ */ createBinder();

/** @internal */
export function bindSourceFile(file: SourceFile, options: CompilerOptions) {
export function bindSourceFile(file: SourceFile, options: CompilerOptions): void {
performance.mark("beforeBind");
binder(file, options);
performance.mark("afterBind");
Expand Down
11 changes: 6 additions & 5 deletions src/compiler/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
BuilderProgramHost,
BuilderState,
BuildInfo,
BuildInfoFileVersionMap,
CancellationToken,
CommandLineOption,
compareStringsCaseSensitive,
Expand Down Expand Up @@ -277,7 +278,7 @@ function toBuilderProgramStateWithDefinedProgram(state: ReusableBuilderProgramSt
*
* @internal
*/
export function getBuilderFileEmit(options: CompilerOptions) {
export function getBuilderFileEmit(options: CompilerOptions): BuilderFileEmit {
let result = BuilderFileEmit.Js;
if (options.sourceMap) result = result | BuilderFileEmit.JsMap;
if (options.inlineSourceMap) result = result | BuilderFileEmit.JsInlineMap;
Expand Down Expand Up @@ -733,7 +734,7 @@ export function getPendingEmitKindWithSeen(
seenOldOptionsOrEmitKind: CompilerOptions | BuilderFileEmit | undefined,
emitOnlyDtsFiles: boolean | undefined,
isForDtsErrors: boolean,
) {
): BuilderFileEmit {
let pendingKind = getPendingEmitKind(optionsOrEmitKind, seenOldOptionsOrEmitKind);
if (emitOnlyDtsFiles) pendingKind = pendingKind & BuilderFileEmit.AllDts;
if (isForDtsErrors) pendingKind = pendingKind & BuilderFileEmit.DtsErrors;
Expand Down Expand Up @@ -1623,7 +1624,7 @@ export function computeSignatureWithDiagnostics(
text: string,
host: HostForComputeHash,
data: WriteFileCallbackData | undefined,
) {
): string {
text = getTextHandlingSourceMapForSignature(text, data);
let sourceFileDirectory: string | undefined;
if (data?.diagnostics?.length) {
Expand Down Expand Up @@ -2395,7 +2396,7 @@ export function getBuildInfoFileVersionMap(
program: IncrementalBuildInfo,
buildInfoPath: string,
host: Pick<ReadBuildProgramHost, "useCaseSensitiveFileNames" | "getCurrentDirectory">,
) {
): BuildInfoFileVersionMap {
const buildInfoDirectory = getDirectoryPath(getNormalizedAbsolutePath(buildInfoPath, host.getCurrentDirectory()));
const getCanonicalFileName = createGetCanonicalFileName(host.useCaseSensitiveFileNames());
const fileInfos = new Map<Path, string>();
Expand Down Expand Up @@ -2440,7 +2441,7 @@ export function getNonIncrementalBuildInfoRoots(
buildInfo: BuildInfo,
buildInfoPath: string,
host: Pick<ReadBuildProgramHost, "useCaseSensitiveFileNames" | "getCurrentDirectory">,
) {
): Path[] | undefined {
if (!isNonIncrementalBuildInfo(buildInfo)) return undefined;
const buildInfoDirectory = getDirectoryPath(getNormalizedAbsolutePath(buildInfoPath, host.getCurrentDirectory()));
const getCanonicalFileName = createGetCanonicalFileName(host.useCaseSensitiveFileNames());
Expand Down
16 changes: 8 additions & 8 deletions src/compiler/builderState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,11 @@ export namespace BuilderState {
/**
* Returns true if oldState is reusable, that is the emitKind = module/non module has not changed
*/
export function canReuseOldState(newReferencedMap: ReadonlyManyToManyPathMap | undefined, oldState: BuilderState | undefined) {
export function canReuseOldState(newReferencedMap: ReadonlyManyToManyPathMap | undefined, oldState: BuilderState | undefined): boolean | undefined {
return oldState && !oldState.referencedMap === !newReferencedMap;
}

export function createReferencedMap(options: CompilerOptions) {
export function createReferencedMap(options: CompilerOptions): ManyToManyPathMap | undefined {
return options.module !== ModuleKind.None && !options.outFile ?
createManyToManyPathMap() :
undefined;
Expand Down Expand Up @@ -346,7 +346,7 @@ export namespace BuilderState {
/**
* Releases needed properties
*/
export function releaseCache(state: BuilderState) {
export function releaseCache(state: BuilderState): void {
state.allFilesExcludingDefaultLibraryFile = undefined;
state.allFileNames = undefined;
}
Expand Down Expand Up @@ -391,7 +391,7 @@ export namespace BuilderState {
return (state.referencedMap ? getFilesAffectedByUpdatedShapeWhenModuleEmit : getFilesAffectedByUpdatedShapeWhenNonModuleEmit)(state, programOfThisState, sourceFile, cancellationToken, host);
}

export function updateSignatureOfFile(state: BuilderState, signature: string | undefined, path: Path) {
export function updateSignatureOfFile(state: BuilderState, signature: string | undefined, path: Path): void {
state.fileInfos.get(path)!.signature = signature;
(state.hasCalledUpdateShapeSignature ||= new Set()).add(path);
}
Expand All @@ -402,7 +402,7 @@ export namespace BuilderState {
cancellationToken: CancellationToken | undefined,
host: HostForComputeHash,
onNewSignature: (signature: string, sourceFiles: readonly SourceFile[]) => void,
) {
): void {
programOfThisState.emit(
sourceFile,
(fileName, text, _writeByteOrderMark, _onError, sourceFiles, data) => {
Expand Down Expand Up @@ -434,8 +434,8 @@ export namespace BuilderState {
sourceFile: SourceFile,
cancellationToken: CancellationToken | undefined,
host: HostForComputeHash,
useFileVersionAsSignature = state.useFileVersionAsSignature,
) {
useFileVersionAsSignature: boolean | undefined = state.useFileVersionAsSignature,
): boolean {
// If we have cached the result for this file, that means hence forth we should assume file shape is uptodate
if (state.hasCalledUpdateShapeSignature?.has(sourceFile.resolvedPath)) return false;

Expand Down Expand Up @@ -507,7 +507,7 @@ export namespace BuilderState {
/**
* Gets the files referenced by the the file path
*/
export function getReferencedByPaths(state: Readonly<BuilderState>, referencedFilePath: Path) {
export function getReferencedByPaths(state: Readonly<BuilderState>, referencedFilePath: Path): Path[] {
const keys = state.referencedMap!.getKeys(referencedFilePath);
return keys ? arrayFrom(keys.keys()) : [];
}
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1450,7 +1450,7 @@ export function getSymbolId(symbol: Symbol): SymbolId {
}

/** @internal */
export function isInstantiatedModule(node: ModuleDeclaration, preserveConstEnums: boolean) {
export function isInstantiatedModule(node: ModuleDeclaration, preserveConstEnums: boolean): boolean {
const moduleState = getModuleInstanceState(node);
return moduleState === ModuleInstanceState.Instantiated ||
(preserveConstEnums && moduleState === ModuleInstanceState.ConstEnumOnly);
Expand Down Expand Up @@ -52709,7 +52709,7 @@ function getIterationTypesKeyFromIterationTypeKind(typeKind: IterationTypeKind)
}

/** @internal */
export function signatureHasRestParameter(s: Signature) {
export function signatureHasRestParameter(s: Signature): boolean {
return !!(s.flags & SignatureFlags.HasRestParameter);
}

Expand Down
36 changes: 22 additions & 14 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ const jsxOptionMap = new Map(Object.entries({
}));

/** @internal */
export const inverseJsxOptionMap = new Map(mapIterator(jsxOptionMap.entries(), ([key, value]: [string, JsxEmit]) => ["" + value, key] as const));
export const inverseJsxOptionMap: Map<string, string> = new Map(mapIterator(jsxOptionMap.entries(), ([key, value]: [string, JsxEmit]) => ["" + value, key] as const));

// NOTE: The order here is important to default lib ordering as entries will have the same
// order in the generated program (see `getDefaultLibPriority` in program.ts). This
Expand Down Expand Up @@ -248,15 +248,15 @@ const libEntries: [string, string][] = [
*
* @internal
*/
export const libs = libEntries.map(entry => entry[0]);
export const libs: string[] = libEntries.map(entry => entry[0]);

/**
* A map of lib names to lib files. This map is used both for parsing the "lib" command line
* option as well as for resolving lib reference directives.
*
* @internal
*/
export const libMap = new Map(libEntries);
export const libMap: Map<string, string> = new Map(libEntries);

// Watch related options

Expand Down Expand Up @@ -1800,7 +1800,7 @@ function createDiagnosticForInvalidCustomType(opt: CommandLineOptionOfCustomType
}

/** @internal */
export function parseCustomTypeOption(opt: CommandLineOptionOfCustomType, value: string | undefined, errors: Diagnostic[]) {
export function parseCustomTypeOption(opt: CommandLineOptionOfCustomType, value: string | undefined, errors: Diagnostic[]): string | number | undefined {
return convertJsonOptionOfCustomType(opt, (value ?? "").trim(), errors);
}

Expand Down Expand Up @@ -1835,6 +1835,14 @@ export interface OptionsBase {
[option: string]: CompilerOptionsValue | TsConfigSourceFile | undefined;
}

/** @internal */
export interface BaseParsedCommandLine {
options: OptionsBase;
watchOptions: WatchOptions | undefined;
fileNames: string[];
errors: Diagnostic[];
}

/** @internal */
export interface ParseCommandLineWorkerDiagnostics extends DidYouMeanOptionsDiagnostics {
getOptionsNameMap: () => OptionsNameMap;
Expand Down Expand Up @@ -1875,7 +1883,7 @@ export function parseCommandLineWorker(
diagnostics: ParseCommandLineWorkerDiagnostics,
commandLine: readonly string[],
readFile?: (path: string) => string | undefined,
) {
): BaseParsedCommandLine {
const options = {} as OptionsBase;
let watchOptions: WatchOptions | undefined;
const fileNames: string[] = [];
Expand Down Expand Up @@ -2602,11 +2610,11 @@ export function convertToTSConfig(configParseResult: ParsedCommandLine, configFi
const providedKeys = new Set(optionMap.keys());
const impliedCompilerOptions: Record<string, CompilerOptionsValue> = {};
for (const option in computedOptions) {
if (!providedKeys.has(option) && some(computedOptions[option as keyof typeof computedOptions].dependencies, dep => providedKeys.has(dep))) {
const implied = computedOptions[option as keyof typeof computedOptions].computeValue(configParseResult.options);
const defaultValue = computedOptions[option as keyof typeof computedOptions].computeValue({});
if (!providedKeys.has(option) && some(computedOptions[option].dependencies, dep => providedKeys.has(dep))) {
const implied = computedOptions[option].computeValue(configParseResult.options);
const defaultValue = computedOptions[option].computeValue({});
if (implied !== defaultValue) {
impliedCompilerOptions[option] = computedOptions[option as keyof typeof computedOptions].computeValue(configParseResult.options);
impliedCompilerOptions[option] = computedOptions[option].computeValue(configParseResult.options);
}
}
}
Expand Down Expand Up @@ -2866,7 +2874,7 @@ export function generateTSConfig(options: CompilerOptions, fileNames: readonly s
}

/** @internal */
export function convertToOptionsWithAbsolutePaths(options: CompilerOptions, toAbsolutePath: (path: string) => string) {
export function convertToOptionsWithAbsolutePaths(options: CompilerOptions, toAbsolutePath: (path: string) => string): CompilerOptions {
const result: CompilerOptions = {};
const optionsNameMap = getOptionsNameMap().optionsNameMap;

Expand Down Expand Up @@ -2927,7 +2935,7 @@ export function parseJsonSourceFileConfigFileContent(sourceFile: TsConfigSourceF
}

/** @internal */
export function setConfigFileInOptions(options: CompilerOptions, configFile: TsConfigSourceFile | undefined) {
export function setConfigFileInOptions(options: CompilerOptions, configFile: TsConfigSourceFile | undefined): void {
if (configFile) {
Object.defineProperty(options, "configFile", { enumerable: false, writable: false, value: configFile });
}
Expand Down Expand Up @@ -3243,12 +3251,12 @@ function shouldReportNoInputFiles(fileNames: string[], canJsonReportNoInutFiles:
}

/** @internal */
export function canJsonReportNoInputFiles(raw: any) {
export function canJsonReportNoInputFiles(raw: any): boolean {
return !hasProperty(raw, "files") && !hasProperty(raw, "references");
}

/** @internal */
export function updateErrorForNoInputFiles(fileNames: string[], configFileName: string, configFileSpecs: ConfigFileSpecs, configParseDiagnostics: Diagnostic[], canJsonReportNoInutFiles: boolean) {
export function updateErrorForNoInputFiles(fileNames: string[], configFileName: string, configFileSpecs: ConfigFileSpecs, configParseDiagnostics: Diagnostic[], canJsonReportNoInutFiles: boolean): boolean {
const existingErrors = configParseDiagnostics.length;
if (shouldReportNoInputFiles(fileNames, canJsonReportNoInutFiles)) {
configParseDiagnostics.push(getErrorForNoInputFiles(configFileSpecs, configFileName));
Expand Down Expand Up @@ -3943,7 +3951,7 @@ export function matchesExclude(
excludeSpecs: readonly string[] | undefined,
useCaseSensitiveFileNames: boolean,
currentDirectory: string,
) {
): boolean {
return matchesExcludeWorker(
pathToCheck,
filter(excludeSpecs, spec => !invalidDotDotAfterRecursiveWildcard(spec)),
Expand Down
Loading

0 comments on commit 52eaa7b

Please sign in to comment.