merman-analysis 0.8.0-alpha.3

Diagnostics-first analysis contracts and source mapping for Merman.
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
use crate::{
    AnalysisDiagnostic, AnalysisStatus, DiagnosticCategory, DiagnosticFix, DiagnosticFixEdit,
    DiagnosticSeverity, DiagnosticSpan, SourceMap,
    source_directives::{
        directive_keyword_spans, frontmatter_config_key_spans, init_directive_config_key_spans,
    },
};
use merman_core::{
    BLOCK_WIDTH_WARNING_RULE_ID, DiagramWarningFact, FLOWCHART_EXPLICIT_DIRECTION_WARNING_RULE_ID,
    FLOWCHART_UNKNOWN_STYLE_TARGET_WARNING_RULE_ID, GIT_GRAPH_DUPLICATE_COMMIT_WARNING_RULE_ID,
};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::{BTreeMap, BTreeSet};

pub const PREFER_INIT_DIRECTIVE_RULE_ID: &str = "merman.authoring.config.prefer_init_directive";
pub const PREFER_FRONTMATTER_CONFIG_RULE_ID: &str =
    "merman.authoring.config.prefer_frontmatter_config";
pub const DEPRECATED_FLOWCHART_HTML_LABELS_RULE_ID: &str =
    "merman.compatibility.config.deprecated_flowchart_html_labels";
pub const DEPRECATED_EXTERNAL_DIAGRAM_LOADING_RULE_ID: &str =
    "merman.compatibility.config.deprecated_external_diagram_loading";
pub const NO_DIAGRAM_RULE_ID: &str = "merman.parse.no_diagram";
pub const DIAGRAM_PARSE_RULE_ID: &str = "merman.parse.diagram_parse";
pub const UNSUPPORTED_DIAGRAM_RULE_ID: &str = "merman.compatibility.unsupported_diagram";
pub const RECOVERED_EDITOR_FACTS_RULE_ID: &str = "merman.parse.recovered_editor_facts";
pub const RESOURCE_LIMIT_RULE_ID: &str = "merman.resource.source_bytes_exceeded";
pub const MALFORMED_FRONT_MATTER_RULE_ID: &str = "merman.config.malformed_front_matter";
pub const INVALID_DIRECTIVE_JSON_RULE_ID: &str = "merman.config.invalid_directive_json";
pub const INVALID_FRONT_MATTER_YAML_RULE_ID: &str = "merman.config.invalid_front_matter_yaml";
pub const PANIC_RULE_ID: &str = "merman.internal.panic";
pub const INTERNAL_RULE_REGISTRY_GAP_RULE_ID: &str = "merman.internal.rule_registry_gap";
pub const FLOWCHART_FACTS_PROJECTION_RULE_ID: &str = "merman.internal.flowchart_facts_projection";
pub const BLOCK_WIDTH_RULE_ID: &str = "merman.block.width_exceeds_columns";
pub const FLOWCHART_EXPLICIT_DIRECTION_RULE_ID: &str =
    "merman.authoring.flowchart.explicit_direction";
pub const FLOWCHART_UNKNOWN_STYLE_TARGET_RULE_ID: &str =
    "merman.semantic.flowchart.unknown_style_target";
pub const GIT_GRAPH_DUPLICATE_COMMIT_RULE_ID: &str = "merman.git_graph.duplicate_commit_id";
pub const RULE_CATALOG_RESPONSE_VERSION: u32 = 1;

const DEPRECATED_FLOWCHART_HTML_LABELS_INIT_CONFIG_PATHS: [&[&str]; 1] =
    [&["flowchart", "htmlLabels"]];
const DEPRECATED_FLOWCHART_HTML_LABELS_FLOWCHART_INIT_WRAPPER_PATHS: [&[&str]; 2] = [
    &["config", "htmlLabels"],
    &["config", "flowchart", "htmlLabels"],
];
const DEPRECATED_FLOWCHART_HTML_LABELS_FRONTMATTER_CONFIG_PATHS: [&[&str]; 2] = [
    &["flowchart", "htmlLabels"],
    &["config", "flowchart", "htmlLabels"],
];
const DEPRECATED_EXTERNAL_DIAGRAM_LOADING_CONFIG_PATHS: [&[&str]; 2] =
    [&["lazyLoadedDiagrams"], &["loadExternalDiagramsAtStartup"]];
const DEPRECATED_EXTERNAL_DIAGRAM_LOADING_FRONTMATTER_CONFIG_PATHS: [&[&str]; 2] = [
    &["config", "lazyLoadedDiagrams"],
    &["config", "loadExternalDiagramsAtStartup"],
];

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AnalysisRuleProfile {
    #[default]
    Core,
    Recommended,
    Strict,
}

