perl-diagnostics 0.13.3

Unified diagnostic codes, types, and catalog for Perl LSP.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
//! Tests verifying completeness and correctness of the DiagnosticCode registry.
//!
//! These tests check that every diagnostic path in `perl-lsp-diagnostics`
//! has a corresponding stable code in `perl-diagnostics-codes`, and that
//! all code strings follow the expected PL/PC naming convention.

use perl_diagnostics::codes::DiagnosticCode;

// ---------------------------------------------------------------------------
// Scope diagnostic codes (PL104-PL110)
// ---------------------------------------------------------------------------

#[test]
fn scope_variable_shadowing_code_exists() -> Result<(), Box<dyn std::error::Error>> {
    let code = DiagnosticCode::VariableShadowing;
    assert_eq!(code.as_str(), "PL104", "VariableShadowing should have stable code PL104");
    assert_eq!(DiagnosticCode::parse_code("PL104"), Some(code), "parse_code round-trip failed");
    Ok(())
}

#[test]
fn scope_variable_redeclaration_code_exists() -> Result<(), Box<dyn std::error::Error>> {
    let code = DiagnosticCode::VariableRedeclaration;
    assert_eq!(code.as_str(), "PL105", "VariableRedeclaration should have stable code PL105");
    assert_eq!(DiagnosticCode::parse_code("PL105"), Some(code), "parse_code round-trip failed");
    Ok(())
}

#[test]
fn scope_duplicate_parameter_code_exists() -> Result<(), Box<dyn std::error::Error>> {
    let code = DiagnosticCode::DuplicateParameter;
    assert_eq!(code.as_str(), "PL106", "DuplicateParameter should have stable code PL106");
    assert_eq!(DiagnosticCode::parse_code("PL106"), Some(code), "parse_code round-trip failed");
    Ok(())
}

#[test]
fn scope_parameter_shadows_global_code_exists() -> Result<(), Box<dyn std::error::Error>> {
    let code = DiagnosticCode::ParameterShadowsGlobal;
    assert_eq!(code.as_str(), "PL107", "ParameterShadowsGlobal should have stable code PL107");
    assert_eq!(DiagnosticCode::parse_code("PL107"), Some(code), "parse_code round-trip failed");
    Ok(())
}

#[test]
fn scope_unused_parameter_code_exists() -> Result<(), Box<dyn std::error::Error>> {
    let code = DiagnosticCode::UnusedParameter;
    assert_eq!(code.as_str(), "PL108", "UnusedParameter should have stable code PL108");
    assert_eq!(DiagnosticCode::parse_code("PL108"), Some(code), "parse_code round-trip failed");
    Ok(())
}

#[test]
fn scope_unquoted_bareword_code_exists() -> Result<(), Box<dyn std::error::Error>> {
    let code = DiagnosticCode::UnquotedBareword;
    assert_eq!(code.as_str(), "PL109", "UnquotedBareword should have stable code PL109");
    assert_eq!(DiagnosticCode::parse_code("PL109"), Some(code), "parse_code round-trip failed");
    Ok(())
}

#[test]
fn scope_uninitialized_variable_code_exists() -> Result<(), Box<dyn std::error::Error>> {
    let code = DiagnosticCode::UninitializedVariable;
    assert_eq!(code.as_str(), "PL110", "UninitializedVariable should have stable code PL110");
    assert_eq!(DiagnosticCode::parse_code("PL110"), Some(code), "parse_code round-trip failed");
    Ok(())
}

// ---------------------------------------------------------------------------
// Pragma diagnostic codes (PL102, PL103 already exist; PL111 for misspelled)
// ---------------------------------------------------------------------------

#[test]
fn misspelled_pragma_code_exists() -> Result<(), Box<dyn std::error::Error>> {
    let code = DiagnosticCode::MisspelledPragma;
    assert_eq!(code.as_str(), "PL111", "MisspelledPragma should have stable code PL111");
    assert_eq!(DiagnosticCode::parse_code("PL111"), Some(code), "parse_code round-trip failed");
    Ok(())
}

// ---------------------------------------------------------------------------
// Lint diagnostic codes — common mistakes (PL4xx range)
// ---------------------------------------------------------------------------

#[test]
fn assignment_in_condition_code_exists() -> Result<(), Box<dyn std::error::Error>> {
    let code = DiagnosticCode::AssignmentInCondition;
    assert_eq!(code.as_str(), "PL403", "AssignmentInCondition should have stable code PL403");
    assert_eq!(DiagnosticCode::parse_code("PL403"), Some(code), "parse_code round-trip failed");
    Ok(())
}

