Fix panic when parsing wrong import statement

Have to be several tests because the parser bails out at the first error.
The error message is not optimal but better than a panic
This commit is contained in:
Olivier Goffart 2021-04-14 12:45:41 +02:00
parent 49868a8dae
commit 1e730eb845
5 changed files with 65 additions and 4 deletions

View file

@ -0,0 +1,13 @@
/* LICENSE BEGIN
This file is part of the SixtyFPS Project -- https://sixtyfps.io
Copyright (c) 2020 Olivier Goffart <olivier.goffart@sixtyfps.io>
Copyright (c) 2020 Simon Hausmann <simon.hausmann@sixtyfps.io>
SPDX-License-Identifier: GPL-3.0-only
This file is also available under commercial licensing terms.
Please contact info@sixtyfps.io for more information.
LICENSE END */
import;
// ^error{Syntax error: expected LBrace}

View file

@ -0,0 +1,13 @@
/* LICENSE BEGIN
This file is part of the SixtyFPS Project -- https://sixtyfps.io
Copyright (c) 2020 Olivier Goffart <olivier.goffart@sixtyfps.io>
Copyright (c) 2020 Simon Hausmann <simon.hausmann@sixtyfps.io>
SPDX-License-Identifier: GPL-3.0-only
This file is also available under commercial licensing terms.
Please contact info@sixtyfps.io for more information.
LICENSE END */
import foo from bar;
// ^error{Syntax error: expected LBrace}

View file

@ -0,0 +1,13 @@
/* LICENSE BEGIN
This file is part of the SixtyFPS Project -- https://sixtyfps.io
Copyright (c) 2020 Olivier Goffart <olivier.goffart@sixtyfps.io>
Copyright (c) 2020 Simon Hausmann <simon.hausmann@sixtyfps.io>
SPDX-License-Identifier: GPL-3.0-only
This file is also available under commercial licensing terms.
Please contact info@sixtyfps.io for more information.
LICENSE END */
import { A } yo;
// ^error{Expected from keyword for import statement}

View file

@ -0,0 +1,13 @@
/* LICENSE BEGIN
This file is part of the SixtyFPS Project -- https://sixtyfps.io
Copyright (c) 2020 Olivier Goffart <olivier.goffart@sixtyfps.io>
Copyright (c) 2020 Simon Hausmann <simon.hausmann@sixtyfps.io>
SPDX-License-Identifier: GPL-3.0-only
This file is also available under commercial licensing terms.
Please contact info@sixtyfps.io for more information.
LICENSE END */
import { } from yo;
// ^error{Expected plain string literal}

View file

@ -297,10 +297,15 @@ impl<'a> TypeLoader<'a> {
// If there was error (esp parse error) we don't want to report further error in this document. // If there was error (esp parse error) we don't want to report further error in this document.
// because they might be nonsense (TODO: we should check that the parse error were really in this document). // because they might be nonsense (TODO: we should check that the parse error were really in this document).
// But we still want to create a document to give better error messages in the root document. // But we still want to create a document to give better error messages in the root document.
let mut ignore_diag = BuildDiagnostics::default();
ignore_diag.push_error_with_span(
"Dummy error because some of the code asserts there there was an error".into(),
Default::default(),
);
let doc = crate::object_tree::Document::from_node( let doc = crate::object_tree::Document::from_node(
dependency_doc, dependency_doc,
foreign_imports, foreign_imports,
&mut BuildDiagnostics::default(), // New diagnostics that we will ignore &mut ignore_diag,
&dependency_registry, &dependency_registry,
); );
self.all_documents.docs.insert(path.to_owned(), doc); self.all_documents.docs.insert(path.to_owned(), doc);
@ -406,9 +411,13 @@ impl<'a> TypeLoader<'a> {
let mut dependencies = DependenciesByFile::new(); let mut dependencies = DependenciesByFile::new();
for import in doc.ImportSpecifier() { for import in doc.ImportSpecifier() {
let import_uri = import.child_token(SyntaxKind::StringLiteral).expect( let import_uri = match import.child_token(SyntaxKind::StringLiteral) {
"Internal error: missing import uri literal, this is a parsing/grammar bug", Some(import_uri) => import_uri,
); None => {
debug_assert!(doc_diagnostics.has_error());
continue;
}
};
let path_to_import = import_uri.text().to_string(); let path_to_import = import_uri.text().to_string();
let path_to_import = path_to_import.trim_matches('\"').to_string(); let path_to_import = path_to_import.trim_matches('\"').to_string();
if path_to_import.is_empty() { if path_to_import.is_empty() {