Skip to content

Commit c447ebc

Browse files
authored
Refactor: No more than 1 namespace declaration per file (microsoft#35373)
* Refactor: No more than 1 namespace declaration per file * Simplify refs where possible
1 parent 1320c36 commit c447ebc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+14488
-14430
lines changed

.eslintrc.json

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"simple-indent": "error",
5555
"debug-assert": "error",
5656
"no-keywords": "error",
57+
"one-namespace-per-file": "error",
5758

5859
// eslint-plugin-import
5960
"import/no-extraneous-dependencies": ["error", { "optionalDependencies": false }],

Gulpfile.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -587,15 +587,15 @@ task("generate-spec").description = "Generates a Markdown version of the Languag
587587
task("clean", series(parallel(cleanTasks), cleanBuilt));
588588
task("clean").description = "Cleans build outputs";
589589

590-
const configureNightly = () => exec(process.execPath, ["scripts/configurePrerelease.js", "dev", "package.json", "src/compiler/core.ts"]);
590+
const configureNightly = () => exec(process.execPath, ["scripts/configurePrerelease.js", "dev", "package.json", "src/compiler/corePublic.ts"]);
591591
task("configure-nightly", series(buildScripts, configureNightly));
592592
task("configure-nightly").description = "Runs scripts/configurePrerelease.ts to prepare a build for nightly publishing";
593593

594-
const configureInsiders = () => exec(process.execPath, ["scripts/configurePrerelease.js", "insiders", "package.json", "src/compiler/core.ts"]);
594+
const configureInsiders = () => exec(process.execPath, ["scripts/configurePrerelease.js", "insiders", "package.json", "src/compiler/corePublic.ts"]);
595595
task("configure-insiders", series(buildScripts, configureInsiders));
596596
task("configure-insiders").description = "Runs scripts/configurePrerelease.ts to prepare a build for insiders publishing";
597597

598-
const configureExperimental = () => exec(process.execPath, ["scripts/configurePrerelease.js", "experimental", "package.json", "src/compiler/core.ts"]);
598+
const configureExperimental = () => exec(process.execPath, ["scripts/configurePrerelease.js", "experimental", "package.json", "src/compiler/corePublic.ts"]);
599599
task("configure-experimental", series(buildScripts, configureExperimental));
600600
task("configure-experimental").description = "Runs scripts/configurePrerelease.ts to prepare a build for experimental publishing";
601601

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/experimental-utils";
2+
import { createRule } from "./utils";
3+
4+
export = createRule({
5+
name: "one-namespace-per-file",
6+
meta: {
7+
docs: {
8+
description: `Limits each file to having at most one top-level namespace declaration`,
9+
category: "Possible Errors",
10+
recommended: "error",
11+
},
12+
messages: {
13+
excessNamespaceError: `All but one of these namespaces should be moved into seperate files.`,
14+
},
15+
schema: [],
16+
type: "problem",
17+
},
18+
defaultOptions: [],
19+
20+
create(context) {
21+
const isNamespaceDeclaration = (node: TSESTree.Node): node is TSESTree.TSModuleDeclaration => node.type === AST_NODE_TYPES.TSModuleDeclaration;
22+
23+
const checkSourceFile = (node: TSESTree.Program) => {
24+
if (context.getFilename().endsWith(".d.ts")) {
25+
return;
26+
}
27+
const members = node.body;
28+
const namespaces = members.filter(isNamespaceDeclaration);
29+
if (namespaces.length <= 1) {
30+
return;
31+
}
32+
33+
namespaces.forEach(n => {
34+
context.report({
35+
messageId: "excessNamespaceError", node: n
36+
});
37+
});
38+
};
39+
40+
return {
41+
Program: checkSourceFile,
42+
};
43+
},
44+
});

src/compiler/builder.ts

+1-162
Original file line numberDiff line numberDiff line change
@@ -1183,165 +1183,4 @@ namespace ts {
11831183
return Debug.assertDefined(state.program);
11841184
}
11851185
}
1186-
}
1187-
1188-
namespace ts {
1189-
export type AffectedFileResult<T> = { result: T; affected: SourceFile | Program; } | undefined;
1190-
1191-
export interface BuilderProgramHost {
1192-
/**
1193-
* return true if file names are treated with case sensitivity
1194-
*/
1195-
useCaseSensitiveFileNames(): boolean;
1196-
/**
1197-
* If provided this would be used this hash instead of actual file shape text for detecting changes
1198-
*/
1199-
createHash?: (data: string) => string;
1200-
/**
1201-
* When emit or emitNextAffectedFile are called without writeFile,
1202-
* this callback if present would be used to write files
1203-
*/
1204-
writeFile?: WriteFileCallback;
1205-
}
1206-
1207-
/**
1208-
* Builder to manage the program state changes
1209-
*/
1210-
export interface BuilderProgram {
1211-
/*@internal*/
1212-
getState(): ReusableBuilderProgramState;
1213-
/*@internal*/
1214-
backupState(): void;
1215-
/*@internal*/
1216-
restoreState(): void;
1217-
/**
1218-
* Returns current program
1219-
*/
1220-
getProgram(): Program;
1221-
/**
1222-
* Returns current program that could be undefined if the program was released
1223-
*/
1224-
/*@internal*/
1225-
getProgramOrUndefined(): Program | undefined;
1226-
/**
1227-
* Releases reference to the program, making all the other operations that need program to fail.
1228-
*/
1229-
/*@internal*/
1230-
releaseProgram(): void;
1231-
/**
1232-
* Get compiler options of the program
1233-
*/
1234-
getCompilerOptions(): CompilerOptions;
1235-
/**
1236-
* Get the source file in the program with file name
1237-
*/
1238-
getSourceFile(fileName: string): SourceFile | undefined;
1239-
/**
1240-
* Get a list of files in the program
1241-
*/
1242-
getSourceFiles(): readonly SourceFile[];
1243-
/**
1244-
* Get the diagnostics for compiler options
1245-
*/
1246-
getOptionsDiagnostics(cancellationToken?: CancellationToken): readonly Diagnostic[];
1247-
/**
1248-
* Get the diagnostics that dont belong to any file
1249-
*/
1250-
getGlobalDiagnostics(cancellationToken?: CancellationToken): readonly Diagnostic[];
1251-
/**
1252-
* Get the diagnostics from config file parsing
1253-
*/
1254-
getConfigFileParsingDiagnostics(): readonly Diagnostic[];
1255-
/**
1256-
* Get the syntax diagnostics, for all source files if source file is not supplied
1257-
*/
1258-
getSyntacticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): readonly Diagnostic[];
1259-
/**
1260-
* Get the declaration diagnostics, for all source files if source file is not supplied
1261-
*/
1262-
getDeclarationDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): readonly DiagnosticWithLocation[];
1263-
/**
1264-
* Get all the dependencies of the file
1265-
*/
1266-
getAllDependencies(sourceFile: SourceFile): readonly string[];
1267-
1268-
/**
1269-
* Gets the semantic diagnostics from the program corresponding to this state of file (if provided) or whole program
1270-
* The semantic diagnostics are cached and managed here
1271-
* Note that it is assumed that when asked about semantic diagnostics through this API,
1272-
* the file has been taken out of affected files so it is safe to use cache or get from program and cache the diagnostics
1273-
* In case of SemanticDiagnosticsBuilderProgram if the source file is not provided,
1274-
* it will iterate through all the affected files, to ensure that cache stays valid and yet provide a way to get all semantic diagnostics
1275-
*/
1276-
getSemanticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): readonly Diagnostic[];
1277-
/**
1278-
* Emits the JavaScript and declaration files.
1279-
* When targetSource file is specified, emits the files corresponding to that source file,
1280-
* otherwise for the whole program.
1281-
* In case of EmitAndSemanticDiagnosticsBuilderProgram, when targetSourceFile is specified,
1282-
* it is assumed that that file is handled from affected file list. If targetSourceFile is not specified,
1283-
* it will only emit all the affected files instead of whole program
1284-
*
1285-
* The first of writeFile if provided, writeFile of BuilderProgramHost if provided, writeFile of compiler host
1286-
* in that order would be used to write the files
1287-
*/
1288-
emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback, cancellationToken?: CancellationToken, emitOnlyDtsFiles?: boolean, customTransformers?: CustomTransformers): EmitResult;
1289-
/**
1290-
* Get the current directory of the program
1291-
*/
1292-
getCurrentDirectory(): string;
1293-
}
1294-
1295-
/**
1296-
* The builder that caches the semantic diagnostics for the program and handles the changed files and affected files
1297-
*/
1298-
export interface SemanticDiagnosticsBuilderProgram extends BuilderProgram {
1299-
/**
1300-
* Gets the semantic diagnostics from the program for the next affected file and caches it
1301-
* Returns undefined if the iteration is complete
1302-
*/
1303-
getSemanticDiagnosticsOfNextAffectedFile(cancellationToken?: CancellationToken, ignoreSourceFile?: (sourceFile: SourceFile) => boolean): AffectedFileResult<readonly Diagnostic[]>;
1304-
}
1305-
1306-
/**
1307-
* The builder that can handle the changes in program and iterate through changed file to emit the files
1308-
* The semantic diagnostics are cached per file and managed by clearing for the changed/affected files
1309-
*/
1310-
export interface EmitAndSemanticDiagnosticsBuilderProgram extends SemanticDiagnosticsBuilderProgram {
1311-
/**
1312-
* Emits the next affected file's emit result (EmitResult and sourceFiles emitted) or returns undefined if iteration is complete
1313-
* The first of writeFile if provided, writeFile of BuilderProgramHost if provided, writeFile of compiler host
1314-
* in that order would be used to write the files
1315-
*/
1316-
emitNextAffectedFile(writeFile?: WriteFileCallback, cancellationToken?: CancellationToken, emitOnlyDtsFiles?: boolean, customTransformers?: CustomTransformers): AffectedFileResult<EmitResult>;
1317-
}
1318-
1319-
/**
1320-
* Create the builder to manage semantic diagnostics and cache them
1321-
*/
1322-
export function createSemanticDiagnosticsBuilderProgram(newProgram: Program, host: BuilderProgramHost, oldProgram?: SemanticDiagnosticsBuilderProgram, configFileParsingDiagnostics?: readonly Diagnostic[]): SemanticDiagnosticsBuilderProgram;
1323-
export function createSemanticDiagnosticsBuilderProgram(rootNames: readonly string[] | undefined, options: CompilerOptions | undefined, host?: CompilerHost, oldProgram?: SemanticDiagnosticsBuilderProgram, configFileParsingDiagnostics?: readonly Diagnostic[], projectReferences?: readonly ProjectReference[]): SemanticDiagnosticsBuilderProgram;
1324-
export function createSemanticDiagnosticsBuilderProgram(newProgramOrRootNames: Program | readonly string[] | undefined, hostOrOptions: BuilderProgramHost | CompilerOptions | undefined, oldProgramOrHost?: CompilerHost | SemanticDiagnosticsBuilderProgram, configFileParsingDiagnosticsOrOldProgram?: readonly Diagnostic[] | SemanticDiagnosticsBuilderProgram, configFileParsingDiagnostics?: readonly Diagnostic[], projectReferences?: readonly ProjectReference[]) {
1325-
return createBuilderProgram(BuilderProgramKind.SemanticDiagnosticsBuilderProgram, getBuilderCreationParameters(newProgramOrRootNames, hostOrOptions, oldProgramOrHost, configFileParsingDiagnosticsOrOldProgram, configFileParsingDiagnostics, projectReferences));
1326-
}
1327-
1328-
/**
1329-
* Create the builder that can handle the changes in program and iterate through changed files
1330-
* to emit the those files and manage semantic diagnostics cache as well
1331-
*/
1332-
export function createEmitAndSemanticDiagnosticsBuilderProgram(newProgram: Program, host: BuilderProgramHost, oldProgram?: EmitAndSemanticDiagnosticsBuilderProgram, configFileParsingDiagnostics?: readonly Diagnostic[]): EmitAndSemanticDiagnosticsBuilderProgram;
1333-
export function createEmitAndSemanticDiagnosticsBuilderProgram(rootNames: readonly string[] | undefined, options: CompilerOptions | undefined, host?: CompilerHost, oldProgram?: EmitAndSemanticDiagnosticsBuilderProgram, configFileParsingDiagnostics?: readonly Diagnostic[], projectReferences?: readonly ProjectReference[]): EmitAndSemanticDiagnosticsBuilderProgram;
1334-
export function createEmitAndSemanticDiagnosticsBuilderProgram(newProgramOrRootNames: Program | readonly string[] | undefined, hostOrOptions: BuilderProgramHost | CompilerOptions | undefined, oldProgramOrHost?: CompilerHost | EmitAndSemanticDiagnosticsBuilderProgram, configFileParsingDiagnosticsOrOldProgram?: readonly Diagnostic[] | EmitAndSemanticDiagnosticsBuilderProgram, configFileParsingDiagnostics?: readonly Diagnostic[], projectReferences?: readonly ProjectReference[]) {
1335-
return createBuilderProgram(BuilderProgramKind.EmitAndSemanticDiagnosticsBuilderProgram, getBuilderCreationParameters(newProgramOrRootNames, hostOrOptions, oldProgramOrHost, configFileParsingDiagnosticsOrOldProgram, configFileParsingDiagnostics, projectReferences));
1336-
}
1337-
1338-
/**
1339-
* Creates a builder thats just abstraction over program and can be used with watch
1340-
*/
1341-
export function createAbstractBuilder(newProgram: Program, host: BuilderProgramHost, oldProgram?: BuilderProgram, configFileParsingDiagnostics?: readonly Diagnostic[]): BuilderProgram;
1342-
export function createAbstractBuilder(rootNames: readonly string[] | undefined, options: CompilerOptions | undefined, host?: CompilerHost, oldProgram?: BuilderProgram, configFileParsingDiagnostics?: readonly Diagnostic[], projectReferences?: readonly ProjectReference[]): BuilderProgram;
1343-
export function createAbstractBuilder(newProgramOrRootNames: Program | readonly string[] | undefined, hostOrOptions: BuilderProgramHost | CompilerOptions | undefined, oldProgramOrHost?: CompilerHost | BuilderProgram, configFileParsingDiagnosticsOrOldProgram?: readonly Diagnostic[] | BuilderProgram, configFileParsingDiagnostics?: readonly Diagnostic[], projectReferences?: readonly ProjectReference[]): BuilderProgram {
1344-
const { newProgram, configFileParsingDiagnostics: newConfigFileParsingDiagnostics } = getBuilderCreationParameters(newProgramOrRootNames, hostOrOptions, oldProgramOrHost, configFileParsingDiagnosticsOrOldProgram, configFileParsingDiagnostics, projectReferences);
1345-
return createRedirectedBuilderProgram({ program: newProgram, compilerOptions: newProgram.getCompilerOptions() }, newConfigFileParsingDiagnostics);
1346-
}
1347-
}
1186+
}

0 commit comments

Comments
 (0)