#[test]
fn numeric_comparison_with_undef_code_exists() -> Result<(), Box<dyn std::error::Error>> {
    let code = DiagnosticCode::NumericComparisonWithUndef;
    assert_eq!(code.as_str(), "PL404", "NumericComparisonWithUndef should have stable code PL404");
    assert_eq!(DiagnosticCode::parse_code("PL404"), Some(code), "parse_code round-trip failed");
    Ok(())
}

#[test]
fn duplicate_hash_key_code_exists() -> Result<(), Box<dyn std::error::Error>> {
    let code = DiagnosticCode::DuplicateHashKey;
    assert_eq!(code.as_str(), "PL408", "DuplicateHashKey should have code PL408");
    assert_eq!(
        code.severity(),
        perl_diagnostics::codes::DiagnosticSeverity::Warning,
        "DuplicateHashKey should have Warning severity"
    );
    assert!(code.context_hint().is_some(), "DuplicateHashKey should have a context hint");
    assert_eq!(
        DiagnosticCode::parse_code("PL408"),
        Some(DiagnosticCode::DuplicateHashKey),
        "parse_code('PL408') should return DuplicateHashKey"
    );
    Ok(())
}

#[test]
fn goto_undefined_label_code_exists() -> Result<(), Box<dyn std::error::Error>> {
    let code = DiagnosticCode::GotoUndefinedLabel;
    assert_eq!(code.as_str(), "PL409", "GotoUndefinedLabel should have code PL409");
    assert_eq!(
        code.severity(),
        perl_diagnostics::codes::DiagnosticSeverity::Warning,
        "GotoUndefinedLabel should have Warning severity"
    );
    assert!(code.context_hint().is_some(), "GotoUndefinedLabel should have a context hint");
    assert_eq!(
        DiagnosticCode::parse_code("PL409"),
        Some(DiagnosticCode::GotoUndefinedLabel),
        "parse_code('PL409') should return GotoUndefinedLabel"
    );
    assert_eq!(
        code.category(),
        perl_diagnostics::codes::DiagnosticCategory::BestPractices,
        "GotoUndefinedLabel belongs to the BestPractices (PL4xx) range"
    );
    assert_eq!(
        code.documentation_url(),
        Some("https://docs.perl-lsp.org/errors/PL409"),
        "PL409 should advertise a docs URL following the conventional scheme"
    );
    Ok(())
}

#[test]
fn loop_control_undefined_label_code_exists() -> Result<(), Box<dyn std::error::Error>> {
    let code = DiagnosticCode::LoopControlUndefinedLabel;
    assert_eq!(code.as_str(), "PL410", "LoopControlUndefinedLabel should have code PL410");
    assert_eq!(
        code.severity(),
        perl_diagnostics::codes::DiagnosticSeverity::Warning,
        "LoopControlUndefinedLabel should have Warning severity"
    );
    assert!(code.context_hint().is_some(), "LoopControlUndefinedLabel should have a context hint");
    assert_eq!(
        DiagnosticCode::parse_code("PL410"),
        Some(DiagnosticCode::LoopControlUndefinedLabel),
        "parse_code('PL410') should return LoopControlUndefinedLabel"
    );
    assert_eq!(
        code.category(),
        perl_diagnostics::codes::DiagnosticCategory::BestPractices,
        "LoopControlUndefinedLabel belongs to the BestPractices (PL4xx) range"
    );
    assert_eq!(
        code.documentation_url(),
        Some("https://docs.perl-lsp.org/errors/PL410"),
        "PL410 should advertise a docs URL following the conventional scheme"
    );
    Ok(())
}

// ---------------------------------------------------------------------------
// Deprecated syntax codes (PL5xx range)
// ---------------------------------------------------------------------------

#[test]
fn deprecated_defined_code_exists() -> Result<(), Box<dyn std::error::Error>> {
    let code = DiagnosticCode::DeprecatedDefined;
    assert_eq!(code.as_str(), "PL500", "DeprecatedDefined should have stable code PL500");
    assert_eq!(DiagnosticCode::parse_code("PL500"), Some(code), "parse_code round-trip failed");
    Ok(())
}

#[test]
fn deprecated_array_base_code_exists() -> Result<(), Box<dyn std::error::Error>> {
    let code = DiagnosticCode::DeprecatedArrayBase;
    assert_eq!(code.as_str(), "PL501", "DeprecatedArrayBase should have stable code PL501");
    assert_eq!(DiagnosticCode::parse_code("PL501"), Some(code), "parse_code round-trip failed");
    Ok(())
}

