function walk()

in src/rules/strictExportDeclareModifiersRule.ts [21:118]


function walk(ctx: Lint.WalkContext<void>): void {
    const { sourceFile } = ctx;
    const isExternal = sourceFile.isDeclarationFile
        && !sourceFile.statements.some(
            s => s.kind === ts.SyntaxKind.ExportAssignment ||
                 s.kind === ts.SyntaxKind.ExportDeclaration && !!(s as ts.ExportDeclaration).exportClause)
        && ts.isExternalModule(sourceFile);

    for (const node of sourceFile.statements) {
        if (isExternal) {
            checkInExternalModule(node, isAutomaticExport(sourceFile));
        } else {
            checkInOther(node, sourceFile.isDeclarationFile);
        }

        if (isModuleDeclaration(node) && (sourceFile.isDeclarationFile || isDeclare(node))) {
            checkModule(node);
        }
    }

    function checkInExternalModule(node: ts.Statement, autoExportEnabled: boolean) {
        // Ignore certain node kinds (these can't have 'export' or 'default' modifiers)
        switch (node.kind) {
            case ts.SyntaxKind.ImportDeclaration:
            case ts.SyntaxKind.ImportEqualsDeclaration:
            case ts.SyntaxKind.ExportDeclaration:
            case ts.SyntaxKind.NamespaceExportDeclaration:
                return;
        }

        // `declare global` and `declare module "foo"` OK. `declare namespace N` not OK, should be `export namespace`.
        if (!isDeclareGlobalOrExternalModuleDeclaration(node)) {
            if (isDeclare(node)) {
                fail(mod(node, ts.SyntaxKind.DeclareKeyword), "'declare' keyword is redundant here.");
            }
            if (autoExportEnabled && !isExport(node)) {
                fail(
                    (node as ts.DeclarationStatement).name || node,
                    "All declarations in this module are exported automatically. " +
                    "Prefer to explicitly write 'export' for clarity. " +
                    "If you have a good reason not to export this declaration, " +
                    "add 'export {}' to the module to shut off automatic exporting.");
            }
        }
    }

    function checkInOther(node: ts.Statement, inDeclarationFile: boolean): void {
        // Compiler will enforce presence of 'declare' where necessary. But types do not need 'declare'.
        if (isDeclare(node)) {
            if (isExport(node) && inDeclarationFile ||
                node.kind === ts.SyntaxKind.InterfaceDeclaration ||
                node.kind === ts.SyntaxKind.TypeAliasDeclaration) {
                fail(mod(node, ts.SyntaxKind.DeclareKeyword), "'declare' keyword is redundant here.");
            }
        }
    }

    function fail(node: ts.Node, reason: string): void {
        ctx.addFailureAtNode(node, failure(Rule.metadata.ruleName, reason));
    }

    function mod(node: ts.Statement, kind: ts.SyntaxKind): ts.Node {
        return node.modifiers!.find(m => m.kind === kind)!;
    }

    function checkModule(moduleDeclaration: ts.ModuleDeclaration): void {
        const body = moduleDeclaration.body;
        if (!body) {
            return;
        }

        switch (body.kind) {
            case ts.SyntaxKind.ModuleDeclaration:
                checkModule(body);
                break;
            case ts.SyntaxKind.ModuleBlock:
                checkBlock(body, isAutomaticExport(moduleDeclaration));
                break;
        }
    }

    function checkBlock(block: ts.ModuleBlock, autoExportEnabled: boolean): void {
        for (const s of block.statements) {
            // Compiler will error for 'declare' here anyway, so just check for 'export'.
            if (isExport(s) && autoExportEnabled && !isDefault(s)) {
                fail(mod(s, ts.SyntaxKind.ExportKeyword),
                    "'export' keyword is redundant here because " +
                    "all declarations in this module are exported automatically. " +
                    "If you have a good reason to export some declarations and not others, " +
                    "add 'export {}' to the module to shut off automatic exporting.");
            }

            if (isModuleDeclaration(s)) {
                checkModule(s);
            }
        }
    }
}