Skip to main content

boa_ast/expression/operator/update/
op.rs

1/// A update operator is one that takes a single operand/argument and performs an operation.
2///
3/// More information:
4///  - [ECMAScript reference][spec]
5///  - [MDN documentation][mdn]
6///
7/// [spec]: https://tc39.es/ecma262/#prod-UpdateExpression
8/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators#increment_and_decrement
9#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
11#[derive(Clone, Copy, Debug, PartialEq, Eq)]
12pub enum UpdateOp {
13    /// The increment operator increments (adds one to) its operand and returns a value.
14    ///
15    /// Syntax: `x++`
16    ///
17    /// This operator increments and returns the value after incrementing.
18    ///
19    /// More information:
20    ///  - [ECMAScript reference][spec]
21    ///  - [MDN documentation][mdn]
22    ///
23    /// [spec]: https://tc39.es/ecma262/#sec-postfix-increment-operator
24    /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Increment
25    IncrementPost,
26
27    /// The increment operator increments (adds one to) its operand and returns a value.
28    ///
29    /// Syntax: `++x`
30    ///
31    /// This operator increments and returns the value before incrementing.
32    ///
33    /// More information:
34    ///  - [ECMAScript reference][spec]
35    ///  - [MDN documentation][mdn]
36    ///
37    /// [spec]: https://tc39.es/ecma262/#sec-prefix-increment-operator
38    /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Increment
39    IncrementPre,
40
41    /// The decrement operator decrements (subtracts one from) its operand and returns a value.
42    ///
43    /// Syntax: `x--`
44    ///
45    /// This operator decrements and returns the value before decrementing.
46    ///
47    /// More information:
48    ///  - [ECMAScript reference][spec]
49    ///  - [MDN documentation][mdn]
50    ///
51    /// [spec]: https://tc39.es/ecma262/#sec-postfix-decrement-operator
52    /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Decrement
53    DecrementPost,
54
55    /// The decrement operator decrements (subtracts one from) its operand and returns a value.
56    ///
57    /// Syntax: `--x`
58    ///
59    /// This operator decrements the operand and returns the value after decrementing.
60    ///
61    /// More information:
62    ///  - [ECMAScript reference][spec]
63    ///  - [MDN documentation][mdn]
64    ///
65    /// [spec]: https://tc39.es/ecma262/#sec-prefix-decrement-operator
66    /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Decrement
67    DecrementPre,
68}
69
70impl UpdateOp {
71    /// Retrieves the operation as a static string.
72    const fn as_str(self) -> &'static str {
73        match self {
74            Self::IncrementPost | Self::IncrementPre => "++",
75            Self::DecrementPost | Self::DecrementPre => "--",
76        }
77    }
78}
79
80impl std::fmt::Display for UpdateOp {
81    #[inline]
82    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83        write!(f, "{}", self.as_str())
84    }
85}