|
| 1 | +use oxc_ast::{ |
| 2 | + ast::{BindingPatternKind, Expression, FormalParameter, FormalParameters}, |
| 3 | + AstKind, |
| 4 | +}; |
| 5 | +use oxc_diagnostics::OxcDiagnostic; |
| 6 | +use oxc_macros::declare_oxc_lint; |
| 7 | +use oxc_span::Span; |
| 8 | +use regex::Regex; |
| 9 | + |
| 10 | +use crate::{context::LintContext, rule::Rule, AstNode}; |
| 11 | + |
| 12 | +fn param_names_diagnostic(span0: Span, x0: &str) -> OxcDiagnostic { |
| 13 | + OxcDiagnostic::warn(format!("eslint-plugin-promise(param-names): Promise constructor parameters must be named to match `{x0}`")).with_label(span0) |
| 14 | +} |
| 15 | + |
| 16 | +#[derive(Debug, Default, Clone)] |
| 17 | +pub struct ParamNames(Box<ParamNamesConfig>); |
| 18 | + |
| 19 | +#[derive(Debug, Default, Clone)] |
| 20 | +pub struct ParamNamesConfig { |
| 21 | + resolve_pattern: Option<Regex>, |
| 22 | + reject_pattern: Option<Regex>, |
| 23 | +} |
| 24 | + |
| 25 | +impl std::ops::Deref for ParamNames { |
| 26 | + type Target = ParamNamesConfig; |
| 27 | + |
| 28 | + fn deref(&self) -> &Self::Target { |
| 29 | + &self.0 |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +enum ParamType { |
| 34 | + Resolve, |
| 35 | + Reject, |
| 36 | +} |
| 37 | + |
| 38 | +declare_oxc_lint!( |
| 39 | + /// ### What it does |
| 40 | + /// |
| 41 | + /// Enforce standard parameter names for Promise constructors. |
| 42 | + /// |
| 43 | + /// ### Why is this bad? |
| 44 | + /// |
| 45 | + /// Ensures that new Promise() is instantiated with the parameter names resolve, reject to |
| 46 | + /// avoid confusion with order such as reject, resolve. The Promise constructor uses the |
| 47 | + /// RevealingConstructor pattern. Using the same parameter names as the language specification |
| 48 | + /// makes code more uniform and easier to understand. |
| 49 | + /// |
| 50 | + /// ### Example |
| 51 | + /// ```javascript |
| 52 | + /// new Promise(function (reject, resolve) { ... }) // incorrect order |
| 53 | + /// new Promise(function (ok, fail) { ... }) // non-standard parameter names |
| 54 | + /// ``` |
| 55 | + ParamNames, |
| 56 | + style, |
| 57 | +); |
| 58 | + |
| 59 | +impl Rule for ParamNames { |
| 60 | + fn from_configuration(value: serde_json::Value) -> Self { |
| 61 | + let mut cfg = ParamNamesConfig::default(); |
| 62 | + |
| 63 | + if let Some(config) = value.get(0) { |
| 64 | + if let Some(val) = config.get("resolvePattern").and_then(serde_json::Value::as_str) { |
| 65 | + cfg.resolve_pattern = Regex::new(val).ok(); |
| 66 | + } |
| 67 | + if let Some(val) = config.get("rejectPattern").and_then(serde_json::Value::as_str) { |
| 68 | + cfg.reject_pattern = Regex::new(val).ok(); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + Self(Box::new(cfg)) |
| 73 | + } |
| 74 | + |
| 75 | + fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { |
| 76 | + let AstKind::NewExpression(new_expr) = node.kind() else { |
| 77 | + return; |
| 78 | + }; |
| 79 | + |
| 80 | + if !new_expr.callee.is_specific_id("Promise") || new_expr.arguments.len() != 1 { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + for argument in &new_expr.arguments { |
| 85 | + let Some(arg_expr) = argument.as_expression() else { |
| 86 | + continue; |
| 87 | + }; |
| 88 | + match arg_expr { |
| 89 | + Expression::ArrowFunctionExpression(arrow_expr) => { |
| 90 | + self.check_parameter_names(&arrow_expr.params, ctx); |
| 91 | + } |
| 92 | + Expression::FunctionExpression(func_expr) => { |
| 93 | + self.check_parameter_names(&func_expr.params, ctx); |
| 94 | + } |
| 95 | + _ => continue, |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +impl ParamNames { |
| 102 | + fn check_parameter_names(&self, params: &FormalParameters, ctx: &LintContext) { |
| 103 | + if params.items.is_empty() { |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + self.check_parameter(¶ms.items[0], &ParamType::Resolve, ctx); |
| 108 | + |
| 109 | + if params.items.len() > 1 { |
| 110 | + self.check_parameter(¶ms.items[1], &ParamType::Reject, ctx); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + fn check_parameter(&self, param: &FormalParameter, param_type: &ParamType, ctx: &LintContext) { |
| 115 | + let BindingPatternKind::BindingIdentifier(param_ident) = ¶m.pattern.kind else { |
| 116 | + return; |
| 117 | + }; |
| 118 | + |
| 119 | + let param_pattern = if matches!(param_type, ParamType::Reject) { |
| 120 | + &self.reject_pattern |
| 121 | + } else { |
| 122 | + &self.resolve_pattern |
| 123 | + }; |
| 124 | + |
| 125 | + match param_pattern { |
| 126 | + Some(pattern) => { |
| 127 | + if !pattern.is_match(param_ident.name.as_str()) { |
| 128 | + ctx.diagnostic(param_names_diagnostic(param_ident.span, pattern.as_str())); |
| 129 | + } |
| 130 | + } |
| 131 | + None => { |
| 132 | + if matches!(param_type, ParamType::Resolve) |
| 133 | + && !matches!(param_ident.name.as_str(), "_resolve" | "resolve") |
| 134 | + { |
| 135 | + ctx.diagnostic(param_names_diagnostic(param_ident.span, "^_?resolve$")); |
| 136 | + } else if matches!(param_type, ParamType::Reject) |
| 137 | + && !matches!(param_ident.name.as_str(), "_reject" | "reject") |
| 138 | + { |
| 139 | + ctx.diagnostic(param_names_diagnostic(param_ident.span, "^_?reject$")); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +#[test] |
| 147 | +fn test() { |
| 148 | + use crate::tester::Tester; |
| 149 | + |
| 150 | + let pass = vec![ |
| 151 | + ("new Promise(function(resolve, reject) {})", None), |
| 152 | + ("new Promise(function(resolve, _reject) {})", None), |
| 153 | + ("new Promise(function(_resolve, reject) {})", None), |
| 154 | + ("new Promise(function(_resolve, _reject) {})", None), |
| 155 | + ("new Promise(function(resolve) {})", None), |
| 156 | + ("new Promise(function(_resolve) {})", None), |
| 157 | + ("new Promise(resolve => {})", None), |
| 158 | + ("new Promise((resolve, reject) => {})", None), |
| 159 | + ("new Promise(() => {})", None), |
| 160 | + ("new NonPromise()", None), |
| 161 | + ( |
| 162 | + "new Promise((yes, no) => {})", |
| 163 | + Some(serde_json::json!([{ "resolvePattern": "^yes$", "rejectPattern": "^no$" }])), |
| 164 | + ), |
| 165 | + ]; |
| 166 | + |
| 167 | + let fail = vec![ |
| 168 | + ("new Promise(function(reject, resolve) {})", None), |
| 169 | + ("new Promise(function(resolve, rej) {})", None), |
| 170 | + ("new Promise(yes => {})", None), |
| 171 | + ("new Promise((yes, no) => {})", None), |
| 172 | + ( |
| 173 | + "new Promise(function(resolve, reject) { config(); })", |
| 174 | + Some(serde_json::json!([{ "resolvePattern": "^yes$", "rejectPattern": "^no$" }])), |
| 175 | + ), |
| 176 | + ]; |
| 177 | + |
| 178 | + Tester::new(ParamNames::NAME, pass, fail).test_and_snapshot(); |
| 179 | +} |
0 commit comments