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
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
//! This module provides file formating utilities using
//! [`dprint`](https://github.com/dsherret/dprint).
//!
//! At the moment it is only consumed using CLI but in
//! the future it can be easily extended to provide
//! the same functions as ops available in JS runtime.
use crate::fs::files_in_subtree;
use crate::op_error::OpError;
use deno_core::ErrBox;
use dprint_plugin_typescript as dprint;
use std::fs;
use std::io::stdin;
use std::io::stdout;
use std::io::Read;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;
fn is_supported(path: &Path) -> bool {
if let Some(ext) = path.extension() {
if ext == "tsx" || ext == "js" || ext == "jsx" {
true
} else if ext == "ts" {
// Currently dprint does not support d.ts files.
// https://github.com/dsherret/dprint/issues/100
!path.as_os_str().to_string_lossy().ends_with(".d.ts")
} else {
false
}
} else {
false
}
}
fn get_config() -> dprint::configuration::Configuration {
dprint::configuration::ConfigurationBuilder::new()
.line_width(80)
.indent_width(2)
.next_control_flow_position(
dprint::configuration::NextControlFlowPosition::SameLine,
)
.binary_expression_operator_position(
dprint::configuration::OperatorPosition::SameLine,
)
.build()
}
fn check_source_files(
config: dprint::configuration::Configuration,
paths: Vec<PathBuf>,
) -> Result<(), ErrBox> {
let mut not_formatted_files = vec![];
for file_path in paths {
let file_path_str = file_path.to_string_lossy();
let file_contents = fs::read_to_string(&file_path).unwrap();
match dprint::format_text(&file_path_str, &file_contents, &config) {
Ok(None) => {
// nothing to format, pass
}
Ok(Some(formatted_text)) => {
if formatted_text != file_contents {
not_formatted_files.push(file_path);
}
}
Err(e) => {
eprintln!("Error checking: {}", &file_path_str);
eprintln!(" {}", e);
}
}
}
if not_formatted_files.is_empty() {
Ok(())
} else {
let f = if not_formatted_files.len() == 1 {
"file"
} else {
"files"
};
Err(
OpError::other(format!(
"Found {} not formatted {}",
not_formatted_files.len(),
f,
))
.into(),
)
}
}
fn format_source_files(
config: dprint::configuration::Configuration,
paths: Vec<PathBuf>,
) -> Result<(), ErrBox> {
let mut not_formatted_files = vec![];
for file_path in paths {
let file_path_str = file_path.to_string_lossy();
let file_contents = fs::read_to_string(&file_path)?;
// TODO(ry) dprint seems to panic unnecessarally sometimes. Until it matures
// we'll use a catch_unwind to avoid passing it on to our users.
let catch_unwind_result = std::panic::catch_unwind(|| {
dprint::format_text(&file_path_str, &file_contents, &config)
});
if let Ok(dprint_result) = catch_unwind_result {
match dprint_result {
Ok(None) => {
// nothing to format, pass
}
Ok(Some(formatted_text)) => {
if formatted_text != file_contents {
println!("{}", file_path_str);
fs::write(&file_path, formatted_text)?;
not_formatted_files.push(file_path);
}
}
Err(e) => {
eprintln!("Error formatting: {}", &file_path_str);
eprintln!(" {}", e);
}
}
} else {
eprintln!("dprint panic {}", file_path_str);
}
}
let f = if not_formatted_files.len() == 1 {
"file"
} else {
"files"
};
debug!("Formatted {} {}", not_formatted_files.len(), f);
Ok(())
}
/// Format JavaScript/TypeScript files.
///
/// First argument supports globs, and if it is `None`
/// then the current directory is recursively walked.
pub fn format(args: Vec<String>, check: bool) -> Result<(), ErrBox> {
if args.len() == 1 && args[0] == "-" {
return format_stdin(check);
}
let mut target_files: Vec<PathBuf> = vec![];
if args.is_empty() {
target_files.extend(files_in_subtree(
std::env::current_dir().unwrap(),
is_supported,
));
} else {
for arg in args {
let p = PathBuf::from(arg);
if p.is_dir() {
target_files.extend(files_in_subtree(p, is_supported));
} else {
target_files.push(p);
};
}
}
let config = get_config();
if check {
check_source_files(config, target_files)?;
} else {
format_source_files(config, target_files)?;
}
Ok(())
}
/// Format stdin and write result to stdout.
/// Treats input as TypeScript.
/// Compatible with `--check` flag.
fn format_stdin(check: bool) -> Result<(), ErrBox> {
let mut source = String::new();
if stdin().read_to_string(&mut source).is_err() {
return Err(OpError::other("Failed to read from stdin".to_string()).into());
}
let config = get_config();
match dprint::format_text("_stdin.ts", &source, &config) {
Ok(None) => unreachable!(),
Ok(Some(formatted_text)) => {
if check {
if formatted_text != source {
println!("Not formatted stdin");
}
} else {
stdout().write_all(formatted_text.as_bytes())?;
}
}
Err(e) => {
return Err(OpError::other(e).into());
}
}
Ok(())
}
#[test]
fn test_is_supported() {
assert!(!is_supported(Path::new("tests/subdir/redirects")));
assert!(!is_supported(Path::new("README.md")));
assert!(!is_supported(Path::new("lib/typescript.d.ts")));
assert!(is_supported(Path::new("cli/tests/001_hello.js")));
assert!(is_supported(Path::new("cli/tests/002_hello.ts")));
assert!(is_supported(Path::new("foo.jsx")));
assert!(is_supported(Path::new("foo.tsx")));
}
#[test]
fn check_tests_dir() {
// Because of cli/tests/error_syntax.js the following should fail but not
// crash.
let r = format(vec!["./tests".to_string()], true);
assert!(r.is_err());
}
|