From 56d0d612d50ade34d89cbba1c4cda0ccbfc7da08 Mon Sep 17 00:00:00 2001 From: GearsDatapacks Date: Tue, 19 Nov 2024 21:12:06 +0000 Subject: [PATCH] Use target instead of string --- compiler-core/src/parse.rs | 4 +-- compiler-core/src/warning.rs | 66 +++++++++++++++++++++--------------- 2 files changed, 41 insertions(+), 29 deletions(-) diff --git a/compiler-core/src/parse.rs b/compiler-core/src/parse.rs index 3eef936b7b2..eeebc00a015 100644 --- a/compiler-core/src/parse.rs +++ b/compiler-core/src/parse.rs @@ -3298,7 +3298,7 @@ functions are declared separately from types."; self.warnings .push(DeprecatedSyntaxWarning::DeprecatedTargetShorthand { location: SrcSpan { start, end }, - full_name: "javascript", + target: Target::JavaScript, }); Ok(Target::JavaScript) } @@ -3306,7 +3306,7 @@ functions are declared separately from types."; self.warnings .push(DeprecatedSyntaxWarning::DeprecatedTargetShorthand { location: SrcSpan { start, end }, - full_name: "erlang", + target: Target::Erlang, }); Ok(Target::Erlang) } diff --git a/compiler-core/src/warning.rs b/compiler-core/src/warning.rs index ff3109182a9..4031c77640b 100644 --- a/compiler-core/src/warning.rs +++ b/compiler-core/src/warning.rs @@ -1,5 +1,6 @@ use crate::{ ast::{SrcSpan, TodoKind}, + build::Target, diagnostic::{self, Diagnostic, Location}, error::wrap, type_::{ @@ -167,7 +168,9 @@ pub enum Warning { pub enum DeprecatedSyntaxWarning { /// If someone uses the deprecated syntax to append to a list: /// `["a"..rest]`, notice how there's no comma! - DeprecatedListPrepend { location: SrcSpan }, + DeprecatedListPrepend { + location: SrcSpan, + }, /// If someone uses the deprecated syntax to pattern match on a list: /// ```gleam @@ -178,7 +181,9 @@ pub enum DeprecatedSyntaxWarning { /// } /// ``` /// - DeprecatedListPattern { location: SrcSpan }, + DeprecatedListPattern { + location: SrcSpan, + }, /// If someone uses the deprecated syntax to match on all lists instead of /// a common `_`: @@ -190,7 +195,9 @@ pub enum DeprecatedSyntaxWarning { /// } /// ``` /// - DeprecatedListCatchAllPattern { location: SrcSpan }, + DeprecatedListCatchAllPattern { + location: SrcSpan, + }, /// If a record pattern has a spread that is not preceded by a comma: /// ```gleam @@ -200,10 +207,12 @@ pub enum DeprecatedSyntaxWarning { /// } /// ``` /// - DeprecatedRecordSpreadPattern { location: SrcSpan }, + DeprecatedRecordSpreadPattern { + location: SrcSpan, + }, DeprecatedTargetShorthand { - full_name: &'static str, + target: Target, location: SrcSpan, }, } @@ -319,28 +328,31 @@ To match on all possible lists, use the `_` catch-all pattern instead.", Warning::DeprecatedSyntax { path, src, - warning: - DeprecatedSyntaxWarning::DeprecatedTargetShorthand { - location, - full_name, - }, - } => Diagnostic { - title: "Deprecated target shorthand syntax".into(), - text: wrap(&format!( - "This shorthand target name is deprecated. Use the full name: `{full_name}` instead." - )), - hint: None, - level: diagnostic::Level::Warning, - location: Some(Location { - label: diagnostic::Label { - text: Some(format!("This should be replaced with `{full_name}`")), - span: *location, - }, - path: path.clone(), - src: src.clone(), - extra_labels: vec![], - }), - }, + warning: DeprecatedSyntaxWarning::DeprecatedTargetShorthand { location, target }, + } => { + let full_name = match target { + Target::Erlang => "erlang", + Target::JavaScript => "javascript", + }; + + Diagnostic { + title: "Deprecated target shorthand syntax".into(), + text: wrap(&format!( + "This shorthand target name is deprecated. Use the full name: `{full_name}` instead." + )), + hint: None, + level: diagnostic::Level::Warning, + location: Some(Location { + label: diagnostic::Label { + text: Some(format!("This should be replaced with `{full_name}`")), + span: *location, + }, + path: path.clone(), + src: src.clone(), + extra_labels: vec![], + }), + } + } Self::Type { path, warning, src } => match warning { type_::Warning::Todo {