This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 657
/
main.rs
802 lines (692 loc) · 26.7 KB
/
main.rs
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
use pulldown_cmark::{html::write_html, CodeBlockKind, Event, LinkType, Parser, Tag};
use rome_analyze::{
AnalysisFilter, AnalyzerOptions, ControlFlow, GroupCategory, Queryable, RegistryVisitor, Rule,
RuleCategory, RuleFilter, RuleGroup, RuleMetadata,
};
use rome_console::fmt::Termcolor;
use rome_console::{
fmt::{Formatter, HTML},
markup, Console, Markup, MarkupBuf,
};
use rome_diagnostics::termcolor::NoColor;
use rome_diagnostics::{Diagnostic, DiagnosticExt, PrintDiagnostic};
use rome_js_parser::JsParserOptions;
use rome_js_syntax::{JsFileSource, JsLanguage, Language, LanguageVariant, ModuleKind};
use rome_json_parser::JsonParserOptions;
use rome_json_syntax::JsonLanguage;
use rome_service::settings::WorkspaceSettings;
use std::{
collections::BTreeMap,
fmt::Write as _,
io::{self, Write as _},
path::Path,
slice,
str::{self, FromStr},
};
use xtask::{glue::fs2, *};
fn main() -> Result<()> {
let root = project_root().join("website/src/pages/lint/rules");
let reference_groups = project_root().join("website/src/components/generated/Groups.astro");
let reference_number_of_rules =
project_root().join("website/src/components/generated/NumberOfRules.astro");
// Clear the rules directory ignoring "not found" errors
if let Err(err) = fs2::remove_dir_all(&root) {
let is_not_found = err
.source()
.and_then(|err| err.downcast_ref::<io::Error>())
.map_or(false, |err| matches!(err.kind(), io::ErrorKind::NotFound));
if !is_not_found {
return Err(err);
}
}
fs2::create_dir_all(&root)?;
// Content of the index page
let mut index = Vec::new();
let mut reference_buffer = Vec::new();
writeln!(index, "---")?;
writeln!(index, "title: Lint Rules")?;
writeln!(index, "parent: linter/index")?;
writeln!(index, "emoji: 📏")?;
writeln!(index, "description: List of available lint rules.")?;
writeln!(index, "category: reference")?;
writeln!(index, "mainClass: rules")?;
writeln!(index, "---")?;
writeln!(index)?;
writeln!(index, "# Rules")?;
writeln!(index)?;
// Accumulate errors for all lint rules to print all outstanding issues on
// failure instead of just the first one
let mut errors = Vec::new();
#[derive(Default)]
struct LintRulesVisitor {
groups: BTreeMap<&'static str, BTreeMap<&'static str, RuleMetadata>>,
number_or_rules: u16,
}
impl RegistryVisitor<JsLanguage> for LintRulesVisitor {
fn record_category<C: GroupCategory<Language = JsLanguage>>(&mut self) {
if matches!(C::CATEGORY, RuleCategory::Lint) {
C::record_groups(self);
}
}
fn record_rule<R>(&mut self)
where
R: Rule + 'static,
R::Query: Queryable<Language = JsLanguage>,
<R::Query as Queryable>::Output: Clone,
{
self.number_or_rules += 1;
self.groups
.entry(<R::Group as RuleGroup>::NAME)
.or_insert_with(BTreeMap::new)
.insert(R::METADATA.name, R::METADATA);
}
}
impl RegistryVisitor<JsonLanguage> for LintRulesVisitor {
fn record_category<C: GroupCategory<Language = JsonLanguage>>(&mut self) {
if matches!(C::CATEGORY, RuleCategory::Lint) {
C::record_groups(self);
}
}
fn record_rule<R>(&mut self)
where
R: Rule + 'static,
R::Query: Queryable<Language = JsonLanguage>,
<R::Query as Queryable>::Output: Clone,
{
self.number_or_rules += 1;
self.groups
.entry(<R::Group as RuleGroup>::NAME)
.or_insert_with(BTreeMap::new)
.insert(R::METADATA.name, R::METADATA);
}
}
let mut visitor = LintRulesVisitor::default();
rome_js_analyze::visit_registry(&mut visitor);
rome_json_analyze::visit_registry(&mut visitor);
let LintRulesVisitor {
mut groups,
number_or_rules,
} = visitor;
let nursery_rules = groups
.remove("nursery")
.expect("Expected nursery group to exist");
writeln!(
reference_buffer,
"<!-- this file is auto generated, use `cargo lintdoc` to update it -->"
)?;
write!(reference_buffer, "<ul>")?;
for (group, rules) in groups {
generate_group(group, rules, &root, &mut index, &mut errors)?;
generate_reference(group, &mut reference_buffer)?;
}
generate_group("nursery", nursery_rules, &root, &mut index, &mut errors)?;
generate_reference("nursery", &mut reference_buffer)?;
write!(reference_buffer, "</ul>")?;
if !errors.is_empty() {
bail!(
"failed to generate documentation pages for the following rules:\n{}",
errors
.into_iter()
.map(|(rule, err)| format!("- {rule}: {err:?}\n"))
.collect::<String>()
);
}
let number_of_rules_buffer = format!(
"<!-- this file is auto generated, use `cargo lintdoc` to update it -->\n \
<p>Rome's linter has a total of <strong><a href='/lint/rules'>{} rules</a></strong><p>",
number_or_rules
);
fs2::write(root.join("index.mdx"), index)?;
fs2::write(reference_groups, reference_buffer)?;
fs2::write(reference_number_of_rules, number_of_rules_buffer)?;
Ok(())
}
fn generate_group(
group: &'static str,
rules: BTreeMap<&'static str, RuleMetadata>,
root: &Path,
mut index: &mut dyn io::Write,
errors: &mut Vec<(&'static str, Error)>,
) -> io::Result<()> {
let (group_name, description) = extract_group_metadata(group);
let is_nursery = group == "nursery";
writeln!(index, "\n## {group_name}")?;
writeln!(index)?;
write_markup_to_string(index, description)?;
writeln!(index)?;
writeln!(index, "<div class=\"category-rules\">")?;
for (rule, meta) in rules {
let is_recommended = !is_nursery && meta.recommended;
match generate_rule(root, group, rule, meta.docs, meta.version, is_recommended) {
Ok(summary) => {
writeln!(index, "<section class=\"rule\">")?;
writeln!(index, "<h3 data-toc-exclude id=\"{rule}\">")?;
writeln!(index, " <a href=\"/lint/rules/{rule}\">{rule}</a>")?;
if is_recommended {
writeln!(index, " <span class=\"recommended\">recommended</span>")?;
}
writeln!(index, "</h3>")?;
write_html(&mut index, summary.into_iter())?;
writeln!(index, "\n</section>")?;
}
Err(err) => {
errors.push((rule, err));
}
}
}
writeln!(index, "\n</div>")?;
Ok(())
}
/// Generates the documentation page for a single lint rule
fn generate_rule(
root: &Path,
group: &'static str,
rule: &'static str,
docs: &'static str,
version: &'static str,
recommended: bool,
) -> Result<Vec<Event<'static>>> {
let mut content = Vec::new();
// Write the header for this lint rule
writeln!(content, "---")?;
writeln!(content, "title: Lint Rule {rule}")?;
writeln!(content, "parent: lint/rules/index")?;
writeln!(content, "---")?;
writeln!(content)?;
writeln!(content, "# {rule} (since v{version})")?;
writeln!(content)?;
if recommended {
writeln!(content, "> This rule is recommended by Rome.")?;
writeln!(content)?;
}
let summary = parse_documentation(group, rule, docs, &mut content)?;
writeln!(content, "## Related links")?;
writeln!(content)?;
writeln!(content, "- [Disable a rule](/linter/#disable-a-lint-rule)")?;
writeln!(content, "- [Rule options](/linter/#rule-options)")?;
fs2::write(root.join(format!("{rule}.md")), content)?;
Ok(summary)
}
/// Parse the documentation fragment for a lint rule (in markdown) and generates
/// the content for the corresponding documentation page
fn parse_documentation(
group: &'static str,
rule: &'static str,
docs: &'static str,
content: &mut Vec<u8>,
) -> Result<Vec<Event<'static>>> {
let parser = Parser::new(docs);
// Parser events for the first paragraph of documentation in the resulting
// content, used as a short summary of what the rule does in the rules page
let mut summary = Vec::new();
let mut is_summary = false;
// Tracks the content of the current code block if it's using a
// language supported for analysis
let mut language = None;
let mut list_order = None;
for event in parser {
if is_summary {
if matches!(event, Event::End(Tag::Paragraph)) {
is_summary = false;
} else {
summary.push(event.clone());
}
}
match event {
// CodeBlock-specific handling
Event::Start(Tag::CodeBlock(CodeBlockKind::Fenced(meta))) => {
// Track the content of code blocks to pass them through the analyzer
let test = CodeBlockTest::from_str(meta.as_ref())?;
// Erase the lintdoc-specific attributes in the output by
// re-generating the language ID from the source type
write!(content, "```")?;
if !meta.is_empty() {
if let BlockType::Js(source_type) = test.block_type {
match source_type.language() {
Language::JavaScript => write!(content, "js")?,
Language::TypeScript { .. } => write!(content, "ts")?,
}
match source_type.variant() {
LanguageVariant::Standard => {}
LanguageVariant::Jsx => write!(content, "x")?,
}
}
}
writeln!(content)?;
language = Some((test, String::new()));
}
Event::End(Tag::CodeBlock(_)) => {
writeln!(content, "```")?;
writeln!(content)?;
if let Some((test, block)) = language.take() {
if test.expect_diagnostic {
write!(
content,
"<pre class=\"language-text\"><code class=\"language-text\">"
)?;
}
assert_lint(group, rule, &test, &block, content)
.context("snapshot test failed")?;
if test.expect_diagnostic {
writeln!(content, "</code></pre>")?;
writeln!(content)?;
}
}
}
Event::Text(text) => {
if let Some((_, block)) = &mut language {
write!(block, "{text}")?;
}
write!(content, "{text}")?;
}
// Other markdown events are emitted as-is
Event::Start(Tag::Heading(level, ..)) => {
write!(content, "{} ", "#".repeat(level as usize))?;
}
Event::End(Tag::Heading(..)) => {
writeln!(content)?;
writeln!(content)?;
}
Event::Start(Tag::Paragraph) => {
if summary.is_empty() && !is_summary {
is_summary = true;
}
}
Event::End(Tag::Paragraph) => {
writeln!(content)?;
writeln!(content)?;
}
Event::Code(text) => {
write!(content, "`{text}`")?;
}
Event::Start(Tag::Link(kind, _, _)) => match kind {
LinkType::Inline => {
write!(content, "[")?;
}
LinkType::Shortcut => {
write!(content, "[")?;
}
_ => {
panic!("unimplemented link type")
}
},
Event::End(Tag::Link(_, url, title)) => {
write!(content, "]({url}")?;
if !title.is_empty() {
write!(content, " \"{title}\"")?;
}
write!(content, ")")?;
}
Event::SoftBreak => {
writeln!(content)?;
}
Event::Start(Tag::List(num)) => {
if let Some(num) = num {
list_order = Some(num);
}
}
Event::End(Tag::List(_)) => {
list_order = None;
writeln!(content)?;
}
Event::Start(Tag::Item) => {
if let Some(num) = list_order {
write!(content, "{num}. ")?;
} else {
write!(content, "- ")?;
}
}
Event::End(Tag::Item) => {
list_order = list_order.map(|item| item + 1);
writeln!(content)?;
}
Event::Start(Tag::Strong) => {
write!(content, "**")?;
}
Event::End(Tag::Strong) => {
write!(content, "**")?;
}
Event::Start(Tag::Emphasis) => {
write!(content, "_")?;
}
Event::End(Tag::Emphasis) => {
write!(content, "_")?;
}
Event::Start(Tag::Strikethrough) => {
write!(content, "~")?;
}
Event::End(Tag::Strikethrough) => {
write!(content, "~")?;
}
Event::Start(Tag::BlockQuote) => {
write!(content, ">")?;
}
Event::End(Tag::BlockQuote) => {
writeln!(content)?;
}
_ => {
// TODO: Implement remaining events as required
bail!("unimplemented event {event:?}")
}
}
}
Ok(summary)
}
enum BlockType {
Js(JsFileSource),
Json,
}
struct CodeBlockTest {
block_type: BlockType,
expect_diagnostic: bool,
ignore: bool,
}
impl FromStr for CodeBlockTest {
type Err = xtask::Error;
fn from_str(input: &str) -> Result<Self> {
// This is based on the parsing logic for code block languages in `rustdoc`:
// https://github.com/rust-lang/rust/blob/6ac8adad1f7d733b5b97d1df4e7f96e73a46db42/src/librustdoc/html/markdown.rs#L873
let tokens = input
.split(|c| c == ',' || c == ' ' || c == '\t')
.map(str::trim)
.filter(|token| !token.is_empty());
let mut test = CodeBlockTest {
block_type: BlockType::Js(JsFileSource::default()),
expect_diagnostic: false,
ignore: false,
};
for token in tokens {
match token {
// Determine the language, using the same list of extensions as `compute_source_type_from_path_or_extension`
"cjs" => {
test.block_type = BlockType::Js(
JsFileSource::js_module().with_module_kind(ModuleKind::Script),
);
}
"js" | "mjs" | "jsx" => {
test.block_type = BlockType::Js(JsFileSource::jsx());
}
"ts" | "mts" | "cts" => {
test.block_type = BlockType::Js(JsFileSource::ts());
}
"tsx" => {
test.block_type = BlockType::Js(JsFileSource::tsx());
}
// Other attributes
"expect_diagnostic" => {
test.expect_diagnostic = true;
}
"ignore" => {
test.ignore = true;
}
"json" => {
test.block_type = BlockType::Json;
}
_ => {
bail!("unknown code block attribute {token:?}")
}
}
}
Ok(test)
}
}
/// Parse and analyze the provided code block, and asserts that it emits
/// exactly zero or one diagnostic depending on the value of `expect_diagnostic`.
/// That diagnostic is then emitted as text into the `content` buffer
fn assert_lint(
group: &'static str,
rule: &'static str,
test: &CodeBlockTest,
code: &str,
content: &mut Vec<u8>,
) -> Result<()> {
let file = format!("{group}/{rule}.js");
let mut write = HTML(content);
let mut diagnostic_count = 0;
let mut all_diagnostics = vec![];
let mut write_diagnostic = |code: &str, diag: rome_diagnostics::Error| {
let category = diag.category().map_or("", |code| code.name());
Formatter::new(&mut write).write_markup(markup! {
{PrintDiagnostic::verbose(&diag)}
})?;
all_diagnostics.push(diag);
// Fail the test if the analysis returns more diagnostics than expected
if test.expect_diagnostic {
// Print all diagnostics to help the user
if all_diagnostics.len() > 1 {
let mut console = rome_console::EnvConsole::default();
for diag in all_diagnostics.iter() {
console.println(
rome_console::LogLevel::Error,
markup! {
{PrintDiagnostic::verbose(diag)}
},
);
}
}
ensure!(
diagnostic_count == 0,
"analysis returned multiple diagnostics, code snippet: \n\n{}",
code
);
} else {
// Print all diagnostics to help the user
let mut console = rome_console::EnvConsole::default();
for diag in all_diagnostics.iter() {
console.println(
rome_console::LogLevel::Error,
markup! {
{PrintDiagnostic::verbose(diag)}
},
);
}
bail!(format!(
"analysis returned an unexpected diagnostic, code `snippet:\n\n{:?}\n\n{}",
category, code
));
}
diagnostic_count += 1;
Ok(())
};
if test.ignore {
return Ok(());
}
match test.block_type {
BlockType::Js(source_type) => {
let parse = rome_js_parser::parse(code, source_type, JsParserOptions::default());
if parse.has_errors() {
for diag in parse.into_diagnostics() {
let error = diag
.with_file_path(file.clone())
.with_file_source_code(code);
write_diagnostic(code, error)?;
}
} else {
let root = parse.tree();
let settings = WorkspaceSettings::default();
let rule_filter = RuleFilter::Rule(group, rule);
let filter = AnalysisFilter {
enabled_rules: Some(slice::from_ref(&rule_filter)),
..AnalysisFilter::default()
};
let options = AnalyzerOptions::default();
let (_, diagnostics) = rome_js_analyze::analyze(
&root,
filter,
&options,
source_type,
|signal| {
if let Some(mut diag) = signal.diagnostic() {
let category = diag.category().expect("linter diagnostic has no code");
let severity = settings.get_severity_from_rule_code(category).expect(
"If you see this error, it means you need to run cargo codegen-configuration",
);
for action in signal.actions() {
if !action.is_suppression() {
diag = diag.add_code_suggestion(action.into());
}
}
let error = diag
.with_severity(severity)
.with_file_path(file.clone())
.with_file_source_code(code);
let res = write_diagnostic(code, error);
// Abort the analysis on error
if let Err(err) = res {
return ControlFlow::Break(err);
}
}
ControlFlow::Continue(())
},
);
// Result is Some(_) if analysis aborted with an error
for diagnostic in diagnostics {
write_diagnostic(code, diagnostic)?;
}
}
if test.expect_diagnostic {
// Fail the test if the analysis didn't emit any diagnostic
ensure!(
diagnostic_count == 1,
"analysis returned no diagnostics.\n code snippet:\n {}",
code
);
}
}
BlockType::Json => {
let parse = rome_json_parser::parse_json(code, JsonParserOptions::default());
if parse.has_errors() {
for diag in parse.into_diagnostics() {
let error = diag
.with_file_path(file.clone())
.with_file_source_code(code);
write_diagnostic(code, error)?;
}
} else {
let root = parse.tree();
let settings = WorkspaceSettings::default();
let rule_filter = RuleFilter::Rule(group, rule);
let filter = AnalysisFilter {
enabled_rules: Some(slice::from_ref(&rule_filter)),
..AnalysisFilter::default()
};
let options = AnalyzerOptions::default();
let (_, diagnostics) = rome_json_analyze::analyze(
&root.value().unwrap(),
filter,
&options,
|signal| {
if let Some(mut diag) = signal.diagnostic() {
let category = diag.category().expect("linter diagnostic has no code");
let severity = settings.get_severity_from_rule_code(category).expect(
"If you see this error, it means you need to run cargo codegen-configuration",
);
for action in signal.actions() {
if !action.is_suppression() {
diag = diag.add_code_suggestion(action.into());
}
}
let error = diag
.with_severity(severity)
.with_file_path(file.clone())
.with_file_source_code(code);
let res = write_diagnostic(code, error);
// Abort the analysis on error
if let Err(err) = res {
return ControlFlow::Break(err);
}
}
ControlFlow::Continue(())
},
);
// Result is Some(_) if analysis aborted with an error
for diagnostic in diagnostics {
write_diagnostic(code, diagnostic)?;
}
}
}
}
Ok(())
}
fn generate_reference(group: &'static str, buffer: &mut dyn io::Write) -> io::Result<()> {
let (group_name, description) = extract_group_metadata(group);
let description = markup_to_string(&description.to_owned());
let description = description.replace('\n', " ");
writeln!(
buffer,
"<li><code>{}</code>: {}</li>",
group_name.to_lowercase(),
description
)
}
fn extract_group_metadata(group: &str) -> (&str, Markup) {
match group {
"a11y" => (
"Accessibility",
markup! {
"Rules focused on preventing accessibility problems."
},
),
"complexity" => (
"Complexity",
markup! {
"Rules that focus on inspecting complex code that could be simplified."
},
),
"correctness" => (
"Correctness",
markup! {
"Rules that detect code that is guaranteed to be incorrect or useless."
},
),
"nursery" => (
"Nursery",
markup! {
"New rules that are still under development.
Nursery rules require explicit opt-in via configuration on stable versions because they may still have bugs or performance problems.
They are enabled by default on nightly builds, but as they are unstable their diagnostic severity may be set to either error or
warning, depending on whether we intend for the rule to be recommended or not when it eventually gets stabilized.
Nursery rules get promoted to other groups once they become stable or may be removed.
Rules that belong to this group "<Emphasis>"are not subject to semantic version"</Emphasis>"."
},
),
"performance" => (
"Performance",
markup! {
"Rules catching ways your code could be written to run faster, or generally be more efficient."
},
),
"security" => (
"Security",
markup! {
"Rules that detect potential security flaws."
},
),
"style" => (
"Style",
markup! {
"Rules enforcing a consistent and idiomatic way of writing your code."
},
),
"suspicious" => (
"Suspicious",
markup! {
"Rules that detect code that is likely to be incorrect or useless."
},
),
_ => panic!("Unknown group ID {group:?}"),
}
}
pub fn write_markup_to_string(buffer: &mut dyn io::Write, markup: Markup) -> io::Result<()> {
let mut write = HTML(buffer);
let mut fmt = Formatter::new(&mut write);
fmt.write_markup(markup)
}
fn markup_to_string(markup: &MarkupBuf) -> String {
let mut buffer = Vec::new();
let mut write = Termcolor(NoColor::new(&mut buffer));
let mut fmt = Formatter::new(&mut write);
fmt.write_markup(markup! { {markup} })
.expect("to have written in the buffer");
String::from_utf8(buffer).expect("to have convert a buffer into a String")
}