#[test]
fn phase_scoped_strict_pragma_code_exists() -> Result<(), Box<dyn std::error::Error>> {
    let code = DiagnosticCode::PhaseScopedStrictPragma;
    assert_eq!(code.as_str(), "PL502", "PhaseScopedStrictPragma should have code PL502");
    assert_eq!(
        code.severity(),
        perl_diagnostics::codes::DiagnosticSeverity::Warning,
        "PhaseScopedStrictPragma should have Warning severity"
    );
    assert!(code.context_hint().is_some(), "PhaseScopedStrictPragma should have a context hint");
    assert_eq!(
        DiagnosticCode::parse_code("PL502"),
        Some(DiagnosticCode::PhaseScopedStrictPragma),
        "parse_code('PL502') should return PhaseScopedStrictPragma"
    );
    Ok(())
}

#[test]
fn phase_scoped_warnings_pragma_code_exists() -> Result<(), Box<dyn std::error::Error>> {
    let code = DiagnosticCode::PhaseScopedWarningsPragma;
    assert_eq!(code.as_str(), "PL503", "PhaseScopedWarningsPragma should have code PL503");
    assert_eq!(
        code.severity(),
        perl_diagnostics::codes::DiagnosticSeverity::Warning,
        "PhaseScopedWarningsPragma should have Warning severity"
    );
    assert!(code.context_hint().is_some(), "PhaseScopedWarningsPragma should have a context hint");
    assert_eq!(
        DiagnosticCode::parse_code("PL503"),
        Some(DiagnosticCode::PhaseScopedWarningsPragma),
        "parse_code('PL503') should return PhaseScopedWarningsPragma"
    );
    Ok(())
}

// ---------------------------------------------------------------------------
// Security diagnostic codes (PL6xx range)
// ---------------------------------------------------------------------------

#[test]
fn security_string_eval_code_exists() -> Result<(), Box<dyn std::error::Error>> {
    let code = DiagnosticCode::SecurityStringEval;
    assert_eq!(code.as_str(), "PL600", "SecurityStringEval should have stable code PL600");
    assert_eq!(DiagnosticCode::parse_code("PL600"), Some(code), "parse_code round-trip failed");
    Ok(())
}

#[test]
fn security_backtick_exec_code_exists() -> Result<(), Box<dyn std::error::Error>> {
    let code = DiagnosticCode::SecurityBacktickExec;
    assert_eq!(code.as_str(), "PL601", "SecurityBacktickExec should have stable code PL601");
    assert_eq!(DiagnosticCode::parse_code("PL601"), Some(code), "parse_code round-trip failed");
    Ok(())
}

#[test]
fn security_signal_handler_code_exists() -> Result<(), Box<dyn std::error::Error>> {
    let code = DiagnosticCode::SecuritySignalHandler;
    assert_eq!(code.as_str(), "PL602", "SecuritySignalHandler should have code PL602");
    assert_eq!(
        code.severity(),
        perl_diagnostics::codes::DiagnosticSeverity::Warning,
        "SecuritySignalHandler should have Warning severity"
    );
    assert!(code.context_hint().is_some(), "SecuritySignalHandler should have a context hint");
    assert_eq!(
        DiagnosticCode::parse_code("PL602"),
        Some(DiagnosticCode::SecuritySignalHandler),
        "parse_code('PL602') should return SecuritySignalHandler"
    );
    Ok(())
}

// ---------------------------------------------------------------------------
// Import diagnostic codes
// ---------------------------------------------------------------------------

#[test]
fn unused_import_code_exists() -> Result<(), Box<dyn std::error::Error>> {
    let code = DiagnosticCode::UnusedImport;
    assert_eq!(code.as_str(), "PL700", "UnusedImport should have stable code PL700");
    assert_eq!(DiagnosticCode::parse_code("PL700"), Some(code), "parse_code round-trip failed");
    Ok(())
}

#[test]
fn module_not_found_code_exists() -> Result<(), Box<dyn std::error::Error>> {
    let code = DiagnosticCode::ModuleNotFound;
    assert_eq!(code.as_str(), "PL701", "ModuleNotFound should have code PL701");
    assert_eq!(
        code.severity(),
        perl_diagnostics::codes::DiagnosticSeverity::Warning,
        "ModuleNotFound should have Warning severity"
    );
    assert!(code.context_hint().is_some(), "ModuleNotFound should have a context hint");
    assert_eq!(
        DiagnosticCode::parse_code("PL701"),
        Some(DiagnosticCode::ModuleNotFound),
        "parse_code('PL701') should return ModuleNotFound"
    );
    Ok(())
}

// ---------------------------------------------------------------------------
// Heredoc anti-pattern codes
// ---------------------------------------------------------------------------

#[test]
fn heredoc_in_format_code_exists() -> Result<(), Box<dyn std::error::Error>> {
    let code = DiagnosticCode::HeredocInFormat;
    assert_eq!(code.as_str(), "PL800", "HeredocInFormat should have stable code PL800");
    assert_eq!(DiagnosticCode::parse_code("PL800"), Some(code), "parse_code round-trip failed");
    Ok(())
}

