Repository: nushell/nushell
File: crates/nu-parser/src/parse_keywords.rs (Rust)

Code before:
                        "missing 'export const' command".into(),
                        export_span,
                    ));
                    return (garbage_pipeline(working_set, spans), vec![]);
                };

                // Trying to warp the 'const' call into the 'export const' in a very clumsy way
                if let Some(Expr::Call(def_call)) = pipeline.elements.first().map(|e| &e.expr.expr)
                {
                    call.clone_from(def_call);

                    call.head = Span::concat(&spans[0..=1]);
                    call.decl_id = export_const_decl_id;
                } else {
                    working_set.error(ParseError::InternalError(
                        "unexpected output from parsing a definition".into(),
                        Span::concat(&spans[1..]),
                    ));
                };

                let mut result = vec![];

                if let Some(var_name_span) = spans.get(2) {
                    let var_name = working_set.get_span_contents(*var_name_span);
                    let var_name = trim_quotes(match var_name.last() {
                        Some(&b':') => &var_name[0..(var_name.len() - 1)],
                        _ => var_name,
                    });

                    if let Some(var_id) = working_set.find_variable(var_name) {
                        if let Err(err) = working_set.get_constant(var_id) {
                            working_set.error(err);
                        } else {
                            result.push(Exportable::VarDecl {
                                name: var_name.to_vec(),
                                id: var_id,
                            });
                        }
                    } else {
                        working_set.error(ParseError::InternalError(
                            "failed to find added variable".into(),
                            Span::concat(&spans[1..]),
                        ));
                    }
                }

                result
            }
            _ => {
                working_set.error(ParseError::Expected(
                    "def, alias, use, module, const or extern keyword",

Review comment (suggestion):
I don't think this should do manual parsing here. Can we reuse some of the general machinery for variable/constant declaration parsing?

This may break if we would relax the requirement for the type `:` to be located without space after the variable name.

Code after:
                        "missing 'export const' command".into(),
                        export_span,
                    ));
                    return (garbage_pipeline(working_set, spans), vec![]);
                };

                // Trying to warp the 'const' call into the 'export const' in a very clumsy way
                if let Some(Expr::Call(def_call)) = pipeline.elements.first().map(|e| &e.expr.expr)
                {
                    call.clone_from(def_call);

                    call.head = Span::concat(&spans[0..=1]);
                    call.decl_id = export_const_decl_id;
                } else {
                    working_set.error(ParseError::InternalError(
                        "unexpected output from parsing a definition".into(),
                        Span::concat(&spans[1..]),
                    ));
                };

                let mut result = vec![];

                if let Some(var_name_span) = var_name_span {
                    let var_name = working_set.get_span_contents(var_name_span);
                    let var_name = trim_quotes(var_name);

                    if let Some(var_id) = working_set.find_variable(var_name) {
                        if let Err(err) = working_set.get_constant(var_id) {
                            working_set.error(err);
                        } else {
                            result.push(Exportable::VarDecl {
                                name: var_name.to_vec(),
                                id: var_id,
                            });
                        }
                    } else {
                        working_set.error(ParseError::InternalError(
                            "failed to find added variable".into(),
                            Span::concat(&spans[1..]),
                        ));
                    }
                }

                result
            }
            _ => {
                working_set.error(ParseError::Expected(
                    "def, alias, use, module, const or extern keyword",
                    spans[1],
                ));