-
-
Notifications
You must be signed in to change notification settings - Fork 540
/
Copy pathstatic_value.rs
155 lines (147 loc) · 5.13 KB
/
static_value.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use biome_rowan::TextRange;
use crate::{JsSyntaxKind, JsSyntaxToken};
#[derive(Debug, Clone, Eq, PartialEq)]
/// static values defined in JavaScript's expressions
pub enum StaticValue {
Boolean(JsSyntaxToken),
Null(JsSyntaxToken),
Undefined(JsSyntaxToken),
Number(JsSyntaxToken),
BigInt(JsSyntaxToken),
// The string can be empty.
String(JsSyntaxToken),
/// This is used to represent the empty template string.
EmptyString(TextRange),
}
impl StaticValue {
/// Return `true` if the value is falsy
///
/// ## Examples
///
/// ```
/// use biome_js_syntax::{T, static_value::StaticValue};
/// use biome_js_factory::make;
///
/// let bool = make::token(T![false]);
/// assert!(StaticValue::Boolean(bool).is_falsy());
/// ```
pub fn is_falsy(&self) -> bool {
match self {
StaticValue::Boolean(token) => token.text_trimmed() == "false",
StaticValue::Null(_) | StaticValue::Undefined(_) | StaticValue::EmptyString(_) => true,
StaticValue::Number(token) => token.text_trimmed() == "0",
StaticValue::BigInt(token) => token.text_trimmed() == "0n",
StaticValue::String(_) => self.text().is_empty(),
}
}
/// Return a string of the static value
///
/// ## Examples
///
/// ```
/// use biome_js_syntax::{T, static_value::StaticValue};
/// use biome_js_factory::make;
///
/// let bool = make::token(T![false]);
/// assert_eq!(StaticValue::Boolean(bool).text(), "false");
/// ```
pub fn text(&self) -> &str {
match self {
StaticValue::Boolean(token)
| StaticValue::Null(token)
| StaticValue::Undefined(token)
| StaticValue::Number(token)
| StaticValue::BigInt(token) => token.text_trimmed(),
StaticValue::String(token) => {
let text = token.text_trimmed();
if matches!(
token.kind(),
JsSyntaxKind::JS_STRING_LITERAL | JsSyntaxKind::JSX_STRING_LITERAL
) {
// SAFETY: string literal token have a delimiters at the start and the end of the string
return &text[1..text.len() - 1];
}
text
}
StaticValue::EmptyString(_) => "",
}
}
/// Return teh range of the static value.
///
/// ## Examples
///
/// ```
/// use biome_js_syntax::{T, static_value::StaticValue};
/// use biome_js_factory::make;
///
/// let bool = make::token(T![false]);
/// assert_eq!(StaticValue::Boolean(bool.clone()).range(), bool.text_trimmed_range());
/// ```
pub fn range(&self) -> TextRange {
match self {
StaticValue::Boolean(token)
| StaticValue::Null(token)
| StaticValue::Undefined(token)
| StaticValue::Number(token)
| StaticValue::BigInt(token)
| StaticValue::String(token) => token.text_trimmed_range(),
StaticValue::EmptyString(range) => *range,
}
}
/// Return `true` if the static value doesn't match the given string value and it is
/// 1. A string literal
/// 2. A template literal with no substitutions
///
/// ## Examples
///
/// ```
/// use biome_js_syntax::static_value::StaticValue;
/// use biome_js_factory::make;
/// use biome_rowan::TriviaPieceKind;
///
/// let str_literal = make::js_string_literal("foo")
/// .with_leading_trivia(vec![(TriviaPieceKind::Whitespace, " ")]);
/// assert!(StaticValue::String(str_literal).is_not_string_constant("bar"));
/// ```
pub fn is_not_string_constant(&self, text: &str) -> bool {
match self {
StaticValue::String(_) | StaticValue::EmptyString(_) => self.text() != text,
_ => false,
}
}
/// Return a string if the static value is
/// 1. A string literal
/// 2. A template literal with no substitutions
///
/// ## Examples
///
/// ```
/// use biome_js_syntax::static_value::StaticValue;
/// use biome_js_factory::make;
/// use biome_rowan::TriviaPieceKind;
///
/// let str_literal = make::js_string_literal("foo")
/// .with_leading_trivia(vec![(TriviaPieceKind::Whitespace, " ")]);
/// assert_eq!(StaticValue::String(str_literal).as_string_constant().unwrap(), "foo");
/// ```
pub fn as_string_constant(&self) -> Option<&str> {
match self {
StaticValue::String(_) | StaticValue::EmptyString(_) => Some(self.text()),
_ => None,
}
}
/// Return `true` if the value is null or undefined
///
/// ## Examples
///
/// ```
/// use biome_js_syntax::{T, static_value::StaticValue};
/// use biome_js_factory::make::{js_null_literal_expression, token};
///
/// let null = js_null_literal_expression(token(T![null]));
/// assert!(StaticValue::Null(null.value_token().ok().unwrap()).is_null_or_undefined());
/// ```
pub fn is_null_or_undefined(&self) -> bool {
matches!(self, StaticValue::Null(_) | StaticValue::Undefined(_))
}
}