#[test]
fn heredoc_in_begin_code_exists() -> Result<(), Box<dyn std::error::Error>> {
    let code = DiagnosticCode::HeredocInBegin;
    assert_eq!(code.as_str(), "PL801", "HeredocInBegin should have stable code PL801");
    assert_eq!(DiagnosticCode::parse_code("PL801"), Some(code), "parse_code round-trip failed");
    Ok(())
}

#[test]
fn heredoc_dynamic_delimiter_code_exists() -> Result<(), Box<dyn std::error::Error>> {
    let code = DiagnosticCode::HeredocDynamicDelimiter;
    assert_eq!(code.as_str(), "PL802", "HeredocDynamicDelimiter should have stable code PL802");
    assert_eq!(DiagnosticCode::parse_code("PL802"), Some(code), "parse_code round-trip failed");
    Ok(())
}

#[test]
fn heredoc_in_source_filter_code_exists() -> Result<(), Box<dyn std::error::Error>> {
    let code = DiagnosticCode::HeredocInSourceFilter;
    assert_eq!(code.as_str(), "PL803", "HeredocInSourceFilter should have stable code PL803");
    assert_eq!(DiagnosticCode::parse_code("PL803"), Some(code), "parse_code round-trip failed");
    Ok(())
}

#[test]
fn heredoc_in_regex_code_code_exists() -> Result<(), Box<dyn std::error::Error>> {
    let code = DiagnosticCode::HeredocInRegexCode;
    assert_eq!(code.as_str(), "PL804", "HeredocInRegexCode should have stable code PL804");
    assert_eq!(DiagnosticCode::parse_code("PL804"), Some(code), "parse_code round-trip failed");
    Ok(())
}

#[test]
fn heredoc_in_eval_code_exists() -> Result<(), Box<dyn std::error::Error>> {
    let code = DiagnosticCode::HeredocInEval;
    assert_eq!(code.as_str(), "PL805", "HeredocInEval should have stable code PL805");
    assert_eq!(DiagnosticCode::parse_code("PL805"), Some(code), "parse_code round-trip failed");
    Ok(())
}

#[test]
fn heredoc_tied_handle_code_exists() -> Result<(), Box<dyn std::error::Error>> {
    let code = DiagnosticCode::HeredocTiedHandle;
    assert_eq!(code.as_str(), "PL806", "HeredocTiedHandle should have stable code PL806");
    assert_eq!(DiagnosticCode::parse_code("PL806"), Some(code), "parse_code round-trip failed");
    Ok(())
}

// ---------------------------------------------------------------------------
// Code string format invariants
// ---------------------------------------------------------------------------

/// All codes must start with PL or PC prefix.
#[test]
fn all_codes_have_valid_prefix() -> Result<(), Box<dyn std::error::Error>> {
    // Existing codes that are already stable
    let existing_codes = [
        DiagnosticCode::ParseError,
        DiagnosticCode::SyntaxError,
        DiagnosticCode::UnexpectedEof,
        DiagnosticCode::MissingStrict,
        DiagnosticCode::MissingWarnings,
        DiagnosticCode::UnusedVariable,
        DiagnosticCode::UndefinedVariable,
        DiagnosticCode::MissingPackageDeclaration,
        DiagnosticCode::DuplicatePackage,
        DiagnosticCode::DuplicateSubroutine,
        DiagnosticCode::MissingReturn,
        DiagnosticCode::BarewordFilehandle,
        DiagnosticCode::TwoArgOpen,
        DiagnosticCode::ImplicitReturn,
        DiagnosticCode::CriticSeverity1,
        DiagnosticCode::CriticSeverity2,
        DiagnosticCode::CriticSeverity3,
        DiagnosticCode::CriticSeverity4,
        DiagnosticCode::CriticSeverity5,
    ];

    for code in &existing_codes {
        let s = code.as_str();
        assert!(
            s.starts_with("PL") || s.starts_with("PC"),
            "Code {s} does not start with PL or PC"
        );
    }
    Ok(())
}

/// parse_code round-trip: as_str() output must be parseable back.
#[test]
fn existing_codes_round_trip_through_parse_code() -> Result<(), Box<dyn std::error::Error>> {
    let codes = [
        DiagnosticCode::ParseError,
        DiagnosticCode::MissingStrict,
        DiagnosticCode::TwoArgOpen,
        DiagnosticCode::CriticSeverity3,
    ];
    for code in codes {
        let s = code.as_str();
        let parsed = DiagnosticCode::parse_code(s);
        assert_eq!(parsed, Some(code), "parse_code({s}) should return {code:?}");
    }
    Ok(())
}