Allow trailing comma in import statements (#8223)

Adding support for (optional) trailing commas like this:

    import {
        Foo,
        Bar,
    } from "foobar.slint";

This way it's more convenient to keep component imports sorted and
leads to smaller diffs when adding more components to the end of the
import statement.

ChangeLog: Allow trailing comma in import statements

Closes #4922
This commit is contained in:
U. Bruhin 2025-04-21 17:24:18 +02:00 committed by GitHub
parent 7f352ed764
commit a5ec77ac99
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 35 additions and 24 deletions

View file

@ -292,10 +292,14 @@ export { MathLogic } // known as "MathLogic" when using native APIs to access gl
The following syntax is supported for importing types:
```slint no-test
import { export1 } from "module.slint";
import { export1, export2 } from "module.slint";
import { export1 as alias1 } from "module.slint";
import { export1, export2 as alias2, /* ... */ } from "module.slint";
import { MyButton } from "module.slint";
import { MyButton, MySwitch } from "module.slint";
import { MyButton as OtherButton } from "module.slint";
import {
MyButton,
/* ... */,
MySwitch as OtherSwitch,
} from "module.slint";
```
The following syntax is supported for exporting types:

View file

@ -313,8 +313,11 @@ fn parse_import_specifier(p: &mut impl Parser) -> bool {
#[cfg_attr(test, parser_test)]
/// ```test,ImportIdentifierList
/// { Type1 }
/// { Type2, Type3 }
/// { Type as Alias1, Type as AnotherAlias }
/// { Type2, }
/// { Type3, Type4 }
/// { Type5, Type6, }
/// { Type as Alias1, Type as AnotherAlias1 }
/// { Type as Alias2, Type as AnotherAlias2, }
/// {}
/// ```
fn parse_import_identifier_list(p: &mut impl Parser) -> bool {
@ -322,23 +325,14 @@ fn parse_import_identifier_list(p: &mut impl Parser) -> bool {
if !p.expect(SyntaxKind::LBrace) {
return false;
}
if p.test(SyntaxKind::RBrace) {
return true;
}
loop {
if p.test(SyntaxKind::RBrace) {
return true;
}
parse_import_identifier(&mut *p);
match p.nth(0).kind() {
SyntaxKind::RBrace => {
p.consume();
return true;
}
SyntaxKind::Comma => {
p.consume();
}
_ => {
p.error("Expected comma or brace");
return false;
}
if !p.test(SyntaxKind::Comma) && p.nth(0).kind() != SyntaxKind::RBrace {
p.error("Expected comma or brace");
return false;
}
}
}

View file

@ -0,0 +1,5 @@
// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
import { , SomeRect } from "../../typeloader/incpath/local_helper_type.slint";
// ^error{Syntax error: expected Identifier}

View file

@ -0,0 +1,5 @@
// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
import { SomeRect SomeRect as OtherRect } from "../../typeloader/incpath/local_helper_type.slint";
// ^error{Expected comma or brace}

View file

@ -2,7 +2,10 @@
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
//include_path: ../../helper_components
import { UseGlobal, ExportedGlobal as FromExport } from "export_globals.slint";
import {
UseGlobal,
ExportedGlobal as FromExport,
} from "export_globals.slint";
global NotExported := {
property<int> abc: 1000;

View file

@ -2,7 +2,7 @@
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
//include_path: ../../helper_components
import { UseStruct , ExportedStruct, ExportEnum } from "export_structs.slint";
import { UseStruct , ExportedStruct, ExportEnum , } from "export_structs.slint";
TestCase := Rectangle {
property <ExportedStruct> exp: { d: 3001, e: {a: 2001} };
u := UseStruct {

View file

@ -2,7 +2,7 @@
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
//include_path: ../../helper_components
import { TestButton as RealButton } from "test_button.slint";
import { TestButton as RealButton, } from "test_button.slint";
import { ColorButton } from "../helper_components/test_button.slint";
import { Button } from "std-widgets.slint";
import { TestButton as ReExportedButton } from "re_export.slint";