Skip to content

Commit c7499d4

Browse files
committed
fix: remove single-char string exemption from TestNoMagicStrings
No string literal gets a free pass — "/" is a directory separator, "-" is a dash, "." is a dot. All must use config constants. Added token.Slash, token.Dot, token.Plus, token.Hash to config/token/delim.go. Migrated 30 single-char magic strings across 16 files to use token.*, marker.*, or config constants. Signed-off-by: Jose Alekhinne <jose@ctx.ist>
1 parent 24397fe commit c7499d4

18 files changed

Lines changed: 46 additions & 44 deletions

File tree

internal/audit/magic_strings_test.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -126,27 +126,6 @@ func checkMagicString(
126126
return
127127
}
128128

129-
// Single-character strings that are already defined
130-
// as token constants are exempt only when used as
131-
// such. But we cannot distinguish usage context in
132-
// a simple AST walk, so we exempt the most common
133-
// structural punctuation: slash, dot, colon, hash,
134-
// equals, pipe, ampersand, question mark, at-sign,
135-
// semicolon, tilde, underscore, and quotes.
136-
if len(s) == 1 {
137-
r := rune(s[0])
138-
switch r {
139-
case '/', '.', ':', '#', '=', '|', '&',
140-
'?', '@', ';', '~', '_', '\'', '"',
141-
'*', '>', '<', '!', '+', ',', '\\',
142-
'(', ')', '[', ']', '{', '}':
143-
return
144-
}
145-
// Non-punctuation single chars (letters, digits,
146-
// special symbols like "-") fall through to the
147-
// violation check.
148-
}
149-
150129
// Format verbs ("%s", "%d %s", etc.).
151130
if isFormatString(s) {
152131
return

internal/cli/dep/core/format.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ import (
2626
// - string: Safe Mermaid node identifier
2727
func MermaidID(pkg string) string {
2828
r := strings.NewReplacer(
29-
"/", "_", ".", "_", token.Dash, "_",
29+
token.Slash, token.Underscore,
30+
token.Dot, token.Underscore,
31+
token.Dash, token.Underscore,
3032
)
3133
return r.Replace(pkg)
3234
}

internal/cli/dep/core/go.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"strings"
1414

1515
cfgDep "github.com/ActiveMemory/ctx/internal/config/dep"
16+
"github.com/ActiveMemory/ctx/internal/config/token"
1617
execDep "github.com/ActiveMemory/ctx/internal/exec/dep"
1718
)
1819

@@ -97,10 +98,10 @@ func ListGoPackages() ([]GoPackage, error) {
9798
// - bool: True if the path is a stdlib package
9899
func IsStdlib(path string) bool {
99100
first := path
100-
if i := strings.Index(path, "/"); i >= 0 {
101+
if i := strings.Index(path, token.Slash); i >= 0 {
101102
first = path[:i]
102103
}
103-
return !strings.Contains(first, ".")
104+
return !strings.Contains(first, token.Dot)
104105
}
105106

106107
// ShortPkgName strips the module prefix for readability.
@@ -112,7 +113,7 @@ func IsStdlib(path string) bool {
112113
// Returns:
113114
// - string: Shortened path, or original if prefix doesn't match
114115
func ShortPkgName(importPath, modPath string) string {
115-
if modPath != "" && strings.HasPrefix(importPath, modPath+"/") {
116+
if modPath != "" && strings.HasPrefix(importPath, modPath+token.Slash) {
116117
return importPath[len(modPath)+1:]
117118
}
118119
return importPath

internal/cli/journal/core/format/fmt.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ func Size(bytes int64) string {
4646
// Returns:
4747
// - string: Safe slug (e.g., "internal_config_x_go")
4848
func KeyFileSlug(path string) string {
49-
slug := strings.ReplaceAll(path, "/", "_")
50-
slug = strings.ReplaceAll(slug, ".", "_")
51-
slug = strings.ReplaceAll(slug, "*", token.GlobReplace)
49+
slug := strings.ReplaceAll(path, token.Slash, token.Underscore)
50+
slug = strings.ReplaceAll(slug, token.Dot, token.Underscore)
51+
slug = strings.ReplaceAll(slug, token.GlobStar, token.GlobReplace)
5252
return slug
5353
}
5454

internal/cli/journal/core/normalize/normalize.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func Content(content string, fencesVerified bool) string {
142142
inner := m[1 : len(m)-1] // strip backticks
143143
inner = strings.ReplaceAll(inner, marker.AngleLT, marker.EntityLT)
144144
inner = strings.ReplaceAll(inner, marker.AngleGT, marker.EntityGT)
145-
return `"` + inner + `"`
145+
return token.DoubleQuote + inner + token.DoubleQuote
146146
}
147147
line = regex.InlineCodeAngle.ReplaceAllStringFunc(
148148
line, replacer,

internal/cli/journal/core/parse/parse.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/ActiveMemory/ctx/internal/config/embed/text"
1919
"github.com/ActiveMemory/ctx/internal/config/file"
2020
"github.com/ActiveMemory/ctx/internal/config/journal"
21+
"github.com/ActiveMemory/ctx/internal/config/marker"
2122
"github.com/ActiveMemory/ctx/internal/config/regex"
2223
"github.com/ActiveMemory/ctx/internal/config/token"
2324
"github.com/ActiveMemory/ctx/internal/entity"
@@ -177,8 +178,10 @@ func JournalEntry(path, filename string) entity.JournalEntry {
177178
// become HTML entities; backticks and # are stripped (they add no
178179
// meaning inside [...] link labels).
179180
entry.Title = strings.NewReplacer(
180-
"<", "&lt;", ">", "&gt;",
181-
token.Backtick, "", "#", "",
181+
marker.AngleLT, marker.EntityLT,
182+
marker.AngleGT, marker.EntityGT,
183+
token.Backtick, "",
184+
token.Hash, "",
182185
).Replace(entry.Title)
183186
entry.Title = strings.TrimSpace(entry.Title)
184187

internal/cli/journal/core/wrap/wrap.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111

1212
"github.com/ActiveMemory/ctx/internal/config/journal"
13+
"github.com/ActiveMemory/ctx/internal/config/marker"
1314
"github.com/ActiveMemory/ctx/internal/config/token"
1415
)
1516

@@ -45,7 +46,7 @@ func Content(content string) string {
4546

4647
// Wrap long lines (skip tables)
4748
if len(line) > journal.LineWrapWidth &&
48-
!strings.HasPrefix(strings.TrimSpace(line), "|") {
49+
!strings.HasPrefix(strings.TrimSpace(line), marker.TablePipe) {
4950
out = append(out, Soft(line, journal.LineWrapWidth)...)
5051
} else {
5152
out = append(out, line)

internal/cli/pad/cmd/export/cmd.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/ActiveMemory/ctx/internal/config/embed/cmd"
1414
"github.com/ActiveMemory/ctx/internal/config/embed/flag"
1515
cFlag "github.com/ActiveMemory/ctx/internal/config/flag"
16+
"github.com/ActiveMemory/ctx/internal/config/token"
1617
)
1718

1819
// Cmd returns the pad export subcommand.
@@ -29,7 +30,7 @@ func Cmd() *cobra.Command {
2930
Long: long,
3031
Args: cobra.MaximumNArgs(1),
3132
RunE: func(cmd *cobra.Command, args []string) error {
32-
dir := "."
33+
dir := token.Dot
3334
if len(args) > 0 {
3435
dir = args[0]
3536
}

internal/cli/system/core/event/event.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/ActiveMemory/ctx/internal/config/embed/text"
1717
"github.com/ActiveMemory/ctx/internal/config/event"
1818
cfgTime "github.com/ActiveMemory/ctx/internal/config/time"
19+
"github.com/ActiveMemory/ctx/internal/config/token"
1920
"github.com/ActiveMemory/ctx/internal/notify"
2021
)
2122

@@ -49,7 +50,7 @@ func ExtractHookName(e notify.Payload) string {
4950
return e.Detail.Hook
5051
}
5152
// Fall back to extracting from message prefix (e.g., "qa-reminder: ...")
52-
if idx := strings.Index(e.Message, ":"); idx > 0 {
53+
if idx := strings.Index(e.Message, token.Colon); idx > 0 {
5354
return e.Message[:idx]
5455
}
5556
return event.HookFallback

internal/cli/system/core/version/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import (
3535
// - minor: minor version number
3636
// - ok: true if parsing succeeded
3737
func ParseMajorMinor(ver string) (major, minor int, ok bool) {
38-
parts := strings.SplitN(ver, ".", 3)
38+
parts := strings.SplitN(ver, token.Dot, 3)
3939
if len(parts) < 2 {
4040
return 0, 0, false
4141
}

0 commit comments

Comments
 (0)