MCPcopy Create free account
hub / github.com/github/gh-aw / ParseImportDirective

Function ParseImportDirective

pkg/parser/import_directive.go:28–72  ·  view source on GitHub ↗

ParseImportDirective parses an import directive and returns its components

(line string)

Source from the content-addressed store, hash-verified

26
27// ParseImportDirective parses an import directive and returns its components
28func ParseImportDirective(line string) *ImportDirectiveMatch {
29 trimmedLine := strings.TrimSpace(line)
30
31 // Fast-path: import directives must start with '@' or '{'; skip the regex for all other lines.
32 if trimmedLine == "" || (trimmedLine[0] != '@' && trimmedLine[0] != '{') {
33 return nil
34 }
35
36 // Check if it matches the import pattern at all
37 matches := IncludeDirectivePattern.FindStringSubmatch(trimmedLine)
38 if matches == nil {
39 return nil
40 }
41
42 // All matched forms are now deprecated/legacy.
43 // Group 2 non-empty → @-style (@include/@import), Group 4 non-empty → {{#import}} style.
44 // Both are legacy; the distinction is kept for message formatting.
45 atStyleLegacy := matches[2] != ""
46 isLegacy := true // every form matched by IncludeDirectivePattern is deprecated
47 importDirectiveLog.Printf("Parsing import directive: legacy=%t, atStyle=%t, line=%s", isLegacy, atStyleLegacy, trimmedLine)
48
49 var isOptional bool
50 var path string
51
52 if atStyleLegacy {
53 // @-style legacy syntax: @include? path or @import? path
54 // Group 1: optional marker, Group 2: path
55 isOptional = matches[1] == "?"
56 path = strings.TrimSpace(matches[2])
57 } else {
58 // {{#import}} deprecated syntax: {{#import?: path}} or {{#import: path}} (colon is optional)
59 // Group 3: optional marker, Group 4: path
60 isOptional = matches[3] == "?"
61 path = strings.TrimSpace(matches[4])
62 }
63
64 match := &ImportDirectiveMatch{
65 IsOptional: isOptional,
66 Path: path,
67 IsLegacy: isLegacy,
68 Original: trimmedLine,
69 }
70 importDirectiveLog.Printf("Parsed import directive: path=%s, optional=%t, legacy=%t", path, isOptional, isLegacy)
71 return match
72}

Calls 1

PrintfMethod · 0.45

Tested by 2

TestParseImportDirectiveFunction · 0.68