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 659
/
make.rs
72 lines (62 loc) · 2.2 KB
/
make.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
use std::fmt::Display;
use rome_js_syntax::{JsSyntaxKind, JsSyntaxToken, TriviaPieceKind};
use rome_rowan::TriviaPiece;
pub use crate::generated::node_factory::*;
/// Create a new identifier token with no attached trivia
pub fn ident(text: &str) -> JsSyntaxToken {
JsSyntaxToken::new_detached(JsSyntaxKind::IDENT, text, [], [])
}
/// Create a new identifier token with no attached trivia
pub fn jsx_ident(text: &str) -> JsSyntaxToken {
JsSyntaxToken::new_detached(JsSyntaxKind::JSX_IDENT, text, [], [])
}
/// Create a new string literal token with no attached trivia
pub fn js_string_literal(text: &str) -> JsSyntaxToken {
JsSyntaxToken::new_detached(
JsSyntaxKind::JS_STRING_LITERAL,
&format!("\"{text}\""),
[],
[],
)
}
/// Create a new string literal token with no attached trivia
pub fn jsx_string_literal(text: &str) -> JsSyntaxToken {
JsSyntaxToken::new_detached(
JsSyntaxKind::JSX_STRING_LITERAL,
&format!("\"{text}\""),
[],
[],
)
}
/// Create a new string literal token with no attached trivia
pub fn js_number_literal<N>(text: N) -> JsSyntaxToken
where
N: Display + Copy,
{
JsSyntaxToken::new_detached(JsSyntaxKind::JS_NUMBER_LITERAL, &text.to_string(), [], [])
}
/// Create a new token with the specified syntax kind and no attached trivia
pub fn token(kind: JsSyntaxKind) -> JsSyntaxToken {
if let Some(text) = kind.to_string() {
JsSyntaxToken::new_detached(kind, text, [], [])
} else {
panic!("token kind {kind:?} cannot be transformed to text")
}
}
/// Create a new token with the specified syntax kind, and a whitespace trivia
/// piece on both the leading and trailing positions
pub fn token_decorated_with_space(kind: JsSyntaxKind) -> JsSyntaxToken {
if let Some(text) = kind.to_string() {
JsSyntaxToken::new_detached(
kind,
&format!(" {text} "),
[TriviaPiece::new(TriviaPieceKind::Whitespace, 1)],
[TriviaPiece::new(TriviaPieceKind::Whitespace, 1)],
)
} else {
panic!("token kind {kind:?} cannot be transformed to text")
}
}
pub fn eof() -> JsSyntaxToken {
JsSyntaxToken::new_detached(JsSyntaxKind::EOF, "", [], [])
}