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
use nu_engine::{find_in_dirs_env, get_dirs_var_from_call, CallExt};
use nu_parser::{parse, parse_module_block, unescape_unquote_string};
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack, StateWorkingSet};
use nu_protocol::{
    Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value,
};

#[derive(Clone)]
pub struct NuCheck;

impl Command for NuCheck {
    fn name(&self) -> &str {
        "nu-check"
    }

    fn signature(&self) -> Signature {
        Signature::build("nu-check")
            .input_output_types(vec![(Type::String, Type::Bool),
            (Type::ListStream, Type::Bool),
            (Type::List(Box::new(Type::Any)), Type::Bool)])
            // type is string to avoid automatically canonicalizing the path
            .optional("path", SyntaxShape::String, "File path to parse.")
            .switch("as-module", "Parse content as module", Some('m'))
            .switch("debug", "Show error messages", Some('d'))
            .switch("all", "Parse content as script first, returns result if success, otherwise, try with module", Some('a'))
            .category(Category::Strings)
    }

    fn usage(&self) -> &str {
        "Validate and parse input content."
    }

    fn search_terms(&self) -> Vec<&str> {
        vec!["syntax", "parse", "debug"]
    }

    fn run(
        &self,
        engine_state: &EngineState,
        stack: &mut Stack,
        call: &Call,
        input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        let path: Option<Spanned<String>> = call.opt(engine_state, stack, 0)?;
        let is_module = call.has_flag(engine_state, stack, "as-module")?;
        let is_debug = call.has_flag(engine_state, stack, "debug")?;
        let is_all = call.has_flag(engine_state, stack, "all")?;
        let config = engine_state.get_config();
        let mut contents = vec![];

        // DO NOT ever try to merge the working_set in this command
        let mut working_set = StateWorkingSet::new(engine_state);

        if is_all && is_module {
            return Err(ShellError::GenericError {
                error: "Detected command flags conflict".into(),
                msg: "You cannot have both `--all` and `--as-module` on the same command line, please refer to `nu-check --help` for more details".into(),
                span: Some(call.head),
                help: None,
                inner: vec![]
            });
        }

        let span = input.span().unwrap_or(call.head);
        match input {
            PipelineData::Value(Value::String { val, .. }, ..) => {
                let contents = Vec::from(val);
                if is_all {
                    heuristic_parse(&mut working_set, None, &contents, is_debug, call.head)
                } else if is_module {
                    parse_module(&mut working_set, None, &contents, is_debug, span)
                } else {
                    parse_script(&mut working_set, None, &contents, is_debug, span)
                }
            }
            PipelineData::ListStream(stream, ..) => {
                let list_stream = stream.into_string("\n", config);
                let contents = Vec::from(list_stream);

                if is_all {
                    heuristic_parse(&mut working_set, None, &contents, is_debug, call.head)
                } else if is_module {
                    parse_module(&mut working_set, None, &contents, is_debug, call.head)
                } else {
                    parse_script(&mut working_set, None, &contents, is_debug, call.head)
                }
            }
            PipelineData::ExternalStream {
                stdout: Some(stream),
                ..
            } => {
                let raw_stream: Vec<_> = stream.stream.collect();
                for r in raw_stream {
                    match r {
                        Ok(v) => contents.extend(v),
                        Err(error) => return Err(error),
                    };
                }

                if is_all {
                    heuristic_parse(&mut working_set, None, &contents, is_debug, call.head)
                } else if is_module {
                    parse_module(&mut working_set, None, &contents, is_debug, call.head)
                } else {
                    parse_script(&mut working_set, None, &contents, is_debug, call.head)
                }
            }
            _ => {
                if let Some(path_str) = path {
                    // look up the path as relative to FILE_PWD or inside NU_LIB_DIRS (same process as source-env)
                    let path = match find_in_dirs_env(
                        &path_str.item,
                        engine_state,
                        stack,
                        get_dirs_var_from_call(call),
                    ) {
                        Ok(path) => {
                            if let Some(path) = path {
                                path
                            } else {
                                return Err(ShellError::FileNotFound {
                                    file: path_str.item,
                                    span: path_str.span,
                                });
                            }
                        }
                        Err(error) => return Err(error),
                    };

                    // get the expanded path as a string
                    let path_str = path.to_string_lossy().to_string();

                    let ext: Vec<_> = path_str.rsplitn(2, '.').collect();
                    if ext[0] != "nu" {
                        return Err(ShellError::GenericError {
                            error: "Cannot parse input".into(),
                            msg: "File extension must be the type of .nu".into(),
                            span: Some(call.head),
                            help: None,
                            inner: vec![],
                        });
                    }

                    // Change currently parsed directory
                    let prev_currently_parsed_cwd = if let Some(parent) = path.parent() {
                        let prev = working_set.currently_parsed_cwd.clone();

                        working_set.currently_parsed_cwd = Some(parent.into());

                        prev
                    } else {
                        working_set.currently_parsed_cwd.clone()
                    };

                    let result = if is_all {
                        heuristic_parse_file(path_str, &mut working_set, call, is_debug)
                    } else if is_module {
                        parse_file_module(path_str, &mut working_set, call, is_debug)
                    } else {
                        parse_file_script(path_str, &mut working_set, call, is_debug)
                    };

                    // Restore the currently parsed directory back
                    working_set.currently_parsed_cwd = prev_currently_parsed_cwd;

                    result
                } else {
                    Err(ShellError::GenericError {
                        error: "Failed to execute command".into(),
                        msg: "Please run 'nu-check --help' for more details".into(),
                        span: Some(call.head),
                        help: None,
                        inner: vec![],
                    })
                }
            }
        }
    }

