refactor(ast): VariableDeclarationKind::to_string -> as_str (#1321)

This commit is contained in:
Boshen 2023-11-14 23:28:13 +08:00 committed by GitHub
parent d2b73122c5
commit be043c37e9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 5 additions and 6 deletions

View file

@ -1113,7 +1113,7 @@ impl VariableDeclarationKind {
matches!(self, Self::Const | Self::Let) matches!(self, Self::Const | Self::Let)
} }
pub fn to_string(&self) -> &str { pub fn as_str(&self) -> &'static str {
match self { match self {
Self::Var => "var", Self::Var => "var",
Self::Const => "const", Self::Const => "const",
@ -1124,7 +1124,7 @@ impl VariableDeclarationKind {
impl fmt::Display for VariableDeclarationKind { impl fmt::Display for VariableDeclarationKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let s = self.to_string(); let s = self.as_str();
write!(f, "{s}") write!(f, "{s}")
} }
} }

View file

@ -353,12 +353,12 @@ impl<'a> Format<'a> for Declaration<'a> {
impl<'a> Format<'a> for VariableDeclaration<'a> { impl<'a> Format<'a> for VariableDeclaration<'a> {
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> { fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
let kind = self.kind.to_string(); let kind = self.kind.as_str();
let mut decls = p.vec(); let mut decls = p.vec();
decls.extend(self.declarations.iter().map(|decl| decl.format(p))); decls.extend(self.declarations.iter().map(|decl| decl.format(p)));
let mut parts = p.vec(); let mut parts = p.vec();
parts.push(p.str(kind)); parts.push(ss!(kind));
parts.push(ss!(" ")); parts.push(ss!(" "));
parts.push(Doc::Array(decls)); parts.push(Doc::Array(decls));

View file

@ -216,8 +216,7 @@ fn check_binding_identifier<'a>(
)] )]
#[diagnostic()] #[diagnostic()]
struct InvalidLetDeclaration(String, #[label] Span); struct InvalidLetDeclaration(String, #[label] Span);
return ctx return ctx.error(InvalidLetDeclaration(decl.kind.as_str().into(), ident.span));
.error(InvalidLetDeclaration(decl.kind.to_string().into(), ident.span));
} }
AstKind::VariableDeclaration(_) | AstKind::Function(_) | AstKind::Program(_) => { AstKind::VariableDeclaration(_) | AstKind::Function(_) | AstKind::Program(_) => {
break; break;