|
15 | 15 | package parser |
16 | 16 |
|
17 | 17 | import ( |
18 | | - "bufio" |
19 | | - "bytes" |
20 | 18 | "container/list" |
21 | 19 | "fmt" |
22 | 20 | "go/ast" |
23 | 21 | "go/build" |
24 | 22 | "go/types" |
25 | | - "io" |
26 | 23 | "os" |
27 | 24 | "os/exec" |
28 | 25 | "path" |
@@ -154,41 +151,26 @@ func getPackageAlias(importPath string) string { |
154 | 151 | } |
155 | 152 |
|
156 | 153 | func hasNoDeps(modFilePath string) bool { |
157 | | - mod, err := modfile.Parse(modFilePath, nil, nil) |
| 154 | + content, err := os.ReadFile(modFilePath) |
158 | 155 | if err != nil { |
159 | 156 | return false |
160 | 157 | } |
161 | 158 |
|
162 | | - return len(mod.Require) == 0 |
163 | | -} |
164 | | - |
165 | | -func getModuleName(modFilePath string) (string, []byte, error) { |
166 | | - file, err := os.Open(modFilePath) |
167 | | - if err != nil { |
168 | | - return "", nil, fmt.Errorf("failed to open file: %v", err) |
169 | | - } |
170 | | - defer file.Close() |
171 | | - data, err := io.ReadAll(file) |
| 159 | + modf, err := modfile.Parse(modFilePath, content, nil) |
172 | 160 | if err != nil { |
173 | | - return "", nil, fmt.Errorf("failed to read file: %v", err) |
174 | | - } |
175 | | - scanner := bufio.NewScanner(bytes.NewBuffer(data)) |
176 | | - for scanner.Scan() { |
177 | | - line := scanner.Text() |
178 | | - if strings.HasPrefix(line, "module") { |
179 | | - // Assuming 'module' keyword is followed by module name |
180 | | - parts := strings.Split(line, " ") |
181 | | - if len(parts) > 1 { |
182 | | - return parts[1], data, nil |
183 | | - } |
184 | | - } |
| 161 | + return false |
185 | 162 | } |
186 | 163 |
|
187 | | - if err := scanner.Err(); err != nil { |
188 | | - return "", data, fmt.Errorf("failed to scan file: %v", err) |
| 164 | + return len(modf.Require) == 0 |
| 165 | +} |
| 166 | + |
| 167 | +func getModuleName(modFilePath string) (string, error) { |
| 168 | + content, err := os.ReadFile(modFilePath) |
| 169 | + if err != nil { |
| 170 | + return "", fmt.Errorf("failed to read file: %s", err.Error()) |
189 | 171 | } |
190 | 172 |
|
191 | | - return "", data, nil |
| 173 | + return modfile.ModulePath(content), nil |
192 | 174 | } |
193 | 175 |
|
194 | 176 | func isGoBuiltins(name string) bool { |
|
0 commit comments