    fn examples(&self) -> Vec<Example> {
        vec![
            Example {
                description: "Parse a input file as script(Default)",
                example: "nu-check script.nu",
                result: None,
            },
            Example {
                description: "Parse a input file as module",
                example: "nu-check --as-module module.nu",
                result: None,
            },
            Example {
                description: "Parse a input file by showing error message",
                example: "nu-check --debug script.nu",
                result: None,
            },
            Example {
                description: "Parse an external stream as script by showing error message",
                example: "open foo.nu | nu-check --debug script.nu",
                result: None,
            },
            Example {
                description: "Parse an internal stream as module by showing error message",
                example: "open module.nu | lines | nu-check --debug --as-module module.nu",
                result: None,
            },
            Example {
                description: "Parse a string as script",
                example: "$'two(char nl)lines' | nu-check ",
                result: None,
            },
            Example {
                description: "Heuristically parse which begins with script first, if it sees a failure, try module afterwards",
                example: "nu-check -a script.nu",
                result: None,
            },
            Example {
                description: "Heuristically parse by showing error message",
                example: "open foo.nu | lines | nu-check --all --debug",
                result: None,
            },
        ]
    }
}

fn heuristic_parse(
    working_set: &mut StateWorkingSet,
    filename: Option<&str>,
    contents: &[u8],
    is_debug: bool,
    span: Span,
) -> Result<PipelineData, ShellError> {
    match parse_script(working_set, filename, contents, is_debug, span) {
        Ok(v) => Ok(v),
        Err(_) => {
            match parse_module(
                working_set,
                filename.map(|f| f.to_string()),
                contents,
                is_debug,
                span,
            ) {
                Ok(v) => Ok(v),
                Err(_) => {
                    if is_debug {
                        Err(ShellError::GenericError {
                            error: "Failed to parse content,tried both script and module".into(),
                            msg: "syntax error".into(),
                            span: Some(span),
                            help: Some("Run `nu-check --help` for more details".into()),
                            inner: vec![],
                        })
                    } else {
                        Ok(PipelineData::Value(Value::bool(false, span), None))
                    }
                }
            }
        }
    }
}

fn heuristic_parse_file(
    path: String,
    working_set: &mut StateWorkingSet,
    call: &Call,
    is_debug: bool,
) -> Result<PipelineData, ShellError> {
    let starting_error_count = working_set.parse_errors.len();
    let bytes = working_set.get_span_contents(call.head);
    let (filename, err) = unescape_unquote_string(bytes, call.head);
    if let Some(err) = err {
        working_set.error(err);
    }
    if starting_error_count == working_set.parse_errors.len() {
        if let Ok(contents) = std::fs::read(path) {
            match parse_script(
                working_set,
                Some(filename.as_str()),
                &contents,
                is_debug,
                call.head,
            ) {
                Ok(v) => Ok(v),
                Err(_) => {
                    match parse_module(working_set, Some(filename), &contents, is_debug, call.head)
                    {
                        Ok(v) => Ok(v),
                        Err(_) => {
                            if is_debug {
                                Err(ShellError::GenericError {
                                    error: "Failed to parse content,tried both script and module"
                                        .into(),
                                    msg: "syntax error".into(),
                                    span: Some(call.head),
                                    help: Some("Run `nu-check --help` for more details".into()),
                                    inner: vec![],
                                })
                            } else {
                                Ok(PipelineData::Value(Value::bool(false, call.head), None))
                            }
                        }
                    }
                }
            }
        } else {
            Err(ShellError::IOError {
                msg: "Can not read input".to_string(),
            })
        }
    } else {
        Err(ShellError::NotFound { span: call.head })
    }
}

