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
use super::*;
use pest::Parser as P;
use pest_derive::Parser;
#[derive(Parser)]
#[grammar = "../grammar/javascript.pest"]
struct JavaScriptParser;
#[allow(dead_code)]
pub fn format_javascript(text: &str) -> code::FormatResult {
let pairs = JavaScriptParser::parse(Rule::item, text);
let text = code::FormatResult::new(text);
return code::format_pairs(text, pairs);
}
#[allow(dead_code)]
pub fn lint_javascript(text: &str) -> code::LintResult {
let pairs = JavaScriptParser::parse(Rule::item, text);
let text = code::LintResult::new(text);
return code::format_pairs(text, pairs);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_format_javascript() {
let example = r###"
// 第1行注释
// 第2行注释
function helloWorld(a) {
const a = '第1个';
const b = "第2个" + "第3个";
const re = /包含#regexp测试/;
const re1 = new RegExp("RegExp不处理");
const re2 = new RegExp('不处理RegExp');
const str_literal = `这个${foo}不会处理`;
/**
* Hello你好
* 这是第2行
*/
const c = `这是string第1行
这是string第2行`;
// autocorrect-disable
const disable_1 = "这行将会disable掉";
const disable_2 = "这行将也会disable掉";
// autocorrect-enable
return <>
<div className="react-name">
<List renderItem={(item) => (
<Item className="list-item">
<span>nested项</span>
<span>{item}</span>
</Item>
)} />
<h1>Hello你好<strong>你好foo世界</strong></h1>
外部HTML结果
<div>{ a && t("这里string也要处理")}</div>
</div>
</>
}
"###;
let expect = r###"
// 第 1 行注释
// 第 2 行注释
function helloWorld(a) {
const a = '第 1 个';
const b = "第 2 个" + "第 3 个";
const re = /包含#regexp测试/;
const re1 = new RegExp("RegExp不处理");
const re2 = new RegExp('不处理RegExp');
const str_literal = `这个${foo}不会处理`;
/**
* Hello 你好
* 这是第 2 行
*/
const c = `这是 string 第 1 行
这是 string 第 2 行`;
// autocorrect-disable
const disable_1 = "这行将会disable掉";
const disable_2 = "这行将也会disable掉";
// autocorrect-enable
return <>
<div className="react-name">
<List renderItem={(item) => (
<Item className="list-item">
<span>nested 项</span>
<span>{item}</span>
</Item>
)} />
<h1>Hello 你好<strong>你好 foo 世界</strong></h1>
外部 HTML 结果
<div>{ a && t("这里 string 也要处理")}</div>
</div>
</>
}
"###;
assert_eq!(expect, format_javascript(example).to_string());
}
macro_rules! assert_json_eq {
($expected:expr, $actual:expr) => {{
let expected = $expected;
let actual = $actual;
let expect_json = serde_json::from_str(expected).unwrap_or(serde_json::Value::default());
let result = serde_json::from_str(actual.as_str()).unwrap_or(serde_json::Value::default());
pretty_assertions::assert_eq!(expect_json, result);
}};
}
#[test]
fn it_format_javascript_without_any_string() {
let example = r###"
function helloWorld(a) {
const a = "";
return <div className="tags">
{tags.map(tag => <Tag color="orange"><Icon name="label" /> {tag.name}</Tag>)}
</div>;
}
"###;
let expect = r###"
function helloWorld(a) {
const a = "";
return <div className="tags">
{tags.map(tag => <Tag color="orange"><Icon name="label" /> {tag.name}</Tag>)}
</div>;
}
"###;
assert_eq!(expect, format_javascript(example).to_string());
}
#[test]
fn it_lint_javascript() {
let example = r###"
/**
* Hello你好
* 这是第2行
*/
function application() {
let example = "这是single line单行注释";
console.log(`这是string第1行
这是string第2行
`)
// autocorrect-disable
const disable_1 = "这行将会disable掉";
const disable_2 = "这行将也会disable掉";
// autocorrect-enable
const c = "这是string第3行";
}
"###;
let expect = r###"
{
"filepath":"",
"lines":[
{"c":5,"l":3,"new":"* Hello 你好","old":"* Hello你好"},
{"c":5,"l":4,"new":"* 这是第 2 行","old":"* 这是第2行"},
{"c":21,"l":7,"new":"\"这是 single line 单行注释\"","old":"\"这是single line单行注释\""},
{"c":19,"l":8,"new":"`这是 string 第 1 行","old":"`这是string第1行"},
{"c":7,"l":9,"new":"这是 string 第 2 行","old":"这是string第2行"},
{"c":17,"l":17,"new":"\"这是 string 第 3 行\"","old":"\"这是string第3行\""}
],
"error": ""
}
"###;
assert_json_eq!(expect, lint_javascript(example).to_json());
assert_json_eq!(expect, lint_javascript(example).to_json_pretty());
}
}