impl AnalysisRuleProfile {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Core => "core",
            Self::Recommended => "recommended",
            Self::Strict => "strict",
        }
    }

    const fn includes(self, minimum: Self) -> bool {
        self as u8 >= minimum as u8
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RuleOrigin {
    MermaidSyntax,
    MermaidCompatibility,
    MermanAuthoring,
    MermanResourcePolicy,
    MermanInternal,
}

impl RuleOrigin {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::MermaidSyntax => "mermaid_syntax",
            Self::MermaidCompatibility => "mermaid_compatibility",
            Self::MermanAuthoring => "merman_authoring",
            Self::MermanResourcePolicy => "merman_resource_policy",
            Self::MermanInternal => "merman_internal",
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RuleDescriptor {
    pub id: &'static str,
    pub description: &'static str,
    pub evidence: &'static [&'static str],
    pub default_severity: DiagnosticSeverity,
    pub category: DiagnosticCategory,
    pub default_enabled: bool,
    pub default_profile: AnalysisRuleProfile,
    pub origin: RuleOrigin,
    pub fixable: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub struct RuleCatalogEntry {
    pub id: &'static str,
    pub description: &'static str,
    pub evidence: &'static [&'static str],
    pub default_severity: DiagnosticSeverity,
    pub category: DiagnosticCategory,
    pub default_enabled: bool,
    pub default_profile: AnalysisRuleProfile,
    pub origin: RuleOrigin,
    pub configurable: bool,
    pub fixable: bool,
}

impl RuleCatalogEntry {
    fn from_descriptor(descriptor: RuleDescriptor) -> Self {
        Self {
            id: descriptor.id,
            description: descriptor.description,
            evidence: descriptor.evidence,
            default_severity: descriptor.default_severity,
            category: descriptor.category,
            default_enabled: descriptor.default_enabled,
            default_profile: descriptor.default_profile,
            origin: descriptor.origin,
            configurable: is_configurable_rule_descriptor(descriptor),
            fixable: descriptor.fixable,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct RuleCatalogResponse {
    pub version: u32,
    pub rules: Vec<RuleCatalogEntry>,
}

impl RuleCatalogResponse {
    pub fn from_rules(rules: Vec<RuleCatalogEntry>) -> Self {
        Self {
            version: RULE_CATALOG_RESPONSE_VERSION,
            rules,
        }
    }

    pub fn current() -> Self {
        Self::from_rules(rule_catalog())
    }

    pub fn configurable() -> Self {
        Self::from_rules(configurable_rule_catalog())
    }
}

const PREFER_INIT_DIRECTIVE_RULE: RuleDescriptor = RuleDescriptor {
    id: PREFER_INIT_DIRECTIVE_RULE_ID,
    description: "Prefer the canonical `init` directive keyword over the accepted `initialize` alias.",
    evidence: &[
        "https://github.com/mermaid-js/mermaid/blob/41646dfd43ac83f001b03c70605feb036afae46d/packages/mermaid/src/utils.ts",
        "docs/adr/0072-lint-rule-governance.md",
    ],
    default_severity: DiagnosticSeverity::Hint,
    category: DiagnosticCategory::Config,
    default_enabled: false,
    default_profile: AnalysisRuleProfile::Recommended,
    origin: RuleOrigin::MermanAuthoring,
    fixable: true,
};

const PREFER_FRONTMATTER_CONFIG_RULE: RuleDescriptor = RuleDescriptor {
    id: PREFER_FRONTMATTER_CONFIG_RULE_ID,
    description: "Prefer diagram frontmatter `config` over Mermaid init directives.",
    evidence: &[
        "https://github.com/mermaid-js/mermaid/blob/41646dfd43ac83f001b03c70605feb036afae46d/packages/mermaid/src/docs/config/directives.md",
        "https://github.com/mermaid-js/mermaid/blob/41646dfd43ac83f001b03c70605feb036afae46d/packages/mermaid/src/docs/config/configuration.md",
    ],
    default_severity: DiagnosticSeverity::Hint,
    category: DiagnosticCategory::Config,
    default_enabled: false,
    default_profile: AnalysisRuleProfile::Recommended,
    origin: RuleOrigin::MermanAuthoring,
    fixable: true,
};

const DEPRECATED_FLOWCHART_HTML_LABELS_RULE: RuleDescriptor = RuleDescriptor {
    id: DEPRECATED_FLOWCHART_HTML_LABELS_RULE_ID,
    description: "Report deprecated `flowchart.htmlLabels` config and recommend the root-level `htmlLabels` option.",
    evidence: &[
        "https://github.com/mermaid-js/mermaid/blob/41646dfd43ac83f001b03c70605feb036afae46d/packages/mermaid/src/config.ts",
        "https://github.com/mermaid-js/mermaid/blob/41646dfd43ac83f001b03c70605feb036afae46d/packages/mermaid/src/config.type.ts",
        "https://github.com/mermaid-js/mermaid/blob/41646dfd43ac83f001b03c70605feb036afae46d/packages/mermaid/src/docs/config/directives.md",
    ],
    default_severity: DiagnosticSeverity::Warning,
    category: DiagnosticCategory::Config,
    default_enabled: true,
    default_profile: AnalysisRuleProfile::Core,
    origin: RuleOrigin::MermaidCompatibility,
    fixable: false,
};

const DEPRECATED_EXTERNAL_DIAGRAM_LOADING_RULE: RuleDescriptor = RuleDescriptor {
    id: DEPRECATED_EXTERNAL_DIAGRAM_LOADING_RULE_ID,
    description: "Report deprecated external diagram loading config and recommend `registerExternalDiagrams`.",
    evidence: &[
        "https://github.com/mermaid-js/mermaid/blob/41646dfd43ac83f001b03c70605feb036afae46d/packages/mermaid/src/config.ts",
        "https://github.com/mermaid-js/mermaid/blob/41646dfd43ac83f001b03c70605feb036afae46d/packages/mermaid/src/mermaid.ts",
        "https://github.com/mermaid-js/mermaid/blob/41646dfd43ac83f001b03c70605feb036afae46d/packages/mermaid/src/mermaid.spec.ts",
    ],
    default_severity: DiagnosticSeverity::Warning,
    category: DiagnosticCategory::Config,
    default_enabled: true,
    default_profile: AnalysisRuleProfile::Core,
    origin: RuleOrigin::MermaidCompatibility,
    fixable: false,
};

const NO_DIAGRAM_RULE: RuleDescriptor = RuleDescriptor {
    id: NO_DIAGRAM_RULE_ID,
    description: "Report input that does not contain a Mermaid diagram.",
    evidence: &[
        "https://github.com/mermaid-js/mermaid/blob/41646dfd43ac83f001b03c70605feb036afae46d/packages/mermaid/src/diagram-api/detectType.ts",
        "https://github.com/mermaid-js/mermaid/blob/41646dfd43ac83f001b03c70605feb036afae46d/packages/mermaid/src/mermaid.spec.ts",
    ],
    default_severity: DiagnosticSeverity::Error,
    category: DiagnosticCategory::Parse,
    default_enabled: true,
    default_profile: AnalysisRuleProfile::Core,
    origin: RuleOrigin::MermaidSyntax,
    fixable: false,
};

const DIAGRAM_PARSE_RULE: RuleDescriptor = RuleDescriptor {
    id: DIAGRAM_PARSE_RULE_ID,
    description: "Report Mermaid diagram syntax that the parser cannot accept.",
    evidence: &[
        "https://github.com/mermaid-js/mermaid/blob/41646dfd43ac83f001b03c70605feb036afae46d/packages/mermaid/src/mermaid.ts",
        "docs/adr/0070-diagnostics-first-analysis-contract.md",
    ],
    default_severity: DiagnosticSeverity::Error,
    category: DiagnosticCategory::Parse,
    default_enabled: true,
    default_profile: AnalysisRuleProfile::Core,
    origin: RuleOrigin::MermaidSyntax,
    fixable: false,
};

const UNSUPPORTED_DIAGRAM_RULE: RuleDescriptor = RuleDescriptor {
    id: UNSUPPORTED_DIAGRAM_RULE_ID,
    description: "Report Mermaid diagram types that are recognized but unavailable in this build.",
    evidence: &[
        "https://github.com/mermaid-js/mermaid/blob/41646dfd43ac83f001b03c70605feb036afae46d/packages/mermaid/src/diagram-api/detectType.ts",
        "docs/release/PACKAGE_SURFACES.md",
    ],
    default_severity: DiagnosticSeverity::Error,
    category: DiagnosticCategory::Compatibility,
    default_enabled: true,
    default_profile: AnalysisRuleProfile::Core,
    origin: RuleOrigin::MermaidCompatibility,
    fixable: false,
};

const RECOVERED_EDITOR_FACTS_RULE: RuleDescriptor = RuleDescriptor {
    id: RECOVERED_EDITOR_FACTS_RULE_ID,
    description: "Report parser recovery diagnostics emitted while producing editor semantic facts.",
    evidence: &[
        "https://github.com/mermaid-js/mermaid/blob/41646dfd43ac83f001b03c70605feb036afae46d/packages/mermaid/src/mermaid.ts",
        "docs/adr/0070-diagnostics-first-analysis-contract.md",
    ],
    default_severity: DiagnosticSeverity::Warning,
    category: DiagnosticCategory::Parse,
    default_enabled: true,
    default_profile: AnalysisRuleProfile::Core,
    origin: RuleOrigin::MermaidSyntax,
    fixable: false,
};

const RESOURCE_LIMIT_RULE: RuleDescriptor = RuleDescriptor {
    id: RESOURCE_LIMIT_RULE_ID,
    description: "Report Mermaid sources that exceed the configured analysis source byte budget.",
    evidence: &[
        "docs/adr/0070-diagnostics-first-analysis-contract.md",
        "docs/bindings/OPTIONS_JSON.md",
    ],
    default_severity: DiagnosticSeverity::Error,
    category: DiagnosticCategory::Resource,
    default_enabled: true,
    default_profile: AnalysisRuleProfile::Core,
    origin: RuleOrigin::MermanResourcePolicy,
    fixable: false,
};

const MALFORMED_FRONT_MATTER_RULE: RuleDescriptor = RuleDescriptor {
    id: MALFORMED_FRONT_MATTER_RULE_ID,
    description: "Report malformed YAML front matter blocks before diagram parsing.",
    evidence: &[
        "https://github.com/mermaid-js/mermaid/blob/41646dfd43ac83f001b03c70605feb036afae46d/packages/mermaid/src/diagram-api/frontmatter.ts",
        "https://github.com/mermaid-js/mermaid/blob/41646dfd43ac83f001b03c70605feb036afae46d/packages/mermaid/src/diagram-api/frontmatter.spec.ts",
    ],
    default_severity: DiagnosticSeverity::Error,
    category: DiagnosticCategory::Config,
    default_enabled: true,
    default_profile: AnalysisRuleProfile::Core,
    origin: RuleOrigin::MermaidSyntax,
    fixable: false,
};

const INVALID_DIRECTIVE_JSON_RULE: RuleDescriptor = RuleDescriptor {
    id: INVALID_DIRECTIVE_JSON_RULE_ID,
    description: "Report Mermaid directive blocks whose JSON payload cannot be parsed.",
    evidence: &[
        "https://github.com/mermaid-js/mermaid/blob/41646dfd43ac83f001b03c70605feb036afae46d/packages/mermaid/src/diagram-api/regexes.ts",
        "https://github.com/mermaid-js/mermaid/blob/41646dfd43ac83f001b03c70605feb036afae46d/packages/mermaid/src/utils.ts",
    ],
    default_severity: DiagnosticSeverity::Error,
    category: DiagnosticCategory::Config,
    default_enabled: true,
    default_profile: AnalysisRuleProfile::Core,
    origin: RuleOrigin::MermaidSyntax,
    fixable: false,
};

const INVALID_FRONT_MATTER_YAML_RULE: RuleDescriptor = RuleDescriptor {
    id: INVALID_FRONT_MATTER_YAML_RULE_ID,
    description: "Report Mermaid front matter whose YAML payload cannot be parsed.",
    evidence: &[
        "https://github.com/mermaid-js/mermaid/blob/41646dfd43ac83f001b03c70605feb036afae46d/packages/mermaid/src/diagram-api/frontmatter.ts",
        "https://github.com/mermaid-js/mermaid/blob/41646dfd43ac83f001b03c70605feb036afae46d/packages/mermaid/src/diagram-api/frontmatter.spec.ts",
    ],
    default_severity: DiagnosticSeverity::Error,
    category: DiagnosticCategory::Config,
    default_enabled: true,
    default_profile: AnalysisRuleProfile::Core,
    origin: RuleOrigin::MermaidSyntax,
    fixable: false,
};

const PANIC_RULE: RuleDescriptor = RuleDescriptor {
    id: PANIC_RULE_ID,
    description: "Report an internal panic caught while analyzing Mermaid source.",
    evidence: &["docs/adr/0070-diagnostics-first-analysis-contract.md"],
    default_severity: DiagnosticSeverity::Error,
    category: DiagnosticCategory::Internal,
    default_enabled: true,
    default_profile: AnalysisRuleProfile::Core,
    origin: RuleOrigin::MermanInternal,
    fixable: false,
};

const INTERNAL_RULE_REGISTRY_GAP_RULE: RuleDescriptor = RuleDescriptor {
    id: INTERNAL_RULE_REGISTRY_GAP_RULE_ID,
    description: "Report an internal rule registry gap while projecting diagnostics.",
    evidence: &["docs/adr/0072-lint-rule-governance.md"],
    default_severity: DiagnosticSeverity::Error,
    category: DiagnosticCategory::Internal,
    default_enabled: true,
    default_profile: AnalysisRuleProfile::Core,
    origin: RuleOrigin::MermanInternal,
    fixable: false,
};

const FLOWCHART_FACTS_PROJECTION_RULE: RuleDescriptor = RuleDescriptor {
    id: FLOWCHART_FACTS_PROJECTION_RULE_ID,
    description: "Report an internal failure while projecting flowchart parser model facts.",
    evidence: &["docs/adr/0070-diagnostics-first-analysis-contract.md"],
    default_severity: DiagnosticSeverity::Error,
    category: DiagnosticCategory::Internal,
    default_enabled: true,
    default_profile: AnalysisRuleProfile::Core,
    origin: RuleOrigin::MermanInternal,
    fixable: false,
};

const BLOCK_WIDTH_RULE: RuleDescriptor = RuleDescriptor {
    id: BLOCK_WIDTH_RULE_ID,
    description: "Report block diagram entries that exceed the configured column width.",
    evidence: &[
        "https://github.com/mermaid-js/mermaid/blob/41646dfd43ac83f001b03c70605feb036afae46d/packages/mermaid/src/docs/syntax/block.md",
    ],
    default_severity: DiagnosticSeverity::Warning,
    category: DiagnosticCategory::Semantic,
    default_enabled: true,
    default_profile: AnalysisRuleProfile::Core,
    origin: RuleOrigin::MermaidCompatibility,
    fixable: false,
};
const FLOWCHART_EXPLICIT_DIRECTION_RULE: RuleDescriptor = RuleDescriptor {
    id: FLOWCHART_EXPLICIT_DIRECTION_RULE_ID,
    description: "Recommend explicit flowchart header directions and offer an insertion quickfix.",
    evidence: &[
        "https://github.com/mermaid-js/mermaid/blob/41646dfd43ac83f001b03c70605feb036afae46d/packages/mermaid/src/docs/syntax/flowchart.md",
        "docs/adr/0072-lint-rule-governance.md",
    ],
    default_severity: DiagnosticSeverity::Hint,
    category: DiagnosticCategory::Semantic,
    default_enabled: false,
    default_profile: AnalysisRuleProfile::Recommended,
    origin: RuleOrigin::MermanAuthoring,
    fixable: true,
};
const FLOWCHART_UNKNOWN_STYLE_TARGET_RULE: RuleDescriptor = RuleDescriptor {
    id: FLOWCHART_UNKNOWN_STYLE_TARGET_RULE_ID,
    description: "Report flowchart `style` directives that would auto-create an unknown node target.",
    evidence: &[
        "https://github.com/mermaid-js/mermaid/blob/41646dfd43ac83f001b03c70605feb036afae46d/packages/mermaid/src/diagrams/flowchart/flowDb.ts",
    ],
    default_severity: DiagnosticSeverity::Warning,
    category: DiagnosticCategory::Semantic,
    default_enabled: true,
    default_profile: AnalysisRuleProfile::Core,
    origin: RuleOrigin::MermaidCompatibility,
    fixable: false,
};
const GIT_GRAPH_DUPLICATE_COMMIT_RULE: RuleDescriptor = RuleDescriptor {
    id: GIT_GRAPH_DUPLICATE_COMMIT_RULE_ID,
    description: "Report duplicate gitGraph commit ids.",
    evidence: &[
        "https://github.com/mermaid-js/mermaid/blob/41646dfd43ac83f001b03c70605feb036afae46d/packages/mermaid/src/diagrams/git/gitGraphAst.ts",
    ],
    default_severity: DiagnosticSeverity::Warning,
    category: DiagnosticCategory::Semantic,
    default_enabled: true,
    default_profile: AnalysisRuleProfile::Core,
    origin: RuleOrigin::MermaidCompatibility,
    fixable: false,
};
const RULE_DESCRIPTORS: &[RuleDescriptor] = &[
    PREFER_INIT_DIRECTIVE_RULE,
    PREFER_FRONTMATTER_CONFIG_RULE,
    DEPRECATED_FLOWCHART_HTML_LABELS_RULE,
    DEPRECATED_EXTERNAL_DIAGRAM_LOADING_RULE,
    NO_DIAGRAM_RULE,
    DIAGRAM_PARSE_RULE,
    UNSUPPORTED_DIAGRAM_RULE,
    RECOVERED_EDITOR_FACTS_RULE,
    RESOURCE_LIMIT_RULE,
    MALFORMED_FRONT_MATTER_RULE,
    INVALID_DIRECTIVE_JSON_RULE,
    INVALID_FRONT_MATTER_YAML_RULE,
    PANIC_RULE,
    INTERNAL_RULE_REGISTRY_GAP_RULE,
    FLOWCHART_FACTS_PROJECTION_RULE,
    BLOCK_WIDTH_RULE,
    FLOWCHART_EXPLICIT_DIRECTION_RULE,
    FLOWCHART_UNKNOWN_STYLE_TARGET_RULE,
    GIT_GRAPH_DUPLICATE_COMMIT_RULE,
];

pub fn rule_descriptors() -> &'static [RuleDescriptor] {
    RULE_DESCRIPTORS
}

pub fn rule_catalog() -> Vec<RuleCatalogEntry> {
    RULE_DESCRIPTORS
        .iter()
        .copied()
        .map(RuleCatalogEntry::from_descriptor)
        .collect()
}

pub fn configurable_rule_catalog() -> Vec<RuleCatalogEntry> {
    configurable_rule_descriptors()
        .map(RuleCatalogEntry::from_descriptor)
        .collect()
}

pub fn rule_catalog_response() -> RuleCatalogResponse {
    RuleCatalogResponse::current()
}

pub fn configurable_rule_catalog_response() -> RuleCatalogResponse {
    RuleCatalogResponse::configurable()
}

pub fn rule_catalog_response_json_bytes() -> Result<Vec<u8>, serde_json::Error> {
    serde_json::to_vec(&rule_catalog_response())
}

pub fn configurable_rule_catalog_response_json_bytes() -> Result<Vec<u8>, serde_json::Error> {
    serde_json::to_vec(&configurable_rule_catalog_response())
}

pub fn configurable_rule_descriptors() -> impl Iterator<Item = RuleDescriptor> {
    RULE_DESCRIPTORS
        .iter()
        .copied()
        .filter(|descriptor| is_configurable_rule_descriptor(*descriptor))
}

pub fn configurable_rule_descriptor(rule_id: &str) -> Option<RuleDescriptor> {
    configurable_rule_descriptors().find(|descriptor| descriptor.id == rule_id)
}

pub fn rule_descriptor(rule_id: &str) -> Option<RuleDescriptor> {
    RULE_DESCRIPTORS
        .iter()
        .copied()
        .find(|descriptor| descriptor.id == rule_id)
}

fn is_configurable_rule_descriptor(descriptor: RuleDescriptor) -> bool {
    !matches!(
        descriptor.category,
        DiagnosticCategory::Internal | DiagnosticCategory::Resource
    )
}

#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct AnalysisRuleConfig {
    #[serde(default)]
    profile: AnalysisRuleProfile,
    #[serde(default)]
    enabled_rules: BTreeSet<String>,
    #[serde(default)]
    disabled_rules: BTreeSet<String>,
    #[serde(default)]
    severity_overrides: BTreeMap<String, DiagnosticSeverity>,
}

impl AnalysisRuleConfig {
    pub fn with_profile(mut self, profile: AnalysisRuleProfile) -> Self {
        self.profile = profile;
        self
    }

    pub fn profile(&self) -> AnalysisRuleProfile {
        self.profile
    }

    pub fn with_rule_enabled(mut self, rule_id: impl Into<String>) -> Self {
        self.enable_rule(rule_id);
        self
    }

    pub fn with_rule_disabled(mut self, rule_id: impl Into<String>) -> Self {
        self.disable_rule(rule_id);
        self
    }

    pub fn with_rule_severity(
        mut self,
        rule_id: impl Into<String>,
        severity: DiagnosticSeverity,
    ) -> Self {
        self.set_rule_severity(rule_id, severity);
        self
    }

    pub fn set_profile(&mut self, profile: AnalysisRuleProfile) {
        self.profile = profile;
    }

    pub fn enable_rule(&mut self, rule_id: impl Into<String>) {
        self.enabled_rules.insert(rule_id.into());
    }

    pub fn disable_rule(&mut self, rule_id: impl Into<String>) {
        self.disabled_rules.insert(rule_id.into());
    }

    pub fn set_rule_severity(&mut self, rule_id: impl Into<String>, severity: DiagnosticSeverity) {
        self.severity_overrides.insert(rule_id.into(), severity);
    }

    pub fn is_rule_enabled(&self, descriptor: RuleDescriptor) -> bool {
        if self.disabled_rules.contains(descriptor.id) {
            return false;
        }
        if self.enabled_rules.contains(descriptor.id) {
            return true;
        }
        descriptor.default_enabled || self.profile.includes(descriptor.default_profile)
    }

    pub fn severity_for(&self, descriptor: RuleDescriptor) -> DiagnosticSeverity {
        self.severity_overrides
            .get(descriptor.id)
            .copied()
            .unwrap_or(descriptor.default_severity)
    }
}

pub(crate) fn source_lint_diagnostics(
    source: &str,
    source_map: &SourceMap,
    rule_config: &AnalysisRuleConfig,
) -> Vec<AnalysisDiagnostic> {
    let mut diagnostics = init_directive_alias_diagnostics(source, source_map, rule_config);
    diagnostics.extend(prefer_frontmatter_config_diagnostics(
        source,
        source_map,
        rule_config,
    ));
    diagnostics.extend(deprecated_flowchart_html_labels_diagnostics(
        source,
        source_map,
        rule_config,
        &DEPRECATED_FLOWCHART_HTML_LABELS_INIT_CONFIG_PATHS,
        &DEPRECATED_FLOWCHART_HTML_LABELS_FRONTMATTER_CONFIG_PATHS,
    ));
    diagnostics.extend(deprecated_external_diagram_loading_diagnostics(
        source,
        source_map,
        rule_config,
    ));
    diagnostics
}

pub(crate) fn parsed_source_lint_diagnostics(
    source: &str,
    source_map: &SourceMap,
    rule_config: &AnalysisRuleConfig,
    diagram_type: &str,
) -> Vec<AnalysisDiagnostic> {
    if merman_core::diagram_type_family_kind(diagram_type) != Some("flowchart") {
        return Vec::new();
    }

    deprecated_flowchart_html_labels_diagnostics(
        source,
        source_map,
        rule_config,
        &DEPRECATED_FLOWCHART_HTML_LABELS_FLOWCHART_INIT_WRAPPER_PATHS,
        &[],
    )
}

pub(crate) fn semantic_warning_diagnostics(
    diagram_type: &str,
    model: &Value,
    source_map: &SourceMap,
    rule_config: &AnalysisRuleConfig,
) -> Vec<AnalysisDiagnostic> {
    let span = source_map.whole_source_span().ok();
    let Some(warning_facts) = model
        .get("warningFacts")
        .and_then(|value| serde_json::from_value::<Vec<DiagramWarningFact>>(value.clone()).ok())
    else {
        return Vec::new();
    };

    semantic_warning_fact_diagnostics(diagram_type, warning_facts, span, source_map, rule_config)
}

fn semantic_warning_fact_diagnostics(
    diagram_type: &str,
    warning_facts: Vec<DiagramWarningFact>,
    fallback_span: Option<DiagnosticSpan>,
    source_map: &SourceMap,
    rule_config: &AnalysisRuleConfig,
) -> Vec<AnalysisDiagnostic> {
    let mut diagnostics = Vec::with_capacity(warning_facts.len());

    for fact in warning_facts {
        match warning_fact_rule_descriptor(&fact.rule_id) {
            Some(descriptor) if rule_config.is_rule_enabled(descriptor) => {
                diagnostics.push(warning_for_fact(
                    diagram_type,
                    fact,
                    fallback_span.clone(),
                    source_map,
                    descriptor,
                    rule_config,
                ))
            }
            Some(_) => {}
            None => diagnostics.push(
                internal_rule_registry_gap_diagnostic(
                    format!(
                        "unknown warning fact rule id `{}`: {}",
                        fact.rule_id, fact.message
                    ),
                    fallback_span.clone(),
                )
                .with_diagram_type(diagram_type),
            ),
        }
    }

    diagnostics
}

fn warning_for_fact(
    diagram_type: &str,
    fact: DiagramWarningFact,
    fallback_span: Option<DiagnosticSpan>,
    source_map: &SourceMap,
    descriptor: RuleDescriptor,
    rule_config: &AnalysisRuleConfig,
) -> AnalysisDiagnostic {
    let span = warning_fact_span(&fact, source_map, fallback_span);
    let fix = warning_fact_fix(&fact, descriptor, source_map);
    let mut diagnostic = AnalysisDiagnostic::new(
        descriptor.id,
        rule_config.severity_for(descriptor),
        descriptor.category,
        fact.message,
    )
    .with_diagram_type(diagram_type);

    if let Some(span) = span {
        diagnostic = diagnostic.with_span(span);
    }

    if let Some(fix) = fix {
        diagnostic = diagnostic.with_fix(fix);
    }

    diagnostic
}

fn warning_fact_span(
    fact: &DiagramWarningFact,
    source_map: &SourceMap,
    fallback_span: Option<DiagnosticSpan>,
) -> Option<DiagnosticSpan> {
    fact.span
        .and_then(|span| source_map.span(span.start, span.end).ok())
        .or(fallback_span)
}

fn warning_fact_fix(
    fact: &DiagramWarningFact,
    descriptor: RuleDescriptor,
    source_map: &SourceMap,
) -> Option<DiagnosticFix> {
    let fix_span = fact.fix_span.or(fact.span)?;
    let fix_span = source_map.span(fix_span.start, fix_span.end).ok()?;
    match descriptor.id {
        FLOWCHART_EXPLICIT_DIRECTION_RULE_ID => Some(
            DiagnosticFix::new(
                "Insert `TB` into the flowchart header",
                vec![DiagnosticFixEdit::new(fix_span, " TB")],
            )
            .preferred(),
        ),
        _ => None,
    }
}

fn warning_fact_rule_descriptor(rule_id: &str) -> Option<RuleDescriptor> {
    match rule_id {
        BLOCK_WIDTH_WARNING_RULE_ID => Some(BLOCK_WIDTH_RULE),
        FLOWCHART_EXPLICIT_DIRECTION_WARNING_RULE_ID => Some(FLOWCHART_EXPLICIT_DIRECTION_RULE),
        FLOWCHART_UNKNOWN_STYLE_TARGET_WARNING_RULE_ID => Some(FLOWCHART_UNKNOWN_STYLE_TARGET_RULE),
        GIT_GRAPH_DUPLICATE_COMMIT_WARNING_RULE_ID => Some(GIT_GRAPH_DUPLICATE_COMMIT_RULE),
        _ => None,
    }
}

pub(crate) fn internal_rule_registry_gap_diagnostic(
    message: impl Into<String>,
    span: Option<DiagnosticSpan>,
) -> AnalysisDiagnostic {
    let mut diagnostic = AnalysisDiagnostic::error(
        INTERNAL_RULE_REGISTRY_GAP_RULE_ID,
        DiagnosticCategory::Internal,
        message,
    )
    .with_code(
        AnalysisStatus::InternalError.code(),
        AnalysisStatus::InternalError.code_name(),
    );

    if let Some(span) = span {
        diagnostic = diagnostic.with_span(span);
    }

    diagnostic
}

fn init_directive_alias_diagnostics(
    source: &str,
    source_map: &SourceMap,
    rule_config: &AnalysisRuleConfig,
) -> Vec<AnalysisDiagnostic> {
    if !rule_config.is_rule_enabled(PREFER_INIT_DIRECTIVE_RULE) {
        return Vec::new();
    }
    if rule_config.is_rule_enabled(PREFER_FRONTMATTER_CONFIG_RULE) {
        return Vec::new();
    }
    let severity = rule_config.severity_for(PREFER_INIT_DIRECTIVE_RULE);

    directive_keyword_spans(source)
        .into_iter()
        .filter_map(|keyword| {
            (source.get(keyword.start..keyword.end) == Some("initialize"))
                .then_some(keyword)
        })
        .filter_map(|keyword| {
            let span = source_map.span(keyword.start, keyword.end).ok()?;
            Some(
                AnalysisDiagnostic::new(
                    PREFER_INIT_DIRECTIVE_RULE.id,
                    severity,
                    PREFER_INIT_DIRECTIVE_RULE.category,
                    "prefer `init` directive keyword over the `initialize` alias",
                )
                .with_span(span.clone())
                .with_help("`initialize` is accepted as an alias; `init` is the canonical Mermaid directive keyword.")
                .with_fix(
                    DiagnosticFix::new(
                        "Replace `initialize` with `init`",
                        vec![DiagnosticFixEdit::new(span, "init")],
                    )
                    .preferred(),
                ),
            )
        })
        .collect()
}

fn prefer_frontmatter_config_diagnostics(
    source: &str,
    source_map: &SourceMap,
    rule_config: &AnalysisRuleConfig,
) -> Vec<AnalysisDiagnostic> {
    if !rule_config.is_rule_enabled(PREFER_FRONTMATTER_CONFIG_RULE) {
        return Vec::new();
    }
    let severity = rule_config.severity_for(PREFER_FRONTMATTER_CONFIG_RULE);
    let fix = crate::source_config_rewrite::init_directives_to_frontmatter_fix(source, source_map);

    directive_keyword_spans(source)
        .into_iter()
        .filter_map(|keyword| {
            matches!(source.get(keyword.start..keyword.end), Some("init" | "initialize"))
                .then_some(keyword)
        })
        .filter_map(|keyword| {
            let span = source_map.span(keyword.start, keyword.end).ok()?;
            let mut diagnostic = AnalysisDiagnostic::new(
                PREFER_FRONTMATTER_CONFIG_RULE.id,
                severity,
                PREFER_FRONTMATTER_CONFIG_RULE.category,
                "prefer frontmatter `config` over Mermaid init directives",
            )
            .with_span(span)
            .with_help(
                "Mermaid deprecated directives from v10.5.0; diagram authors should move configuration into the diagram frontmatter `config` block.",
            );
            if let Some(fix) = fix.clone() {
                diagnostic = diagnostic.with_fix(fix);
            }
            Some(diagnostic)
        })
        .collect()
}

fn deprecated_flowchart_html_labels_diagnostics(
    source: &str,
    source_map: &SourceMap,
    rule_config: &AnalysisRuleConfig,
    init_matching_paths: &[&[&str]],
    frontmatter_matching_paths: &[&[&str]],
) -> Vec<AnalysisDiagnostic> {
    config_key_diagnostics(
        source,
        source_map,
        rule_config,
        DEPRECATED_FLOWCHART_HTML_LABELS_RULE,
        init_matching_paths,
        frontmatter_matching_paths,
        "`flowchart.htmlLabels` is deprecated; use root-level `htmlLabels` instead",
        "Mermaid keeps `flowchart.htmlLabels` as a compatibility fallback, but root-level `htmlLabels` takes precedence.",
    )
}

fn deprecated_external_diagram_loading_diagnostics(
    source: &str,
    source_map: &SourceMap,
    rule_config: &AnalysisRuleConfig,
) -> Vec<AnalysisDiagnostic> {
    config_key_diagnostics(
        source,
        source_map,
        rule_config,
        DEPRECATED_EXTERNAL_DIAGRAM_LOADING_RULE,
        &DEPRECATED_EXTERNAL_DIAGRAM_LOADING_CONFIG_PATHS,
        &DEPRECATED_EXTERNAL_DIAGRAM_LOADING_FRONTMATTER_CONFIG_PATHS,
        "deprecated external diagram loading config; use `registerExternalDiagrams` instead",
        "Mermaid warns that `lazyLoadedDiagrams` and `loadExternalDiagramsAtStartup` are deprecated in favor of the `registerExternalDiagrams` API.",
    )
}

fn config_key_diagnostics(
    source: &str,
    source_map: &SourceMap,
    rule_config: &AnalysisRuleConfig,
    descriptor: RuleDescriptor,
    init_matching_paths: &[&[&str]],
    frontmatter_matching_paths: &[&[&str]],
    message: &'static str,
    help: &'static str,
) -> Vec<AnalysisDiagnostic> {
    if !rule_config.is_rule_enabled(descriptor) {
        return Vec::new();
    }
    let severity = rule_config.severity_for(descriptor);

    let mut spans = init_directive_config_key_spans(source, init_matching_paths);
    spans.extend(frontmatter_config_key_spans(
        source,
        frontmatter_matching_paths,
    ));

    spans
        .into_iter()
        .filter_map(|span| {
            let span = source_map.span(span.start, span.end).ok()?;
            Some(
                AnalysisDiagnostic::new(descriptor.id, severity, descriptor.category, message)
                    .with_span(span)
                    .with_help(help),
            )
        })
        .collect()
}

#[cfg(test)]
mod tests;