fn parse_module(
    working_set: &mut StateWorkingSet,
    filename: Option<String>,
    contents: &[u8],
    is_debug: bool,
    span: Span,
) -> Result<PipelineData, ShellError> {
    let filename = filename.unwrap_or_else(|| "empty".to_string());

    let file_id = working_set.add_file(filename.clone(), contents);
    let new_span = working_set.get_span_for_file(file_id);

    let starting_error_count = working_set.parse_errors.len();
    parse_module_block(working_set, new_span, filename.as_bytes());

    if starting_error_count != working_set.parse_errors.len() {
        if is_debug {
            let msg = format!(
                r#"Found : {}"#,
                working_set
                    .parse_errors
                    .first()
                    .expect("Unable to parse content as module")
            );
            Err(ShellError::GenericError {
                error: "Failed to parse content".into(),
                msg,
                span: Some(span),
                help: Some("If the content is intended to be a script, please try to remove `--as-module` flag ".into()),
                inner: vec![],
            })
        } else {
            Ok(PipelineData::Value(Value::bool(false, new_span), None))
        }
    } else {
        Ok(PipelineData::Value(Value::bool(true, new_span), None))
    }
}

fn parse_script(
    working_set: &mut StateWorkingSet,
    filename: Option<&str>,
    contents: &[u8],
    is_debug: bool,
    span: Span,
) -> Result<PipelineData, ShellError> {
    let starting_error_count = working_set.parse_errors.len();
    parse(working_set, filename, contents, false);
    if starting_error_count != working_set.parse_errors.len() {
        let msg = format!(
            r#"Found : {}"#,
            working_set
                .parse_errors
                .first()
                .expect("Unable to parse content")
        );
        if is_debug {
            Err(ShellError::GenericError {
                error: "Failed to parse content".into(),
                msg,
                span: Some(span),
                help: Some("If the content is intended to be a module, please consider flag of `--as-module` ".into()),
                inner: vec![],
            })
        } else {
            Ok(PipelineData::Value(Value::bool(false, span), None))
        }
    } else {
        Ok(PipelineData::Value(Value::bool(true, span), None))
    }
}

fn parse_file_script(
    path: String,
    working_set: &mut StateWorkingSet,
    call: &Call,
    is_debug: bool,
) -> Result<PipelineData, ShellError> {
    let starting_error_count = working_set.parse_errors.len();
    let bytes = working_set.get_span_contents(call.head);
    let (filename, err) = unescape_unquote_string(bytes, call.head);
    if let Some(err) = err {
        working_set.error(err)
    }
    if starting_error_count == working_set.parse_errors.len() {
        if let Ok(contents) = std::fs::read(path) {
            parse_script(
                working_set,
                Some(filename.as_str()),
                &contents,
                is_debug,
                call.head,
            )
        } else {
            Err(ShellError::IOError {
                msg: "Can not read path".to_string(),
            })
        }
    } else {
        Err(ShellError::NotFound { span: call.head })
    }
}

fn parse_file_module(
    path: String,
    working_set: &mut StateWorkingSet,
    call: &Call,
    is_debug: bool,
) -> Result<PipelineData, ShellError> {
    let starting_error_count = working_set.parse_errors.len();
    let bytes = working_set.get_span_contents(call.head);
    let (filename, err) = unescape_unquote_string(bytes, call.head);
    if let Some(err) = err {
        working_set.error(err);
    }
    if starting_error_count == working_set.parse_errors.len() {
        if let Ok(contents) = std::fs::read(path) {
            parse_module(working_set, Some(filename), &contents, is_debug, call.head)
        } else {
            Err(ShellError::IOError {
                msg: "Can not read path".to_string(),
            })
        }
    } else {
        Err(ShellError::NotFound { span: call.head })
    }
}