@@ -5,6 +5,7 @@ extern crate json_schema_validator_core;
55extern crate lazy_static;
66extern crate regex;
77extern crate serde_json;
8+ extern crate serde_json5;
89extern crate walkdir;
910
1011use std:: fmt;
@@ -13,24 +14,58 @@ use std::path;
1314use std:: process;
1415
1516lazy_static:: lazy_static! {
16- /// DEFAULT_JSON_FILE_PATTERNS collects patterns for identifying JSON files.
17- pub static ref DEFAULT_JSON_FILE_PATTERNS : regex:: Regex = regex:: Regex :: new(
18- & [
19- // eslint
20- r".*\.eslintrc" ,
17+ /// JSON_FILE_EXTENSIONS collects common JSON file extensions.
18+ pub static ref JSON_FILE_EXTENSIONS : Vec <String > = vec![
19+ // eslint
20+ "eslintrc" . to_string( ) ,
2121
22- // jsfmt
23- r".*\. jsfmtrc",
22+ // jsfmt
23+ " jsfmtrc". to_string ( ) ,
2424
25- // jslint
26- r".*\. jslintrc",
25+ // jslint
26+ " jslintrc". to_string ( ) ,
2727
28- // jshint
29- r".*\. jshintrc",
28+ // jshint
29+ " jshintrc". to_string ( ) ,
3030
31- // JSON
32- r".*\.json" ,
33- ] . join( "|" )
31+ // JSON
32+ "json" . to_string( ) ,
33+ ] ;
34+
35+ /// JSON5_FILE_EXTENSIONS collects common JSON5 file extensions.
36+ pub static ref JSON5_FILE_EXTENSIONS : Vec <String > = JSON_FILE_EXTENSIONS
37+ . iter( )
38+ . chain(
39+ & [
40+ // JSON5
41+ "json5" . to_string( ) ,
42+ ]
43+ ) . cloned( )
44+ . collect( ) ;
45+
46+ /// DEFAULT_JSON_FILE_PATTERNS collects patterns for identifying JSON files.
47+ pub static ref DEFAULT_JSON_FILE_PATTERNS : regex:: Regex = regex:: Regex :: new(
48+ & format!(
49+ "({})$" ,
50+ JSON_FILE_EXTENSIONS
51+ . iter( )
52+ . map( |e| format!( r".*\.{}" , e) )
53+ . collect:: <Vec <String >>( )
54+ . join( "|" )
55+ )
56+ )
57+ . unwrap( ) ;
58+
59+ /// DEFAULT_JSON5_FILE_PATTERNS collects patterns for identifying JSON files.
60+ pub static ref DEFAULT_JSON5_FILE_PATTERNS : regex:: Regex = regex:: Regex :: new(
61+ & format!(
62+ "({})$" ,
63+ JSON5_FILE_EXTENSIONS
64+ . iter( )
65+ . map( |e| format!( r".*\.{}" , e) )
66+ . collect:: <Vec <String >>( )
67+ . join( "|" )
68+ )
3469 )
3570 . unwrap( ) ;
3671
@@ -68,6 +103,7 @@ pub enum KirillError {
68103 UnsupportedPathError ( String ) ,
69104 PathRenderError ( String ) ,
70105 JSONParseError ( serde_json:: Error ) ,
106+ JSON5ParseError ( serde_json5:: Error ) ,
71107 JSONSchemaError ( String ) ,
72108}
73109
@@ -79,6 +115,7 @@ impl fmt::Display for KirillError {
79115 KirillError :: UnsupportedPathError ( e) => write ! ( f, "{}" , e) ,
80116 KirillError :: PathRenderError ( e) => write ! ( f, "{}" , e) ,
81117 KirillError :: JSONParseError ( e) => write ! ( f, "{}" , e) ,
118+ KirillError :: JSON5ParseError ( e) => write ! ( f, "{}" , e) ,
82119 KirillError :: JSONSchemaError ( e) => write ! ( f, "{}" , e) ,
83120 }
84121 }
@@ -94,7 +131,10 @@ impl die::PrintExit for KirillError {
94131/// find_json_documents recursively searches
95132/// the given directories and/or file root paths
96133/// for JSON documents.
97- pub fn find_json_documents ( roots : Vec < & path:: Path > ) -> Result < Vec < String > , KirillError > {
134+ pub fn find_json_documents (
135+ roots : Vec < & path:: Path > ,
136+ parse_json5 : bool ,
137+ ) -> Result < Vec < String > , KirillError > {
98138 let mut pth_bufs = Vec :: < path:: PathBuf > :: new ( ) ;
99139
100140 for root in roots {
@@ -146,7 +186,13 @@ pub fn find_json_documents(roots: Vec<&path::Path>) -> Result<Vec<String>, Kiril
146186 continue ;
147187 }
148188
149- if DEFAULT_JSON_FILE_PATTERNS . is_match ( pth_abs_str) {
189+ let pattern = if parse_json5 {
190+ & * DEFAULT_JSON5_FILE_PATTERNS
191+ } else {
192+ & * DEFAULT_JSON_FILE_PATTERNS
193+ } ;
194+
195+ if pattern. is_match ( pth_abs_str) {
150196 let pth_clean_buf = clean_path:: clean ( pth) ;
151197 let pth_clean = pth_clean_buf. as_path ( ) ;
152198 let pth_clean_str = pth_clean
@@ -163,8 +209,11 @@ pub fn find_json_documents(roots: Vec<&path::Path>) -> Result<Vec<String>, Kiril
163209}
164210
165211/// find_json_documents_sorted lexicographically sorts any JSON document results.
166- pub fn find_json_documents_sorted ( roots : Vec < & path:: Path > ) -> Result < Vec < String > , KirillError > {
167- let mut json_documents = find_json_documents ( roots) ?;
212+ pub fn find_json_documents_sorted (
213+ roots : Vec < & path:: Path > ,
214+ parse_json5 : bool ,
215+ ) -> Result < Vec < String > , KirillError > {
216+ let mut json_documents = find_json_documents ( roots, parse_json5) ?;
168217 json_documents. sort ( ) ;
169218 Ok ( json_documents)
170219}
@@ -173,7 +222,7 @@ pub fn find_json_documents_sorted(roots: Vec<&path::Path>) -> Result<Vec<String>
173222///
174223/// Returns Some(error) on error.
175224/// Otherwise, returns None.
176- pub fn validate_json_file_basic ( s : & str ) -> Option < KirillError > {
225+ pub fn validate_json_file_basic ( s : & str , parse_json5 : bool ) -> Option < KirillError > {
177226 let pth = path:: Path :: new ( s) ;
178227
179228 match fs:: read_to_string ( pth) {
@@ -182,9 +231,17 @@ pub fn validate_json_file_basic(s: &str) -> Option<KirillError> {
182231 pth. display( )
183232 ) ) ) ,
184233
185- Ok ( contents) => serde_json:: from_str :: < serde_json:: Value > ( & contents)
186- . map_err ( KirillError :: JSONParseError )
187- . err ( ) ,
234+ Ok ( contents) => {
235+ if parse_json5 {
236+ serde_json5:: from_str :: < serde_json:: Value > ( & contents)
237+ . map_err ( KirillError :: JSON5ParseError )
238+ . err ( )
239+ } else {
240+ serde_json:: from_str :: < serde_json:: Value > ( & contents)
241+ . map_err ( KirillError :: JSONParseError )
242+ . err ( )
243+ }
244+ }
188245 }
189246}
190247
0 commit comments