|
| 1 | +use oxc_ast::{ast::Expression, AstKind}; |
| 2 | +use oxc_diagnostics::OxcDiagnostic; |
| 3 | +use oxc_macros::declare_oxc_lint; |
| 4 | +use oxc_span::Span; |
| 5 | + |
| 6 | +use crate::{context::LintContext, rule::Rule, AstNode}; |
| 7 | + |
| 8 | +fn static_promise_diagnostic(x0: &str, span0: Span) -> OxcDiagnostic { |
| 9 | + OxcDiagnostic::warn(format!( |
| 10 | + "eslint-plugin-promise(no-new-statics): Disallow calling `new` on a `Promise.{x0}`" |
| 11 | + )) |
| 12 | + .with_label(span0) |
| 13 | +} |
| 14 | + |
| 15 | +#[derive(Debug, Default, Clone)] |
| 16 | +pub struct NoNewStatics; |
| 17 | + |
| 18 | +declare_oxc_lint!( |
| 19 | + /// ### What it does |
| 20 | + /// |
| 21 | + /// Disallow calling new on a Promise static method. |
| 22 | + /// |
| 23 | + /// ### Why is this bad? |
| 24 | + /// |
| 25 | + /// Calling a Promise static method with new is invalid, resulting in a TypeError at runtime. |
| 26 | + /// |
| 27 | + /// ### Example |
| 28 | + /// ```javascript |
| 29 | + /// new Promise.resolve(value); |
| 30 | + /// ``` |
| 31 | + NoNewStatics, |
| 32 | + correctness |
| 33 | +); |
| 34 | + |
| 35 | +impl Rule for NoNewStatics { |
| 36 | + fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { |
| 37 | + let AstKind::NewExpression(new_expr) = node.kind() else { |
| 38 | + return; |
| 39 | + }; |
| 40 | + |
| 41 | + let Some(member_expr) = &new_expr.callee.get_member_expr() else { |
| 42 | + return; |
| 43 | + }; |
| 44 | + |
| 45 | + let Expression::Identifier(ident) = &member_expr.object() else { |
| 46 | + return; |
| 47 | + }; |
| 48 | + |
| 49 | + if ident.name != "Promise" || !ctx.semantic().is_reference_to_global_variable(ident) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + let Some(prop_name) = member_expr.static_property_name() else { |
| 54 | + return; |
| 55 | + }; |
| 56 | + |
| 57 | + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise |
| 58 | + if matches!( |
| 59 | + prop_name, |
| 60 | + "resolve" | "reject" | "all" | "allSettled" | "race" | "any" | "withResolvers" |
| 61 | + ) { |
| 62 | + ctx.diagnostic_with_fix( |
| 63 | + static_promise_diagnostic( |
| 64 | + prop_name, |
| 65 | + Span::new(new_expr.span.start, ident.span.start - 1), |
| 66 | + ), |
| 67 | + |fixer| fixer.delete_range(Span::new(new_expr.span.start, ident.span.start)), |
| 68 | + ); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +#[test] |
| 74 | +fn test() { |
| 75 | + use crate::tester::Tester; |
| 76 | + |
| 77 | + let pass = vec![ |
| 78 | + "Promise.resolve()", |
| 79 | + "Promise.reject()", |
| 80 | + "Promise.all()", |
| 81 | + "Promise.race()", |
| 82 | + "new Promise(function (resolve, reject) {})", |
| 83 | + "new SomeClass()", |
| 84 | + "SomeClass.resolve()", |
| 85 | + "new SomeClass.resolve()", |
| 86 | + ]; |
| 87 | + |
| 88 | + let fail = vec![ |
| 89 | + "new Promise.resolve()", |
| 90 | + "new Promise.reject()", |
| 91 | + "new Promise.all()", |
| 92 | + "new Promise.allSettled()", |
| 93 | + "new Promise.any()", |
| 94 | + "new Promise.race()", |
| 95 | + "function foo() { |
| 96 | + var a = getA() |
| 97 | + return new Promise.resolve(a) |
| 98 | + }", |
| 99 | + ]; |
| 100 | + |
| 101 | + let fix = vec![ |
| 102 | + ("new Promise.resolve()", "Promise.resolve()", None), |
| 103 | + ("new Promise.reject()", "Promise.reject()", None), |
| 104 | + ("new Promise.all()", "Promise.all()", None), |
| 105 | + ("new Promise.allSettled()", "Promise.allSettled()", None), |
| 106 | + ("new Promise.any()", "Promise.any()", None), |
| 107 | + ("new Promise.race()", "Promise.race()", None), |
| 108 | + ]; |
| 109 | + Tester::new(NoNewStatics::NAME, pass, fail).expect_fix(fix).test_and_snapshot(); |
| 110 | +} |
0 commit comments