summaryrefslogtreecommitdiff
path: root/tools/ts_library_builder/ast_util.ts
diff options
context:
space:
mode:
Diffstat (limited to 'tools/ts_library_builder/ast_util.ts')
-rw-r--r--tools/ts_library_builder/ast_util.ts73
1 files changed, 63 insertions, 10 deletions
diff --git a/tools/ts_library_builder/ast_util.ts b/tools/ts_library_builder/ast_util.ts
index 4590dcea5..81fc45f0c 100644
--- a/tools/ts_library_builder/ast_util.ts
+++ b/tools/ts_library_builder/ast_util.ts
@@ -75,14 +75,24 @@ export function checkDiagnostics(project: Project, onlyFor?: string[]) {
})
.map(diagnostic => diagnostic.compilerObject);
+ logDiagnostics(diagnostics);
+
if (diagnostics.length) {
- console.log(
- ts.formatDiagnosticsWithColorAndContext(diagnostics, formatDiagnosticHost)
- );
process.exit(1);
}
}
+function createDeclarationError(
+ msg: string,
+ declaration: ImportDeclaration | ExportDeclaration
+): Error {
+ return new Error(
+ `${msg}\n` +
+ ` In: "${declaration.getSourceFile().getFilePath()}"\n` +
+ ` Text: "${declaration.getText()}"`
+ );
+}
+
export interface FlattenNamespaceOptions {
customSources?: { [sourceFilePath: string]: string };
debug?: boolean;
@@ -151,7 +161,12 @@ export function flattenNamespace({
}
sourceFile.getExportDeclarations().forEach(exportDeclaration => {
- processSourceFile(exportDeclaration.getModuleSpecifierSourceFileOrThrow());
+ const exportedSourceFile = exportDeclaration.getModuleSpecifierSourceFile();
+ if (exportedSourceFile) {
+ processSourceFile(exportedSourceFile);
+ } else {
+ throw createDeclarationError("Missing source file.", exportDeclaration);
+ }
exportDeclaration.remove();
});
@@ -254,9 +269,19 @@ export function loadFiles(project: Project, filePaths: string[]) {
}
}
+/** Log diagnostics to the console with colour. */
+export function logDiagnostics(diagnostics: ts.Diagnostic[]): void {
+ if (diagnostics.length) {
+ console.log(
+ ts.formatDiagnosticsWithColorAndContext(diagnostics, formatDiagnosticHost)
+ );
+ }
+}
+
export interface NamespaceSourceFileOptions {
debug?: boolean;
namespace?: string;
+ namespaces: Set<string>;
rootPath: string;
sourceFileMap: Map<SourceFile, string>;
}
@@ -267,7 +292,13 @@ export interface NamespaceSourceFileOptions {
*/
export function namespaceSourceFile(
sourceFile: SourceFile,
- { debug, namespace, rootPath, sourceFileMap }: NamespaceSourceFileOptions
+ {
+ debug,
+ namespace,
+ namespaces,
+ rootPath,
+ sourceFileMap
+ }: NamespaceSourceFileOptions
): string {
if (sourceFileMap.has(sourceFile)) {
return "";
@@ -300,22 +331,42 @@ export function namespaceSourceFile(
const output = sourceFile
.getImportDeclarations()
+ .filter(declaration => {
+ const dsf = declaration.getModuleSpecifierSourceFile();
+ if (dsf == null) {
+ try {
+ const namespaceName = declaration
+ .getNamespaceImportOrThrow()
+ .getText();
+ if (!namespaces.has(namespaceName)) {
+ throw createDeclarationError(
+ "Already defined source file under different namespace.",
+ declaration
+ );
+ }
+ } catch (e) {
+ throw createDeclarationError(
+ "Unsupported import clause.",
+ declaration
+ );
+ }
+ declaration.remove();
+ }
+ return dsf;
+ })
.map(declaration => {
if (
declaration.getNamedImports().length ||
!declaration.getNamespaceImport()
) {
- throw new Error(
- "Unsupported import clause.\n" +
- ` In: "${declaration.getSourceFile().getFilePath()}"\n` +
- ` Text: "${declaration.getText()}"`
- );
+ throw createDeclarationError("Unsupported import clause.", declaration);
}
const text = namespaceSourceFile(
declaration.getModuleSpecifierSourceFileOrThrow(),
{
debug,
namespace: declaration.getNamespaceImportOrThrow().getText(),
+ namespaces,
rootPath,
sourceFileMap
}
@@ -328,6 +379,8 @@ export function namespaceSourceFile(
.getExportDeclarations()
.forEach(declaration => declaration.remove());
+ namespaces.add(namespace);
+
return `${output}
${globalNamespaceText || ""}