@@ -8,9 +8,10 @@ use biome_js_syntax::{
88 JsComputedMemberAssignment , JsComputedMemberExpression , JsFormalParameter , JsInitializerClause ,
99 JsNumberLiteralExpression , JsObjectBindingPatternShorthandProperty , JsParenthesizedExpression ,
1010 JsPropertyClassMember , JsPropertyObjectMember , JsSyntaxNode , JsUnaryExpression ,
11- JsUnaryOperator , JsxExpressionAttributeValue , JsxExpressionChild , TsEnumMemberList ,
12- TsIndexedAccessType , TsNumberLiteralType , TsReturnTypeAnnotation , TsTypeAnnotation ,
13- TsUnionTypeVariantList ,
11+ JsUnaryOperator , JsxExpressionAttributeValue , JsxExpressionChild , TsAsExpression ,
12+ TsEnumMemberList , TsIndexedAccessType , TsNonNullAssertionExpression , TsNumberLiteralType ,
13+ TsPredicateReturnType , TsReturnTypeAnnotation , TsSatisfiesExpression , TsTypeAnnotation ,
14+ TsTypeAssertionExpression , TsUnionTypeVariantList ,
1415} ;
1516use biome_rowan:: { AstNode , declare_node_union} ;
1617use biome_rule_options:: no_magic_numbers:: NoMagicNumbersOptions ;
@@ -45,6 +46,11 @@ declare_lint_rule! {
4546 /// const TAX_RATE = 1.23;
4647 /// let total = price * TAX_RATE;
4748 /// ```
49+ ///
50+ /// ```ts
51+ /// const TAX_RATE = 1.23 as const;
52+ /// let total = price * TAX_RATE;
53+ /// ```
4854 pub NoMagicNumbers {
4955 version: "2.1.0" ,
5056 name: "noMagicNumbers" ,
@@ -372,43 +378,28 @@ fn is_ts_numeric_literal_return_type(numeric_literal: &TsNumberLiteralType) -> b
372378 . is_some_and ( |parent| TsReturnTypeAnnotation :: can_cast ( parent. kind ( ) ) )
373379}
374380
375- /// Omits unary plus or minus expressions by returning the parent node if
376- /// the current node is a unary expression with a plus or minus operator.
377- /// Example: `-5` or `+3` will return the parent node, effectively skipping the unary operator parent node.
378- fn omit_unary_plus_minus_parent ( node : JsSyntaxNode ) -> Option < JsSyntaxNode > {
379- let unary_expression_plus_or_minus =
380- JsUnaryExpression :: cast ( node. clone ( ) ) . and_then ( |unary_node| {
381- if unary_node. operator ( ) . is_ok_and ( |operator| {
382- operator == JsUnaryOperator :: Plus || operator == JsUnaryOperator :: Minus
383- } ) {
384- Some ( unary_node)
385- } else {
386- None
387- }
388- } ) ;
389-
390- if unary_expression_plus_or_minus. is_some ( ) {
391- node. parent ( )
392- } else {
393- Some ( node)
394- }
395- }
396-
397- /// Omits the parenthesized expression if the node is wrapped in parentheses.
398- /// Example: `(5)` will return the parent node, effectively skipping the parenthesized expression.
399- fn omit_parenthesized_parent ( node : JsSyntaxNode ) -> Option < JsSyntaxNode > {
400- if JsParenthesizedExpression :: can_cast ( node. kind ( ) ) {
401- node. parent ( )
402- } else {
403- Some ( node)
404- }
405- }
406-
407- /// Returns the parent node of the given node, skipping over any unary plus/minus or parenthesized expression wrappers.
381+ /// Returns the parent node of the given node, skipping over any unary plus/minus or parenthesized expression wrappers,
382+ /// as well as all type-level wrappers (casts and `satisfies`).
408383/// It helps determine the true syntactic context of the node by ignoring these common, but semantically insignificant, wrappers.
409384fn get_sanitized_parent_node ( node : & JsSyntaxNode ) -> Option < JsSyntaxNode > {
410- node. parent ( )
411- . and_then ( |parent| omit_unary_plus_minus_parent ( parent) . and_then ( omit_parenthesized_parent) )
385+ node. ancestors ( ) . skip ( 1 ) . find ( |parent| {
386+ if TsAsExpression :: can_cast ( parent. kind ( ) )
387+ || TsNonNullAssertionExpression :: can_cast ( parent. kind ( ) )
388+ || TsSatisfiesExpression :: can_cast ( parent. kind ( ) )
389+ || TsTypeAssertionExpression :: can_cast ( parent. kind ( ) )
390+ || TsPredicateReturnType :: can_cast ( parent. kind ( ) )
391+ || JsParenthesizedExpression :: can_cast ( parent. kind ( ) )
392+ {
393+ false
394+ } else if let Some ( parent) = JsUnaryExpression :: cast_ref ( parent) {
395+ match parent. operator ( ) {
396+ Err ( _) => true ,
397+ Ok ( op) => !matches ! ( op, JsUnaryOperator :: Plus | JsUnaryOperator :: Minus ) ,
398+ }
399+ } else {
400+ true
401+ }
402+ } )
412403}
413404
414405const ALWAYS_IGNORED_IN_ARITHMETIC_OPERATIONS : & [ & str ] = & [
0 commit comments