tsv_ts 0.8.0

TypeScript/JS formatter, type stripper and acorn-compatible parser — part of tsv
Documentation
// Import and export specifier writers.

use super::declarations::{write_class_declaration, write_function_declaration};
use super::expressions::write_expression;
use super::types::{write_declare_function, write_interface_declaration};
use super::{
    Ctx, JsonWriter, close_node, node_header, write_export_kind_field, write_identifier_plain,
    write_import_kind_field, write_literal,
};
use crate::ast;

/// Emit an import specifier (`ImportDefaultSpecifier` / `ImportSpecifier` /
/// `ImportNamespaceSpecifier`).
pub(super) fn write_import_specifier(
    w: &mut JsonWriter,
    spec: &ast::ImportSpecifier<'_>,
    ctx: &Ctx<'_>,
) {
    match spec {
        ast::ImportSpecifier::Default(default_spec) => {
            node_header(w, "ImportDefaultSpecifier", default_spec.span, ctx);
            w.raw(",\"local\":");
            write_identifier_plain(w, &default_spec.local, ctx);
            close_node(w, "ImportDefaultSpecifier", default_spec.span, ctx);
        }
        ast::ImportSpecifier::Named(named_spec) => {
            node_header(w, "ImportSpecifier", named_spec.span, ctx);
            w.raw(",\"imported\":");
            write_module_export_name(w, &named_spec.imported, ctx);
            w.raw(",\"local\":");
            write_identifier_plain(w, &named_spec.local, ctx);
            write_import_kind_field(w, named_spec.import_kind, ctx);
            close_node(w, "ImportSpecifier", named_spec.span, ctx);
        }
        ast::ImportSpecifier::Namespace(ns_spec) => {
            node_header(w, "ImportNamespaceSpecifier", ns_spec.span, ctx);
            w.raw(",\"local\":");
            write_identifier_plain(w, &ns_spec.local, ctx);
            close_node(w, "ImportNamespaceSpecifier", ns_spec.span, ctx);
        }
    }
}

/// Emits an `ImportAttribute` node.
pub(super) fn write_import_attribute(
    w: &mut JsonWriter,
    attr: &ast::ImportAttribute<'_>,
    ctx: &Ctx<'_>,
) {
    node_header(w, "ImportAttribute", attr.span, ctx);
    w.raw(",\"key\":");
    match &attr.key {
        ast::ImportAttributeKey::Identifier(id) => write_identifier_plain(w, id, ctx),
        ast::ImportAttributeKey::Literal(lit) => write_literal(w, lit, ctx),
    }
    w.raw(",\"value\":");
    write_literal(w, &attr.value, ctx);
    close_node(w, "ImportAttribute", attr.span, ctx);
}

/// Emits an `ExportSpecifier` node. Field order: `local`, `exported`,
/// `exportKind?`.
pub(super) fn write_export_specifier(
    w: &mut JsonWriter,
    spec: &ast::ExportSpecifier<'_>,
    ctx: &Ctx<'_>,
) {
    node_header(w, "ExportSpecifier", spec.span, ctx);
    w.raw(",\"local\":");
    write_module_export_name(w, &spec.local, ctx);
    w.raw(",\"exported\":");
    write_module_export_name(w, &spec.exported, ctx);
    write_export_kind_field(w, spec.export_kind, ctx);
    close_node(w, "ExportSpecifier", spec.span, ctx);
}

/// Emit a module export name: an identifier emits an `Identifier` node, a
/// string a `Literal` node.
pub(super) fn write_module_export_name(
    w: &mut JsonWriter,
    name: &ast::ModuleExportName<'_>,
    ctx: &Ctx<'_>,
) {
    match name {
        ast::ModuleExportName::Identifier(id) => write_identifier_plain(w, id, ctx),
        ast::ModuleExportName::Literal(lit) => write_literal(w, lit, ctx),
    }
}

/// Emit an `export default` value (an expression or a function/class/interface
/// declaration).
pub(super) fn write_export_default_value(
    w: &mut JsonWriter,
    value: &ast::ExportDefaultValue<'_>,
    ctx: &Ctx<'_>,
) {
    match value {
        ast::ExportDefaultValue::Expression(expr) => write_expression(w, expr, ctx),
        ast::ExportDefaultValue::FunctionDeclaration(func) => {
            write_function_declaration(w, func, ctx);
        }
        ast::ExportDefaultValue::TSDeclareFunction(func) => {
            // `declare` can't syntactically appear in an `export default`, so
            // the `exported` declare-position flag is inert here.
            write_declare_function(w, func, ctx, true);
        }
        ast::ExportDefaultValue::ClassDeclaration(class) => {
            // As with `TSDeclareFunction` above, `declare` can't syntactically appear
            // in an `export default`, so the declare-position flag is inert here.
            write_class_declaration(w, class, ctx, false);
        }
        ast::ExportDefaultValue::TSInterfaceDeclaration(iface) => {
            write_interface_declaration(w, iface, ctx, true);
        }
    }
}