@@ -3260,6 +3260,13 @@ void parser::parse_and_visit_if(parse_visitor_base &v) {
32603260}
32613261
32623262void parser::parse_and_visit_import (parse_visitor_base &v) {
3263+ this ->parse_and_visit_import (
3264+ v, /* declare_namespace_declare_keyword=*/ std::nullopt );
3265+ }
3266+
3267+ void parser::parse_and_visit_import (
3268+ parse_visitor_base &v,
3269+ std::optional<source_code_span> declare_namespace_declare_keyword) {
32633270 QLJS_ASSERT (this ->peek ().type == token_type::kw_import);
32643271 source_code_span import_span = this ->peek ().span ();
32653272 this ->skip ();
@@ -3477,6 +3484,14 @@ void parser::parse_and_visit_import(parse_visitor_base &v) {
34773484 if (this ->peek ().type == token_type::left_paren &&
34783485 namespace_name.normalized_name () == u8" require" _sv) {
34793486 // import fs = require("fs");
3487+ if (declare_namespace_declare_keyword.has_value ()) {
3488+ this ->diag_reporter_ ->report (
3489+ diag_declare_namespace_cannot_import_module{
3490+ .import_keyword = import_span,
3491+ .declare_keyword = *declare_namespace_declare_keyword,
3492+ });
3493+ }
3494+
34803495 this ->skip ();
34813496 QLJS_PARSER_UNIMPLEMENTED_IF_NOT_TOKEN (token_type::string);
34823497 this ->skip ();
@@ -3522,6 +3537,12 @@ void parser::parse_and_visit_import(parse_visitor_base &v) {
35223537 switch (this ->peek ().type ) {
35233538 // import fs from 'fs';
35243539 case token_type::string:
3540+ if (declare_namespace_declare_keyword.has_value ()) {
3541+ this ->diag_reporter_ ->report (diag_declare_namespace_cannot_import_module{
3542+ .import_keyword = import_span,
3543+ .declare_keyword = *declare_namespace_declare_keyword,
3544+ });
3545+ }
35253546 this ->skip ();
35263547 break ;
35273548
@@ -4612,16 +4633,20 @@ parser::parse_and_visit_possible_declare_statement(parse_visitor_base &v) {
46124633 case token_type::kw_type:
46134634 case token_type::kw_var:
46144635 case token_type::kw_namespace:
4615- parse_as_declare_statement:
46164636 this ->lexer_ .commit_transaction (std::move (transaction));
46174637 this ->parse_and_visit_declare_statement (v, declare_keyword_span);
46184638 return parse_possible_declare_result::parsed;
46194639
4640+ // declare import fs from 'fs'; // Invalid.
4641+ // declare import ns = otherns; // Invalid.
46204642 case token_type::kw_import:
4643+ this ->lexer_ .commit_transaction (std::move (transaction));
46214644 this ->diag_reporter_ ->report (diag_import_cannot_have_declare_keyword{
46224645 .declare_keyword = declare_keyword_span,
46234646 });
4624- goto parse_as_declare_statement;
4647+ this ->parse_and_visit_import (
4648+ v, /* declare_namespace_declare_keyword=*/ std::nullopt );
4649+ return parse_possible_declare_result::parsed;
46254650
46264651 // declare: // Label.
46274652 // declare();
@@ -4826,8 +4851,12 @@ void parser::parse_and_visit_declare_statement(
48264851 break ;
48274852
48284853 // declare namespace ns { import a = b; }
4854+ // declare namespace ns { import a from "b"; } // Invalid.
48294855 case token_type::kw_import:
4830- this ->parse_and_visit_import (v);
4856+ // NOTE(strager): 'declare import a from "b";' is handled by
4857+ // parse_and_visit_possible_declare_statement.
4858+ this ->parse_and_visit_import (
4859+ v, /* declare_namespace_declare_keyword=*/ declare_keyword_span);
48314860 break ;
48324861
48334862 // declare: // Label.
0 commit comments