Repository: daixiang0/gci
Pull request #178 (2023-10-17 11:17:21)

Add Alias Section
To Support [#175](https://github.com/daixian5g0/gci/issues/175)
Added Section Alias

RyoKusnadi: revised boss @daixiang0 
daixiang0: LGTM


Patch:
diff --git a/README.md b/README.md
index 0412257..9d9a04d 100644
--- a/README.md
+++ b/README.md
@@ -34,9 +34,10 @@ GCI splits all import blocks into different sections, now support five section t
 - default: All rest import blocks
 - blank: Put blank imports together in a separate group
 - dot: Put dot imports together in a separate group
+- alias: Put alias imports together in a separate group
 
-The priority is standard > default > custom > blank > dot, all sections sort alphabetically inside.
-By default, blank and dot sections are not used and the corresponding lines end up in the other groups.
+The priority is standard > default > custom > blank > dot > alias, all sections sort alphabetically inside.
+By default, blank , dot and alias sections are not used and the corresponding lines end up in the other groups.
 
 All import blocks use one TAB(`\t`) as Indent.
 
@@ -88,12 +89,13 @@ Flags:
       --custom-order          Enable custom order of sections
   -d, --debug                 Enables debug output from the formatter
   -h, --help                  help for print
-  -s, --section stringArray   Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). The section order is standard > default > custom > blank > dot. The default value is [standard,default].
+  -s, --section stringArray   Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). The section order is standard > default > custom > blank > dot > alias. The default value is [standard,default].
                               standard - standard section that Go provides officially, like "fmt"
                               Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix. Multiple custom prefixes may be provided, they will be rendered as distinct sections separated by newline. You can regroup multiple prefixes by separating them with comma: Prefix(github.com/daixiang0,gitlab.com/daixiang0,daixiang0)
                               default - default section, contains all rest imports
                               blank - blank section, contains all blank imports.
                               dot - dot section, contains all dot imports. (default [standard,default])
+                              alias - alias section, contains all alias imports.
       --skip-generated        Skip generated files
       --skip-vendor           Skip files inside vendor directory
 ```
@@ -112,12 +114,13 @@ Flags:
       --custom-order          Enable custom order of sections
   -d, --debug                 Enables debug output from the formatter
   -h, --help                  help for write
-  -s, --section stringArray   Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). The section order is standard > default > custom > blank > dot. The default value is [standard,default].
+  -s, --section stringArray   Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). The section order is standard > default > custom > blank > dot > alias. The default value is [standard,default].
                               standard - standard section that Go provides officially, like "fmt"
                               Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix. Multiple custom prefixes may be provided, they will be rendered as distinct sections separated by newline. You can regroup multiple prefixes by separating them with comma: Prefix(github.com/daixiang0,gitlab.com/daixiang0,daixiang0)
                               default - default section, contains all rest imports
                               blank - blank section, contains all blank imports.
                               dot - dot section, contains all dot imports. (default [standard,default])
+                              alias - alias section, contains all alias imports.
       --skip-generated        Skip generated files
       --skip-vendor           Skip files inside vendor directory
 ```
@@ -133,11 +136,12 @@ Flags:
       --custom-order          Enable custom order of sections
   -d, --debug                 Enables debug output from the formatter
   -h, --help                  help for list
-  -s, --section stringArray   Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). The section order is standard > default > custom > blank > dot. The default value is [standard,default].
+  -s, --section stringArray   Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). The section order is standard > default > custom > blank > dot > alias. The default value is [standard,default].
                               standard - standard section that Go provides officially, like "fmt"
                               Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix. Multiple custom prefixes may be provided, they will be rendered as distinct sections separated by newline. You can regroup multiple prefixes by separating them with comma: Prefix(github.com/daixiang0,gitlab.com/daixiang0,daixiang0)
                               default - default section, contains all rest imports
                               blank - blank section, contains all blank imports.
+                              alias - alias section, contains all alias imports.
                               dot - dot section, contains all dot imports. (default [standard,default])
       --skip-generated        Skip generated files
       --skip-vendor           Skip files inside vendor directory
@@ -154,11 +158,12 @@ Flags:
       --custom-order          Enable custom order of sections
   -d, --debug                 Enables debug output from the formatter
   -h, --help                  help for diff
-  -s, --section stringArray   Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). The section order is standard > default > custom > blank > dot. The default value is [standard,default].
+  -s, --section stringArray   Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). The section order is standard > default > custom > blank > dot > alias. The default value is [standard,default].
                               standard - standard section that Go provides officially, like "fmt"
                               Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix. Multiple custom prefixes may be provided, they will be rendered as distinct sections separated by newline. You can regroup multiple prefixes by separating them with comma: Prefix(github.com/daixiang0,gitlab.com/daixiang0,daixiang0)
                               default - default section, contains all rest imports
                               blank - blank section, contains all blank imports.
+                              alias - alias section, contains all alias imports.
                               dot - dot section, contains all dot imports. (default [standard,default])
       --skip-generated        Skip generated files
       --skip-vendor           Skip files inside vendor directory
