Skip to content

Commit

Permalink
SourceFile.Fix returns original content additonaly
Browse files Browse the repository at this point in the history
  • Loading branch information
incu6us committed Dec 25, 2023
1 parent a12a1a9 commit 5bf2048
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 23 deletions.
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func main() {
return
}
}
formattedOutput, hasChange, err = reviser.NewSourceFile(originProjectName, originPath).Fix(options...)
formattedOutput, _, hasChange, err = reviser.NewSourceFile(originProjectName, originPath).Fix(options...)
if err != nil {
log.Fatalf("Failed to fix file: %+v\n", err)
}
Expand All @@ -344,7 +344,7 @@ func main() {
log.Fatalf("Failed to write file hash: %+v\n", err)
}
} else {
formattedOutput, hasChange, err = reviser.NewSourceFile(originProjectName, originPath).Fix(options...)
formattedOutput, _, hasChange, err = reviser.NewSourceFile(originProjectName, originPath).Fix(options...)
if err != nil {
log.Fatalf("Failed to fix file: %+v\n", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/goanalysis/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func run(localPkgPrefixes string, options ...reviser.SourceFileOption) func(pass
}
}

formattedFileContent, hasChanged, err := reviser.NewSourceFile(projectName, filePath).Fix(options...)
formattedFileContent, _, hasChanged, err := reviser.NewSourceFile(projectName, filePath).Fix(options...)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion reviser/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (d *SourceDir) walk(callback walkCallbackFunc, options ...SourceFileOption)
return filepath.SkipDir
}
if isGoFile(path) && !dirEntry.IsDir() && !d.isExcluded(path) {
content, hasChange, err := NewSourceFile(d.projectName, path).Fix(options...)
content, _, hasChange, err := NewSourceFile(d.projectName, path).Fix(options...)
if err != nil {
return fmt.Errorf("failed to fix: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions reviser/dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func main() {
var sortedContent string
exec(t, func(tt *testing.T) error {
// get sorted content via SourceFile.Fix
sortedData, changed, err := NewSourceFile("testdata", testFile).Fix()
sortedData, _, changed, err := NewSourceFile("testdata", testFile).Fix()
assert.NoError(tt, err)
sortedContent = string(sortedData)
assert.Equal(tt, true, changed)
Expand Down Expand Up @@ -194,7 +194,7 @@ func main() {
}
var sortedContent string
exec(t, func(tt *testing.T) error {
sortedData, changed, err := NewSourceFile("testdata", testFile).Fix()
sortedData, _, changed, err := NewSourceFile("testdata", testFile).Fix()
assert.NoError(tt, err)
sortedContent = string(sortedData)
assert.Equal(tt, true, changed)
Expand Down
20 changes: 10 additions & 10 deletions reviser/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ func NewSourceFile(projectName, filePath string) *SourceFile {
}
}

// Fix is for revise imports and format the code
func (f *SourceFile) Fix(options ...SourceFileOption) ([]byte, bool, error) {
// Fix is for revise imports and format the code. Returns formated content, original content, true if formatted content is different from original and error.
func (f *SourceFile) Fix(options ...SourceFileOption) ([]byte, []byte, bool, error) {
for _, option := range options {
err := option(f)
if err != nil {
return nil, false, err
return nil, nil, false, err
}
}

Expand All @@ -67,23 +67,23 @@ func (f *SourceFile) Fix(options ...SourceFileOption) ([]byte, bool, error) {
originalContent, err = os.ReadFile(f.filePath)
}
if err != nil {
return nil, false, err
return nil, originalContent, false, err
}

fset := token.NewFileSet()

pf, err := parser.ParseFile(fset, "", originalContent, parser.ParseComments)
if err != nil {
return nil, false, err
return nil, originalContent, false, err
}

if f.shouldSkipAutoGenerated && isFileAutoGenerate(pf) {
return originalContent, false, nil
return originalContent, originalContent, false, nil
}

importsWithMetadata, err := f.parseImports(pf)
if err != nil {
return nil, false, err
return nil, originalContent, false, err
}

groups := f.groupImports(
Expand All @@ -103,15 +103,15 @@ func (f *SourceFile) Fix(options ...SourceFileOption) ([]byte, bool, error) {

fixedImportsContent, err := generateFile(fset, pf)
if err != nil {
return nil, false, err
return nil, originalContent, false, err
}

formattedContent, err := format.Source(fixedImportsContent)
if err != nil {
return nil, false, err
return nil, originalContent, false, err
}

return formattedContent, !bytes.Equal(originalContent, formattedContent), nil
return formattedContent, originalContent, !bytes.Equal(originalContent, formattedContent), nil
}

func isFileAutoGenerate(pf *ast.File) bool {
Expand Down
14 changes: 7 additions & 7 deletions reviser/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ import "C"
}

t.Run(tt.name, func(t *testing.T) {
got, hasChange, err := NewSourceFile(tt.args.projectName, tt.args.filePath).Fix()
got, _, hasChange, err := NewSourceFile(tt.args.projectName, tt.args.filePath).Fix()
if tt.wantErr {
assert.Error(t, err)
return
Expand Down Expand Up @@ -767,7 +767,7 @@ import (
t.Run(tt.name, func(t *testing.T) {
order, err := StringToImportsOrders(tt.args.importsOrder)
assert.Nil(t, err)
got, hasChange, err := NewSourceFile(tt.args.projectName, tt.args.filePath).
got, _, hasChange, err := NewSourceFile(tt.args.projectName, tt.args.filePath).
Fix(WithImportsOrder(order))
if tt.wantErr {
assert.Error(t, err)
Expand Down Expand Up @@ -1090,7 +1090,7 @@ func main() {
require.NoError(t, os.WriteFile(tt.args.filePath, []byte(tt.args.fileContent), 0o644))

t.Run(tt.name, func(t *testing.T) {
got, hasChange, err := NewSourceFile(tt.args.projectName, tt.args.filePath).
got, _, hasChange, err := NewSourceFile(tt.args.projectName, tt.args.filePath).
Fix(WithRemovingUnusedImports)
if tt.wantErr {
assert.Error(t, err)
Expand Down Expand Up @@ -1239,7 +1239,7 @@ func main() {
require.NoError(t, os.WriteFile(tt.args.filePath, []byte(tt.args.fileContent), 0o644))

t.Run(tt.name, func(t *testing.T) {
got, hasChange, err := NewSourceFile(tt.args.projectName, tt.args.filePath).
got, _, hasChange, err := NewSourceFile(tt.args.projectName, tt.args.filePath).
Fix(WithUsingAliasForVersionSuffix)
if tt.wantErr {
assert.Error(t, err)
Expand Down Expand Up @@ -1454,7 +1454,7 @@ func main() {
require.NoError(t, os.WriteFile(tt.args.filePath, []byte(tt.args.fileContent), 0o644))

t.Run(tt.name, func(t *testing.T) {
got, hasChange, err := NewSourceFile(tt.args.projectName, tt.args.filePath).
got, _, hasChange, err := NewSourceFile(tt.args.projectName, tt.args.filePath).
Fix(WithCompanyPackagePrefixes(tt.args.localPkgPrefixes))
if tt.wantErr {
assert.Error(t, err)
Expand Down Expand Up @@ -1543,7 +1543,7 @@ func test1() {}
require.NoError(t, os.WriteFile(tt.args.filePath, []byte(tt.args.fileContent), 0o644))

t.Run(tt.name, func(t *testing.T) {
got, hasChange, err := NewSourceFile(tt.args.projectName, tt.args.filePath).Fix(WithCodeFormatting)
got, _, hasChange, err := NewSourceFile(tt.args.projectName, tt.args.filePath).Fix(WithCodeFormatting)
if tt.wantErr {
assert.Error(t, err)
return
Expand Down Expand Up @@ -1919,7 +1919,7 @@ import (
}

t.Run(tt.name, func(t *testing.T) {
got, hasChange, err := NewSourceFile(tt.args.projectName, tt.args.filePath).Fix(WithSkipGeneratedFile)
got, _, hasChange, err := NewSourceFile(tt.args.projectName, tt.args.filePath).Fix(WithSkipGeneratedFile)
if tt.wantErr {
assert.Error(t, err)
return
Expand Down

0 comments on commit 5bf2048

Please sign in to comment.