|
1 | 1 | use oxc_macros::declare_oxc_lint; |
| 2 | +use schemars::JsonSchema; |
| 3 | +use serde::{Deserialize, Serialize}; |
2 | 4 |
|
3 | | -use crate::rule::Rule; |
| 5 | +use crate::rule::{DefaultRuleConfig, Rule}; |
4 | 6 |
|
5 | 7 | #[derive(Debug, Default, Clone)] |
6 | | -pub struct NoFloatingPromises; |
| 8 | +pub struct NoFloatingPromises(Box<NoFloatingPromisesConfig>); |
| 9 | + |
| 10 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] |
| 11 | +#[serde(rename_all = "camelCase", default)] |
| 12 | +pub struct NoFloatingPromisesConfig { |
| 13 | + /// Allows specific calls to be ignored, specified as type or value specifiers. |
| 14 | + pub allow_for_known_safe_calls: Vec<TypeOrValueSpecifier>, |
| 15 | + /// Allows specific Promise types to be ignored, specified as type or value specifiers. |
| 16 | + pub allow_for_known_safe_promises: Vec<TypeOrValueSpecifier>, |
| 17 | + /// Check for thenable objects that are not necessarily Promises. |
| 18 | + pub check_thenables: bool, |
| 19 | + /// Ignore immediately invoked function expressions (IIFEs). |
| 20 | + #[serde(rename = "ignoreIIFE")] |
| 21 | + pub ignore_iife: bool, |
| 22 | + /// Ignore Promises that are void expressions. |
| 23 | + pub ignore_void: bool, |
| 24 | +} |
| 25 | + |
| 26 | +/// Type or value specifier for matching specific declarations |
| 27 | +/// |
| 28 | +/// Supports four types of specifiers: |
| 29 | +/// |
| 30 | +/// 1. **String specifier** (deprecated): Universal match by name |
| 31 | +/// ```json |
| 32 | +/// "Promise" |
| 33 | +/// ``` |
| 34 | +/// |
| 35 | +/// 2. **File specifier**: Match types/values declared in local files |
| 36 | +/// ```json |
| 37 | +/// { "from": "file", "name": "MyType" } |
| 38 | +/// { "from": "file", "name": ["Type1", "Type2"] } |
| 39 | +/// { "from": "file", "name": "MyType", "path": "./types.ts" } |
| 40 | +/// ``` |
| 41 | +/// |
| 42 | +/// 3. **Lib specifier**: Match TypeScript built-in lib types |
| 43 | +/// ```json |
| 44 | +/// { "from": "lib", "name": "Promise" } |
| 45 | +/// { "from": "lib", "name": ["Promise", "PromiseLike"] } |
| 46 | +/// ``` |
| 47 | +/// |
| 48 | +/// 4. **Package specifier**: Match types/values from npm packages |
| 49 | +/// ```json |
| 50 | +/// { "from": "package", "name": "Observable", "package": "rxjs" } |
| 51 | +/// { "from": "package", "name": ["Observable", "Subject"], "package": "rxjs" } |
| 52 | +/// ``` |
| 53 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] |
| 54 | +#[serde(untagged)] |
| 55 | +pub enum TypeOrValueSpecifier { |
| 56 | + /// Universal string specifier - matches all types and values with this name regardless of declaration source. |
| 57 | + /// Not recommended - will be removed in a future major version. |
| 58 | + String(String), |
| 59 | + /// Describes specific types or values declared in local files. |
| 60 | + File(FileSpecifier), |
| 61 | + /// Describes specific types or values declared in TypeScript's built-in lib.*.d.ts types. |
| 62 | + Lib(LibSpecifier), |
| 63 | + /// Describes specific types or values imported from packages. |
| 64 | + Package(PackageSpecifier), |
| 65 | +} |
| 66 | + |
| 67 | +/// Describes specific types or values declared in local files. |
| 68 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] |
| 69 | +#[serde(rename_all = "camelCase")] |
| 70 | +pub struct FileSpecifier { |
| 71 | + /// Must be "file" |
| 72 | + from: FileFrom, |
| 73 | + /// The name(s) of the type or value to match |
| 74 | + name: NameSpecifier, |
| 75 | + /// Optional file path to specify where the types or values must be declared. |
| 76 | + /// If omitted, all files will be matched. |
| 77 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 78 | + path: Option<String>, |
| 79 | +} |
| 80 | + |
| 81 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] |
| 82 | +#[serde(rename_all = "lowercase")] |
| 83 | +enum FileFrom { |
| 84 | + File, |
| 85 | +} |
| 86 | + |
| 87 | +/// Describes specific types or values declared in TypeScript's built-in lib.*.d.ts types. |
| 88 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] |
| 89 | +#[serde(rename_all = "camelCase")] |
| 90 | +pub struct LibSpecifier { |
| 91 | + /// Must be "lib" |
| 92 | + from: LibFrom, |
| 93 | + /// The name(s) of the lib type or value to match |
| 94 | + name: NameSpecifier, |
| 95 | +} |
| 96 | + |
| 97 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] |
| 98 | +#[serde(rename_all = "lowercase")] |
| 99 | +enum LibFrom { |
| 100 | + Lib, |
| 101 | +} |
| 102 | + |
| 103 | +/// Describes specific types or values imported from packages. |
| 104 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] |
| 105 | +#[serde(rename_all = "camelCase")] |
| 106 | +pub struct PackageSpecifier { |
| 107 | + /// Must be "package" |
| 108 | + from: PackageFrom, |
| 109 | + /// The name(s) of the type or value to match |
| 110 | + name: NameSpecifier, |
| 111 | + /// The package name to match |
| 112 | + package: String, |
| 113 | +} |
| 114 | + |
| 115 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] |
| 116 | +#[serde(rename_all = "lowercase")] |
| 117 | +enum PackageFrom { |
| 118 | + Package, |
| 119 | +} |
| 120 | + |
| 121 | +/// Name specifier that can be a single string or array of strings |
| 122 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] |
| 123 | +#[serde(untagged)] |
| 124 | +pub enum NameSpecifier { |
| 125 | + /// Single name |
| 126 | + Single(String), |
| 127 | + /// Multiple names |
| 128 | + Multiple(Vec<String>), |
| 129 | +} |
| 130 | + |
| 131 | +impl Default for NoFloatingPromisesConfig { |
| 132 | + fn default() -> Self { |
| 133 | + Self { |
| 134 | + allow_for_known_safe_calls: Vec::new(), |
| 135 | + allow_for_known_safe_promises: Vec::new(), |
| 136 | + check_thenables: false, |
| 137 | + ignore_iife: false, |
| 138 | + ignore_void: true, |
| 139 | + } |
| 140 | + } |
| 141 | +} |
7 | 142 |
|
8 | 143 | declare_oxc_lint!( |
9 | 144 | /// ### What it does |
@@ -74,6 +209,105 @@ declare_oxc_lint!( |
74 | 209 | typescript, |
75 | 210 | correctness, |
76 | 211 | pending, |
| 212 | + config = NoFloatingPromisesConfig, |
77 | 213 | ); |
78 | 214 |
|
79 | | -impl Rule for NoFloatingPromises {} |
| 215 | +impl Rule for NoFloatingPromises { |
| 216 | + fn from_configuration(value: serde_json::Value) -> Self { |
| 217 | + Self(Box::new( |
| 218 | + serde_json::from_value::<DefaultRuleConfig<NoFloatingPromisesConfig>>(value) |
| 219 | + .unwrap_or_default() |
| 220 | + .into_inner(), |
| 221 | + )) |
| 222 | + } |
| 223 | + |
| 224 | + fn to_configuration(&self) -> Option<Result<serde_json::Value, serde_json::Error>> { |
| 225 | + Some(serde_json::to_value(&*self.0)) |
| 226 | + } |
| 227 | +} |
| 228 | + |
| 229 | +#[cfg(test)] |
| 230 | +mod tests { |
| 231 | + use super::*; |
| 232 | + use serde_json::json; |
| 233 | + |
| 234 | + #[test] |
| 235 | + fn test_default_config() { |
| 236 | + let rule = NoFloatingPromises::default(); |
| 237 | + let config = rule.to_configuration().unwrap().unwrap(); |
| 238 | + |
| 239 | + // Verify the default values |
| 240 | + assert_eq!(config["allowForKnownSafeCalls"], json!([])); |
| 241 | + assert_eq!(config["allowForKnownSafePromises"], json!([])); |
| 242 | + assert_eq!(config["checkThenables"], json!(false)); |
| 243 | + assert_eq!(config["ignoreIIFE"], json!(false)); |
| 244 | + assert_eq!(config["ignoreVoid"], json!(true)); |
| 245 | + } |
| 246 | + |
| 247 | + #[test] |
| 248 | + fn test_from_configuration() { |
| 249 | + let config_value = json!([{ |
| 250 | + "allowForKnownSafeCalls": [{"from": "package", "name": "foo", "package": "some-package"}], |
| 251 | + "checkThenables": true, |
| 252 | + "ignoreVoid": false |
| 253 | + }]); |
| 254 | + |
| 255 | + let rule = NoFloatingPromises::from_configuration(config_value); |
| 256 | + |
| 257 | + assert!(rule.0.check_thenables); |
| 258 | + assert!(!rule.0.ignore_void); |
| 259 | + assert_eq!(rule.0.allow_for_known_safe_calls.len(), 1); |
| 260 | + } |
| 261 | + |
| 262 | + #[test] |
| 263 | + fn test_round_trip() { |
| 264 | + let original_config = json!([{ |
| 265 | + "allowForKnownSafeCalls": [{"from": "package", "name": "bar", "package": "test-pkg"}], |
| 266 | + "allowForKnownSafePromises": [{"from": "lib", "name": "Promise"}], |
| 267 | + "checkThenables": true, |
| 268 | + "ignoreIIFE": true, |
| 269 | + "ignoreVoid": false |
| 270 | + }]); |
| 271 | + |
| 272 | + let rule = NoFloatingPromises::from_configuration(original_config); |
| 273 | + let serialized = rule.to_configuration().unwrap().unwrap(); |
| 274 | + |
| 275 | + // Verify all fields are present in serialized output |
| 276 | + assert_eq!( |
| 277 | + serialized["allowForKnownSafeCalls"], |
| 278 | + json!([{"from": "package", "name": "bar", "package": "test-pkg"}]) |
| 279 | + ); |
| 280 | + assert_eq!( |
| 281 | + serialized["allowForKnownSafePromises"], |
| 282 | + json!([{"from": "lib", "name": "Promise"}]) |
| 283 | + ); |
| 284 | + assert_eq!(serialized["checkThenables"], json!(true)); |
| 285 | + assert_eq!(serialized["ignoreIIFE"], json!(true)); |
| 286 | + assert_eq!(serialized["ignoreVoid"], json!(false)); |
| 287 | + } |
| 288 | + |
| 289 | + #[test] |
| 290 | + fn test_all_specifier_types() { |
| 291 | + let config_value = json!([{ |
| 292 | + "allowForKnownSafeCalls": [ |
| 293 | + "SomeType", // string specifier |
| 294 | + {"from": "file", "name": "MyType", "path": "./types.ts"}, // file specifier with path |
| 295 | + {"from": "file", "name": ["Type1", "Type2"]}, // file specifier with multiple names |
| 296 | + {"from": "lib", "name": "Promise"}, // lib specifier |
| 297 | + {"from": "package", "name": "Observable", "package": "rxjs"} // package specifier |
| 298 | + ], |
| 299 | + "checkThenables": false, |
| 300 | + "ignoreVoid": true |
| 301 | + }]); |
| 302 | + |
| 303 | + let rule = NoFloatingPromises::from_configuration(config_value); |
| 304 | + |
| 305 | + assert_eq!(rule.0.allow_for_known_safe_calls.len(), 5); |
| 306 | + assert!(!rule.0.check_thenables); |
| 307 | + assert!(rule.0.ignore_void); |
| 308 | + |
| 309 | + // Verify serialization preserves all types |
| 310 | + let serialized = rule.to_configuration().unwrap().unwrap(); |
| 311 | + assert_eq!(serialized["allowForKnownSafeCalls"].as_array().unwrap().len(), 5); |
| 312 | + } |
| 313 | +} |
0 commit comments