@@ -284,6 +289,38 @@ import (
 )
 ```
 
+### with alias grouping enabled
+
+```go
+package main
+
+import (
+	testing "github.com/daixiang0/test"
+	"fmt"
+
+	g "github.com/golang"
+
+	"github.com/daixiang0/gci"
+	"github.com/daixiang0/gci/subtest"
+)
+```
+
+to
+
+```go
+package main
+
+import (
+	"fmt"
+
+	"github.com/daixiang0/gci"
+	"github.com/daixiang0/gci/subtest"
+
+	testing "github.com/daixiang0/test"
+	g "github.com/golang"
+)
+```
+
 ## TODO
 
 - Ensure only one blank between `Name` and `Path` in an import block
diff --git a/cmd/gci/gcicommand.go b/cmd/gci/gcicommand.go
index 5a7b679..a1a2236 100644
--- a/cmd/gci/gcicommand.go
+++ b/cmd/gci/gcicommand.go
@@ -48,12 +48,13 @@ func (e *Executor) newGciCommand(use, short, long string, aliases []string, stdI
 
 	debug = cmd.Flags().BoolP("debug", "d", false, "Enables debug output from the formatter")
 
-	sectionHelp := `Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). The section order is standard > default > custom > blank > dot. The default value is [standard,default].
+	sectionHelp := `Sections define how inputs will be processed. Section names are case-insensitive and may contain parameters in (). The section order is standard > default > custom > blank > dot > alias. The default value is [standard,default].
 standard - standard section that Go provides officially, like "fmt"
 Prefix(github.com/daixiang0) - custom section, groups all imports with the specified Prefix. Imports will be matched to the longest Prefix. Multiple custom prefixes may be provided, they will be rendered as distinct sections separated by newline. You can regroup multiple prefixes by separating them with comma: Prefix(github.com/daixiang0,gitlab.com/daixiang0,daixiang0)
 default - default section, contains all rest imports
 blank - blank section, contains all blank imports.
-dot - dot section, contains all dot imports.`
+dot - dot section, contains all dot imports.
+alias - alias section, contains all alias imports.`
 
 	skipGenerated = cmd.Flags().Bool("skip-generated", false, "Skip generated files")
 	skipVendor = cmd.Flags().Bool("skip-vendor", false, "Skip files inside vendor directory")
diff --git a/pkg/config/config.go b/pkg/config/config.go
index 98513c0..51f6ccf 100644
--- a/pkg/config/config.go
+++ b/pkg/config/config.go
@@ -15,6 +15,7 @@ var defaultOrder = map[string]int{
 	section.CustomType:   2,
 	section.BlankType:    3,
 	section.DotType:      4,
+	section.AliasType:    5,
 }
 
 type BoolConfig struct {
diff --git a/pkg/section/alias.go b/pkg/section/alias.go
new file mode 100644
index 0000000..423e96a
--- /dev/null
+++ b/pkg/section/alias.go
@@ -0,0 +1,25 @@
+package section
+
+import (
+	"github.com/daixiang0/gci/pkg/parse"
+	"github.com/daixiang0/gci/pkg/specificity"
+)
+
+type Alias struct{}
+
+const AliasType = "alias"
+
+func (b Alias) MatchSpecificity(spec *parse.GciImports) specificity.MatchSpecificity {
+	if spec.Name != "." && spec.Name != "_" && spec.Name != "" {
+		return specificity.NameMatch{}
+	}
+	return specificity.MisMatch{}
+}
+
+func (b Alias) String() string {
+	return AliasType
+}
+
+func (b Alias) Type() string {
+	return AliasType
+}
diff --git a/pkg/section/parser.go b/pkg/section/parser.go
index 9834dcd..38435f5 100644
--- a/pkg/section/parser.go
+++ b/pkg/section/parser.go
@@ -33,6 +33,8 @@ func Parse(data []string) (SectionList, error) {
 			list = append(list, Dot{})
 		} else if s == "blank" {
 			list = append(list, Blank{})
+		} else if s == "alias" {
+			list = append(list, Alias{})
 		} else {
 			errString += fmt.Sprintf(" %s", s)
 		}


Test patch:
diff --git a/pkg/gci/testdata.go b/pkg/gci/testdata.go
index a48ce35..77c06dc 100644
--- a/pkg/gci/testdata.go
+++ b/pkg/gci/testdata.go
@@ -1240,6 +1240,39 @@ import (
 	"github.com/daixiang0/gci"
 	"github.com/daixiang0/gci/subtest"
 )
+`,
+	},
+	{
+		"alias",
+
+		`sections:
+  - Standard
+  - Default
+  - Alias
+`,
+		`package main
+
+import (
+	testing "github.com/daixiang0/test"
+	"fmt"
+
+	g "github.com/golang"
+
+	"github.com/daixiang0/gci"
+	"github.com/daixiang0/gci/subtest"
+)
+`,
+		`package main
+
+import (
+	"fmt"
+
+	"github.com/daixiang0/gci"
+	"github.com/daixiang0/gci/subtest"
+
+	testing "github.com/daixiang0/test"
+	g "github.com/golang"
+)
 `,
 	},
 }