MCPcopy Create free account
hub / github.com/GuyARoss/orbit / formatImportLine

Method formatImportLine

pkg/jsparse/document.go:284–358  ·  view source on GitHub ↗

formatImportLine parses an import line to create an import dependency

(line string)

Source from the content-addressed store, hash-verified

282
283// formatImportLine parses an import line to create an import dependency
284func (p *DefaultJSDocument) formatImportLine(line string) *ImportDependency {
285 importType := lineImportType(line)
286 if importType == ModuleImportType {
287 return &ImportDependency{
288 InitialPath: line,
289 FinalStatement: line,
290 Type: ModuleImportType,
291 }
292 }
293
294 pathChar := '"'
295 path := subsetRune(line, '"', '"')
296 if len(path) == 0 {
297 path = subsetRune(line, '\'', '\'')
298 pathChar = '\''
299 }
300
301 webDirPaths := strings.Split(p.webDir, "/")
302 cleanWebDirPaths := make([]string, 0)
303
304 for _, dp := range webDirPaths {
305 if len(dp) <= 1 && strings.Contains(dp, ".") {
306 continue
307 }
308
309 cleanWebDirPaths = append(cleanWebDirPaths, dp)
310 }
311
312 tokenPathPaths := strings.Split(path, "/")
313 for _, tk := range tokenPathPaths {
314 // tk = "." should provide support for localized paths such as "./"
315 if tk == "." {
316 validPageDir := p.pageDir
317
318 if p.pageDir[:2] == "./" {
319 validPageDir = p.pageDir[2:]
320 }
321
322 pageDirs := strings.Split(validPageDir, "/")
323 cleanWebDirPaths = pageDirs[0 : len(pageDirs)-1]
324
325 continue
326 }
327
328 if strings.Contains(tk, "..") {
329 continue
330 }
331
332 cleanWebDirPaths = append(cleanWebDirPaths, tk)
333 }
334
335 finalPath := strings.Join(cleanWebDirPaths, "/")
336
337 // possible that the path is referencing an index
338 // we can validate this by checking if the import path is a dir
339 if isIndexPath(finalPath) {
340 finalPath += "/index"
341 cleanWebDirPaths = append(cleanWebDirPaths, "/index")

Callers 3

tokenizeLineMethod · 0.95
TestFormatImportLineFunction · 0.95

Calls 6

lineImportTypeFunction · 0.85
subsetRuneFunction · 0.85
isIndexPathFunction · 0.85
pageExtensionFunction · 0.85
verifyPathFunction · 0.85
ContainsMethod · 0.80

Tested by 2

TestFormatImportLineFunction · 0.76