|
| 1 | +use oxc_ast::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 no_optional_chaining_diagnostic(span0: Span, x1: &str) -> OxcDiagnostic { |
| 9 | + if x1.is_empty() { |
| 10 | + OxcDiagnostic::warn("oxc(no-optional-chaining): Optional chaining is not allowed.") |
| 11 | + .with_labels([span0.into()]) |
| 12 | + } else { |
| 13 | + OxcDiagnostic::warn("oxc(no-optional-chaining): Optional chaining is not allowed.") |
| 14 | + .with_help(x1) |
| 15 | + .with_labels([span0.into()]) |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +#[derive(Debug, Default, Clone)] |
| 20 | +pub struct NoOptionalChaining(Box<NoOptionalChainingConfig>); |
| 21 | + |
| 22 | +#[derive(Debug, Default, Clone)] |
| 23 | +pub struct NoOptionalChainingConfig { |
| 24 | + message: String, |
| 25 | +} |
| 26 | + |
| 27 | +impl std::ops::Deref for NoOptionalChaining { |
| 28 | + type Target = NoOptionalChainingConfig; |
| 29 | + |
| 30 | + fn deref(&self) -> &Self::Target { |
| 31 | + &self.0 |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +declare_oxc_lint!( |
| 36 | + /// ### What it does |
| 37 | + /// |
| 38 | + /// Disallow [optional chaining](https://github.com/tc39/proposal-optional-chaining). |
| 39 | + /// |
| 40 | + /// ### Example |
| 41 | + /// |
| 42 | + /// ```javascript |
| 43 | + /// const foo = obj?.foo; |
| 44 | + /// obj.fn?.(); |
| 45 | + /// ``` |
| 46 | + /// |
| 47 | + /// ### Options |
| 48 | + /// |
| 49 | + /// ```json |
| 50 | + /// { |
| 51 | + /// "rules": { |
| 52 | + /// "no-optional-chaining": [ |
| 53 | + /// "error", |
| 54 | + /// { |
| 55 | + /// "message": "Our output target is ES2016, and optional chaining results in verbose |
| 56 | + /// helpers and should be avoided.", |
| 57 | + /// } |
| 58 | + /// ] |
| 59 | + /// } |
| 60 | + /// } |
| 61 | + /// ``` |
| 62 | + /// |
| 63 | + /// - `message`: A custom help message to display when optional chaining is found. |
| 64 | + /// |
| 65 | + NoOptionalChaining, |
| 66 | + restriction, |
| 67 | +); |
| 68 | + |
| 69 | +impl Rule for NoOptionalChaining { |
| 70 | + fn from_configuration(value: serde_json::Value) -> Self { |
| 71 | + let config = value.get(0); |
| 72 | + let message = config |
| 73 | + .and_then(|v| v.get("message")) |
| 74 | + .and_then(serde_json::Value::as_str) |
| 75 | + .unwrap_or_default(); |
| 76 | + |
| 77 | + Self(Box::new(NoOptionalChainingConfig { message: message.to_string() })) |
| 78 | + } |
| 79 | + |
| 80 | + fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { |
| 81 | + if let AstKind::ChainExpression(expr) = node.kind() { |
| 82 | + ctx.diagnostic(no_optional_chaining_diagnostic(expr.span, &self.message)); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// Test cases port from: https://github.com/mysticatea/eslint-plugin-es/blob/v4.1.0/tests/lib/rules/no-optional-chaining.js |
| 88 | +#[test] |
| 89 | +fn test() { |
| 90 | + use crate::tester::Tester; |
| 91 | + |
| 92 | + let pass = vec![("var x = a.b", None), ("var x = a[b]", None), ("foo()", None)]; |
| 93 | + |
| 94 | + let fail = vec![ |
| 95 | + ("var x = a?.b", None), |
| 96 | + ("var x = a?.[b]", None), |
| 97 | + ("foo?.()", None), |
| 98 | + ("var x = ((a?.b)?.c)?.()", None), |
| 99 | + ("var x = a/*?.*/?.b", None), |
| 100 | + ("var x = '?.'?.['?.']", None), |
| 101 | + ("var x = '?.'?.['?.']", None), |
| 102 | + ( |
| 103 | + "var x = a?.b", |
| 104 | + Some(serde_json::json!([{ |
| 105 | + "message": "Our output target is ES2016, and optional chaining results in verbose helpers and should be avoided." |
| 106 | + }])), |
| 107 | + ), |
| 108 | + ]; |
| 109 | + |
| 110 | + Tester::new(NoOptionalChaining::NAME, pass, fail).test_and_snapshot(); |
| 111 | +} |
0 commit comments